← Quantum Store · Guides · Lessons · Glossary · About · Türkçe

Qiskit course: real quantum programming · Lesson 3 / 6 · 8 min

Bit order: reading Qiskit results correctly

Why is q0 on the right in Qiskit results? Learn the bit order so you never mix up textbook results with Qiskit’s output.

Read from right to left

Qiskit writes q0 at the right end of a result: with three qubits the order is q2 q1 q0. In the circuit below only q0 gets an X and the result is 001. Had you written qc.x(2), the result would be 100.

This convention is called little-endian and matches ordinary binary numbers: q0 is the lowest digit. The binary number 001 is 1 and 100 is 4; in Python int("100", 2) also gives 4.

from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler

qc = QuantumCircuit(3)
qc.x(0)
qc.measure_all()

counts = StatevectorSampler().run([qc], shots=1000).result()[0].data.meas.get_counts()
print(counts)   # {'001': 1000}

Why it matters

Many textbooks write qubits from left to right as q0 q1 q2. When you look for a textbook result in Qiskit’s output you may have to reverse the string. That is often why the hidden string in algorithms such as Bernstein–Vazirani appears “backwards”.

To read a single qubit from a result, count from the end of the string: bits[-1] is q0 and bits[-2] is q1.

for bits, n in counts.items():
    q0 = bits[-1]               # the rightmost character is q0
    textbook_order = bits[::-1] # reverse so that q0 is on the left
    print(bits, '→', textbook_order, n)

In drawings and in the statevector

Careful: in the qc.draw() picture q_0 is the top line. Drawings are read from top to bottom, result strings from right to left.

The statevector uses the same order as the results: amplitude number 1 belongs to |001⟩, the state in which only q0 is 1. In the Pauli strings of the next lesson the rightmost letter also acts on q0. This site’s playground uses the same order, which is why the results carry the note that the rightmost bit is q0.

Try it yourself

In the bit order example remove the X and put it on q2: the result becomes 100. Then check in the product state example that the right-hand bit (q0) always stays 1.

Open the circuit in your browser

The circuit playground is in Turkish. Button guide: “1000 kez ölç” = measure 1000 times, “Durum vektörünü göster” = show the state vector, “Bu devrenin Qiskit kodu” = this circuit’s Qiskit code, “Geri al” = undo, “Temizle” = clear. Click a gate in the circuit to remove it.

Key points

Test yourself

In a circuit created with QuantumCircuit(3), only qc.x(2) is applied. Which result does Qiskit give?

Show the answer

100. q2 is the leftmost bit because Qiskit writes results in the order q2 q1 q0: 100.

← Running a circuit: the Sampler and counts · The statevector and expectation values →

Share: X · LinkedIn