def test_qobj_to_circuits_with_nothing(self):
     """Verify that qobj_to_circuits returns None without any data."""
     qobj = QasmQobj(qobj_id='abc123',
                     config=QasmQobjConfig(),
                     header=QobjHeader(),
                     experiments=[])
     self.assertIsNone(qobj_to_circuits(qobj))
 def test_qobj_to_circuits_single_no_qasm(self):
     """Check that qobj_to_circuits's result matches the qobj ini."""
     backend = Aer.get_backend('qasm_simulator_py')
     qobj_in = compile(self.circuit, backend, pass_manager=PassManager())
     for i in qobj_in.experiments:
         del i.header.compiled_circuit_qasm
     out_circuit = qobj_to_circuits(qobj_in)
     self.assertEqual(circuit_to_dag(out_circuit[0]), self.dag)
예제 #3
0
 def test_qobj_to_circuits_with_identity(self):
     """Check qobj_to_circuit's result with initialize."""
     q = QuantumRegister(2, name='q')
     circ = QuantumCircuit(q, name='circ')
     circ.iden(q)
     dag = circuit_to_dag(circ)
     qobj = assemble(circ)
     out_circuit = qobj_to_circuits(qobj)[0]
     self.assertEqual(circuit_to_dag(out_circuit), dag)
예제 #4
0
 def test_qobj_to_circuits_with_initialize(self):
     """Check qobj_to_circuit's result with initialize."""
     q = QuantumRegister(2, name='q')
     circ = QuantumCircuit(q, name='circ')
     circ.initialize([1 / np.sqrt(2), 0, 0, 1 / np.sqrt(2)], q[:])
     dag = circuit_to_dag(circ)
     qobj = assemble(circ)
     out_circuit = qobj_to_circuits(qobj)[0]
     self.assertEqual(circuit_to_dag(out_circuit), dag)
 def test_qobj_to_circuit_with_sim_instructions(self):
     """Check qobj_to_circuit result with asimulator instruction."""
     qr = QuantumRegister(3)
     cr = ClassicalRegister(3)
     circuit = QuantumCircuit(qr, cr)
     circuit.ccx(qr[0], qr[1], qr[2])
     circuit.snapshot(1)
     circuit.measure(qr, cr)
     dag = circuit_to_dag(circuit)
     qobj_in = assemble_circuits(circuit)
     out_circuit = qobj_to_circuits(qobj_in)
     self.assertEqual(circuit_to_dag(out_circuit[0]), dag)
 def test_qobj_to_circuit_with_sim_instructions(self):
     """Check qobj_to_circuit result with asimulator instruction."""
     backend = Aer.get_backend('qasm_simulator_py')
     qr = QuantumRegister(3)
     cr = ClassicalRegister(3)
     circuit = QuantumCircuit(qr, cr)
     circuit.ccx(qr[0], qr[1], qr[2])
     circuit.snapshot(1)
     circuit.measure(qr, cr)
     dag = circuit_to_dag(circuit)
     qobj_in = compile(circuit, backend, pass_manager=PassManager())
     out_circuit = qobj_to_circuits(qobj_in)
     self.assertEqual(circuit_to_dag(out_circuit[0]), dag)
예제 #7
0
 def test_qobj_to_circuits_with_opaque(self):
     """Check qobj_to_circuit's result with an opaque instruction."""
     opaque_inst = Instruction(name='my_inst',
                               num_qubits=4,
                               num_clbits=2,
                               params=[0.5, 0.4])
     q = QuantumRegister(6, name='q')
     c = ClassicalRegister(4, name='c')
     circ = QuantumCircuit(q, c, name='circ')
     circ.append(opaque_inst, [q[0], q[2], q[5], q[3]], [c[3], c[0]])
     dag = circuit_to_dag(circ)
     qobj = assemble(circ)
     out_circuit = qobj_to_circuits(qobj)[0]
     self.assertEqual(circuit_to_dag(out_circuit), dag)
 def test_qobj_to_circuits_multiple(self):
     """Check that qobj_to_circuits's result with multiple circuits"""
     qreg1 = QuantumRegister(2)
     qreg2 = QuantumRegister(3)
     creg1 = ClassicalRegister(2)
     creg2 = ClassicalRegister(2)
     circuit_b = QuantumCircuit(qreg1, qreg2, creg1, creg2)
     circuit_b.x(qreg1)
     circuit_b.h(qreg2)
     circuit_b.measure(qreg1, creg1)
     circuit_b.measure(qreg2[0], creg2[1])
     qobj = assemble_circuits([self.circuit, circuit_b])
     dag_list = [circuit_to_dag(x) for x in qobj_to_circuits(qobj)]
     self.assertEqual(dag_list, [self.dag, circuit_to_dag(circuit_b)])
 def test_qobj_to_circuits_multiple(self):
     """Check that qobj_to_circuits's result with multiple circuits"""
     backend = Aer.get_backend('qasm_simulator_py')
     qreg1 = QuantumRegister(2)
     qreg2 = QuantumRegister(3)
     creg1 = ClassicalRegister(2)
     creg2 = ClassicalRegister(2)
     circuit_b = QuantumCircuit(qreg1, qreg2, creg1, creg2)
     circuit_b.x(qreg1)
     circuit_b.h(qreg2)
     circuit_b.measure(qreg1, creg1)
     circuit_b.measure(qreg2[0], creg2[1])
     qobj = compile([self.circuit, circuit_b], backend, pass_manager=PassManager())
     dag_list = [circuit_to_dag(x) for x in qobj_to_circuits(qobj)]
     self.assertEqual(dag_list, [self.dag, circuit_to_dag(circuit_b)])
예제 #10
0
 def test_qobj_to_circuit_with_parameters(self):
     """Check qobj_to_circuit result with a gate that uses parameters."""
     qreg1 = QuantumRegister(2)
     qreg2 = QuantumRegister(3)
     creg1 = ClassicalRegister(2)
     creg2 = ClassicalRegister(2)
     circuit_b = QuantumCircuit(qreg1, qreg2, creg1, creg2)
     circuit_b.x(qreg1)
     circuit_b.h(qreg2)
     circuit_b.u2(0.2, 0.57, qreg2[1])
     circuit_b.measure(qreg1, creg1)
     circuit_b.measure(qreg2[0], creg2[1])
     qobj = assemble_circuits(circuit_b)
     out_circuit = qobj_to_circuits(qobj)
     self.assertEqual(circuit_to_dag(out_circuit[0]),
                      circuit_to_dag(circuit_b))
 def test_qobj_to_circuit_with_parameters(self):
     """Check qobj_to_circuit result with a gate that uses parameters."""
     backend = Aer.get_backend('qasm_simulator_py')
     qreg1 = QuantumRegister(2)
     qreg2 = QuantumRegister(3)
     creg1 = ClassicalRegister(2)
     creg2 = ClassicalRegister(2)
     circuit_b = QuantumCircuit(qreg1, qreg2, creg1, creg2)
     circuit_b.x(qreg1)
     circuit_b.h(qreg2)
     circuit_b.u2(0.2, 0.57, qreg2[1])
     circuit_b.measure(qreg1, creg1)
     circuit_b.measure(qreg2[0], creg2[1])
     qobj = compile(circuit_b, backend, pass_manager=PassManager())
     out_circuit = qobj_to_circuits(qobj)
     self.assertEqual(circuit_to_dag(out_circuit[0]),
                      circuit_to_dag(circuit_b))
 def test_qobj_to_circuits_with_nothing(self):
     """Verify that qobj_to_circuits returns None without any data."""
     qobj = Qobj('abc123', {}, {}, {})
     self.assertIsNone(qobj_to_circuits(qobj))
예제 #13
0
 def test_qobj_to_circuits_single_no_qasm(self):
     """Check that qobj_to_circuits's result matches the qobj ini."""
     qobj_in = assemble_circuits(self.circuit)
     out_circuit = qobj_to_circuits(qobj_in)
     self.assertEqual(circuit_to_dag(out_circuit[0]), self.dag)
예제 #14
0
    def _gates_from_qobj(cls, qobj):
        # type: (Qobj) -> List[Gate]
        import qiskit.extensions.standard as standard_gates
        import acquantumconnector.model.gates as ac_gates
        from qiskit.converters import qobj_to_circuits, circuit_to_dag

        qubit_labels = qobj.experiments[0].header.qubit_labels  # type: List[List[Any]]

        qc = qobj_to_circuits(qobj)  # type: QuantumCircuit
        # TODO: do all circuits.
        dag = circuit_to_dag(qc[0])  # type: DAGCircuit

        # Result
        gates = []  # type: List[Gate]

        def get_q_index(qarg):
            qubit, y = qarg
            q_index = qubit_labels.index([qubit.name, y])
            return q_index

        current_layer_x_number = 1
        layer = {}  # type: Dict[str, Union[DAGCircuit, list]]
        for layer in dag.layers():
            layer_dag = layer["graph"]  # type: DAGCircuit
            x_numbers = dict((qubit_labels.index([r.name, i]), current_layer_x_number) for r, i in layer_dag.wires
                             if isinstance(r, QuantumRegister))

            for op_node in layer_dag.get_op_nodes(data=True):
                # Given keys:
                # op_node[1]["cargs"]
                # op_node[1]["condition"]
                # op_node[1]["name"]
                # op_node[1]["qargs"]
                # op_node[1]["type"]
                # op_node[1]["op"]

                if isinstance(op_node[1]["op"], standard_gates.U1Gate):
                    qk_gate = op_node[1]["op"]  # type: standard_gates.U1Gate
                    y = get_q_index(qk_gate.qargs[0])
                    [theta] = qk_gate.param
                    gates.append(ac_gates.RzGate(x_numbers[y], y + y, theta))
                    x_numbers[y] += 1

                elif isinstance(op_node[1]["op"], standard_gates.U2Gate):
                    qk_gate = op_node[1]["op"]  # type: standard_gates.U2Gate
                    y = get_q_index(qk_gate.qargs[0])
                    [phi, lam] = qk_gate.param

                    # ignore global phase alpha!
                    # alpha = lam/2 + phi/2
                    beta = phi
                    delta = lam
                    gamma = math.pi / 2
                    gates.append(ac_gates.RzGate(x_numbers[y], y + 1, delta))
                    x_numbers[y] += 1
                    gates.append(ac_gates.RyGate(x_numbers[y], y + 1, gamma))
                    x_numbers[y] += 1
                    gates.append(ac_gates.RzGate(x_numbers[y], y + 1, beta))
                    x_numbers[y] += 1

                elif isinstance(op_node[1]["op"], standard_gates.U3Gate):
                    qk_gate = op_node[1]["op"]  # type: standard_gates.U3Gate
                    # I assume that all qubits have been transformed into the qubit regsiter 'q'
                    # and that the index simply tells me which wire this is.
                    [theta, phi, lam] = qk_gate.param
                    y = get_q_index(qk_gate.qargs[0])

                    # ignore global phase alpha!
                    # alpha = lam/2 + phi/2
                    beta = phi
                    delta = lam
                    gamma = theta
                    gates.append(ac_gates.RzGate(x_numbers[y], y + 1, delta))
                    x_numbers[y] += 1
                    gates.append(ac_gates.RyGate(x_numbers[y], y + 1, gamma))
                    x_numbers[y] += 1
                    gates.append(ac_gates.RzGate(x_numbers[y], y + 1, beta))
                    x_numbers[y] += 1

                elif isinstance(op_node[1]["op"], standard_gates.HGate):
                    qk_gate = op_node[1]["op"]  # type: standard_gates.HGate
                    y = get_q_index(qk_gate.qargs[0])
                    gates.append(ac_gates.HGate(x_numbers[y], y + 1))
                    x_numbers[y] += 1

                elif isinstance(op_node[1]["op"], standard_gates.RXGate):
                    qk_gate = op_node[1]["op"]  # type: standard_gates.RXGate
                    y = get_q_index(qk_gate.qargs[0])
                    [theta] = qk_gate.param
                    gates.append(ac_gates.RxGate(x_numbers[y], y + 1, theta))
                    x_numbers[y] += 1

                elif isinstance(op_node[1]["op"], standard_gates.RYGate):
                    qk_gate = op_node[1]["op"]  # type: standard_gates.RYGate
                    y = get_q_index(qk_gate.qargs[0])
                    [theta] = qk_gate.param
                    gates.append(ac_gates.RyGate(x_numbers[y], y + 1, theta))
                    x_numbers[y] += 1

                elif isinstance(op_node[1]["op"], standard_gates.RZGate):
                    qk_gate = op_node[1]["op"]  # type: standard_gates.RZGate
                    y = get_q_index(qk_gate.qargs[0])
                    [theta] = qk_gate.param
                    gates.append(ac_gates.RzGate(x_numbers[y], y + 1, theta))
                    x_numbers[y] += 1

                elif isinstance(op_node[1]["op"], standard_gates.XGate):
                    qk_gate = op_node[1]["op"]  # type: standard_gates.XGate
                    y = get_q_index(qk_gate.qargs[0])
                    gates.append(ac_gates.XGate(x_numbers[y], y + 1))
                    x_numbers[y] += 1

                elif isinstance(op_node[1]["op"], standard_gates.YGate):
                    qk_gate = op_node[1]["op"]  # type: standard_gates.YGate
                    y = get_q_index(qk_gate.qargs[0])
                    gates.append(ac_gates.YGate(x_numbers[y], y + 1))
                    x_numbers[y] += 1

                elif isinstance(op_node[1]["op"], standard_gates.ZGate):
                    qk_gate = op_node[1]["op"]  # type: standard_gates.ZGate
                    y = get_q_index(qk_gate.qargs[0])
                    gates.append(ac_gates.ZGate(x_numbers[y], y + 1))
                    x_numbers[y] += 1

                elif isinstance(op_node[1]["op"], standard_gates.SGate):
                    qk_gate = op_node[1]["op"]  # type: standard_gates.SGate
                    y = get_q_index(qk_gate.qargs[0])
                    gates.append(ac_gates.SGate(x_numbers[y], y + 1))
                    x_numbers[y] += 1

                elif isinstance(op_node[1]["op"], standard_gates.SdgGate):
                    qk_gate = op_node[1]["op"]  # type: standard_gates.SdgGate
                    y = get_q_index(qk_gate.qargs[0])
                    gates.append(ac_gates.SDag(x_numbers[y], y + 1))
                    x_numbers[y] += 1

                elif isinstance(op_node[1]["op"], standard_gates.TGate):
                    qk_gate = op_node[1]["op"]  # type: standard_gates.TGate
                    y = get_q_index(qk_gate.qargs[0])
                    gates.append(ac_gates.TGate(x_numbers[y], y + 1))
                    x_numbers[y] += 1

                elif isinstance(op_node[1]["op"], standard_gates.TdgGate):
                    qk_gate = op_node[1]["op"]  # type: standard_gates.TdgGate
                    y = get_q_index(qk_gate.qargs[0])
                    gates.append(ac_gates.TDag(x_numbers[y], y + 1))
                    x_numbers[y] += 1

                elif isinstance(op_node[1]["op"], standard_gates.CrzGate):
                    qk_gate = op_node[1]["op"]  # type: standard_gates.CrzGate
                    y = get_q_index(qk_gate.qargs[0])
                    gates.append(ac_gates.CPhase(x_numbers[y], y + 1))
                    x_numbers[y] += 1

                elif isinstance(op_node[1]["op"], standard_gates.CnotGate):
                    cx = op_node[1]["op"]  # type: standard_gates.CnotGate
                    y1 = get_q_index(cx.qargs[0])
                    y2 = get_q_index(cx.qargs[1])
                    gates.append(ac_gates.HGate(x_numbers[y2], y2 + 1))
                    x_numbers[y1] += 1
                    x_numbers[y2] += 1
                    gates.append(ac_gates.CPhase(x_numbers[y2], [y1 + 1, y2 + 1]))
                    x_numbers[y1] += 1
                    x_numbers[y2] += 1
                    gates.append(ac_gates.HGate(x_numbers[y2], y2 + 1))
                    x_numbers[y1] += 1
                    x_numbers[y2] += 1

                elif isinstance(op_node[1]["op"], standard_gates.ToffoliGate):
                    cx = op_node[1]["op"]  # type: standard_gates.ToffoliGate
                    y1 = get_q_index(cx.qargs[0])
                    y2 = get_q_index(cx.qargs[1])
                    y3 = get_q_index(cx.qargs[2])
                    gates.append(ac_gates.HGate(x_numbers[y3], y3 + 1))
                    x_numbers[y1] += 1
                    x_numbers[y2] += 1
                    x_numbers[y3] += 1
                    gates.append(ac_gates.CCPhase(x_numbers[y3], [y1 + 1, y2 + 1, y3 + 1]))
                    x_numbers[y1] += 1
                    x_numbers[y2] += 1
                    x_numbers[y3] += 1
                    gates.append(ac_gates.HGate(x_numbers[y3], y3 + 1))
                    x_numbers[y1] += 1
                    x_numbers[y2] += 1
                    x_numbers[y3] += 1

                elif isinstance(op_node[1]["op"], qiskit.circuit.Measure):
                    measure = op_node[1]["op"]  # type: qiskit.circuit.Measure
                    y = get_q_index(measure.qargs[0])
                    gates.append(ac_gates.Measure(x_numbers[y], y + 1))
                    x_numbers[y] += 1

            current_layer_x_number = max(x_numbers.values())

        return gates
예제 #15
0
 def test_qobj_to_circuits_with_qobj_no_qasm(self):
     """Verify that qobj_to_circuits returns None without QASM."""
     qobj = Qobj('abc123', {}, {}, {})
     self.assertIsNone(qobj_to_circuits(qobj))
예제 #16
0
 def test_qobj_to_circuits_single(self):
     """Check that qobj_to_circuits's result matches the qobj ini."""
     backend = BasicAer.get_backend('qasm_simulator')
     qobj_in = compile(self.circuit, backend, pass_manager=PassManager())
     out_circuit = qobj_to_circuits(qobj_in)
     self.assertEqual(circuit_to_dag(out_circuit[0]), self.dag)
예제 #17
0
 def run(self, qobj):
     circ = converters.qobj_to_circuits(qobj)
     prog = cp.circuit_to_program(circ)
     executable = self.pyquil_qc.compile(prog, False, False)
     return self.pyquil_qc.run(executable)