|q⟩ Bad Qubits

beginner · Programming · Two Qubits & Tensor Products

Basis Ordering and Indexing

A two-qubit state vector has four entries, one per basis state. To read or set the right entry you need to know which index corresponds to which label.

From labels to indices

Each computational basis state is a tensor product q0q1|q_0\rangle \otimes |q_1\rangle, written compactly as q0q1|q_0 q_1\rangle. The bits q0q_0 and q1q_1 each take the value 0 or 1.

In qbit-learning the convention is: qubit 0 is the most significant bit (MSB). So the index is computed exactly like reading a two-digit binary number with q0q_0 in the leading position:

index(q0q1)=q021+q120=2q0+q1.\text{index}(|q_0 q_1\rangle) = q_0 \cdot 2^1 + q_1 \cdot 2^0 = 2q_0 + q_1.

Applying that formula to every two-qubit basis state gives the full table:

| Basis label | q0q_0 | q1q_1 | Index | |-------------|-------|-------|-------| | 00|00\rangle | 0 | 0 | 0 | | 01|01\rangle | 0 | 1 | 1 | | 10|10\rangle | 1 | 0 | 2 | | 11|11\rangle | 1 | 1 | 3 |

For example, 10|10\rangle has q0=1q_0 = 1 and q1=0q_1 = 0, giving index 21+0=22 \cdot 1 + 0 = 2.

Why MSB-first?

The choice to place qubit 0 at the most significant position is common in quantum computing toolkits and makes the index formula clean: the index of q0q1qn1|q_0 q_1 \cdots q_{n-1}\rangle in an nn-qubit system is the ordinary binary integer whose bits are q0q1qn1q_0 q_1 \cdots q_{n-1}.

Generalising to nn qubits

For nn qubits the state vector has 2n2^n entries. The index is still the binary integer formed by reading the qubit values from qubit 0 (MSB) to qubit n1n-1 (LSB):

index(q0q1qn1)=k=0n1qk2n1k.\text{index}(|q_0 q_1 \cdots q_{n-1}\rangle) = \sum_{k=0}^{n-1} q_k \cdot 2^{n-1-k}.

Three qubits give eight basis states (000|000\rangle through 111|111\rangle at indices 0–7), four qubits give sixteen, and so on.

Try it

Use the formula above to determine the state-vector index for the two-qubit basis state 10|10\rangle and return that integer.

Run your code to see the quantum state.

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