# Why Qiskit doesn't invert hard coded unitary matrix?

I was expecting Qiskit to do a upside down version of my unitary as it does for other unitary matrices of CNOT and Toffoli gates given in textbooks : if you convert a toffoli matrix from quantum computing textbooks(target 3rd and ctrl 1st,2nd) you will get an inverted version of it (target as q0 and ctrl as q1,q2).
So I hardcoded a unitary matrix and converted to gate then appended to qc. But the operator returns exactly the same matrix, so I wonder if my unitary is actually upside down but operator just shows flipped output ?

Hard coded matrix : Operator output : Code :

u = QuantumCircuit(qr)
u.unitary(UA, range(qr), label='U')
unitary = u.to_gate()
unitary.name = 'U'


mpl display :

qc = QuantumCircuit(qr)
qc.append(unitary, range(0, qr))
qc.draw("mpl", style='bw') • maybe because the gate is being implemented exactly as it. So if you input in the matrix U = Operator( [[1,0,0,0], [0,1,0,0], [0,0,0,1], [0,0,1,0]]) then what it is implementing is actually the $CX_{q_1, q_0}$ . That is the controlled qubit is the $q_1$ and the target qubit is $q_0$. If you want to implement say $CX_{q_0, q_1}$ then you need to input your unitary as Operator( [ [1,0,0,0], [0,0,0,1] [0,0,1,0], [0,1,0,0] ]) Apr 20, 2022 at 2:29

So I have finally have the time to check up with what I wrote in the comment, and what I commented is indeed the case.

If I passed in the matrix $$U = \begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 1\\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \end{pmatrix}$$ then it does indeed execute the regular $$CNOT$$ gate $$CNOT_{q_0, q_1}$$. You can try it yourself:

from qiskit.quantum_info.operators import Operator
from qiskit import QuantumCircuit, QuantumRegister
import numpy as np
controls = QuantumRegister(2)
circuit = QuantumCircuit(controls)
U = Operator( [
[1, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0],
[0, 1, 0, 0]
])
circuit.unitary(U, [0,1], label='U')
decomp = QuantumCircuit.decompose(circuit) #decompose to well known gates

┌───────────┐     ┌───────────┐
q0_0: ┤ U3(0,0,0) ├──■──┤ U3(0,0,0) ├
├───────────┤┌─┴─┐├───────────┤
q0_1: ┤ U3(0,0,0) ├┤ X ├┤ U3(0,0,0) ├
└───────────┘└───┘└───────────┘