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?
-
2$\begingroup$ It seems like if you want the second gate to be executed after $x$ amount of time then you just send the pulse $x$ amount of time into your circuit execution. Barrier is just a feature to help with the compiler process... ultimately your circuit needs to be decomposed into sequence of microwave pulses to be run on the hardware. The barrier tells the compiler to deal with different parts of the circuit as separate circuits. This documentation might be helpful: qiskit.org/documentation/stubs/… $\endgroup$– KAJ226Mar 12, 2021 at 15:45
-
$\begingroup$ Thanks. Does it mean that the barrier does not have a physical meaning? $\endgroup$– peachnutsMar 12, 2021 at 16:55
-
3$\begingroup$ yes. I do not think it has any physical meaning other than an instructional step to the compiler. $\endgroup$– KAJ226Mar 12, 2021 at 17:29
1 Answer
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.
Transpilation
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())
Scheduled execution
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())