Esempio n. 1
0
def prepare_transpile_response(circuit, provider):
    if provider.lower() in ['rigetti']:
        transpiled_quil = tk_to_pyquil(circuit)
        return {'transpiled-quil': str(transpiled_quil), 'language': "Quil"}
    else:
        # convert the circuit to QASM string
        transpiled_qasm = get_circuit_qasm(circuit)
        return {'transpiled-qasm': transpiled_qasm, 'language': "OpenQASM"}
Esempio n. 2
0
def test_convert(qvm):
    wf_sim = WavefunctionSimulator()
    p = get_test_program()
    initial_wf = wf_sim.wavefunction(p)
    tkc = pyquil_to_tk(p)
    p2 = tk_to_pyquil(tkc)
    final_wf = wf_sim.wavefunction(p2)
    initial_phaseless, final_phaseless = adjust_for_relative_phase(
        initial_wf.amplitudes, final_wf.amplitudes)
    assert np.allclose(initial_phaseless, final_phaseless, atol=1e-10)
Esempio n. 3
0
def test_measure() -> None:
    p = get_test_program(True)
    m_map = {}
    for i in p.instructions:
        if isinstance(i, Measurement):
            m_map[i.qubit] = i.classical_reg.offset  # type: ignore
    tkc = pyquil_to_tk(p)
    p2 = tk_to_pyquil(tkc)
    m_map2 = {}
    for i in p2.instructions:
        if isinstance(i, Measurement):
            m_map2[i.qubit] = i.classical_reg.offset  # type: ignore
    assert m_map == m_map2
Esempio n. 4
0
def test_from_tket() -> None:
    c = Circuit(4, 2)
    c.X(0)
    c.H(1)
    c.S(1)
    c.CX(2, 0)
    c.Ry(0.5, 3)
    c.Measure(3, 0)
    c.Measure(1, 1)
    p = tk_to_pyquil(c)
    assert (
        len(p.instructions) == 8
    )  # 5 gates, 2 measures, and an initial declaration of classical register
Esempio n. 5
0
def test_symbolic(qvm) -> None:
    pi2 = Symbol("pi2")
    pi3 = Symbol("pi3")

    tkc = Circuit(2).Rx(pi2, 1).Rx(-pi3, 1).CX(1, 0)
    RemoveRedundancies().apply(tkc)

    prog = tk_to_pyquil(tkc)
    tkc2 = pyquil_to_tk(prog)

    assert tkc2.free_symbols() == {pi2, pi3}
    tkc2.symbol_substitution({pi2: pi / 2, pi3: -pi / 3})

    backend = ForestStateBackend()
    state1 = backend.get_state(tkc2)
    state0 = np.array(
        [-0.56468689 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.82530523j])
    assert np.allclose(state0, state1)
Esempio n. 6
0
def test_measures_are_last() -> None:
    c = Circuit(2, 2)
    c.Measure(0, 0).X(1).Y(1).Measure(1, 1)
    p = tk_to_pyquil(c)
    assert isinstance(p.instructions[3], Measurement)
    assert isinstance(p.instructions[4], Measurement)