Bit (register) declaration

A bit or bit register must be declared before they can be used. The general form is given as follows:

bit[size]identifier

Grammar for bit (register) declaration

bit-declaration:
bit array-size-declarationopt identifier

array-size-declaration:
[integer-literal]

Its form is similar to the declaration of an arbitrary variable, whereby the type of the variable is specified first, i.e. bit denotes that the declared variable is of type Bit. and bit[size] denotes that the declared variable is of type BitArray. The size of a bit register is declared by an integer value between square brackets [size]. The name of the bit (register) is defined through an identifier.

The declaration of a bit (register) is optional, Nevertheless, since measurement outcomes are stored as bits, measure instructions require a previously declared bit (register).

Find below examples, respectively, of a single bit declaration and bit register declaration.

Example

1
2
3
4
5
6
7
8
version 3

qubit q
qubit b  // Single bit declaration of a bit named 'b'.

H q

b = measure q
version 3

qubit[5] q
bit[2] b  // Bit register declaration of a register containing 2 bits, named 'b'.

H q[0]
CNOT q[0], q[1]

b[0] = measure q[0]
b[1] = measure q[1]

The individual bits of a bit register can be referred to by their register index, e.g. in the example of the Bit register declaration, the statement b[0] = measure q[0] indicates that the measurement outcome is stored at the bit located at index 0 of the bit register b. Note that in the case of a single bit, the bit is referred to through its identifier, not through a register index.