Order of qubits in Qiskit#
While most physics textbooks represent an \(n\)-qubit system as the tensor product \(Q_0\otimes Q_1 \otimes ... \otimes Q_{n-1}\), where \(Q_j\) is the \(j^{\mathrm{th}}\) qubit, Qiskit uses the inverse order, that is, \(Q_{n-1}\otimes ... \otimes Q_1 \otimes Q_{0}\). As explained in this video from Qiskit's YouTube channel, this is done to follow the convention in classical computing, in which the \(n^{\mathrm{th}}\) bit or most significant bit (MSB) is placed on the left (with index 0) while the least significant bit (LSB) is placed on the right (index \(n-1\)). This ordering convention is called little-endian while the one from the physics textbooks is called big-endian.
This means that if we have, for example, a 3-qubit system with qubit 0 in state \(|1\rangle\) and qubits 1 and 2 in state \(|0\rangle\), Qiskit would represent this state as \(|001\rangle\) while most physics textbooks would represent this state as \(|100\rangle\).
The matrix representation of any multi-qubit gate is also affected by this different qubit ordering. For example, if we consider the single-qubit gate
And we want a controlled version \(C_U\) whose control qubit is qubit 0 and whose target is qubit 1, following Qiskit's ordering its matrix representation would be
while in a physics textbook it would be written as
For more details about how this ordering of MSB and LSB affects the matrix representation of any particular gate, check its entry in the circuit library
.
This different order can also make the circuit corresponding to an algorithm from a textbook a bit more complicated to visualize. Fortunately, Qiskit provides a way to represent a QuantumCircuit
with the most significant qubits on top, just like in the textbooks. This can be done by setting the reverse_bits
argument of the draw()
method to True
.
Let's try this for a 3-qubit Quantum Fourier Transform (QFT
).
from qiskit.circuit.library import QFT
qft = QFT(3)
qft.decompose().draw('mpl')

qft.decompose().draw('mpl', reverse_bits=True)
