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:

  1. The barriers seem to be deleted from the QuantumCircuit object - verified by drawing using QuantumCircuit.draw, and by examining the content of the QuantumCircuit.__dict__ attribute.
  2. After transpiling the barriers are back again inside the transpiled QuantumCircuit object returned by the function transpile, 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?



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.

  • $\begingroup$ It seems that you are doing something wrong because RemoveBarriers should work. Can you add your code here? $\endgroup$ Commented Feb 5, 2023 at 17:57
  • $\begingroup$ Hey, thanks. I have edited the original post with a code example. $\endgroup$
    – Ohad
    Commented Feb 6, 2023 at 7:04
  • $\begingroup$ I got the output: 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$ Commented Feb 6, 2023 at 8:00
  • $\begingroup$ I see. When I try an isolated exmaple like yours myself, it also works. So I guess it has something to do with the construction of operator (operator is an object that inherits from QuantumCircuit). Do you have any idea what might cause this issue? Adding the source code of operator here is infeasible.. I can send it to you via a private channel if it's possible. $\endgroup$
    – Ohad
    Commented Feb 6, 2023 at 8:46
  • $\begingroup$ I'm available on Qiskit's Slack channel (same username) $\endgroup$ Commented Feb 6, 2023 at 8:55

1 Answer 1


Here, how to use RemoveBarriers:

from qiskit.transpiler.passes import RemoveBarriers
rb = RemoveBarriers()
circuit_out = rb(circuit_in)

This eliminates the barriers that are visible in the circuit. That is, if an instruction contains barriers as part of its decomposition, it wont be removed.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.