# Buggy code
def run(self, dag):
    """Expand 3+ qubit gates using their decomposition rules.

    Args:
        dag(DAGCircuit): input dag
    Returns:
        DAGCircuit: output dag with maximum node degrees of 2
    Raises:
        QiskitError: if a 3q+ gate is not decomposable
    """
    for node in dag.threeQ_or_more_gates():
        # TODO: allow choosing other possible decompositions
        rule = node.op.definition
        if not rule:
            raise QiskitError(
                "Cannot unroll all 3q or more gates. "
                "No rule to expand instruction %s." % node.op.name
            )

        # 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)
        for inst in rule:
            decomposition.apply_operation_back(*inst)
        decomposition = self.run(decomposition)  # recursively unroll
        dag.substitute_node_with_dag(node, decomposition)
    return dag


# Traceback (DAGCircuitError)
---------------------------------------------------------------------------
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'

# Fixed code
def run(self, dag):
    """Expand 3+ qubit gates using their decomposition rules.

    Args:
        dag(DAGCircuit): input dag
    Returns:
        DAGCircuit: output dag with maximum node degrees of 2
    Raises:
        QiskitError: if a 3q+ gate is not decomposable
    """
    for node in dag.threeQ_or_more_gates():
        # TODO: allow choosing other possible decompositions
        rule = node.op.definition
        if not rule:
            raise QiskitError(
                "Cannot unroll all 3q or more gates. "
                "No rule to expand instruction %s." % node.op.name
            )

        # 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()
        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
        dag.substitute_node_with_dag(node, decomposition)
    return dag