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_tensor_gates_single_qubit():
prog = Program().inst([Hgate(0)])
test_unitary = tensor_gates(gate_matrix, {}, prog.actions[0][1], 1).toarray()
true_unitary = gate_matrix['H']
assert np.allclose(test_unitary, true_unitary)
prog = Program().inst([Hgate(0)])
test_unitary = tensor_gates(gate_matrix, {}, prog.actions[0][1], 5).toarray()
true_unitary = np.kron(np.eye(2**4), gate_matrix['H'])
assert np.allclose(test_unitary, true_unitary)
prog = Program().inst([RXgate(0.2)(3)])
test_unitary = tensor_gates(gate_matrix, {}, prog.actions[0][1], 5).toarray()
true_unitary = np.kron(np.eye(2**1), np.kron(gate_matrix['RX'](0.2), np.eye(2**3)))
assert np.allclose(test_unitary, true_unitary)
prog = Program().inst([RXgate(0.5)(4)])
test_unitary = tensor_gates(gate_matrix, {}, prog.actions[0][1], 5).toarray()
true_unitary = np.kron(np.eye(2**0), np.kron(gate_matrix['RX'](0.5), np.eye(2**4)))
assert np.allclose(test_unitary, true_unitary)
# gonna need this program and hamiltonian for both tests.
# So define them globally
q0 = QubitPlaceholder()
q1 = QubitPlaceholder()
hamiltonian = PauliTerm("Z", q0, 2.5)
hamiltonian += PauliTerm("Z", q1, 0.5)
hamiltonian += PauliTerm("Z", q1, -1) * PauliTerm("Z", q0)
prepare_ansatz = Program()
params = prepare_ansatz.declare("params", memory_type="REAL", memory_size=4)
prepare_ansatz.inst(RX(params[0], q0))
prepare_ansatz.inst(RX(params[1], q1))
prepare_ansatz.inst(CNOT(q0, q1))
prepare_ansatz.inst(RX(params[2], q0))
prepare_ansatz.inst(RX(params[3], q1))
p0 = [0, 5.2, 0, 0]
def test_vqe_on_WFSim_QubitPlaceholders():
qubit_mapping = get_default_qubit_mapping(prepare_ansatz)
sim = WavefunctionSimulator()
cost_fun = PrepareAndMeasureOnWFSim(prepare_ansatz=prepare_ansatz,
make_memory_map=lambda p: {"params": p},
hamiltonian=hamiltonian,
sim=sim,
return_standard_deviation=True,
noisy=False,
qubit_mapping=qubit_mapping)
if index == 0:
return Program(RY(pi / 2, qubit))
else:
return Program(RY(-pi / 2, qubit))
elif label == "Y":
if index == 0:
return Program(RX(-pi / 2, qubit))
else:
return Program(RX(pi / 2, qubit))
elif label == "Z":
if index == 0:
return Program()
else:
return Program(RX(pi, qubit))
raise ValueError(f"Bad Pauli label: {label}")
def _one_q_sic_prep(index, qubit):
"""Prepare the index-th SIC basis state."""
if index == 0:
return Program()
theta = 2 * np.arccos(1 / np.sqrt(3))
zx_plane_rotation = Program([RX(-pi / 2, qubit), RZ(theta - pi, qubit), RX(-pi / 2, qubit)])
if index == 1:
return zx_plane_rotation
elif index == 2:
return zx_plane_rotation + RZ(-2 * pi / 3, qubit)
elif index == 3:
return zx_plane_rotation + RZ(2 * pi / 3, qubit)
raise ValueError(f"Bad SIC index: {index}")
def _local_pauli_eig_meas(op, idx):
"""
Generate gate sequence to measure in the eigenbasis of a Pauli operator, assuming
we are only able to measure in the Z eigenbasis. (Note: The unitary operations of this
Program are essentially the Hermitian conjugates of those in :py:func:`_one_q_pauli_prep`)
"""
if op == "X":
return Program(RY(-pi / 2, idx))
elif op == "Y":
return Program(RX(pi / 2, idx))
elif op == "Z":
return Program()
raise ValueError(f"Unknown operation {op}")
return revp
quil_prog = Program()
change_to_z_basis = Program()
change_to_original_basis = Program()
cnot_seq = Program()
prev_index = None
highest_target_index = None
for index, op in pauli_term:
if "X" == op:
change_to_z_basis.inst(H(index))
change_to_original_basis.inst(H(index))
elif "Y" == op:
change_to_z_basis.inst(RX(np.pi / 2.0, index))
change_to_original_basis.inst(RX(-np.pi / 2.0, index))
elif "I" == op:
continue
if prev_index is not None:
cnot_seq.inst(CNOT(prev_index, index))
prev_index = index
highest_target_index = index
# building rotation circuit
quil_prog += change_to_z_basis
quil_prog += cnot_seq
quil_prog.inst(RZ(2.0 * pauli_term.coefficient * param, highest_target_index))
quil_prog += reverse_hack(cnot_seq)
(lambda q: RX(np.pi, q), (-1j * np.pi / 2 * QX).expm())])
else: # pragma no coverage