As far as I know, we can use barrier method in qiskit to split circuits into two sets to prevent the optimization. However, is there any physical meaning of the barrier? Like a delay operation or buffer gate? If we insert a barrier in the middle of the circuit, the second gate set will begin to be executed until the gates from the first set finishing their executions. What causes the waiting time for the second gate set?
TL;DR There is no "physical" meaning for a barrier. It takes literally no time. The barrier is annotation for the transpiler and for the pulse scheduler. The scheduler waits the duration of the operations before the barrier and then executes the operations after the barrier. In this sense, it is similar to the
delay instruction, but "dynamic". This is, you dont have to known how long the instructions before the barrier are but that is "dynamically" calculated for you.
Long explanation on the effects of barriers:
Barries have two related but relatively independent effects in two different stages: transpilation and scheduled execution.
As you mention, the transpiler does not optimize across barriers. Compare these two situations:
from qiskit import QuantumCircuit, transpile circuit = QuantumCircuit(1) circuit.h(0) circuit.h(0) display(circuit.draw()) display(transpile(circuit, optimization_level=2).draw())
If a barrier is set in the middle, the Hadamard gates wont be optimized:
circuit = QuantumCircuit(1) circuit.h(0) circuit.barrier(0) circuit.h(0) display(circuit.draw()) display(transpile(circuit, optimization_level=2).draw())
The gates in the circuit are scheduled to being executed in the hardware, when possible, in parallel. In this stage, a barrier works like traditional synchronization barrier in the context of concurrent programming. The barrier is a 0-cycle instruction (ie no delay, instantaneous) that tells the scheduler to not start the operations on the right of the barrier until all the operations on the left are finished. The scheduler takes the duration time of the operations on the left and starts the operation on the right after that time.
It is possible see how the circuit is scheduled with the
qiskit.schedule function and a backend that supports OpenPulse.
from qiskit import schedule, QuantumCircuit from qiskit.test.mock import FakeOpenPulse2Q circuit = QuantumCircuit(2) circuit.u2(0.1, 0.2, 0) circuit.u2(0.1, 0.2, 1) schedule(circuit, FakeOpenPulse2Q()).draw()
circuit = QuantumCircuit(2) circuit.u2(0.1, 0.2, 0) circuit.barrier() circuit.u2(0.1, 0.2, 1) display(circuit.draw()) display(schedule(circuit, FakeOpenPulse2Q()).draw())