Computing Library › Quantum Logic Gates
Quantum Logic Gates

Fredkin Gate (Controlled-SWAP)

A controlled swap of two target qubits, universal for reversible classical logic and central to the swap test.

Definition

The Fredkin gate, or controlled-SWAP (CSWAP), exchanges its two target qubits when the control is |1⟩ and leaves them alone when the control is |0⟩. It is a three-qubit gate that is reversible and conservative — it preserves the number of ones in its input — and it is universal for reversible classical computation.

Truth table

ca_inb_ina_outb_out
01010
11001
10110
00101

When the control is 0 the targets pass through; when it is 1 they swap. Because it conserves the count of ones, the Fredkin gate models billiard-ball reversible logic and never erases information, so it dissipates no heat in principle.

The swap test

The most common quantum use of CSWAP is the swap test, which estimates the overlap |⟨ψ|φ⟩|² between two states. A Hadamard on an ancilla, a Fredkin controlled by that ancilla swapping the two registers, and a second Hadamard leave the ancilla with a measurement probability that encodes the fidelity between the states.

python

import numpy as np
def fredkin():
    m = np.eye(8, dtype=complex)
    # swap targets when control (msb) = 1: |101> <-> |110>
    m[[5,6]] = m[[6,5]]
    return m

Decomposition

A Fredkin gate compiles into a Toffoli conjugated by two CNOTs: CSWAP = CNOT(b→a)·Toffoli(c,a→b)·CNOT(b→a). The Toffoli itself needs six CNOTs, so a full Fredkin costs about eight two-qubit gates. See multi-controlled X and Peres gate.