Can I change the byte ordering from little endian to big endian in qiskit?
Asked Answered
M

1

7

The regular Matrix representation of a CNOT gate as found in literature is:

CNOT =

      \begin{bmatrix}
      
      1 & 0 &  0 & 0\\
      0 & 1 &  0 & 0\\
      0 & 0 &  0 & 1\\
      0 & 0 &  1 & 0
      
      \end{bmatrix} 
      

However in Qiskit, the matrix is represented as CNOT =

      \begin{bmatrix}

      1  & 0 &  0 &  0\\
      0  & 0 &  0 &  1\\
      0  & 0 &  1 &  0\\
      0  & 1 &  0 &  0

      \end{bmatrix}

Is this something related to the Big-endian/Little-endian issue? Is there a way to represent my matrix the same way it is recovered in literature?

Mason answered 21/1, 2021 at 18:24 Comment(1)
I think this should be asked on Quantum Computing Stack Exchange, because I just realized that SO does not supports LaTeX.Hesperian
S
8

Yes, as you mentioned this has to do with the little endian bit-order in Qiskit. Most textbooks (and the first matrix you showed) are in big endian order.

If you want to know more you could check out these posts/documentation:

If you want to convert your Qiskit circuit to big endian you can just use the reverse_bits method:

from qiskit import QuantumCircuit
from qiskit.quantum_info import Operator

circuit = QuantumCircuit(2)
circuit.cx(0, 1)

print('Little endian:')
print(Operator(circuit))

print('Big endian:')
print(Operator(circuit.reverse_bits()))

gives:

Little endian:
Operator([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
          [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],
          [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
          [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j]],
         input_dims=(2, 2), output_dims=(2, 2))
Big endian:
Operator([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
          [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
          [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],
          [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j]],
         input_dims=(2, 2), output_dims=(2, 2))
Skite answered 22/1, 2021 at 9:3 Comment(1)
Thanks, just realized that endian-ness applies to matrices (gate operators) as well. +1 for snippet.Hesperian

© 2022 - 2024 — McMap. All rights reserved.