6
$\begingroup$

Say we want to swap qubits $a$, $b$ in the same register, where $a,b \in \left \{ 0, 1,\cdots, n-1 \right \}$. What would be the corresponding matrix.

For those interested, I'm curious about this matrix because I'm implementing the QFT, which by default leaves the qubits in reverse order. This is generally handled with a layer of SWAP operations-but I haven't been able to find the matrix implementation.

For instance it is common knowledge that for $a=0$, $b=1$ (a two-qubit register) the matrix is:

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

I understand how this acts on $\langle 10| \equiv \left [ 0, 1, 0, 0 \right ] \leftrightarrow \langle 01| \equiv \left [ 0, 0, 1, 0 \right ] $ but I fail to see where exactly to place the two off-diagonal $1$s in the general case.

$\endgroup$
1
  • $\begingroup$ I realize now that there are many more than just two off-diagonal $1$s in the general case, and that the amount depends on the choice of $a$ and $b$, so perhaps this is not quite an easy problem. Maybe there is a nice way to do it with tensor products? $\endgroup$
    – LPenguin
    Dec 13, 2020 at 18:54

1 Answer 1

7
$\begingroup$

In the general case I think it's easier to consider the matrix in the form $$ M = \sum_{i_1,\dots,i_n, j_1, \dots j_n} c_{i_1,\dots,j_n} |i_1 \dots i_n\rangle \langle j_1 \dots j_n|, $$ where the $i_1, \dots, i_n,j_1, \dots, j_n$ are all binary and the $c$ with the awful index are the elements of the matrix. Now we know the transformation rules so it's not too hard to indicate which coefficients should be zero and which should be one. (I've labelled the systems $1$ to $n$ for brevity as $n-1$ is a big subscript). Now suppose we want to swap system $p$ with system $q$ where $1 \leq p < q \leq n$. We know exactly what transformation we are after so it shouldn't be too difficult to write down the transformation in the above form.

We know that if our state is $|i_1 \dots i_p \dots i_q \dots i_n\rangle$ then we want the map to output the state $|i_1 \dots i_q \dots i_p \dots i_n \rangle$. Well such a map is precisely defined by $$ \begin{aligned} \mathrm{SWAP}_{p,q} &= \sum_{i_1,\dots,i_n} |i_1\dots i_q \dots i_p \dots i_n\rangle \langle i_1 \dots i_p \dots i_q \dots i_n| \\&= \sum_{i_1, \dots,i_n} |i_1\rangle\langle i_1|\otimes \dots \otimes |i_q\rangle \langle i_p |\otimes \dots \otimes |i_p \rangle \langle i_q| \otimes \dots \otimes |i_n\rangle \langle i_n|. \end{aligned} $$ With this form we immediately know the entries of the matrix which are ones (the rest are zero). Then you have to translate this form into the matrix, if $n$ is small then you can do this by hand but it general I would write some code to generate this matrix. For example, the following (written in Mathematica) should generate the swap matrix for swapping subsystems two and three in a three qubit system.

outer[i_, j_] := 
  Transpose[{UnitVector[2, i + 1]}].{UnitVector[2, j + 1]};
(* Let's swap systems 2 and 3 in a 3 qubit system *)

M = ConstantArray[0, {8, 8}];
For[i1 = 0, i1 <= 1, i1++,
  For[i2 = 0, i2 <= 1, i2++,
   For[i3 = 0, i3 <= 1, i3++,
    M = M + 
       KroneckerProduct[outer[i1, i1] , outer[i3, i2], 
        outer[i2, i3]];
    ]
   ]
  ];
M // MatrixForm

This outputs $$ \left( \begin{array}{cccccccc} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\ \end{array} \right). $$ which is what we expect $I \otimes \mathrm{SWAP}$ (do nothing on the first system and do a two qubit swap on the second and third systems).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.