# Question on using the Collect2qBlocks() and ConsolidateBlocks() optimizations

I'd like to run the optimizations from Qiskit's level 3 transpiler pass on some example programs for benchmarking, but when I try to include the Collect2qBlocks() and ConsolidateBlocks() passes I get an error. The code below is based on the example here.

from qiskit import QuantumCircuit
from qiskit.compiler import transpile
from qiskit.transpiler import PassManager
from qiskit.transpiler.passes import Unroller, Optimize1qGates, CommutativeCancellation, CXCancellation, Depth, FixedPoint, Collect2qBlocks, ConsolidateBlocks

circ = QuantumCircuit.from_qasm_file("example.qasm")

basis_gates = ['u1', 'u2', 'u3', 'cx']
_unroll = Unroller(basis_gates)
_depth_check = [Depth(), FixedPoint('depth')]
def _opt_control(property_set):
return not property_set['depth_fixed_point']
_opt = [Collect2qBlocks(), ConsolidateBlocks(),
Unroller(basis_gates),  # unroll unitaries
Optimize1qGates(), CommutativeCancellation()]
pm = PassManager()
pm.append(_unroll)
pm.append(_depth_check + _opt, do_while=_opt_control)
circ = pm.run(circ)
print(circ.count_ops())


This seems to run fine if I exclude the Collect2qBlocks() and ConsolidateBlocks() passes, but when I include them I get the following error:

Traceback (most recent call last):
File "test_qiskit.py", line 19, in <module>
circ = pm.run(circ)
File "/usr/local/lib/python3.7/site-packages/qiskit/transpiler/passmanager.py", line 146, in run
dag = self._do_pass(pass_, dag, passset.options)
File "/usr/local/lib/python3.7/site-packages/qiskit/transpiler/passmanager.py", line 183, in _do_pass
pass_.run(FencedDAGCircuit(dag))
File "/usr/local/lib/python3.7/site-packages/qiskit/transpiler/passes/collect_2q_blocks.py", line 59, in run
these_qubits = sorted(nd.qargs)
TypeError: '<' not supported between instances of 'QuantumRegister' and 'QuantumRegister'


Am I invoking the methods incorrectly? I have the most recent version of Qiskit available on pip. Please let me know if you have any advice, thanks.

• What version of qiskit are you running? I know you said you have the most recent verison, but looking at this line in the stacktrace these_qubits = sorted(nd.qargs), this looks like it is from an older version of qiskit. When I look at the code in this file for the newest release, it says these_qubits = set(nd.qargs). The newest release version of qiskit is 0.12.0. Granted, I tried your code in v0.12.0 with my own basic circuit and it worked. Maybe there is something happening with how the circuit is constructed? – Matthew Stypulkoski Sep 17 '19 at 14:26
• Apparently I had version 0.10.5. To get 0.12.0, I needed to 'pip uninstall' qiskit and then 'pip install' it again. My code works now, thanks! – Kesha H. Sep 17 '19 at 15:12
• Hi @KeshaH. ! In order to make the answer more visible, would you mind writing a real answer (not a comment) and accepting it? Thank you, and happy to see that you solved your problem. – Nelimee Sep 18 '19 at 7:14