# Qiskit reverse_bits is not equivalent to swapping qubits

For three qubits, I thought swapping the first and third qubit is equivalent to vertically flipping the circuit. However, performing one and then the other in Qiskit returns two different operators, despite the fact that the second should undo the first. My example circuit is the QFT on three qubits on which I am doing some analysis. Below is the circuit with no swap or reverse_bits.

N = 3

qr = QuantumRegister(N,'q')
cr = ClassicalRegister(N,'c')
qftCircuit = QuantumCircuit(qr,cr)
ct = TGate().control(1)

# Begin the QFT (with no swap or reverse_bits)
qftCircuit.h(0)
qftCircuit.cs(1,0)
qftCircuit.append(ct,[2,0])
qftCircuit.h(1)
qftCircuit.cs(2,1)
qftCircuit.h(2)

qftCircuit.draw('mpl')


Now when I get this circuit as an operator and compare it to the operator with the swaps, I can see that the circuit visually looks equivalent but they are not mathematically the same operator.(here sp is Sympy just to display it nicely):

noswaps = quantum_info.Operator(qftCircuit).data
qftCircuit.swap(0,2)
qftCircuit = qftCircuit.reverse_bits()
qftCircuit.draw('mpl')


withswaps = quantum_info.Operator(qftCircuit).data
display(sp.Matrix(np.round(noswaps-withswaps,2)))


The question is why isn't this a matrix of zeros?

The issue is that the qubit values are being mixed together throughout the circuit, so swapping at the end is not enough. However, if you also swap at the beginning of the circuit, the qubit values keep the same relative position and you get the expected matrix of zeros.

swapsCircuit = QuantumCircuit(qr,cr)

swapsCircuit.swap(0,2) # swap at the beginning

swapsCircuit.h(0)
swapsCircuit.cs(1,0)
swapsCircuit.append(ct,[2,0])
swapsCircuit.h(1)
swapsCircuit.cs(2,1)
swapsCircuit.h(2)

swapsCircuit.swap(0,2) # and swap at the end
swapsCircuit = swapsCircuit.reverse_bits() # now reverse the bits

withswaps = quantum_info.Operator(swapsCircuit).data
display(swapsCircuit.draw('mpl'))
sp.Matrix(np.round(noswaps-withswaps,2))


• Thanks! So just to be clear, a swap at the end of a circuit diagram is not sufficient to mean “apply this circuit as if the two qubits are switched”. Instead, to do that, you need a swap at the beginning and the end. Commented Dec 26, 2023 at 17:23