3
$\begingroup$

I have implemented a simple CNOT gate using qiskit

I was expecting a the result the matrix as in the following image (extracted from wikipedia):

CNOT WIKI

from qiskit import *
circuit = QuantumCircuit(2, 2)
circuit.cx(0, 1)
simulator = Aer.get_backend("unitary_simulator")
result = execute(circuit, backend=simulator).result()
unitary = result.get_unitary()
print(circuit.draw())
print(unitary)

The results in the print are:

Results

If you pay attention, it seems the order of the qbits is reversed. I would like to know why this happens and if it's normal because it seems confusing at first glance.

To be really clear: The matrix in the first image and the second (the result) should be the same.

$\endgroup$
2
$\begingroup$

The first matrix is generally seen in books, as you consider computational basis in the order $|0\rangle$, $|1\rangle$, $|2\rangle$, $|3\rangle$ or in bitstrings $|00\rangle$, $|01\rangle$, $|10\rangle$, $|11\rangle$. So basically, you think as $| q_0, q_1\rangle$. This is called little-endian convention.

But in Qiskit, they use the inverse, aka big-endian convention. And then you have to think that it is actually $| q_1, q_0\rangle$. And what you do is that you define $q_0$ as the control. So when $| q_1 = 1, q_0 = 0\rangle$ that is 1 in big-endian, $CNOT |10\rangle = | 10\rangle$, hence 3. Which explains your second row. And when $| q_1 = 1, q_0 = 1\rangle$ that is 3 in big-endian, $CNOT |11\rangle = | 01\rangle$, hence 1 and explaining why you get the last row.

You can find the explanation about the CNOT in Qiskit here.

| improve this answer | |
$\endgroup$
3
$\begingroup$

In Qiskit, the bits are reversed order from most textbook definitions. That is to say the zeroth qubit is the farthest to the right in a bitstring or tensor product.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Ok, thank you for the clarification. Do you know if it's for any particular reason? It seemed confusing at first. Also, any simple numpy function to reverse the order? $\endgroup$ – silgon Apr 8 at 20:29
  • $\begingroup$ It was just a design decision to be more inline with cs notation. $\endgroup$ – Paul Nation Apr 8 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.