I find barriers useful for visualization purposes, naturally it helps observing a circuit that is separated to some logical segments than just watching a mixture of gates and qubits.
However, before transpiling I would love to remove the barriers to avoid possible inefficiencies that can be caused by the presence of the barriers.
I have tried to use qiskit.transpiler.RemoveBarriers
as in the example inside the link, and the following behavior occurs:
- The barriers seem to be deleted from the
QuantumCircuit
object - verified by drawing usingQuantumCircuit.draw
, and by examining the content of theQuantumCircuit.__dict__
attribute. - After transpiling the barriers are back again inside the transpiled
QuantumCircuit
object returned by the functiontranspile
, along with the inefficiencies caused by the barriers which I tried to avoid in the first place.
Is there any simple way to get rid of barriers? Am I misunderstanding the functionality of RemoveBarriers
?
Thanks.
Edit:
I am adding a debugging piece of code:
print(f"operator.count_ops() = {operator.count_ops()}")
no_baerriers_operator = RemoveBarriers()(operator)
print(f"no_baerriers_operator.count_ops() = {no_baerriers_operator.count_ops()}")
transpiled_operator = transpile(no_baerriers_operator, **transpile_kwargs)
print(f"transpiled_operator.count_ops() = {transpiled_operator.count_ops()}")
print(f"transpile_kwargs = {transpile_kwargs}")
And its output:
operator.count_ops() = OrderedDict([('barrier', 3), ('([2] == [3])', 1), ('([0] == [1])', 1), ('ccx', 1), ('Uncomputation', 1)])
no_baerriers_operator.count_ops() = OrderedDict([('([2] == [3])', 1), ('([0] == [1])', 1), ('ccx', 1), ('Uncomputation', 1)])
transpiled_operator.count_ops() = OrderedDict([('cx', 14), ('u', 12), ('barrier', 2)])
transpile_kwargs = {'basis_gates': ['u', 'cx'], 'optimization_level': 3}
It can be seen, somewhat strangely, that in the original QuantumCiruit
object (i.e operator
) there are 3 barriers, then they vanish after using RemoveBarriers
(no barriers in no_barriers_operator
), and finally 2 barriers are back to transpiled_operator
arfter transpilation.
The custom gates in operator
don't contain any barriers.
RemoveBarriers
should work. Can you add your code here? $\endgroup$operator.count_ops() = OrderedDict([('barrier', 3), ('cx', 2), ('h', 1)])
no_baerriers_operator.count_ops() = OrderedDict([('cx', 2), ('h', 1)])
transpiled_operator.count_ops() = OrderedDict([('u', 2)])
$\endgroup$operator
(operator
is an object that inherits fromQuantumCircuit
). Do you have any idea what might cause this issue? Adding the source code ofoperator
here is infeasible.. I can send it to you via a private channel if it's possible. $\endgroup$