Exemplo n.º 1
0
    def test0_default_gates(self):
        """
        Tests all the default QLM gates which conversion in Qiskit
        are supported.
        """
        prog = Program()
        qreg = prog.qalloc(5)

        for gate_op in PYGATES_1QB:
            prog.apply(gate_op, qreg[0])

        for gate_op in PYGATES_2QB:
            prog.apply(gate_op, qreg[0], qreg[1])

        prog.apply(CCNOT, qreg[0], qreg[1], qreg[2])
        prog.apply(SWAP.ctrl(), qreg[0], qreg[1], qreg[2])

        prog.apply(MS(3.14, 3), qreg[1], qreg[2], qreg[4])

        qlm_circuit = prog.to_circ()
        result = qlm_to_qiskit(qlm_circuit)

        qiskit_qreg = QuantumRegister(5)
        qiskit_creg = ClassicalRegister(5)
        expected = QuantumCircuit(qiskit_qreg, qiskit_creg)
        for gate_op in qiskit_1qb(expected):
            gate_op(qiskit_qreg[0])
        for gate_op in qiskit_1qb_1prm(expected):
            gate_op(3.14, qiskit_qreg[0])
        for gate_op in qiskit_1qb_2prm(expected):
            gate_op(3.14, 3.14, qiskit_qreg[0])
        for gate_op in qiskit_1qb_3prm(expected):
            gate_op(3.14, 3.14, 3.14, qiskit_qreg[0])
        for gate_op in qiskit_2qb(expected):
            gate_op(qiskit_qreg[0], qiskit_qreg[1])
        for gate_op in qiskit_2qb_1prm(expected):
            gate_op(3.14, qiskit_qreg[0], qiskit_qreg[1])

        expected.ccx(*qiskit_qreg[:3])
        expected.cswap(*qiskit_qreg[:3])

        # for the MS gate test
        for i in [1, 2, 4]:
            for j in [1, 2, 4]:
                if j > i:
                    expected.rxx(3.14, qiskit_qreg[i], qiskit_qreg[j])

        expected.measure(qiskit_qreg, qiskit_creg)

        LOGGER.debug("qlm_to_qiskit test with standard circuit:")
        expected_str = print_qiskit(expected)
        result_str = print_qiskit(result)
        self.assertEqual(len(result_str), len(expected_str))

        for i in range(len(result.data)):
            r_name, r_params = extract_qiskit(result.data[i])[0:2]
            e_name, e_params = extract_qiskit(expected.data[i])[0:2]
            self.assertEqual(r_name, e_name)
            self.assertEqual(r_params, e_params)
Exemplo n.º 2
0
    def test1_abstract_gate(self):
        """
        Tests an AbstractGate translation to Qiskit.
        Only abstract gates defined via a circuit are supported.
        """
        prog = Program()
        qreg = prog.qalloc(3)
        routine = QRoutine()

        for gate_op in PYGATES_1QB:
            routine.apply(gate_op, [0])

        for gate_op in PYGATES_2QB:
            routine.apply(gate_op, [0, 1])

        routine.apply(CCNOT, [0, 1, 2])
        routine.apply(SWAP.ctrl(), [0, 1, 2])

        prog.apply(routine.box("custom_gate"), qreg)
        qlm_circuit = prog.to_circ()
        result = qlm_to_qiskit(qlm_circuit)

        qiskit_qreg = QuantumRegister(3)
        qiskit_creg = ClassicalRegister(3)
        expected = QuantumCircuit(qiskit_qreg, qiskit_creg)
        for gate_op in qiskit_1qb(expected):
            gate_op(qiskit_qreg[0])
        for gate_op in qiskit_1qb_1prm(expected):
            gate_op(3.14, qiskit_qreg[0])
        for gate_op in qiskit_1qb_2prm(expected):
            gate_op(3.14, 3.14, qiskit_qreg[0])
        for gate_op in qiskit_1qb_3prm(expected):
            gate_op(3.14, 3.14, 3.14, qiskit_qreg[0])
        for gate_op in qiskit_2qb(expected):
            gate_op(qiskit_qreg[0], qiskit_qreg[1])
        for gate_op in qiskit_2qb_1prm(expected):
            gate_op(3.14, qiskit_qreg[0], qiskit_qreg[1])

        expected.ccx(*qiskit_qreg)
        expected.cswap(*qiskit_qreg)

        expected.measure(qiskit_qreg, qiskit_creg)

        LOGGER.debug("qlm_to_qiskit test with a QRoutine:")
        expected_str = print_qiskit(expected)
        result_str = print_qiskit(result)
        self.assertEqual(len(result_str), len(expected_str))

        for i in range(len(result.data)):
            r_name, r_params = extract_qiskit(result.data[i])[0:2]
            e_name, e_params = extract_qiskit(expected.data[i])[0:2]
            self.assertEqual(r_name, e_name)
            self.assertEqual(r_params, e_params)
Exemplo n.º 3
0
    def test2_abstract_variables(self):
        """
        Tests the translation of abstract variables and
        ArithExpression into Qiskit via qlm_to_qiskit()
        """
        prog = Program()
        qubits = prog.qalloc(1)
        var2 = prog.new_var(float, "param2")
        var3 = prog.new_var(float, "param3")
        var4 = 1.0 + 3.14 + var2 - var3
        var5 = 1.0 * 3.14 * (var2 + 4.54) * var3
        var6 = -var5 * var4
        var7 = var4 / (var2 - 7)
        prog.apply(RX(1.0), qubits[0])
        prog.apply(RX(3.14), qubits[0])
        prog.apply(RX(var2), qubits[0])
        prog.apply(RX(var3), qubits[0])
        prog.apply(RX(var4), qubits[0])
        prog.apply(RX(var5), qubits[0])
        prog.apply(RX(var6), qubits[0])
        prog.apply(RX(var7), qubits[0])
        qlm_circ = prog.to_circ()
        qiskit_circ = qlm_to_qiskit(qlm_circ)
        LOGGER.debug("Parameters gotten:")
        for gate_op in qiskit_circ.data:
            for param in gate_op[0]._params:
                LOGGER.debug(param)

        qreg = QuantumRegister(1)
        circ = QuantumCircuit(qreg)
        param2 = Parameter("param2")
        param3 = Parameter("param3")
        param4 = 1.0 + 3.14 + param2 - param3
        param5 = 1.0 * 3.14 * (param2 + 4.54) * param3
        param6 = -param5 * param4
        param7 = param4 / (param2 - 7.0)
        circ.rx(1.0, 0)
        circ.rx(3.14, 0)
        circ.rx(param2, 0)
        circ.rx(param3, 0)
        circ.rx(param4, 0)
        circ.rx(param5, 0)
        circ.rx(param6, 0)
        circ.rx(param7, 0)
        LOGGER.debug("Parameters expected:")
        for gate_op in circ.data:
            for param in gate_op[0]._params:
                LOGGER.debug(param)

        for gotten, expected in zip(qiskit_circ.data, circ.data):
            self.assertEqual(str(gotten[0]._params[0]),
                             str(expected[0]._params[0]))