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

\[\begin{split}U = \begin{pmatrix} u_{00} & u_{01} \\ u_{10} & u_{11} \end{pmatrix}\end{split}\]

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

\[\begin{split}C_U = \begin{pmatrix} 1 & 0 & 0 & 0 \\0 & u_{00} & 0 & u_{01} \\ 0 & 0 & 1 & 0 \\ 0 & u_{10} & 0& u_{11} \end{pmatrix}\end{split}\]

while in a physics textbook it would be written as

\[\begin{split}C_U = \begin{pmatrix} 1 & 0 & 0 & 0 \\0 & 1 & 0 & 0 \\ 0 & 0 & u_{00} & u_{01} \\ 0 & 0 & u_{00} & u_{01} \end{pmatrix}\end{split}\]

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)

(Source code)

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

(Source code)