Does the barrier here won't simplify the circuit between the two H-gates?
-
$\begingroup$ probably just for illustrative purposes... to break the circuit into stages but nothing else $\endgroup$– KAJ226Jun 2, 2022 at 15:37
-
$\begingroup$ take a look at these two videos from qiskit, youtube.com/watch?v=CdoCPz6TC9k , youtube.com/watch?v=tS2CMOyWFMQ $\endgroup$– poigJun 20, 2022 at 19:29
1 Answer
Correct, quoting What is a "barrier" in Qiskit Circuits?:
The barrier 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 (and if there are no barriers they act on the whole circuit)
However, in this example, the circuit cannot be further simplified, so transpiling the circuit without the barriers will not have any effect:
from qiskit import QuantumCircuit
qc = QuantumCircuit(3,1)
qc.h(0)
qc.cnot(0,1)
qc.h(2)
qc.h(0)
qc.draw()
┌───┐ ┌───┐
q_0: ┤ H ├──■──┤ H ├
└───┘┌─┴─┐└───┘
q_1: ─────┤ X ├─────
┌───┐└───┘
q_2: ┤ H ├──────────
└───┘
c: 1/═══════════════
from qiskit.compiler import transpile
qc_transp = transpile(qc)
qc_transp.draw()
┌───┐ ┌───┐
q_0: ┤ H ├──■──┤ H ├
└───┘┌─┴─┐└───┘
q_1: ─────┤ X ├─────
┌───┐└───┘
q_2: ┤ H ├──────────
└───┘
c: 1/═══════════════
Therefore, for the circuit you've given, the inclusion of the barriers does not serve any computational purpose. If more instructions were to be added, then the barriers could once again come into play.