|q⟩ Bad Qubits

intermediate · Programming · Multi-Qubit Registers & State Simulation

Big-Endian vs Little-Endian Qubits

Every quantum toolkit stores the state vector as an ordered list of 2n2^n amplitudes. The question is: which basis state does index kk refer to? The answer depends on whether the framework uses a big-endian or little-endian qubit convention — and mixing them up is one of the most common sources of subtle bugs in quantum programming.

The two conventions

Consider a 2-qubit system with qubits labeled q0q_0 and q1q_1. There are exactly four computational-basis states:

00,01,10,11.|00\rangle,\quad |01\rangle,\quad |10\rangle,\quad |11\rangle.

To turn a bit-string q0q1q_0 q_1 into an integer index kk we need an ordering rule.

Big-endian (MSB first): qubit 0 is the most significant bit (MSB). The index is

k=q02n1+q12n2++qn120.k = q_0 \cdot 2^{n-1} + q_1 \cdot 2^{n-2} + \cdots + q_{n-1} \cdot 2^{0}.

State 10|10\rangle (qubit 0 = 1, qubit 1 = 0) maps to index 12+01=21 \cdot 2 + 0 \cdot 1 = 2.

Little-endian (LSB first): qubit 0 is the least significant bit (LSB). The index is

k=q020+q121++qn12n1.k = q_0 \cdot 2^{0} + q_1 \cdot 2^{1} + \cdots + q_{n-1} \cdot 2^{n-1}.

State 10|10\rangle (qubit 0 = 1, qubit 1 = 0) now maps to index 11+02=11 \cdot 1 + 0 \cdot 2 = 1.

The same physical state, a different index. This platform uses big-endian: qubit 0 is always the most significant bit.

Encoding an integer

To encode a non-negative integer vv into an nn-qubit big-endian register, write vv in binary with exactly nn bits (padding with leading zeros if needed), then flip qubit ii with an XX gate whenever bit n1in - 1 - i equals 1.

For n=3n = 3 and v=5v = 5 (binary 101101):

| Bit position | Value | Qubit | Gate | |---|---|---|---| | bit 2 (MSB) | 1 | qubit 0 | X | | bit 1 | 0 | qubit 1 | — | | bit 0 (LSB) | 1 | qubit 2 | X |

Applying XX to qubits 0 and 2 produces 101|101\rangle, index 5 in big-endian order.

Why it matters

A controlled gate applies to a specific (control, target) qubit pair. If you believe qubit 0 is the MSB but your tool treats it as the LSB, you will write the control and target on the wrong physical lines, computing a completely different unitary. The state vector will look plausible — it will be normalized — but the algorithm will silently produce wrong answers. Keeping the convention explicit is therefore not a stylistic choice: it is correctness.

Try it

Encode the integer 6 (binary 110110) in a 3-qubit big-endian register. Remember: qubit 0 is the MSB. The grader checks the full state vector, so you must target the right qubits.

Run your code to see the quantum state.

After running, look at the state-vector tab: all amplitude should sit at index 6, confirming that 110|110\rangle is the only populated basis state.

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