Repository: Qiskit/qiskit

Issue:
qobj_to_circuits not using the compiled qasm in the qobj header
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Informations

- **Qiskit Terra version**:
- **Python version**:
- **Operating system**:

### What is the current behavior?



### Steps to reproduce the problem



### What is the expected behavior?



### Suggested solutions





Discussion:

Resolving patch:
diff --git a/qiskit/converters/qobj_to_circuits.py b/qiskit/converters/qobj_to_circuits.py
--- a/qiskit/converters/qobj_to_circuits.py
+++ b/qiskit/converters/qobj_to_circuits.py
@@ -6,7 +6,10 @@
 # the LICENSE.txt file in the root directory of this source tree.
 
 """Helper function for converting qobj to a list of circuits"""
-from qiskit.circuit import QuantumCircuit
+
+from qiskit.circuit import classicalregister as cr
+from qiskit.circuit import quantumcircuit as qc
+from qiskit.circuit import quantumregister as qr
 
 
 def qobj_to_circuits(qobj):
@@ -21,10 +24,34 @@ def qobj_to_circuits(qobj):
     if qobj.experiments:
         circuits = []
         for x in qobj.experiments:
-            if hasattr(x.header, 'compiled_circuit_qasm'):
-                circuits.append(
-                    QuantumCircuit.from_qasm_str(x.header.compiled_circuit_qasm))
+            quantum_registers = [
+                qr.QuantumRegister(
+                    i[1], name=i[0]) for i in x.header.qreg_sizes]
+            classical_registers = [
+                cr.ClassicalRegister(
+                    i[1], name=i[0]) for i in x.header.creg_sizes]
+            circuit = qc.QuantumCircuit(*quantum_registers,
+                                        *classical_registers,
+                                        name=x.header.name)
+            qreg_dict = {}
+            creg_dict = {}
+            for reg in quantum_registers:
+                qreg_dict[reg.name] = reg
+            for reg in classical_registers:
+                creg_dict[reg.name] = reg
+            for i in x.instructions:
+                instr_method = getattr(circuit, i.name)
+                qubits = []
+                for qubit in i.qubits:
+                    qubit_label = x.header.qubit_labels[qubit]
+                    qubits.append(
+                        qreg_dict[qubit_label[0]][qubit_label[1]])
+                clbits = []
+                for clbit in i.memory:
+                    clbit_label = x.header.clbit_labels[clbit]
+                    clbits.append(
+                        creg_dict[clbit_label[0]][clbit_label[1]])
+                instr_method(*i.params, *qubits, *clbits)
+            circuits.append(circuit)
         return circuits
-    # TODO(mtreinish): add support for converting a qobj if the qasm isn't
-    # embedded in the header
     return None


Test patch:
diff --git a/test/python/converters/test_qobj_2_circuits.py b/test/python/converters/test_qobj_2_circuits.py
--- a/test/python/converters/test_qobj_2_circuits.py
+++ b/test/python/converters/test_qobj_2_circuits.py
@@ -56,11 +56,58 @@ def test_qobj_to_circuits_multiple(self):
         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_with_qobj_no_qasm(self):
-        """Verify that qobj_to_circuits returns None without QASM."""
+    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))
 
+    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)
+
+    def test_qobj_to_circuits_multiple_no_qasm(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())
+        for i in qobj.experiments:
+            del i.header.compiled_circuit_qasm
+
+        dag_list = [circuit_to_dag(x) for x in qobj_to_circuits(qobj)]
+        self.assertEqual(dag_list, [self.dag, circuit_to_dag(circuit_b)])
+
 
 if __name__ == '__main__':
     unittest.main(verbosity=2)