Esempio n. 1
0
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)
Esempio n. 2
0
def test_basic_compile_defgate():
    p = Program()
    p.inst(RX(pi, 0))
    p.defgate("test", [[0, 1], [1, 0]])
    p.inst(("test", 2))
    p.inst(RZ(pi / 2, 0))

    assert p == basic_compile(p)
Esempio n. 3
0
    def executor(program: Program) -> float:
        p = Program()

        # add reset
        if reset:
            p += RESET()

        # add main body program
        p += program.copy()

        # add memory declaration
        qubits = p.get_qubits()
        ro = p.declare("ro", "BIT", len(qubits))

        # add measurements
        for idx, q in enumerate(qubits):
            p += MEASURE(q, ro[idx])

        # add numshots
        p.wrap_in_numshots_loop(shots)

        # nativize the circuit
        p = basic_compile(p)

        # print out nativized program
        if debug:
            print(p)

        # compile the circuit
        b = qc.compiler.native_quil_to_executable(p)

        # run the circuit, collect bitstrings
        qc.reset()
        results = qc.run(b)

        # compute expectation value
        return expectation_fn(results)
Esempio n. 4
0
def test_other_instructions():
    p = Program("DECLARE ro BIT[2]")
    assert p == basic_compile(p)
Esempio n. 5
0
def test_unsupported_gate():
    with pytest.raises(ValueError):
        basic_compile(Program(CSWAP(0, 1, 2)))
Esempio n. 6
0
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)