def test_defgate():
    # regression test for https://github.com/rigetti/pyquil/issues/1059
    theta = np.pi / 2
    U = np.array([[1, 0, 0, 0], [0, 1, 0, 0],
                  [0, 0, np.cos(theta / 2), -1j * np.sin(theta / 2)],
                  [0, 0, -1j * np.sin(theta / 2),
                   np.cos(theta / 2)]])

    gate_definition = DefGate('U_test', U)
    U_test = gate_definition.get_constructor()

    p = Program()
    p += gate_definition
    p += X(1)
    p += U_test(1, 0)
    qam = PyQVM(n_qubits=2, quantum_simulator_type=NumpyWavefunctionSimulator)
    qam.execute(p)
    wf1 = qam.wf_simulator.wf
    should_be = np.zeros((2, 2), dtype=np.complex128)
    one_over_sqrt2 = 1 / np.sqrt(2)
    should_be[0, 1] = one_over_sqrt2
    should_be[1, 1] = -1j * one_over_sqrt2
    np.testing.assert_allclose(wf1, should_be)

    # Ensure the output of the custom U_test gate matches the standard RX gate. Something like
    # RX(theta, 0).controlled(1) would be a more faithful reproduction of U_test, but
    # NumpyWavefunctionSimulator doesn't (yet) support gate modifiers, so just apply the RX gate
    # unconditionally.
    p = Program()
    p += X(1)
    p += RX(theta, 0)
    qam = PyQVM(n_qubits=2, quantum_simulator_type=NumpyWavefunctionSimulator)
    qam.execute(p)
    wf2 = qam.wf_simulator.wf
    np.testing.assert_allclose(wf1, wf2)
Exemple #2
0
def test_defgate():
    dg = DefGate("TEST", np.array([[1., 0.],
                                   [0., 1.]]))
    assert dg.out() == "DEFGATE TEST:\n    1.0, 0.0\n    0.0, 1.0\n"
    test = dg.get_constructor()
    tg = test(DirectQubit(1), DirectQubit(2))
    assert tg.out() == "TEST 1 2"
Exemple #3
0
def test_defgate():
    dg = DefGate("TEST", np.array([[0 + 0.5j, 0.5], [0.5, 0 - 0.5j]]))
    assert dg.out(
    ) == "DEFGATE TEST:\n    0.0+0.5i, 0.5+0.0i\n    0.5+0.0i, 0.0-0.5i\n"
    test = dg.get_constructor()
    tg = test(DirectQubit(1), DirectQubit(2))
    assert tg.out() == "TEST 1 2"
Exemple #4
0
def controlled_sY(program):
    csy = np.array([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 0., -1.j],
                    [0., 0., 1.j, 0.]])

    dg = DefGate('CSY', csy)
    program.inst(dg)

    return dg.get_constructor()
Exemple #5
0
def controlled_i_X(n, a, b):
    theta = Parameter('theta')
    cirx = controlled_i(
        np.array([[quil_cos(theta / 2), -1j * quil_sin(theta / 2)],
                  [-1j * quil_sin(theta / 2),
                   quil_cos(theta / 2)]]))
    CIRX = DefGate('CIRX', cirx, [theta])
    return str(CIRX) + '\n' + str(CIRX.get_constructor()(n)(a, b))
Exemple #6
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/rigetticomputing/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/rigetticomputing/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'
Exemple #7
0
def controlled_Ry(program):
    theta = Parameter('theta')

    cry = np.array([[1., 0., 0., 0.], [0., 1., 0., 0.],
                    [0., 0.,
                     quil_cos(0.5 * theta),
                     quil_sin(0.5 * theta)],
                    [0., 0., -quil_sin(0.5 * theta),
                     quil_cos(0.5 * theta)]])

    dg = DefGate('CRY', cry, [theta])
    program.inst(dg)

    return dg.get_constructor()
Exemple #8
0
def get_combined_gate(matrix: np.ndarray, name: str) -> Gate:
    """
    :param matrix: The matrix of the combined gate  
    :param name: The of the combined gate
    :return: A Gate (matrix, noisy_name) corresponding to the representation [matrix, name]
            
    :rtype: Gate
    """

    p = Program()
    combined_gate_definition = DefGate(name, matrix)
    p.inst(combined_gate_definition)
    combined_gate = combined_gate_definition.get_constructor()
    return combined_gate
Exemple #9
0
def test_def_gate_with_parameters():
    theta = Parameter('theta')
    rx = np.array([[quil_cos(theta / 2), -1j * quil_sin(theta / 2)],
                   [-1j * quil_sin(theta / 2), quil_cos(theta / 2)]])

    p = Program().defgate("RX", rx, [theta])
    assert p.out() == 'DEFGATE RX(%theta):\n' \
                      '    cos(%theta/2), -i*sin(%theta/2)\n' \
                      '    -i*sin(%theta/2), cos(%theta/2)\n\n'

    dg = DefGate('MY_RX', rx, [theta])
    MY_RX = dg.get_constructor()
    p = Program().inst(MY_RX(np.pi)(0))
    assert p.out() == 'MY_RX(pi) 0\n'
Exemple #10
0
def test_def_gate_with_parameters():
    theta = Parameter("theta")
    rx = np.array([
        [quil_cos(theta / 2), -1j * quil_sin(theta / 2)],
        [-1j * quil_sin(theta / 2),
         quil_cos(theta / 2)],
    ])

    p = Program().defgate("RX", rx, [theta])
    assert (p.out() == "DEFGATE RX(%theta):\n"
            "    COS(%theta/2), -i*SIN(%theta/2)\n"
            "    -i*SIN(%theta/2), COS(%theta/2)\n\n")

    dg = DefGate("MY_RX", rx, [theta])
    MY_RX = dg.get_constructor()
    p = Program().inst(MY_RX(np.pi)(0))
    assert p.out() == "MY_RX(pi) 0\n"
def state_two_prep(state, qubits):
    if not isinstance(state, Program):
        print("The input *state* must be in the form of a PyQuil Program")
    # Define the new gate from a matrix
    theta = Parameter('theta')
    crtest = np.array([[1, 0, 0, 0], [0, 1, 0, 0],
                       [0, 0, quil_cos(theta / 2), -quil_sin(theta / 2)],
                       [0, 0, quil_sin(theta / 2),
                        quil_cos(theta / 2)]])

    gate_definition = DefGate('CRTEST', crtest, [theta])
    CRTEST = gate_definition.get_constructor()

    state += gate_definition
    state += H(qubits[0])
    state += Z(qubits[0])
    state += CRTEST(np.pi / 42)(qubits[0], qubits[1])

    return state
def u2_replacement(phi: float, lam: float):
    """ implemented with a custom gate """
    # implemented with X90 pulse: https://qiskit.org/documentation/stubs/qiskit.circuit.library.U2Gate.html
    # p = Program()
    # p += RZ(phi + np.pi/2, 0)
    # p += RX(np.pi/2, 0)
    # p += RZ(lam - np.pi/2, 0)

    phi_param = Parameter('phi')
    lam_param = Parameter('lam')
    matrix = np.array(
        [[1 / np.sqrt(2), -quil_exp(1j * lam_param) * 1 / np.sqrt(2)],
         [
             quil_exp(1j * phi_param) * 1 / np.sqrt(2),
             quil_exp(1j * (phi_param + lam_param)) * 1 / np.sqrt(2)
         ]])
    definition = DefGate('U2', matrix, [phi_param, lam_param])
    U2 = definition.get_constructor()
    p = Program()
    p += definition
    p += U2(phi, lam)(0)

    return p
def u3_replacement(theta: float, phi: float, lam: float):
    """ implemented with a custom gate """

    # implemented with two X90 pulse: https://arxiv.org/pdf/1707.03429.pdf
    # p = Program()
    # p += RZ(phi + 3*np.pi, 0)
    # p += RX(np.pi/2, 0)
    # p += RZ(np.pi + theta, 0)
    # p += RX(np.pi/2, 0)
    # p += RZ(lam, 0)
    # formula from https://qiskit.org/documentation/stubs/qiskit.circuit.library.U3Gate.html (13.07.2020) gives wrong results
    # p = Program()
    # p += RZ(phi - np.pi/2, 0)
    # p += RX(np.pi/2, 0)
    # p += RZ(np.pi - theta, 0)
    # p += RX(np.pi/2, 0)
    # p += RZ(lam - np.pi/2, 0)

    theta_param = Parameter('theta')
    phi_param = Parameter('phi')
    lam_param = Parameter('lam')
    matrix = np.array(
        [[
            quil_cos(theta_param / 2),
            -quil_exp(1j * lam_param) * quil_sin(theta_param / 2)
        ],
         [
             quil_exp(1j * phi_param) * quil_sin(theta_param / 2),
             quil_exp(1j * (phi_param + lam_param)) * quil_cos(theta_param / 2)
         ]])
    definition = DefGate('U3', matrix, [theta_param, phi_param, lam_param])
    U3 = definition.get_constructor()
    p = Program()
    p += definition
    p += U3(theta, phi, lam)(0)

    return p
Exemple #14
0
def test_defgate_param():
    dgp = DefGate("TEST", [[1.0, 0.0], [0.0, 1.0]])
    assert dgp.out() == "DEFGATE TEST:\n    1.0, 0\n    0, 1.0\n"
    test = dgp.get_constructor()
    tg = test(Qubit(1))
    assert tg.out() == "TEST 1"
Exemple #15
0
qvm = QVMConnection()

k = Parameter('k')
ccrk = np.array([
    [1, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 1, 0, 0, 0, 0],
    [0, 0, 0, 0, 1, 0, 0, 0],
    [0, 0, 0, 0, 0, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 1, 0],
    [0, 0, 0, 0, 0, 0, 0,
     quil_exp((2 * np.pi * 1j) / (2**k))],
])
ccrk_gate_def = DefGate('CCRK', ccrk, [k])
CCRK = ccrk_gate_def.get_constructor()

crk = np.array([
    [1, 0, 0, 0],
    [0, 1, 0, 0],
    [0, 0, 1, 0],
    [0, 0, 0, quil_exp((2 * np.pi * 1j) / (2**k))],
])
crk_gate_def = DefGate('CRK', crk, [k])
CRK = crk_gate_def.get_constructor()

rk = np.array([
    [1, 0],
    [0, quil_exp((2 * np.pi * 1j) / (2**k))],
])
rk_gate_def = DefGate('RK', rk, [k])
Exemple #16
0
from pyquil.quil import Program
from pyquil.gates import *
from pyquil.parameters import Parameter, quil_sin, quil_cos
from pyquil.quilbase import DefGate
#from pyquil.api import QVMConnection
from referenceqvm.api import QVMConnection
import numpy as np
theta = Parameter('theta')
cry = np.array([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, quil_cos(
    theta / 2), -1 * quil_sin(theta / 2)], [0.0, 0.0, quil_sin(theta / 2), quil_cos(theta / 2)]])
dg = DefGate('CRY', cry, [theta])
CRY = dg.get_constructor()
p = Program()
p.inst(dg)
p.inst(X(0))
p.inst(X(1))
p.inst(CRY(4.304)(0, 2))
qvm = QVMConnection()
wf = qvm.wavefunction(p)
print(wf)
Exemple #17
0
from pyquil.quil import Program
from pyquil.gates import *
from pyquil.parameters import Parameter, quil_sin, quil_cos
from pyquil.quilbase import DefGate
#from pyquil.api import QVMConnection
from referenceqvm.api import QVMConnection
import numpy as np
theta = Parameter('theta')
cry = np.array([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0],
                [0.0, 0.0,
                 quil_cos(theta / 2), -1 * quil_sin(theta / 2)],
                [0.0, 0.0, quil_sin(theta / 2),
                 quil_cos(theta / 2)]])
dg = DefGate('CRY', cry, [theta])
CRY = dg.get_constructor()
p = Program()
p.inst(dg)
p.inst(X(0))
p.inst(X(1))
p.inst(CRY(4.304)(0, 2))
qvm = QVMConnection()
wf = qvm.wavefunction(p)
print(wf)
Exemple #18
0
def forward_prop(weights, initialization=None):
    LOGGER.info("Connecting to the QVM...")
    qvm = QVMConnection()
    LOGGER.info("... done")
    LOGGER.info(" ")

    LOGGER.info("Initialising quantum program...")
    p = Program()

    LOGGER.info("... defining custom gates")
    LOGGER.info("... controlled Ry")
    CRY = controlled_Ry(p)
    LOGGER.info("... controlled sY")
    CSY = controlled_sY(p)
    LOGGER.info("... done")
    LOGGER.info(" ")

    a = -1
    rotation = 0.5 * np.pi * (a + 1)
    gamma = 1 / NUM_INPUT

    W1 = weights[:NUM_INPUT * NUM_HIDDEN].reshape((NUM_INPUT, NUM_HIDDEN))
    b1 = weights[NUM_INPUT * NUM_HIDDEN]
    W2 = weights[NUM_INPUT * NUM_HIDDEN + 1:NUM_INPUT * NUM_HIDDEN + 1 +
                 NUM_HIDDEN * NUM_OUTPUT].reshape(NUM_HIDDEN, NUM_OUTPUT)
    b2 = weights[-1]

    # INITIALISE INPUT
    if initialization == None:
        EDI = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0,
                                                                   1]])
        dg = DefGate("EDI", EDI)
        EDI = dg.get_constructor()

        p.inst(dg)
        p.inst(H(0))
        p.inst(H(1))

        p.inst(EDI(0, 1))
    if initialization == "test":
        # p.inst(X(1))  # |01>
        p.inst(X(0))  # |10>
        # pass

    # INTIALISE LABELS
    p.inst(H(NUM_INPUT + NUM_HIDDEN + NUM_OUTPUT))

    classical_flag_register = ANCILLARY_BIT + 1
    # Write out the loop initialization and body programs:
    for n in range(NUM_HIDDEN):
        loop_body = Program()
        for w in range(NUM_INPUT):
            loop_body.inst(CRY(4. * gamma * W1[w, n])(w, ANCILLARY_BIT))
        loop_body.inst(RY(2. * gamma * b1)(ANCILLARY_BIT))

        loop_body.inst(CSY(ANCILLARY_BIT, NUM_INPUT + n))
        loop_body.inst(RZ(-0.5 * np.pi)(ANCILLARY_BIT))
        for w in range(NUM_INPUT):
            loop_body.inst(CRY(-4. * gamma * W1[w, n])(w, ANCILLARY_BIT))
        loop_body.inst(RY(-2. * gamma * b1)(ANCILLARY_BIT))

        loop_body.measure(ANCILLARY_BIT, classical_flag_register)

        then_branch = Program(RY(-0.5 * np.pi)(NUM_INPUT + n))
        then_branch.inst(X(ANCILLARY_BIT))
        else_branch = Program()

        # Add the conditional branching:
        loop_body.if_then(classical_flag_register, then_branch, else_branch)

        init_register = Program(TRUE([classical_flag_register]))
        loop_prog = init_register.while_do(classical_flag_register, loop_body)
        p.inst(loop_prog)

    # Write out the loop initialization and body programs:
    for n in range(NUM_OUTPUT):
        loop_body = Program()
        for w in range(NUM_HIDDEN):
            loop_body.inst(CRY(4. * gamma * W2[w, n])(w, ANCILLARY_BIT))
        loop_body.inst(RY(2. * gamma * b2)(ANCILLARY_BIT))

        loop_body.inst(CSY(ANCILLARY_BIT, NUM_INPUT + NUM_HIDDEN + n))
        loop_body.inst(RZ(-0.5 * np.pi)(ANCILLARY_BIT))
        for w in range(NUM_HIDDEN):
            loop_body.inst(CRY(-4. * gamma * W2[w, n])(w, ANCILLARY_BIT))
        loop_body.inst(RY(-2. * gamma * b1)(ANCILLARY_BIT))

        loop_body.measure(ANCILLARY_BIT, classical_flag_register)

        then_branch = Program(RY(-0.5 * np.pi)(NUM_INPUT + NUM_HIDDEN + n))
        then_branch.inst(X(ANCILLARY_BIT))
        else_branch = Program()

        # Add the conditional branching:
        loop_body.if_then(classical_flag_register, then_branch, else_branch)

        init_register = Program(TRUE([classical_flag_register]))
        loop_prog = init_register.while_do(classical_flag_register, loop_body)
        p.inst(loop_prog)

    p.measure(NUM_INPUT + NUM_HIDDEN, 0)

    LOGGER.info("... executing on the QVM")
    classical_regs = [0]
    output = qvm.run(p, classical_regs)
    LOGGER.info("... %s", output)
    LOGGER.info("")

    return output[0][0]
Exemple #19
0
def compute_circuit(angles_vector_in_degrees_str):
    rotation_deg_of_freedom = 28
    a = [0] * rotation_deg_of_freedom
    for i in range(rotation_deg_of_freedom):
        a[i] = radians(float(angles_vector_in_degrees_str[i]))

    theta = Parameter('theta')

    anot = np.array([[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])

    aary = np.array(
        [[quil_cos(theta / 2), -1 * quil_sin(theta / 2), 0, 0, 0, 0, 0, 0],
         [quil_sin(theta / 2),
          quil_cos(theta / 2), 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0],
         [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0],
         [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0],
         [0, 0, 0, 0, 0, 0, 0, 1]])

    ccry = np.array(
        [[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0],
         [0, 0, 1, 0, 0, 0, 0,
          0], [0, 0, 0, 1, 0, 0, 0,
               0],
         [0, 0, 0, 0, 1, 0, 0,
          0], [0, 0, 0, 0, 0, 1, 0,
               0],
         [0, 0, 0, 0, 0, 0,
          quil_cos(theta / 2), -1 * quil_sin(theta / 2)],
         [0, 0, 0, 0, 0, 0,
          quil_sin(theta / 2),
          quil_cos(theta / 2)]])

    #TODO: Ascertain what kind of gates this is?
    cary = np.array(
        [[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0],
         [0, 0, 1, 0, 0, 0,
          0, 0],
         [0, 0, 0,
          quil_cos(theta / 2), -1 * quil_sin(theta / 2), 0, 0, 0],
         [0, 0, 0, quil_sin(theta / 2),
          quil_cos(theta / 2), 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0],
         [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1]])

    dg_anot = DefGate('ANOT', anot)
    dg_aary = DefGate('AARY', aary, [theta])
    dg_ccry = DefGate('CCRY', ccry, [theta])
    dg_cary = DefGate('CARY', cary, [theta])

    ANOT = dg_anot.get_constructor()
    AARY = dg_aary.get_constructor()
    CCRY = dg_ccry.get_constructor()
    CARY = dg_cary.get_constructor()

    qvm = api.QVMConnection()
    p = pq.Program()

    p.inst(dg_anot)
    p.inst(dg_aary)
    p.inst(dg_ccry)
    p.inst(dg_cary)

    #p.inst(X(0))
    #p.inst(X(1))
    #p.inst(X(2))

    # CD rotation
    p.inst(AARY(a[0] * 2)(2, 1, 0))

    # CE rotation
    p.inst(AARY(a[1] * 2)(2, 0, 1))

    # CF rotation
    p.inst(CNOT(1, 0))
    p.inst(AARY(a[2] * 2)(0, 2, 1))
    p.inst(CNOT(1, 0))

    # CG rotation
    p.inst(AARY(a[3] * 2)(0, 1, 2))

    # CA rotation
    p.inst(CNOT(2, 0))
    p.inst(AARY(a[4] * 2)(0, 1, 2))
    p.inst(CNOT(2, 0))

    # CB rotation
    p.inst(CNOT(2, 1))
    p.inst(AARY(a[5] * 2)(0, 1, 2))
    p.inst(CNOT(2, 1))

    # CC' rotation
    p.inst(CNOT(1, 0))
    p.inst(CNOT(1, 2))
    p.inst(AARY(a[6] * 2)(0, 2, 1))
    p.inst(CNOT(1, 2))
    p.inst(CNOT(1, 0))

    # DE rotation
    p.inst(ANOT(1, 0))
    p.inst(AARY(a[7] * 2)(0, 2, 1))
    p.inst(ANOT(1, 0))

    # DF rotation
    p.inst(X(2))
    p.inst(CCRY(a[8] * 2)(0, 2, 1))
    p.inst(X(2))

    # DG rotation
    p.inst(ANOT(2, 0))
    p.inst(AARY(a[9] * 2)(0, 1, 2))
    p.inst(ANOT(2, 0))

    # DA rotation
    p.inst(X(1))
    p.inst(CCRY(a[10] * 2)(0, 1, 2))
    p.inst(X(1))

    # DB rotation
    p.inst(CNOT(1, 0))
    p.inst(ANOT(1, 2))
    p.inst(CCRY(a[11] * 2)(0, 2, 1))
    p.inst(ANOT(1, 2))
    p.inst(CNOT(1, 0))

    # DC' rotation
    p.inst(ANOT(1, 2))
    p.inst(CCRY(a[12] * 2)(0, 2, 1))
    p.inst(ANOT(1, 2))

    # EF rotation
    p.inst(X(2))
    p.inst(CCRY(a[13] * 2)(1, 2, 0))
    p.inst(X(2))

    # EG rotation
    p.inst(ANOT(2, 1))
    p.inst(AARY(a[14] * 2)(0, 1, 2))
    p.inst(ANOT(2, 1))

    # EA rotation
    p.inst(CNOT(0, 1))
    p.inst(ANOT(0, 2))
    p.inst(CCRY(a[15] * 2)(1, 2, 0))
    p.inst(ANOT(0, 2))
    p.inst(CNOT(0, 1))

    # EB rotation
    p.inst(X(0))
    p.inst(CCRY(a[16] * 2)(0, 1, 2))
    p.inst(X(0))

    # EC' rotation
    p.inst(ANOT(0, 2))
    p.inst(CCRY(a[17] * 2)(1, 2, 0))
    p.inst(ANOT(0, 2))

    # FG rotation
    p.inst(CARY(a[18] * 2)(2, 1, 0))

    # FA rotation
    p.inst(CNOT(2, 1))
    p.inst(CCRY(a[19] * 2)(0, 1, 2))
    p.inst(CNOT(2, 1))

    # FB rotation
    p.inst(CNOT(2, 0))
    p.inst(CCRY(a[20] * 2)(0, 1, 2))
    p.inst(CNOT(2, 0))

    # FC' rotation
    p.inst(CCRY(a[21] * 2)(0, 1, 2))

    # GA rotation
    p.inst(X(1))
    p.inst(CCRY(a[22] * 2)(1, 2, 0))
    p.inst(X(1))

    # GB rotation
    p.inst(X(0))
    p.inst(CCRY(a[23] * 2)(0, 2, 1))
    p.inst(X(0))

    # GC' rotation
    p.inst(ANOT(1, 0))
    p.inst(CCRY(a[24] * 2)(0, 2, 1))
    p.inst(ANOT(1, 0))

    # AB rotation
    p.inst(CNOT(1, 0))
    p.inst(CCRY(a[25] * 2)(0, 2, 1))
    p.inst(CNOT(1, 0))

    # AC' rotation
    p.inst(CCRY(a[26] * 2)(0, 2, 1))

    # BC' rotation
    p.inst(CCRY(a[27] * 2)(1, 2, 0))

    wavefunction = qvm.wavefunction(p)
    print(wavefunction)

    return p
Exemple #20
0
def controlled_X(n, a, b):
    theta = Parameter('theta')
    crx = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, quil_cos(theta / 2), -1j * quil_sin(theta / 2)], [0, 0, -1j * quil_sin(theta / 2), quil_cos(theta / 2)]])
    CRX = DefGate('CRX', crx, [theta])
    return str(CRX) + '\n' + str(CRX.get_constructor()(n)(a, b))
Exemple #21
0
theta = Parameter('theta')

cry = np.array([[1, 0, 0, 0], [0, 1, 0, 0],
                [0, 0, quil_cos(theta / 2), -quil_sin(theta / 2)],
                [0, 0, quil_sin(theta / 2),
                 quil_cos(theta / 2)]])

crx = np.array([[1, 0, 0, 0], [0, 1, 0, 0],
                [0, 0, quil_cos(theta / 2), -1j * quil_sin(theta / 2)],
                [0, 0, -1j * quil_sin(theta / 2),
                 quil_cos(theta / 2)]])

crz = np.array([[1, 0, 0, 0], [0, 1, 0, 0],
                [0, 0, quil_cos(theta / 2) - 1j * quil_sin(theta / 2), 0],
                [0, 0, 0,
                 quil_cos(theta / 2) + 1j * quil_sin(theta / 2)]])

cy = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0 - 1j],
               [0, 0, 0 + 1j, 0]])

dg_cry = DefGate("CRY", cry, [theta])
dg_crx = DefGate("CRX", crx, [theta])
dg_crz = DefGate("CRZ", crz, [theta])
dg_cy = DefGate("CY", cy)
# print(type(dg_cry))
# print(dg_cry)

CRY = dg_cry.get_constructor()
CRX = dg_crx.get_constructor()
CRZ = dg_crz.get_constructor()
CY = dg_cy.get_constructor()
# Using the cartesian rotation gate of the form:

from pyquil.parameters import Parameter, quil_sin, quil_cos
from pyquil.quilbase import DefGate
from pyquil.gates import *
from math import pi
import numpy as np

theta = Paramter('theta')
crx = np.array([[1, 0, 0, 0],
                [0, 1, 0, 0],
                [0, 0, quil_cos(theta/2), -1j * quil_sin(theta/2)],
                 [0, 0, -1j * quil_sin(theta/2), quil_cos(theta/2)]])

dg = DefGate('CRX', crx, [theta])
CRX = dg.get_constructor()


# Would it make more sense to use QFT? Depends on how entanglement is used?

def qft3(q0, q1, q2):
    p = Program()
    p.inst( H(q2),
        CPHASE(pi/2.0, q1, q2),
        H(q1),
        CPHASE(pi/4.0, q0, q2),
        CPHASE(pi/2, q0, q1),
        H(q0),
        SWAP(q0, q2) )
    return p
Exemple #23
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
""")
from pyquil import Program
from pyquil.parameters import Parameter, quil_sin, quil_cos
from pyquil.quilbase import DefGate
from pyquil.gates import *
import numpy as np

thetaParameter = Parameter('theta')
controlledRx = np.array(
    [[1, 0, 0, 0], [0, 1, 0, 0],
     [0, 0,
      quil_cos(thetaParameter / 2), -1j * quil_sin(thetaParameter / 2)],
     [0, 0, -1j * quil_sin(thetaParameter / 2),
      quil_cos(thetaParameter / 2)]])

gate_definition = DefGate('CRX', controlledRx, [thetaParameter])
CONTROLRX = gate_definition.get_constructor()

program = Program()
program = program + gate_definition
program = program + H(0)
program = program + CONTROLRX(np.pi / 2)(0, 1)

print(program)