The order (of the qubits) matters

Those programming using Quantum Computers will, eventually, come across this somewhat awkward observation when using the simulators or the real computers. The topic is often neglected by tutorials in quantum computing, and, to a certain extend, it is understandable that tutorials do not tackle it at first. In fact, if you are simply running operations on circuits in the machine, it will be hard to spot or bother about it. Nevertheless, it can cause a great deal of confusion and pain for those who are unaware or overlook it.

The problem, firstly addressed in the classical computers and now resurfacing with the quantum computers, is related to the order that qubits are organized on a sequence of qubits. Note that, when considering a sequence of qubits, and considering their significance in relation to the sequence, there are two ways to order them: either the qubit on the leftmost position of the sequence is considered the least significant qubit of the sequence, or the one in the rightmost position.

ok, but why this matters?

You will see, next, that it does!


Let me give an example using the CNOT gate depicted in the classical textbook from Nielsen and Chuang, Quantum Computation and Quantum Information. According to the book (page 178), the gate and its respective unitary operator, are as follow: 


CNOT Gate
$ \begin{pmatrix} 1 & 0 & 0 &0\\\ 0 & 1 & 0 & 0 \\\ 0 & 0 & 0 & 1 \\\ 0 & 0 & 1 & 0 \end{pmatrix} $ 
CNOT Unitary Operator



Now, lets look at how the operator works in the IBMQ platform: qiskit. Using the code snippet below... 
 
qc = QuantumCircuit(2)
qc.cnot(0,1)
qi.Operator(qc
 
... one can obtain the following CNOT unitary operator...

$ \begin{pmatrix} 1 & 0 & 0 &0\\\ 0 & 0 & 0 & 1\\\ 0 & 0 & 1 & 0 \\\ 0 & 1 & 0 & 0 \end{pmatrix} $ 
qiskit CNOT Unitary Operator
 
Oopps!!!! The two operators do not match. What is going on here ? Is there an error in the book or qiskit has a bug ? Actually, both operators are correct. They use different convention to represent a sequence of qubits. The textbooks about Quantum Computing normally adopt the least significant qubit as the leftmost qubit in the sequence, whereas the IBMQ plaftorm adopts the rightmost qubit as the least significant one.
 
So, in the circuit diagram above, we have $q_0$ as the control qubit and $q_1$ as the target qubit. In books, the state vector composed by these two qubits is $|q_0q_1\rangle $. On the other hand, in qiskit the same qubits have the state vector $|q_1q_0\rangle $.
 
To demonstrate we actually have the same operator in both cases, lets use the code snippet below, and revert the qubit order in qiskit.
qc = QuantumCircuit(2)
qc.cnot(0,1)
qi.Operator(qc.reverse_bits())
Note that the "new" operator, shown below, matches the textbook one.

 
$ \begin{pmatrix} 1 & 0 & 0 &0\\\ 0 & 1 & 0 & 0 \\\ 0 & 0 & 0 & 1 \\\ 0 & 0 & 1 & 0 \end{pmatrix} $ 
qiskit CNOT "Reversed" Unitary Operator
 
Another, more subtle example, is the partial trace. Assuming the code below intends to trace out the qubit B from svAB, returning the space vector for A.
A = np.array([+1/np.sqrt(2), -1/np.sqrt(2)])
B = np.array([+1/np.sqrt(2), +1/np.sqrt(2)])

svAB = qi.Statevector(np.kron(A, B))
denA = qi.partial_trace(svAB,[1])
 
denA.to_statevector()
As can be seen here, the resulting state vector is not A, but the qubit B: $ \left( +\frac{1}{\sqrt(2)}, +\frac{1}{\sqrt(2)} \right)$

The wrong assumption about the order of the qubits causes a very subtle bug in the code. The next code snippet takes into account the qubit ordering and traces out, correctly, the vector B.
A = np.array([-1/np.sqrt(2), +1/np.sqrt(2)])
B = np.array([+1/np.sqrt(2), +1/np.sqrt(2)])

svBA = qi.Statevector(np.kron(B, A))
denA = qi.partial_trace(svBA,[1]) 

denA.to_statevector()
The code returns, correctly, the vector space: $ \left( -\frac{1}{\sqrt(2)}, +\frac{1}{\sqrt(2)} \right)$
 
So, wrapping it up...
 
Watch out for the ordering of qubits when implementing code. Textbooks and quantum platforms might use a different convention which can cause, at best, confusion when comparing results. In other cases, it can cause bugs in the circuit which are hard to spot!


Comments

Popular posts from this blog

The simplest quantum binary classifier

IBM Qiskit Exam: my four weeks preparation

A View on the Deutsch Algorithm