Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
4.85k stars 2.29k forks source link

Add support for a visual-only `Barrier` #11978

Open BhumikaSaini opened 4 months ago

BhumikaSaini commented 4 months ago

What should we add?

Often, it is helpful to add visual barriers in the rendered circuit for ease of reference. Currently, this is achieved via the Barrier instruction. However, using the Barrier instruction has a direct impact on circuit optimizations (docs).

It also acts as a directive for circuit compilation to separate pieces of a circuit so that any optimizations or re-writes are constrained to only act between barriers.

Is there any existing support for visual-only barriers that don't affect circuit optimization? If not, I think adding this might be helpful.

For instance, the following is much easier to reference and looks better organized with the help of barriers...

image

... as compared to:

image

While this is a toy example, visual barriers would help for more complex circuits.

Proposed approaches:

  1. Add a parameter to Barrier that denotes whether the barrier is a visual-only barrier or if it affects compilation too. The two barriers types should be rendered differently.
  2. Add another instruction for a visual-only barrier.
### Tasks
1ucian0 commented 3 months ago

Adding two barriers with different semantics seems confusing and the full point of barrier is to have optimization consequences (the drawing alignment is more of a collateral effect).

Let me offer you the following workaround. You can remove the barrier before optimizing with qiskit.transpiler.passes.RemoveBarriers:

For example:

from qiskit import QuantumCircuit

circuit = QuantumCircuit(4)
circuit.z(0)
circuit.h(3)
circuit.barrier()
circuit.h(range(4))
circuit.barrier()
circuit.draw('mpl')

image

circuit.draw('mpl', plot_barriers=False)

image

from qiskit.transpiler.passes import RemoveBarriers
remove_barriers = RemoveBarriers()
new_circuit = remove_barriers(circuit)
new_circuit.draw('mpl')

image

What do you think? That covers your use-case?

YuriyPryyma commented 3 months ago

Support idea for only visual barriers

1ucian0 commented 3 weeks ago

Does plot_barriers=Falsehelp?