Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

endianness - Can I change the byte ordering from little endian to big endian in qiskit?

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

CNOT =

      egin{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 =

      egin{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?

question from:https://stackoverflow.com/questions/65833565/can-i-change-the-byte-ordering-from-little-endian-to-big-endian-in-qiskit

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...