def test_tensor_gates_single_qubit():
    prog = Program().inst([Hgate(0)])
    test_unitary = tensor_gates(gate_matrix, {}, prog.instructions[0], 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.instructions[0], 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.instructions[0], 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.instructions[0], 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)
Esempio n. 2
0
def test_qaoa_density():
    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))
    rho_true = np.dot(wf_true, np.conj(wf_true).T)
    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)
    ])

    qvm = QVMConnection(type_trans='density')
    wf_rho = qvm.density(prog)
    assert np.isclose(rho_true, wf_rho.todense()).all()
Esempio n. 3
0
def get_compiled_prog(theta):
    return Program([
        RZgate(-pi / 2, 0),
        RXgate(-pi / 2, 0),
        RZgate(-pi / 2, 1),
        RXgate(pi / 2, 1),
        CZgate(1, 0),
        RZgate(-pi / 2, 1),
        RXgate(-pi / 2, 1),
        RZgate(theta, 1),
        RXgate(pi / 2, 1),
        CZgate(1, 0),
        RXgate(pi / 2, 0),
        RZgate(pi / 2, 0),
        RZgate(-pi / 2, 1),
        RXgate(pi / 2, 1),
        RZgate(-pi / 2, 1),
    ])
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
    ]
    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)