# Matrix representation of SWAP on two qubit registers?

There is a matrix that can represent a swap gate-- a gate that essentially swaps two qubits. This matrix, $$S$$, is:

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

This will swap two single qubits. How would you go about swapping two qubit registers; i.e. two $$2^N$$ dimensional vectors. I tried doing a tensor product on two swap gates:

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

Which I thought would work. To stress test this, I tested two length $$4$$ vectors:

$$\left ( \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \otimes \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \right ) \left ( \begin{bmatrix} 1 \\ 0 \\ 0 \\0 \end{bmatrix} \otimes \begin{bmatrix} 0 \\ 0 \\ 1 \\ 0 \end{bmatrix} \right )$$

I expected to see this:

$$\begin{bmatrix} 9 \: zeros \\ \vdots \\ 1 \\ \vdots \\ 6 \: zeros \end{bmatrix}$$

But instead got this from calculation:

$$\begin{bmatrix} 0 \\ 1 \\ \vdots \\ 14 \: zeros \end{bmatrix}$$

Anyone know how to implement this type of SWAP, or if I went wrong somewhere?

Suppose that you construct a circuit for which $$\vert \theta_1 \rangle = U \vert 0 \rangle^{\otimes 2}$$ and $$\vert \theta_2\rangle = V\vert 0\rangle^{\otimes 2}$$, where I am considering two qubits but this might be generalized to $$n$$. Now, apply SWAP gates;

This circuit essentially does this

Which is the swapping I understood you wanted. Then I think it is easy to have the matrix representation needed.

• So, just to put in the OP's notation (more or less).. you're pointing out that the swap operation is $S_{1,3}\cdot S_{2,4}$, not $S_{1,2}\cdot S_{3,4}=S\otimes S$? Sep 27, 2022 at 10:27
• Yes, I just find the diagrams more clear than the matrices or 1-d notation. But this is precisely the point. At least from how I interpreted the OP's question.
– R.W
Sep 27, 2022 at 11:02

By setting things up properly, we should get what you expected, i.e the following statevector (I will write column vectors as row vectors in this post for convenience):

$$|1000\rangle = \begin{bmatrix} 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \end{bmatrix}$$

By setting things up properly I mean (Using little-endian):

1. Initialize 2 qubit registers, each contains 2 qubits.
2. Intialize a quantum circuit with the 2 registers.
3. In the least significant register, flip the MSB, so that the register will be in the state $$|10\rangle = \begin{bmatrix} 0 & 0 & 1 & 0 \end{bmatrix}$$.
4. Then swap twin qubits between registers (0 to 0, 1 to 1, and so on..)

The following Qiskit code implements the description above and prints the final statevector:

from qiskit import QuantumCircuit, QuantumRegister
from qiskit.quantum_info import Statevector
from qiskit.visualization import array_to_latex

# Initializing registers and circuit
reg_up = QuantumRegister(2, 'reg_up') # Least significant register
reg_down = QuantumRegister(2, 'reg_down') # Most significant register
qc = QuantumCircuit(reg_up, reg_down)

# Setting desired input value
qc.x(reg_up[1])
qc.barrier()

# Swapping
qc.swap(reg_up[0], reg_down[0])
qc.swap(reg_up[1], reg_down[1])

array_to_latex(Statevector(qc), max_size = 16)


Note that if instead of flipping reg_up[1] we flip reg_down[0] the resulted statevector is the wrong one you have mentioned, i.e:

$$|0001\rangle = \begin{bmatrix} 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \end{bmatrix}$$