Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_qaoa_circuit(qvm):
wf_true = [0.00167784 + 1.00210180e-05*1j, 0.50000000 - 4.99997185e-01*1j,
0.50000000 - 4.99997185e-01*1j, 0.00167784 + 1.00210180e-05*1j]
wf_true = np.reshape(np.array(wf_true), (4, 1))
prog = Program()
prog.inst([RYgate(np.pi/2)(0), RXgate(np.pi)(0),
RYgate(np.pi/2)(1), RXgate(np.pi)(1),
CNOTgate(0, 1), RXgate(-np.pi/2)(1), RYgate(4.71572463191)(1),
RXgate(np.pi/2)(1), CNOTgate(0, 1),
RXgate(-2*2.74973750579)(0), RXgate(-2*2.74973750579)(1)])
wf_test, _ = qvm.wavefunction(prog)
assert np.allclose(wf_test.amplitudes, wf_true)
def make_bell_pair(q1, q2):
"""Makes a bell pair between qubits q1 and q2
"""
return Program(H(q1), CNOT(q1, q2))
ro = program.declare('ro', memory_type='BIT', memory_size=n_qubits)
thetas = {var + str(qubit): program.declare(var + str(qubit), memory_type = 'REAL') for qubit in qubits}
sq = int(np.sqrt(n_qubits))
lim = n_qubits - sq - 1
for m in qubits:
program += RX(thetas[var + str(m)], m)
for m in qubits:
m_1 = m + 1
m_sq = m + sq
skip = (m_1) % sq
if m_1 < n_qubits:
if (m_sq >= n_qubits): program += CNOT(m, m_1)
else: program += CNOT(m, m_sq)
if (m < lim) and (skip != 0): program += CNOT(m, m_1)
for m in qubits:
program += MEASURE(m, ro[m])
print("program instructions from pyquil:\n")
for instruction in program.instructions:
print(instruction)
return program
def run_bell_medium_level(n_shots=1000):
# Step 1. Get a device. Either a QVM or a QPU
qc = get_qc("9q-generic-qvm")
q = [4, 5] # qubits
# Step 2. Construct your program
program = Program()
program += H(q[0])
program += CNOT(q[0], q[1])
# Step 2.1. Manage read-out memory
ro = program.declare("ro", memory_type="BIT", memory_size=2)
program += MEASURE(q[0], ro[0])
program += MEASURE(q[1], ro[1])
# Step 2.2. Run the program in a loop
program = program.wrap_in_numshots_loop(n_shots)
# Step 3. Compile and run
executable = qc.compile(program)
bitstrings = qc.run(executable)
# Bincount bitstrings
basis = np.array([2 ** i for i in range(len(q))])
ints = np.sum(bitstrings * basis, axis=1)
def make_bell_pair(q1, q2):
"""Makes a bell pair between qubits q1 and q2
"""
return Program(H(q1), CNOT(q1, q2))
#!/usr/bin/env python
"""
A script appropriate for featuring on rigetti.com to show how easy it is to get started!
The website should be updated to use this idiomatic program once Forest 2 is released.
- new imports
- don't use a wavefunction-based method, as it won't work on a QPU
"""
from pyquil import Program, get_qc
from pyquil.gates import H, CNOT
# construct a Bell State program
p = Program(H(0), CNOT(0, 1))
# run the program on a QVM
qvm = get_qc("9q-generic-qvm")
result = qvm.run_and_measure(p, trials=10)
print(result)
def teleport(start_index, end_index, ancilla_index):
"""Teleport a qubit from start to end using an ancilla qubit
"""
program = make_bell_pair(end_index, ancilla_index)
ro = program.declare("ro", memory_size=3)
# do the teleportation
program.inst(CNOT(start_index, ancilla_index))
program.inst(H(start_index))
# measure the results and store them in classical registers [0] and [1]
program.measure(start_index, ro[0])
program.measure(ancilla_index, ro[1])
program.if_then(ro[1], X(2))
program.if_then(ro[0], Z(2))
program.measure(end_index, ro[2])
print(program)
return program
thetas = {var + str(qubit): program.declare(var + str(qubit), memory_type = 'REAL') for qubit in qubits}
sq = int(np.sqrt(n_qubits))
lim = n_qubits - sq - 1
for m in qubits:
program += RX(thetas[var + str(m)], m)
for m in qubits:
m_1 = m + 1
m_sq = m + sq
skip = (m_1) % sq
if m_1 < n_qubits:
if (m_sq >= n_qubits): program += CNOT(m, m_1)
else: program += CNOT(m, m_sq)
if (m < lim) and (skip != 0): program += CNOT(m, m_1)
for m in qubits:
program += MEASURE(m, ro[m])
print("program instructions from pyquil:\n")
for instruction in program.instructions:
print(instruction)
return program