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_all_close_up_to_global_phase(u1, u2, atol=1e-12)
def test_SWAP(): u1 = program_unitary(Program(SWAP(0, 1)), n_qubits=2) u2 = program_unitary(_SWAP(0, 1), n_qubits=2) assert_all_close_up_to_global_phase(u1, u2, atol=1e-12) u1 = program_unitary(Program(SWAP(1, 0)), n_qubits=2) u2 = program_unitary(_SWAP(1, 0), n_qubits=2) assert_all_close_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 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 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_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_all_close_up_to_global_phase(u1, u2, atol=1e-12)
def test_T(): u1 = program_unitary(Program(T(0)), n_qubits=1) u2 = program_unitary(_T(0), n_qubits=1) assert_all_close_up_to_global_phase(u1, u2, atol=1e-12)
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_all_close_up_to_global_phase(u1, u2)
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(): 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)
def test_unitary_measure(): prog = Program(H(0), H(1), MEASURE(0, 0)) with pytest.raises(ValueError): program_unitary(prog, n_qubits=2)