# Buggy code
def _commute(node1, node2):
    if node1.type != "op" or node2.type != "op":
        return False

    if any(
        [
            nd.name in {"barrier", "snapshot", "measure", "reset", "copy"}
            for nd in [node1, node2]
        ]
    ):
        return False

    qarg = list(set(node1.qargs + node2.qargs))
    qbit_num = len(qarg)

    qarg1 = [qarg.index(q) for q in node1.qargs]
    qarg2 = [qarg.index(q) for q in node2.qargs]

    id_op = Operator(np.eye(2**qbit_num))

    op12 = id_op.compose(node1.op, qargs=qarg1).compose(node2.op, qargs=qarg2)
    op21 = id_op.compose(node2.op, qargs=qarg2).compose(node1.op, qargs=qarg1)

    if_commute = op12 == op21

    return if_commute


# Traceback (qiskit.transpiler.exceptions.TranspilerError)
import qiskit as qk
qc = qk.QuantumCircuit(3,1)
qc.ccx(2,1,0).c_if(qc.cregs[0], 0)
<qiskit.circuit.instructionset.InstructionSet object at 0x129ed7908>
qc.measure(2,0)
<qiskit.circuit.instructionset.InstructionSet object at 0x12a1619e8>
print(qc)
┌───┐
q_0: |0>─┤ X ├────
└─┬─┘
q_1: |0>───■──────
│   ┌─┐
q_2: |0>───■───┤M├
┌──┴──┐└╥┘
c_0: 0 ╡ = 0 ╞═╩═
└─────┘
qk.transpile(qc, optimization_level=2,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 159, 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 180, 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 138, in run
dag = self._do_pass(pass_, dag, passset.options)
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/transpiler/passmanager.py", line 168, in _do_pass
dag = self._run_this_pass(pass_, dag)
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/transpiler/passmanager.py", line 179, in _run_this_pass
new_dag = pass_.run(dag)
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/transpiler/passes/commutative_cancellation.py", line 109, in run
raise TranspilerError("internal error")
qiskit.transpiler.exceptions.TranspilerError: 'internal error'

# Fixed code
def _commute(node1, node2):
    if node1.type != "op" or node2.type != "op":
        return False

    if any(
        [
            nd.name in {"barrier", "snapshot", "measure", "reset", "copy"}
            for nd in [node1, node2]
        ]
    ):
        return False

    if node1.condition or node2.condition:
        return False

    qarg = list(set(node1.qargs + node2.qargs))
    qbit_num = len(qarg)

    qarg1 = [qarg.index(q) for q in node1.qargs]
    qarg2 = [qarg.index(q) for q in node2.qargs]

    id_op = Operator(np.eye(2**qbit_num))

    op12 = id_op.compose(node1.op, qargs=qarg1).compose(node2.op, qargs=qarg2)
    op21 = id_op.compose(node2.op, qargs=qarg2).compose(node1.op, qargs=qarg1)

    if_commute = op12 == op21

    return if_commute