|q⟩ Bad Qubits

intermediate · Programming · Multi-Qubit Registers & State Simulation

Registers of n Qubits

A quantum register is a group of qubits treated as a single unit — the quantum analogue of a classical integer register. Understanding how to allocate registers and address individual qubits is the first step toward writing multi-qubit programs.

Allocating a register

Calling circuit(n) creates a fresh nn-qubit register, with every qubit initialised to 0|0\rangle. The full starting state is the tensor product

ψ0=000n=00.|\psi_0\rangle = \underbrace{|0\rangle \otimes |0\rangle \otimes \cdots \otimes |0\rangle}_{n} = |0\cdots 0\rangle.

For three qubits the state vector has 23=82^3 = 8 amplitudes, one for each basis state 000,001,,111|000\rangle, |001\rangle, \ldots, |111\rangle. Only the amplitude of 000|000\rangle is non-zero at the start.

Qubit indexing

Qubits are indexed 0,1,,n10, 1, \ldots, n-1. In this platform qubit 0 is the most-significant bit (the leftmost position in the ket label). So for a 3-qubit register:

| Qubit | Position in ket | Bit value | |-------|-----------------|-----------| | 0 | leftmost | 22=42^2 = 4 | | 1 | middle | 21=22^1 = 2 | | 2 | rightmost | 20=12^0 = 1 |

Applying XX to qubit 2 flips the least-significant bit, so 000001|000\rangle \to |001\rangle. Applying XX to qubit 0 instead would give 100|100\rangle (decimal 4).

State vector after a single flip

After c.x(2) the register is in 001|001\rangle. In the length-8 state vector only amplitude index 1 is non-zero (decimal value of the bit string 001001):

ψ=001,amplitude vector =(0,1,0,0,0,0,0,0)T.|\psi\rangle = |001\rangle, \qquad \text{amplitude vector } = (0,\,1,\,0,\,0,\,0,\,0,\,0,\,0)^T.

More generally, c.x(k)c.x(k) flips the bit at position kk and shifts the non-zero amplitude to the corresponding new basis state.

Try it

Allocate a 3-qubit register and flip qubit 2. After running, inspect the state vector to confirm the amplitude is on 001|001\rangle (index 1).

Run your code to see the quantum state.

Look at the simulator output: the single non-zero amplitude sits at index 1, confirming the state is 001|001\rangle. Changing the x argument to 0 or 1 would move that amplitude to index 4 (100|100\rangle) or index 2 (010|010\rangle) respectively.

Sign in on the full site to ask questions and join the discussion.