1

This is my code using qiskit below. I am not familiar with Unitarygate, so I tried to creat a cnot-gate.

from qiskit import QuantumCircuit
from qiskit.extensions import UnitaryGate
import numpy as np

matrix1 = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]] gatecx = UnitaryGate(matrix1) matrix2 = [[0,1], [1,0]] gatex = UnitaryGate(matrix2) matrix3 = [[1/np.sqrt(2),1/np.sqrt(2)], [1/np.sqrt(2),-1/np.sqrt(2)]] gateh = UnitaryGate(matrix3)

circuit = QuantumCircuit(2,2) circuit.append(gatex,[0]) circuit.append(gatecx,[0,1]) circuit.draw('mpl',scale=0.6)

I apply a x-gate to qubit0 first, then apply a cx-gate. enter image description here

After running this code, qubit1 haven't be flipped. The result have shown that qubit0 is target-q and qubit1 is control-q. That is beyond comprehension!! enter image description here

What make me confused is why qubit0 isn't control-qubit in code circuit.append(gatecx,[0,1])?

Then I swapped the position of 0 and 1, circuit.append(gatecx,[1,0]), qubit1 was flipped. Is the first position a control qubit? enter image description here

Telore
  • 11
  • 1

1 Answers1

3

This is because Qiskit uses little endian bit ordering. See the details here.

So, in your code should define matrix1 as follows:

matrix1 = [
    [1, 0, 0, 0],
    [0, 0, 0, 1],
    [0, 0, 1, 0],
    [0, 1, 0, 0]
]

You may also use the fact that Qiskit saves matrix representation in the gate to define matrix1

from qiskit.circuit.library import CXGate

matrix1 = CXGate().to_matrix()

Egretta.Thula
  • 9,972
  • 1
  • 11
  • 30
  • Thanks for your answer. After reading the link above and the docs of qiskit, I found that to be the case. Although I think that's a little counter-intuitive... – Telore Mar 19 '23 at 08:21