def get_test_program(measure: bool = False) -> Program:
    PI = float(pi.evalf())
    p = Program()
    p += X(0)
    p += Y(1)
    p += Z(2)
    p += H(3)
    p += S(0)
    p += T(1)
    p += RX(PI / 2, 2)
    p += RY(PI / 2, 3)
    p += RZ(PI / 2, 0)
    p += CZ(0, 1)
    p += CNOT(2, 3)
    p += CCNOT(0, 1, 2)
    p += CPHASE(PI / 4, 2, 1)
    p += SWAP(0, 3)
    if measure:
        ro = p.declare("ro", "BIT", 4)
        p += MEASURE(0, ro[0])
        p += MEASURE(3, ro[1])
        p += MEASURE(2, ro[2])
        p += MEASURE(1, ro[3])
    return p
Esempio n. 2
0
def test_prog_merge():
    prog_0 = Program(X(0))
    prog_1 = Program(Y(0))
    assert merge_programs([prog_0, prog_1]).out() == (prog_0 + prog_1).out()
    test_def = DefGate("test", np.eye(2))
    TEST = test_def.get_constructor()
    prog_0.inst(test_def)
    prog_0.inst(TEST(0))
    prog_1.inst(test_def)
    prog_1.inst(TEST(0))
    assert (merge_programs([prog_0, prog_1]).out() == """DEFGATE test:
    1.0, 0
    0, 1.0

X 0
test 0
Y 0
test 0
""")
    perm_def = DefPermutationGate("PERM", [0, 1, 3, 2])
    PERM = perm_def.get_constructor()
    prog_0.inst(perm_def)
    prog_0.inst(PERM(0, 1))
    prog_1.inst(perm_def)
    prog_1.inst(PERM(1, 0))
    assert (merge_programs([prog_0,
                            prog_1]).out() == """DEFGATE PERM AS PERMUTATION:
    0, 1, 3, 2
DEFGATE test:
    1.0, 0
    0, 1.0

X 0
test 0
PERM 0 1
Y 0
test 0
PERM 1 0
""")
    assert (merge_programs([
        Program("DECLARE ro BIT[1]"),
        Program("H 0"),
        Program("MEASURE 0 ro[0]")
    ]).out() == """DECLARE ro BIT[1]
H 0
MEASURE 0 ro[0]
""")

    q0 = QubitPlaceholder()
    q0_str = "{" + str(q0) + "}"
    p0 = Program(X(q0))
    p1 = Program(Z(q0))
    merged = merge_programs([p0, p1])
    assert (str(merged) == f"""X {q0_str}
Z {q0_str}
""")
    assert (address_qubits(merged, {q0: 1}).out() == """X 1
Z 1
""")
    q1 = QubitPlaceholder()
    p2 = Program(Z(q1))
    assert (address_qubits(merge_programs([p0, p2]), {
        q0: 1,
        q1: 2
    }).out() == """X 1
Z 2
""")
    p0 = address_qubits(p0, {q0: 2})
    p1 = address_qubits(p1, {q0: 1})
    assert (merge_programs([p0, p1]).out() == """X 2
Z 1
""")
Esempio n. 3
0
def test_singles():
    p = Program(I(0), X(0), Y(1), Z(1), H(2), T(2), S(1))
    assert p.out() == "I 0\nX 0\nY 1\nZ 1\nH 2\nT 2\nS 1\n"
Esempio n. 4
0
def test_program_string():
    p = Program()
    p.inst("Y 0", "X 1")
    assert len(p.instructions) == 2
    assert p.instructions == [Y(0), X(1)]
    assert p.out() == "Y 0\nX 1\n"
Esempio n. 5
0
def test_plus_operator():
    p = Program()
    p += H(0)
    p += [X(0), Y(0), Z(0)]
    assert len(p) == 4
    assert p.out() == "H 0\nX 0\nY 0\nZ 0\n"
Esempio n. 6
0
def test_prog_merge():
    prog_0 = Program(X(0))
    prog_1 = Program(Y(0))
    assert merge_programs([prog_0, prog_1]).out() == (prog_0 + prog_1).out()
Esempio n. 7
0
def test_iteration():
    gate_list = [H(0), Y(1), CNOT(0, 1)]
    program = Program(gate_list)
    for ii, instruction in enumerate(program):
        assert instruction == gate_list[ii]
Esempio n. 8
0
def test_indexing():
    program = Program(H(0), Y(1), CNOT(0, 1)).measure(0, 0).if_then(0, Program(X(0)), Program())
    assert program[0] == H(0)
    for ii, instr in enumerate(program.instructions):
        assert program[ii] == instr
Esempio n. 9
0
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)
Esempio n. 10
0
def pauliY(q):
    for i in range(0, len(states)):
        states[i] = states[i] + Y(q)
Esempio n. 11
0
def test_dagger():
    # these gates are their own inverses
    p = Program().inst(I(0), X(0), Y(0), Z(0), H(0), CNOT(0, 1),
                       CCNOT(0, 1, 2), SWAP(0, 1), CSWAP(0, 1, 2))
    assert p.dagger().out() == 'CSWAP 0 1 2\nSWAP 0 1\n' \
                               'CCNOT 0 1 2\nCNOT 0 1\nH 0\n' \
                               'Z 0\nY 0\nX 0\nI 0\n'

    # these gates require negating a parameter
    p = Program().inst(PHASE(pi, 0), RX(pi, 0), RY(pi, 0), RZ(pi, 0),
                       CPHASE(pi, 0, 1), CPHASE00(pi, 0, 1),
                       CPHASE01(pi, 0, 1), CPHASE10(pi, 0, 1), PSWAP(pi, 0, 1))
    assert p.dagger().out() == 'PSWAP(-pi) 0 1\n' \
                               'CPHASE10(-pi) 0 1\n' \
                               'CPHASE01(-pi) 0 1\n' \
                               'CPHASE00(-pi) 0 1\n' \
                               'CPHASE(-pi) 0 1\n' \
                               'RZ(-pi) 0\n' \
                               'RY(-pi) 0\n' \
                               'RX(-pi) 0\n' \
                               'PHASE(-pi) 0\n'

    # these gates are special cases
    p = Program().inst(S(0), T(0), ISWAP(0, 1))
    assert p.dagger().out() == 'PSWAP(pi/2) 0 1\n' \
                               'RZ(pi/4) 0\n' \
                               'PHASE(-pi/2) 0\n'

    # must invert defined gates
    G = np.array([[0, 1], [0 + 1j, 0]])
    p = Program().defgate("G", G).inst(("G", 0))
    assert p.dagger().out() == 'DEFGATE G-INV:\n' \
                               '    0.0, -i\n' \
                               '    1.0, 0.0\n\n' \
                               'G-INV 0\n'

    # can also pass in a list of inverses
    inv_dict = {"G": "J"}
    p = Program().defgate("G", G).inst(("G", 0))
    assert p.dagger(inv_dict=inv_dict).out() == 'J 0\n'

    # defined parameterized gates cannot auto generate daggered version https://github.com/rigetti/pyquil/issues/304
    theta = Parameter('theta')
    gparam_matrix = np.array([[quil_cos(theta / 2), -1j * quil_sin(theta / 2)],
                              [-1j * quil_sin(theta / 2),
                               quil_cos(theta / 2)]])
    g_param_def = DefGate('GPARAM', gparam_matrix, [theta])
    p = Program(g_param_def)
    with pytest.raises(TypeError):
        p.dagger()

    # defined parameterized gates should passback parameters https://github.com/rigetti/pyquil/issues/304
    GPARAM = g_param_def.get_constructor()
    p = Program(GPARAM(pi)(1, 2))
    assert p.dagger().out() == 'GPARAM-INV(pi) 1 2\n'

    # ensure that modifiers are preserved https://github.com/rigetti/pyquil/pull/914
    p = Program()
    control = 0
    target = 1
    cnot_control = 2
    p += X(target).controlled(control)
    p += Y(target).controlled(control)
    p += Z(target).controlled(control)
    p += H(target).controlled(control)
    p += S(target).controlled(control)
    p += T(target).controlled(control)
    p += PHASE(pi, target).controlled(control)
    p += CNOT(cnot_control, target).controlled(control)
    assert p.dagger().out() == 'CONTROLLED CNOT 0 2 1\n' \
                               'CONTROLLED PHASE(-pi) 0 1\n' \
                               'CONTROLLED RZ(pi/4) 0 1\n' \
                               'CONTROLLED PHASE(-pi/2) 0 1\n' \
                               'CONTROLLED H 0 1\n' \
                               'CONTROLLED Z 0 1\n' \
                               'CONTROLLED Y 0 1\n' \
                               'CONTROLLED X 0 1\n'
Esempio n. 12
0
def test_Y():
    u1 = program_unitary(Program(Y(0)), n_qubits=1)
    u2 = program_unitary(_Y(0), n_qubits=1)
    assert equal_up_to_global_phase(u1, u2, atol=1e-12)
Esempio n. 13
0
p = Program(I(0), I(1), I(2), I(3))
wavefunction = quantum_simulator.wavefunction(p)
print("The quantum state is of dimenstion:", len(wavefunction.amplitudes))
p = Program()
for x in range(10):
    p.inst(I(x))
wavefunction = quantum_simulator.wavefunction(p)
print("The quantum state is of dimenstion", len(wavefunction.amplitudes))

# wavefunction(Program) returns a coefficient array that corresponds to outcomes in the following order
wavefunction = quantum_simulator.wavefunction(Program(I(0), I(1)))
print(wavefunction.get_outcome_probs())

# Gates
from pyquil.gates import X, Y, Z
p = Program(X(0))
wavefunction = quantum_simulator.wavefunction(p)
print("X|0 =", wavefunction)
print("The outcome probabilities are ", wavefunction.get_outcome_probs())
print("This looks like a bit flip. \n")
p = Program(Y(0))
wavefunction = quantum_simulator.wavefunction(p)
print("Y|0> =", wavefunction)
print("The outcome probabilities are ", wavefunction.get_outcome_probs())
print("This also looks like a bit flip .\n")
p = Program(Z(0))
wavefunction = quantum_simulator.wavefunction(p)
print("Z|0> = ", wavefunction)
print("The outcome probabilities are ", wavefunction.get_outcome_probs())
print("This state looks unchanged.\n")
Esempio n. 14
0
from pyquil.quil import Program
from pyquil.gates import X, Y, Z, MEASURE

print("Multiple inst arguments with final measurement:")
print(Program().inst(X(0), Y(1), Z(0)).measure(0, 1))

print("Chained inst with explicit MEASURE instruction:")
print(Program().inst(X(0)).inst(Y(1)).measure(0, 1).inst(MEASURE(1, 2)))

print("A mix of chained inst and measures:")
print(Program().inst(X(0)).measure(0, 1).inst(Y(1), X(0)).measure(0, 0))

print("A composition of two programs:")
print(Program(X(0)) + Program(Y(0)))