OpenQASM: Quantum Registers

Qubits are the building blocks of a Quantum Computer. OpenQASM supports the creation of Quantum Registers– a system of multiple Qubits. In this chapter of the OpenQASM Tutorial, you will be learning about declaring and indexing Quantum Registers in the OpenQASM programming language.

Quantum Registers

Quantum Registers are the Quantum Computing counterparts of Classical Registers. They are a system of multiple Qubits. Quantum Registers can also be thought of as an array of Qubits.

A Quantum Register can be declared by using the qreg keyword. The following syntax is used for declaring a Quantum Register in the OpenQASM programming language-

qreg quantum_register_name[size];

A Quantum Register can have any valid identifier name. The size of the Quantum Register must be a positive integer.

Example

Below is an example of creating a Quantum register by the variable name qubits consisting of 2 Qubits.

# Declaring a Quantum Register qubits with 2 Qubits
qreg qubits[2];

Note– All the Qubits of the Quantum Register are initialized to the state |0>.

The Qubits of a Quantum Register can be referred/ indexed by the following syntax-

quantum_register_name[index]

The Qubits of a Quantum Register of size n can be referred as- qubits[0], qubits[1], … ,qubits[n-1]

The Quantum Registers are 0 indexed. Therefore, the first Qubit in the Quantum Register qubits is referred by qubits[0], the second Qubit as qubits[1], and so on.

Example

Below is an example of referring the first Qubit of the 2 Qubit Quantum Register qubits.

# Referring to the first Qubit of Quantum Register qubits
qubits[0]

OpenQASM 2 does not support the declaration of individual Qubits, but only Quantum Registers. Therefore, a single Qubit needs to be declared as a Quantum Register of 1 Qubit.

Example

Below is an example of declaring a Single Qubits Quantum Register.

# Creating a Single Qubit Quantum Register ancilla
qreg ancilla[1];