OpenQASM: Classical Registers

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

Note– Classical Registers are quite commonly also referred to as Registers. However, throughout this chapter, they will be referred to as Classical Registers to clearly distinct them from their Quantum counterparts.

Classical Registers

A Bit is the most fundamental unit of a Classical Computer. A bit at any given time can be in either of the two states- 0 or 1. Classical Registers are a system of multiple bits. Classical Registers can also be thought of as an array of bits.

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

creg classical_register_name[size];

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

Example

Below is an example of creating a Classical Register by the variable name bits consisting of 2 bits.

# Declaring a Classical Register bits with 2 bits
creg bits[2];

Note– All the bits of the Classical Register are initialized to the state 0.

The bits of a Classical Register can be referred/ indexed by the following syntax-

classical_register_name[index]

The bits of a Classical Register of size n can be referred as- bits[0], bits[1], … , bits[n-1]

The Classical Registers are 0 indexed. Therefore, the first bit in the Classical Register bits is referred by bits[0], the second bit as bits[1], and so on.

Example

Below is an example of referring the first bit of the 2 bit Classical Register bits.

# Referring to the first bit of Classical Register bits
bits[0]

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

Example

Below is an example of declaring a Single bit Classical Register.

# Creating a Single bit classical Register ancilla
creg ancilla[1];