示例#1
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)
示例#2
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
示例#3
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)