Ejemplo n.º 1
0
def test_unary_classicals():
    p = Program()
    p.inst(TRUE(0), FALSE(Addr(1)), NOT(Addr(2)), NEG(Addr(3)))
    assert p.out() == 'MOVE ro[0] 1\n' \
                      'MOVE ro[1] 0\n' \
                      'NOT ro[2]\n' \
                      'NEG ro[3]\n'
Ejemplo n.º 2
0
def test_unary_classicals():
    p = Program()
    p.inst(TRUE(0),
           FALSE(Addr(1)),
           NOT(2))
    assert p.out() == 'TRUE [0]\n' \
                      'FALSE [1]\n' \
                      'NOT [2]\n'
Ejemplo n.º 3
0
def test_unary_classicals():
    p = Program()
    p.inst(TRUE(MemoryReference("ro", 0)),
           FALSE(MemoryReference("ro", 1)),
           NOT(MemoryReference("ro", 2)),
           NEG(MemoryReference("ro", 3)))
    assert p.out() == 'MOVE ro[0] 1\n' \
                      'MOVE ro[1] 0\n' \
                      'NOT ro[2]\n' \
                      'NEG ro[3]\n'
Ejemplo n.º 4
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]