Extended Quantum Computing Patterns
SWAP Test
Aliases: –
Tags:
© This pattern is originally published in L. Stiliadou, J. Barzen, M. Beisel, F. Leymann, and B. Weder, "Fundamental Patterns for Quantum Algorithms", in Proceedings of the 1st International Conference on Quantum Software (IQSOFT), Xpert Publishing Services (XPS), 2025.
Intent
How to evaluate how similar two given quantum states are to each other?
Context
Given two \(n\)-qubit quantum states \(\ket{\varphi}\) and \(\ket{\psi}\). Then, the similarity between these states should be calculated.
Forces
The similarity of two quantum states may influence the processing of a quantum algorithm. Performing classical measurements is unsuitable for comparing quantum states as the states are destroyed and can not be used for further computations.
Solution
Perform the SWAP test to determine the similarity of the two given quantum states \(\ket{\varphi}\) and \(\ket{\psi}\).
The structure of the quantum circuit, which is required to perform a SWAP test, is depicted in the sketch.
It requires one ancilla qubit on which a Hadamard gate is applied.
Next, a sequence of controlled SWAP operators is applied to each qubit of the two states using the ancilla qubit as the control qubit.
For example, the first controlled SWAP operation is performed between \(\ket{\varphi_1}\) and \(\ket{\psi_1}\).
, another Hadamard gate is applied to the ancilla qubit, leading to the state:
\(\frac{1}{2} \ket{0}(\ket{\phi}\ket{\psi}+ \ket{\psi}\ket{\phi}) + \frac{1}{2}\ket{1}(\ket{\phi}\ket{\psi}- \ket{\psi}\ket{\phi})\).
Result
After the measurement of the ancilla qubit, the outcome determines the similarity between the states \(\ket{\phi}\) and \(\ket{\psi}\). If the states are identical, the measurement of the ancilla bit results in 0 with probability 1. In contrast, if the states are orthogonal, the measurement results in 0 or 1 with an equal probability of 0.5.
Examples
–
Related Patterns
The QUANTUM CLASSIFICATION pattern [Stiliadou et al., 2025] can utilize the SWAP TEST to estimate the distances between two points. The SWAP TEST can be realized as a QUANTUM MODULE, and its functionality can be provided via a CLASSICAL-QUANTUM INTERFACE to ease its integration with additional classical functionality [Bühler et al., 2023].
Known Uses
The SWAP test was initially introduced by [Buhrman et al., 2001]. [Gitiaux et al., 2022] show how to generalize the SWAP test for an arbitrary number \(m\) of states to compare using \(O(\log(m))\) ancilla qubits. [Foulds et al., 2021] adapt the SWAP test to enable checking the presence of entanglement and show how it can be used to distinguish different entanglement classes. [Zhao et al., 2019] discuss how quantum neural networks can be built using the SWAP test.