def test_quil_to_native_quil(compiler): response = compiler.quil_to_native_quil(BELL_STATE) p_unitary = program_unitary(response, n_qubits=2) compiled_p_unitary = program_unitary(COMPILED_BELL_STATE, n_qubits=2) from pyquil.simulation.tools import scale_out_phase assert np.allclose(p_unitary, scale_out_phase(compiled_p_unitary, p_unitary))
def test_CNOT(): u1 = program_unitary(Program(CNOT(0, 1)), n_qubits=2) u2 = program_unitary(_CNOT(0, 1), n_qubits=2) assert equal_up_to_global_phase(u1, u2, atol=1e-12) u1 = program_unitary(Program(CNOT(1, 0)), n_qubits=2) u2 = program_unitary(_CNOT(1, 0), n_qubits=2) assert equal_up_to_global_phase(u1, u2, atol=1e-12)
def test_CPHASE(): for theta in np.linspace(-2 * np.pi, 2 * np.pi): u1 = program_unitary(Program(CPHASE(theta, 0, 1)), n_qubits=2) u2 = program_unitary(_CPHASE(theta, 0, 1), n_qubits=2) assert equal_up_to_global_phase(u1, u2, atol=1e-12) u1 = program_unitary(Program(CPHASE(theta, 1, 0)), n_qubits=2) u2 = program_unitary(_CPHASE(theta, 1, 0), n_qubits=2) assert equal_up_to_global_phase(u1, u2, atol=1e-12)
def test_XY(): for theta in np.linspace(-2 * np.pi, 2 * np.pi): p = Program(XY(theta, 0, 1)) u1 = program_unitary(p, n_qubits=2) u2 = program_unitary(basic_compile(p), n_qubits=2) assert equal_up_to_global_phase(u1, u2, atol=1e-12) p = Program(XY(theta, 1, 0)) u1 = program_unitary(p, n_qubits=2) u2 = program_unitary(basic_compile(p), n_qubits=2) assert equal_up_to_global_phase(u1, u2, atol=1e-12)
def test_qaoa_unitary(): 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, ] prog = Program([ RY(np.pi / 2, 0), RX(np.pi, 0), RY(np.pi / 2, 1), RX(np.pi, 1), CNOT(0, 1), RX(-np.pi / 2, 1), RY(4.71572463191, 1), RX(np.pi / 2, 1), CNOT(0, 1), RX(-2 * 2.74973750579, 0), RX(-2 * 2.74973750579, 1), ]) test_unitary = program_unitary(prog, n_qubits=2) wf_test = np.zeros(4) wf_test[0] = 1.0 wf_test = test_unitary.dot(wf_test) assert np.allclose(wf_test, wf_true)
def test_random_gates_3(): p = Program(X(2), CNOT(2, 1), CNOT(1, 0)) test_unitary = program_unitary(p, n_qubits=3) # gates are multiplied in 'backwards' order actual_unitary = (np.kron(np.eye(2**1), mat.QUANTUM_GATES["CNOT"]).dot( np.kron(mat.QUANTUM_GATES["CNOT"], np.eye(2**1))).dot( np.kron(mat.QUANTUM_GATES["X"], np.eye(2**2)))) np.testing.assert_allclose(actual_unitary, test_unitary)
def error(order, time_step_length): a_pauli = time_step_length * sZ(0) * sY(1) * sX(2) a_program = a_pauli.program b_pauli = time_step_length * sX(0) * sZ(1) * sY(2) b_program = b_pauli.program num_qubits = len(a_program.get_qubits()) assert num_qubits == len(b_program.get_qubits()) a = program_unitary(a_program, num_qubits) b = program_unitary(b_program, num_qubits) a_plus_b = a + b exp_a_plus_b = expmi(time_step_length * a_plus_b) trotter_program = trotterize(a_pauli, b_pauli, trotter_order=order) trotter = program_unitary(trotter_program, num_qubits) return np.linalg.norm(exp_a_plus_b - trotter, np.inf)
def test_circuit_from_quil(): q0, q1, q2 = LineQubit.range(3) cirq_circuit = Circuit([ I(q0), I(q1), I(q2), X(q0), Y(q1), Z(q2), H(q0), S(q1), T(q2), Z(q0)**(1 / 8), Z(q1)**(1 / 8), Z(q2)**(1 / 8), rx(np.pi / 2)(q0), ry(np.pi / 2)(q1), rz(np.pi / 2)(q2), CZ(q0, q1), CNOT(q1, q2), cphase(np.pi / 2)(q0, q1), cphase00(np.pi / 2)(q1, q2), cphase01(np.pi / 2)(q0, q1), cphase10(np.pi / 2)(q1, q2), ISWAP(q0, q1), pswap(np.pi / 2)(q1, q2), SWAP(q0, q1), xy(np.pi / 2)(q1, q2), CCNOT(q0, q1, q2), CSWAP(q0, q1, q2), MeasurementGate(1, key="ro[0]")(q0), MeasurementGate(1, key="ro[1]")(q1), MeasurementGate(1, key="ro[2]")(q2), ]) # build the same Circuit, using Quil quil_circuit = circuit_from_quil(QUIL_PROGRAM) # test Circuit equivalence assert cirq_circuit == quil_circuit pyquil_circuit = Program(QUIL_PROGRAM) # drop declare and measures, get Program unitary pyquil_unitary = program_unitary(pyquil_circuit[1:-3], n_qubits=3) # fix qubit order convention cirq_circuit_swapped = Circuit(SWAP(q0, q2), cirq_circuit[:-1], SWAP(q0, q2)) # get Circuit unitary cirq_unitary = cirq_circuit_swapped.unitary() # test unitary equivalence assert np.isclose(pyquil_unitary, cirq_unitary).all()
def test_unitary_measure(): prog = Program(Declare("ro", "BIT"), H(0), H(1), MEASURE(0, MemoryReference("ro", 0))) with pytest.raises(ValueError): program_unitary(prog, n_qubits=2)
def test_identity(): p = Program() test_unitary = program_unitary(p, 0) assert np.allclose(test_unitary, np.eye(2**0))
def test_random_gates_2(): p = Program().inst([H(0), X(1), Y(2), Z(3)]) test_unitary = program_unitary(p, n_qubits=4) actual_unitary = np.kron(mat.Z, np.kron(mat.Y, np.kron(mat.X, mat.H))) assert np.allclose(test_unitary, actual_unitary)
def test_RY(): for theta in np.linspace(-2 * np.pi, 2 * np.pi): u1 = program_unitary(Program(RY(theta, 0)), n_qubits=1) u2 = program_unitary(_RY(theta, 0), n_qubits=1) assert equal_up_to_global_phase(u1, u2)
def test_CCNOT(): for perm in itertools.permutations([0, 1, 2]): u1 = program_unitary(Program(CCNOT(*perm)), n_qubits=3) u2 = program_unitary(_CCNOT(*perm), n_qubits=3) assert equal_up_to_global_phase(u1, u2, atol=1e-12)
def test_random_progs(n_qubits, prog_length): for repeat_i in range(10): prog = _generate_random_program(n_qubits=n_qubits, length=prog_length) u1 = program_unitary(prog, n_qubits=n_qubits) u2 = program_unitary(basic_compile(prog), n_qubits=n_qubits) assert equal_up_to_global_phase(u1, u2, atol=1e-12)
def test_Z(): u1 = program_unitary(Program(Z(0)), n_qubits=1) u2 = program_unitary(_Z(0), n_qubits=1) assert equal_up_to_global_phase(u1, u2, atol=1e-12)
def test_quil_to_native_quil(compiler): response = compiler.quil_to_native_quil(BELL_STATE) assert np.allclose(program_unitary(response, n_qubits=2), program_unitary(COMPILED_BELL_STATE, n_qubits=2))
def test_random_gates(): p = Program().inst([H(0), H(1), H(0)]) test_unitary = program_unitary(p, n_qubits=2) actual_unitary = np.kron(mat.H, np.eye(2**1)) assert np.allclose(test_unitary, actual_unitary)