OpenQASM Barrier

Barrier operation prevent simplification and optimization across the barrier on a particular Qubit. In this chapter of the OpenQASM Tutorial, you will be learning about how to use the barrier operation on a Qubit.

Consider the following Quantum Program-

OpenQASM Barrier:
A Quantum Program

Since the application of H Gates twice on the state Qubit has no effect on the state of the Qubit. Both the H Gates are removed by the compiler in the process of simplification and optimization of the Quantum Program. Barrier operation is required for preventing this simplification and optimization process.

Consider the Quantum Program below which is identical to the previous Quantum Program except that it has a barrier between the two H Gates. This Barrier prevents the process of simplification and optimization across the Barries. Hence, the two H Gates will not be removed.

OpenQASM Barrier: A Quantum Program with a Barrier
A Quantum Program with a Barrier

The Barrier operation in OpenQASM can be applied to individual Qubits or to all the Qubits in the Quantum Register at once.

Applying Barrier on Individual Qubits

The Barrier operation can be applied to any particular Qubit of a Quantum Register by using the barrier keyword. The following syntax is used to create a barrier on a Qubit-

barrier quantum_register[index];

Example

Below is an example of applying the barrier operation across the first Qubit of the Quantum Register qubits. This barrier operation is applied between the two H gates that are applied on the same Qubit in the Quantum Program.

h qubits[0];
barrier qubits[0];
h qubits[0];

Barrier operation can be applied to multiple Qubits of a Quantum Register at once. The following is the syntax to create a barrier operation on multiple Qubits at once-

barrier qargs;

where qargs is a list of arguments consisting of various qubits.

Example

Below is an example of applying the barrier operation across the first and second Qubits of the Quantum Register qubits.

barrier qubits[0], qubits[1];

It is not necessary for the arguments to be Qubits from the same Quantum Register.

Example

Below is an example of applying the barrier operation across the first Qubit of the Quantum Register qubits_1 and first Qubit of the Quantum Register qubits_2.

barrier qubits_1[0], qubits_2[0];

Applying Barrier on Quantum Registers

The Barrier operation can be applied to all the Qubits of a Quantum Register by using the barrier keyword. The following syntax is used to create a barrier on all Qubits of the Quantum Register-

barrier quantum_register;

Example

Below is an example of applying the barrier operation across all the Qubits of a Quantum Register qubits.

barrier qubits;

Note– It is also possible to apply barrier operation simultaneously on all Qubits of a Quantum Register, and some Qubits of another Quantum Register.

Example

Below is an example of applying the barrier operation across all the Qubits of Quantum Register qubits_1 and the first Qubit of Quantum Register qubits_2.

barrier qubits_1, qubits_2[0];