Esempio n. 1
0
def oracle(eng):

    ControlledGate(Ph(theta), 2) | (output, des_output, ancilla_qubit2)
    X | output
    X | des_output
    ControlledGate(Ph(theta), 2) | (output, des_output, ancilla_qubit2)
    X | output
    X | des_output
Esempio n. 2
0
def oracle(eng):

    ControlledGate(Ph(theta), 2) | (output_reg[2], des_output, ancilla2)
    X | output_reg[2]
    X | des_output
    ControlledGate(Ph(theta), 2) | (output_reg[2], des_output, ancilla2)
    X | output_reg[2]
    X | des_output
Esempio n. 3
0
def qbn(eng):

    CNOT | (layer1_weight_reg[0], layer1_input_reg[0])
    CNOT | (layer1_weight_reg[1], layer1_input_reg[1])

    ControlledGate(
        NOT, 2) | (layer1_input_reg[0], layer1_input_reg[1], output_reg[0])

    CNOT | (layer1_weight_reg[2], layer1_input_reg[2])
    CNOT | (layer1_weight_reg[3], layer1_input_reg[3])

    ControlledGate(
        NOT, 2) | (layer1_input_reg[2], layer1_input_reg[3], output_reg[1])

    CNOT | (layer2_weight_reg[0], output_reg[0])
    CNOT | (layer2_weight_reg[1], output_reg[1])

    ControlledGate(NOT, 2) | (output_reg[0], output_reg[1], output_reg[2])
    def _evidence_ctrl(self, qureg):
        """here reordering the control qubits"""
        for i in range(len(self.targ_bin_rep)):
            if self.targ_bin_rep[len(self.targ_bin_rep) - 1 - i] == '0':
                X | qureg[i]

        ControlledGate(X, 3) | (qureg[0], qureg[1], qureg[2], self.anc_qubit)

        for i in range(len(self.targ_bin_rep)):
            if self.targ_bin_rep[len(self.targ_bin_rep) - 1 - i] == '0':
                X | qureg[i]
Esempio n. 5
0
def qbn(eng):

    CNOT | (layer1_weight_reg[0], layer1_input_reg[0])
    CNOT | (layer1_weight_reg[1], layer1_input_reg[1])
    CNOT | (layer1_weight_reg[2], layer1_input_reg[2])

    ControlledGate(NOT, 3) | (layer1_input_reg[0], layer1_input_reg[1],
                              layer1_input_reg[2], output)
    X | layer1_input_reg[0]
    ControlledGate(NOT, 3) | (layer1_input_reg[0], layer1_input_reg[1],
                              layer1_input_reg[2], output)
    X | layer1_input_reg[0]
    X | layer1_input_reg[1]
    ControlledGate(NOT, 3) | (layer1_input_reg[0], layer1_input_reg[1],
                              layer1_input_reg[2], output)
    X | layer1_input_reg[1]
    X | layer1_input_reg[2]
    ControlledGate(NOT, 3) | (layer1_input_reg[0], layer1_input_reg[1],
                              layer1_input_reg[2], output)
    X | layer1_input_reg[2]
Esempio n. 6
0
def diffusion(eng):

    with Compute(eng):
        All(H) | layer1_weight_reg

        All(X) | layer1_weight_reg

    ControlledGate(Z, 2) | (layer1_weight_reg[0], layer1_weight_reg[1],
                            layer1_weight_reg[2])

    Uncompute(eng)
Esempio n. 7
0
def add_minus_sign(eng):

    with Compute(eng):
        quanutm_phase_estimation(eng)

    X | phase_reg[2]
    ControlledGate(
        NOT, 3) | (phase_reg[0], phase_reg[1], phase_reg[2], ancilla_qubit)
    X | phase_reg[2]

    Uncompute(eng)
Esempio n. 8
0
    def test_default_gates_and_qbit_reorder(self):
        aq = AqasmPrinter(MainEngine)
        eng = AqasmEngine(aq, engine_list=[aq])
        qreg1 = eng.allocate_qureg(2)
        qreg2 = eng.allocate_qureg(1)
        qreg3 = eng.allocate_qureg(2)
        for op in gates_1qb:
            op | qreg2[0]
        for op in gates_2qb:
            op | (qreg3[0], qreg1[1])

        ControlledGate(ops.Swap, n=1) | (qreg3[1], qreg1[0], qreg2[0])
        Toffoli | (qreg3[1], qreg1[0], qreg2[0])
        All(Measure) | qreg1
        All(Measure) | qreg2
        All(Measure) | qreg3
        # Generating qlm circuit
        result = eng.projectq_to_qlm()

        # Generating equivalent qlm circuit
        prog = Program()
        qubits = prog.qalloc(5)
        cbits = prog.calloc(5)
        for op in pygates_1qb:
            prog.apply(op, qubits[2])
        for op in pygates_2qb:
            prog.apply(op, qubits[3], qubits[1])
        prog.apply(SWAP, qubits[1], qubits[3])
        prog.apply(SWAP.ctrl(), qubits[4], qubits[0], qubits[2])
        prog.apply(X.ctrl().ctrl(), qubits[0], qubits[4], qubits[2])
        for i in range(5):
            prog.measure(qubits[i], cbits[i])
        expected = prog.to_circ()
        self.assertEqual(len(result.ops), len(expected.ops))
        for i in range(len(result.ops)):
            res_op = result.ops[i]
            exp_op = expected.ops[i]
            if res_op.type == OpType.MEASURE:
                self.assertEqual(res_op, exp_op)
                continue
            result_gate_name, result_gate_params = extract_syntax(
                result.gateDic[res_op.gate], result.gateDic)
            # print("got gate {} with params {} on qbits {}"
            #      .format(result_gate_name, result_gate_params,
            #              res_op.qbits))
            expected_gate_name, expected_gate_params = extract_syntax(
                expected.gateDic[exp_op.gate], expected.gateDic)
            # print("expected gate {} with params {} on qbits {}"
            #      .format(expected_gate_name, expected_gate_params,
            #              exp_op.qbits))
            self.assertEqual(expected_gate_name, result_gate_name)
            self.assertEqual(expected_gate_params, result_gate_params)
            self.assertEqual(exp_op.qbits, res_op.qbits)
Esempio n. 9
0
def qbn(eng):

    #operations in the 1st layer
    CNOT | (layer1_weight_reg[0], layer1_input_reg[0])
    CNOT | (layer1_weight_reg[1], layer1_input_reg[1])
    CNOT | (layer1_weight_reg[2], layer1_input_reg[2])
    CNOT | (layer1_weight_reg[3], layer1_input_reg[3])
    CNOT | (layer1_weight_reg[4], layer1_input_reg[4])
    CNOT | (layer1_weight_reg[5], layer1_input_reg[5])

    ControlledGate(NOT, 3) | (layer1_input_reg[0], layer1_input_reg[1],
                              layer1_input_reg[2], output_reg[0])
    X | layer1_input_reg[0]
    ControlledGate(NOT, 3) | (layer1_input_reg[0], layer1_input_reg[1],
                              layer1_input_reg[2], output_reg[0])
    X | layer1_input_reg[0]
    X | layer1_input_reg[1]
    ControlledGate(NOT, 3) | (layer1_input_reg[0], layer1_input_reg[1],
                              layer1_input_reg[2], output_reg[0])
    X | layer1_input_reg[1]
    X | layer1_input_reg[2]
    ControlledGate(NOT, 3) | (layer1_input_reg[0], layer1_input_reg[1],
                              layer1_input_reg[2], output_reg[0])
    X | layer1_input_reg[2]

    ControlledGate(NOT, 3) | (layer1_input_reg[3], layer1_input_reg[4],
                              layer1_input_reg[5], output_reg[1])
    X | layer1_input_reg[3]
    ControlledGate(NOT, 3) | (layer1_input_reg[3], layer1_input_reg[4],
                              layer1_input_reg[5], output_reg[1])
    X | layer1_input_reg[3]
    X | layer1_input_reg[4]
    ControlledGate(NOT, 3) | (layer1_input_reg[3], layer1_input_reg[4],
                              layer1_input_reg[5], output_reg[1])
    X | layer1_input_reg[4]
    X | layer1_input_reg[5]
    ControlledGate(NOT, 3) | (layer1_input_reg[3], layer1_input_reg[4],
                              layer1_input_reg[5], output_reg[1])
    X | layer1_input_reg[5]

    #operations in the 2nd Layer
    CNOT | (layer2_weight_reg[0], output_reg[0])
    CNOT | (layer2_weight_reg[1], output_reg[1])

    ControlledGate(NOT, 2) | (output_reg[0], output_reg[1], output_reg[2])
Esempio n. 10
0
    def _evidence_ctrl(self):
        """here reordering the control qubits"""
        order = [7, 3, 8, 1, 6, 4, 0, 2, 5]
        for i in range(len(self.targ_bin_rep)):
            if self.targ_bin_rep[len(self.targ_bin_rep) - 1 - i] == '0':
                X | self.qureg[order.index(i)]

        # ControlledGate(X, len(search_bin_rep)) | ([self.qureg[int(i)] for i in range(len(search_bin_rep))],
        #                                           self.anc_qubit)
        ControlledGate(
            X, 3) | (self.qureg[order.index(0)], self.qureg[order.index(1)],
                     self.qureg[order.index(2)], self.anc_qubit)

        for i in range(len(self.targ_bin_rep)):
            if self.targ_bin_rep[len(self.targ_bin_rep) - 1 - i] == '0':
                X | self.qureg[order.index(i)]
    def grover_op(self, qureg):
        # # R_{e}^{dag}
        self.oracle_block(qureg)
        # Oracle
        self.Adag(qureg)

        for i in range(len(qureg)):
            X | qureg[i]
        '''exchange 2 and 8'''
        ControlledGate(Z,
                       8) | (qureg[0], qureg[1], qureg[8], qureg[3], qureg[4],
                             qureg[5], qureg[6], qureg[7], qureg[2])

        for i in range(len(qureg)):
            X | qureg[i]

        # re-Oracle
        self.A(qureg)

        if self.oracle == 2:
            self._evidence_ctrl(qureg)
Esempio n. 12
0
    def grover_block(self, repeat_time):
        for t in range(repeat_time):
            # # R_{e}^{dag}
            self.oracle_block()
            # Oracle
            self.run('backward')

            for i in range(len(self.qureg)):
                X | self.qureg[i]
            '''exchange 2 and 8'''
            ControlledGate(Z,
                           8) | (self.qureg[0], self.qureg[1], self.qureg[8],
                                 self.qureg[3], self.qureg[4], self.qureg[5],
                                 self.qureg[6], self.qureg[7], self.qureg[2])

            for i in range(len(self.qureg)):
                X | self.qureg[i]

            # re-Oracle
            self.run('forward')

            if self.oracle == 2:
                self._evidence_ctrl()
Esempio n. 13
0
gates_1qb = [
    ops.X,
    ops.Y,
    ops.Z,
    ops.S,
    ops.T,
    ops.H,
    Sdag,
    Tdag,
    Rx(3.14),
    Ry(3.14),
    Rz(3.14),
    R(3.14),
]
gates_2qb = [ControlledGate(ops.H), CX, ControlledGate(Rz(3.14)), ops.Swap]

pygates_1qb = [
    X,
    Y,
    Z,
    S,
    T,
    H,
    S.dag(),
    T.dag(),
    RX(3.14),
    RY(3.14),
    RZ(3.14),
    PH(3.14),
]