Skip to content

Instantly share code, notes, and snippets.

@akshaykarnawat
Created March 14, 2018 05:17
Show Gist options
  • Save akshaykarnawat/1292e268a2925416ce2cec0f9213d846 to your computer and use it in GitHub Desktop.
Save akshaykarnawat/1292e268a2925416ce2cec0f9213d846 to your computer and use it in GitHub Desktop.
pyQuil lib test
from pyquil.quil import Program
import pyquil.api as api
from pyquil.gates import *
import numpy as np
#open quantum virtual machine and establish connection
qvm = api.QVMConnection()
print("measure 0,0")
p = Program()
p.inst(X(0)).measure(0, 0)
print("Program", p, sep=" ::==> ")
classical_regs = [0]
print(qvm.run(p, classical_regs))
print(qvm.run(p, [0, 1, 2, 3, 4, 5]))
print("measure 0,1")
p = Program()
p.inst(X(1)).measure(0, 1)
print("Program", p, sep=" ::==> ")
print(qvm.run(p, [0, 1, 2, 3, 4, 5, 6, 7, 8]))
print("coinflip")
coin_flip = Program(X(1)).inst(H(0)).measure(0,0)
num_flips = 8
print(qvm.run(coin_flip, [0], num_flips))
print(qvm.run(coin_flip, [2], num_flips))
print(qvm.run(coin_flip, [1], num_flips))
print("wave function for the coin flip")
wave_function = qvm.wavefunction(coin_flip)
print(wave_function)
print(wave_function.amplitudes)
prob_dict = wave_function.get_outcome_probs()
print(prob_dict)
prob_dict.keys()
# read more from doc and try
@akshaykarnawat
Copy link
Author

(devPy3) /Volumes/ExPac/dev/quatum_comp/quil > python try_pyquil.py
measure 0,0
Program ::==> X 0
MEASURE 0 [0]

[[1]]
[[1, 0, 0, 0, 0, 0]]
measure 0,1
Program ::==> X 1
MEASURE 0 [1]

[[0, 0, 0, 0, 0, 0, 0, 0, 0]]
coinflip
[[0], [1], [1], [1], [0], [1], [1], [0]]
[[0], [0], [0], [0], [0], [0], [0], [0]]
[[0], [0], [0], [0], [0], [0], [0], [0]]
wave function for the coin flip
(1+0j)|10>
[0.+0.j 0.+0.j 1.+0.j 0.+0.j]
{'00': 0.0, '01': 0.0, '10': 0.9999999999999996, '11': 0.0}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment