What is a SWAP gate?

What is a SWAP gate?

The SWAP gate is a gate in quantum computing that swaps the states of two qubits.


The diagram below shows how a SWAP gate is represented in quantum circuits.

swap-gate-symbol

It can also be represented as

swap-gate-alternate-symbol

The SWAP gate can be decomposed into three CNOT gates, as shown below.

making-a-swap-gate-from-cnot-gates

Thus, the SWAP gate can be thought of as a convenient shorthand for this sequence of CNOT gates.

Mathematically, the SWAP gate's action on a quantum state is

$$ \text{SWAP} (|\phi_{1}\rangle \otimes |\phi_{2}\rangle) = |\phi_{2}\rangle \otimes |\phi_{1}\rangle. $$

The SWAP gate can be represented in matrix form as

$$ \text{SWAP} \equiv \left[ \begin{matrix}1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{matrix} \right]. $$

The SWAP gate is extremely useful in hardware settings; if two qubits are not physically connected, we can simply swap one of those qubits with another that is physically connected to the other qubit. The SWAP gate may also appear as a necessary part in building the quantum Fourier transform or in other routines such as the SWAP test.

The SWAP gate in PennyLane is available as qml.SWAP. This gate is called SwapGate in Qiskit.