# Buggy code
def collect_runs(self, namelist):
    """Return a set of non-conditional runs of "op" nodes with the given names.

    For example, "... h q[0]; cx q[0],q[1]; cx q[0],q[1]; h q[1]; .."
    would produce the tuple of cx nodes as an element of the set returned
    from a call to collect_runs(["cx"]). If instead the cx nodes were
    "cx q[0],q[1]; cx q[1],q[0];", the method would still return the
    pair in a tuple. The namelist can contain names that are not
    in the circuit's basis.

    Nodes must have only one successor to continue the run.
    """
    group_list = []

    # Iterate through the nodes of self in topological order
    # and form tuples containing sequences of gates
    # on the same qubit(s).
    topo_ops = list(self.topological_op_nodes())
    nodes_seen = dict(zip(topo_ops, [False] * len(topo_ops)))
    for node in topo_ops:
        if node.name in namelist and node.condition is None and not nodes_seen[node]:
            group = [node]
            nodes_seen[node] = True
            s = list(self._multi_graph.successors(node))
            while len(s) == 1 and s[0].type == "op" and s[0].name in namelist:
                group.append(s[0])
                nodes_seen[s[0]] = True
                s = list(self._multi_graph.successors(s[0]))
            if len(group) >= 1:
                group_list.append(tuple(group))
    return set(group_list)


# Traceback (qiskit.transpiler.exceptions.TranspilerError)
qc = qk.QuantumCircuit(2,2)
qc.cy(0,1).c_if(qc.cregs[0], 0)
<qiskit.circuit.instructionset.InstructionSet object at 0x12c540160>
qk.transpile(qc, backend=FakeTenerife())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/compiler/transpile.py", line 147, in transpile
circuits = parallel_map(_transpile_circuit, list(zip(circuits, transpile_configs)))
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/tools/parallel.py", line 100, in parallel_map
return [task(values[0], *task_args, **task_kwargs)]
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/compiler/transpile.py", line 168, in _transpile_circuit
return transpile_circuit(circuit, transpile_config)
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/transpiler/transpile_circuit.py", line 62, in transpile_circuit
return pass_manager.run(circuit)
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/transpiler/passmanager.py", line 147, in run
dag = self._do_pass(pass_, dag, passset.options)
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/transpiler/passmanager.py", line 180, in _do_pass
new_dag = pass_.run(dag)
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/transpiler/passes/optimize_1q_gates.py", line 53, in run
raise TranspilerError("internal error")
qiskit.transpiler.exceptions.TranspilerError: 'internal error'

# Fixed code
def collect_runs(self, namelist):
    """Return a set of non-conditional runs of "op" nodes with the given names.

    For example, "... h q[0]; cx q[0],q[1]; cx q[0],q[1]; h q[1]; .."
    would produce the tuple of cx nodes as an element of the set returned
    from a call to collect_runs(["cx"]). If instead the cx nodes were
    "cx q[0],q[1]; cx q[1],q[0];", the method would still return the
    pair in a tuple. The namelist can contain names that are not
    in the circuit's basis.

    Nodes must have only one successor to continue the run.
    """
    group_list = []

    # Iterate through the nodes of self in topological order
    # and form tuples containing sequences of gates
    # on the same qubit(s).
    topo_ops = list(self.topological_op_nodes())
    nodes_seen = dict(zip(topo_ops, [False] * len(topo_ops)))
    for node in topo_ops:
        if node.name in namelist and node.condition is None and not nodes_seen[node]:
            group = [node]
            nodes_seen[node] = True
            s = list(self._multi_graph.successors(node))
            while (
                len(s) == 1
                and s[0].type == "op"
                and s[0].name in namelist
                and s[0].condition is None
            ):
                group.append(s[0])
                nodes_seen[s[0]] = True
                s = list(self._multi_graph.successors(s[0]))
            if len(group) >= 1:
                group_list.append(tuple(group))
    return set(group_list)