Repository: Qiskit/qiskit

Issue:
can not print/run/transpile a circuit with the custom instruction
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Information

- **Qiskit Terra version**: the master
- **Python version**: 3.7.3
- **Operating system**: macOS

### What is the current behavior?
the circuit built with custom instruction can not be printed/ran/transpiled if there are classical registers/measure gates.

error trace
```
---------------------------------------------------------------------------
DAGCircuitError                           Traceback (most recent call last)
<ipython-input-36-ece1b508a302> in <module>
----> 1 execute(new_qc, backend=qasm_sim).result()

~/Developer/Quantum/qiskit-terra/qiskit/execute.py in execute(experiments, backend, basis_gates, coupling_map, backend_properties, initial_layout, seed_transpiler, optimization_level, pass_manager, qobj_id, qobj_header, shots, memory, max_credits, seed_simulator, default_qubit_los, default_meas_los, schedule_los, meas_level, meas_return, memory_slots, memory_slot_size, rep_time, parameter_binds, **run_config)
    195                             optimization_level=optimization_level,
    196                             backend=backend,
--> 197                             pass_manager=pass_manager,
    198                             )
    199 

~/Developer/Quantum/qiskit-terra/qiskit/compiler/transpile.py in transpile(circuits, backend, basis_gates, coupling_map, backend_properties, initial_layout, seed_transpiler, optimization_level, pass_manager, callback)
    186                                       'in the coupling_map')
    187     # Transpile circuits in parallel
--> 188     circuits = parallel_map(_transpile_circuit, list(zip(circuits, transpile_configs)))
    189 
    190     if len(circuits) == 1:

~/Developer/Quantum/qiskit-terra/qiskit/tools/parallel.py in parallel_map(task, values, task_args, task_kwargs, num_processes)
     98     """
     99     if len(values) == 1:
--> 100         return [task(values[0], *task_args, **task_kwargs)]
    101 
    102     Publisher().publish("terra.parallel.start", len(values))

~/Developer/Quantum/qiskit-terra/qiskit/compiler/transpile.py in _transpile_circuit(circuit_config_tuple)
    207     circuit, transpile_config = circuit_config_tuple
    208 
--> 209     return transpile_circuit(circuit, transpile_config)
    210 
    211 

~/Developer/Quantum/qiskit-terra/qiskit/transpiler/transpile_circuit.py in transpile_circuit(circuit, transpile_config)
     60         pass_manager.callback = transpile_config.callback
     61 
---> 62     return pass_manager.run(circuit)

~/Developer/Quantum/qiskit-terra/qiskit/transpiler/passmanager.py in run(***failed resolving arguments***)
    169         for passset in self.working_list:
    170             for pass_ in passset:
--> 171                 dag = self._do_pass(pass_, dag, passset.options)
    172 
    173         circuit = dag_to_circuit(dag)

~/Developer/Quantum/qiskit-terra/qiskit/transpiler/passmanager.py in _do_pass(self, pass_, dag, options)
    200         # Run the pass itself, if not already run
    201         if pass_ not in self.valid_passes:
--> 202             dag = self._run_this_pass(pass_, dag)
    203 
    204             # update the valid_passes property

~/Developer/Quantum/qiskit-terra/qiskit/transpiler/passmanager.py in _run_this_pass(self, pass_, dag)
    213             if self.log_passes or self.callback:
    214                 start_time = time()
--> 215             new_dag = pass_.run(dag)
    216             if self.log_passes or self.callback:
    217                 end_time = time()

~/Developer/Quantum/qiskit-terra/qiskit/transpiler/passes/unroller.py in run(self, dag)
     78             decomposition.add_qreg(rule[0][1][0].register)
     79             for inst in rule:
---> 80                 decomposition.apply_operation_back(*inst)
     81 
     82             unrolled_dag = self.run(decomposition)  # recursively unroll ops

~/Developer/Quantum/qiskit-terra/qiskit/dagcircuit/dagcircuit.py in apply_operation_back(self, op, qargs, cargs, condition)
    261         self._check_condition(op.name, condition)
    262         self._check_bits(qargs, self.output_map)
--> 263         self._check_bits(all_cbits, self.output_map)
    264 
    265         self._add_op_node(op, qargs, cargs, condition)

~/Developer/Quantum/qiskit-terra/qiskit/dagcircuit/dagcircuit.py in _check_bits(self, args, amap)
    200             if wire not in amap:
    201                 raise DAGCircuitError("(qu)bit %s[%d] not found" %
--> 202                                       (wire[0].name, wire[1]))
    203 
    204     def _bits_in_condition(self, cond):

DAGCircuitError: '(qu)bit c[0] not found'
```

### Steps to reproduce the problem
```python
from qiskit import QuantumCircuit, QuantumRegister, BasicAer, execute, ClassicalRegister
from qiskit.quantum_info import state_fidelity
from qiskit.compiler import transpile

qasm_sim = BasicAer.get_backend('qasm_simulator')

qr = QuantumRegister(2)
cr = ClassicalRegister(2)
qc = QuantumCircuit(qr, cr, name='inst')

qc.x(qr[0])
qc.h(qr[1])
qc.measure(qr[0], cr[0])
qc.measure(qr[1], cr[1])

inst = qc.to_instruction()
new_qc = QuantumCircuit(qr, cr, name='inst2')
new_qc.append(inst, qr[:], cr[:])

t_qc = transpile(new_qc, backend=qasm_sim)

# execute(new_qc, backend=qasm_sim).result()
```


### What is the expected behavior?
Print/run/transpile the circuit with the custom instruction


### Suggested solutions





Discussion:

Resolving patch:
diff --git a/qiskit/transpiler/passes/decompose.py b/qiskit/transpiler/passes/decompose.py
--- a/qiskit/transpiler/passes/decompose.py
+++ b/qiskit/transpiler/passes/decompose.py
@@ -49,9 +49,12 @@ def run(self, dag):
             # hacky way to build a dag on the same register as the rule is defined
             # TODO: need anonymous rules to address wires by index
             decomposition = DAGCircuit()
-            decomposition.add_qreg(rule[0][1][0].register)
-            if rule[0][2]:
-                decomposition.add_creg(rule[0][2][0].register)
+            qregs = {qb.register for inst in rule for qb in inst[1]}
+            cregs = {cb.register for inst in rule for cb in inst[2]}
+            for qreg in qregs:
+                decomposition.add_qreg(qreg)
+            for creg in cregs:
+                decomposition.add_creg(creg)
             for inst in rule:
                 decomposition.apply_operation_back(*inst)
             dag.substitute_node_with_dag(node, decomposition)
diff --git a/qiskit/transpiler/passes/unroll_3q_or_more.py b/qiskit/transpiler/passes/unroll_3q_or_more.py
--- a/qiskit/transpiler/passes/unroll_3q_or_more.py
+++ b/qiskit/transpiler/passes/unroll_3q_or_more.py
@@ -46,7 +46,12 @@ def run(self, dag):
             # hacky way to build a dag on the same register as the rule is defined
             # TODO: need anonymous rules to address wires by index
             decomposition = DAGCircuit()
-            decomposition.add_qreg(rule[0][1][0].register)
+            qregs = {qb.register for inst in rule for qb in inst[1]}
+            cregs = {cb.register for inst in rule for cb in inst[2]}
+            for qreg in qregs:
+                decomposition.add_qreg(qreg)
+            for creg in cregs:
+                decomposition.add_creg(creg)
             for inst in rule:
                 decomposition.apply_operation_back(*inst)
             decomposition = self.run(decomposition)  # recursively unroll
diff --git a/qiskit/transpiler/passes/unroller.py b/qiskit/transpiler/passes/unroller.py
--- a/qiskit/transpiler/passes/unroller.py
+++ b/qiskit/transpiler/passes/unroller.py
@@ -75,7 +75,12 @@ def run(self, dag):
             # hacky way to build a dag on the same register as the rule is defined
             # TODO: need anonymous rules to address wires by index
             decomposition = DAGCircuit()
-            decomposition.add_qreg(rule[0][1][0].register)
+            qregs = {qb.register for inst in rule for qb in inst[1]}
+            cregs = {cb.register for inst in rule for cb in inst[2]}
+            for qreg in qregs:
+                decomposition.add_qreg(qreg)
+            for creg in cregs:
+                decomposition.add_creg(creg)
             for inst in rule:
                 decomposition.apply_operation_back(*inst)
 


Test patch: