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.

It can also be represented as

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

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
The SWAP gate can be represented in matrix form as
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.