Swap test
The Swap test is a procedure in quantum computation that is used to check how much two quantum states differ.[1]
Consider two states: and . The state of the system at the beginning of the protocol is . After the Hadamard gate, the state of the system is . The controlled SWAP gate transforms the state into . The second Hadamard gate results in
The Measurement gate on the first qubit ensures that it's 0 with a probability of
when measured. If and are orthogonal , then the probability that 0 is measured is . If the states are equal , then the probability that 0 is measured is 1.[2]
Pseudocode
Below is the pseudocode for implementing the Swap test:
Algorithm Swap Test
- Inputs Two quantum states and , stored in two separate qubit registers, each containing qubits (We denote the -th qubit in the two registers, respectively, by and )
An ancilla qubit, initialized as (We denote the ancilla qubit by )
Some , representing the number of times the algorithm will be executed
- Output Compute
- For ranging from to :
- Apply a Hadamard gate to the ancilla qubit
- For ranging from to (iterating over each pair of qubits in the two registers):
- Apply ( is the control qubit, while and are the targets)
- Apply a Hadamard gate to the ancilla qubit
- Measure the ancilla qubit in the basis and record the result of the measurement (we assume that measurements yield either or , and we denote the outcome of the measurement by )
- Compute
- Return (Note that , with equality occurring as )
- "←" denotes assignment. For instance, "largest ← item" means that the value of largest changes to the value of item.
- "return" terminates the algorithm and outputs the following value.
References
- Kang Min-Sung, Heo Jino, Choi Seong-Gon, Moon Sung, Han Sang-Wook (2019). "Implementation of SWAP test for two unknown states in photons via cross-Kerr nonlinearities under decoherence effect". Scientific Reports. 9 (1). doi:10.1038/s41598-019-42662-4.CS1 maint: multiple names: authors list (link)
- Harry Buhrman, Richard Cleve, John Watrous, Ronald de Wolf (2001). "Quantum Fingerprinting". Physical Review Letters. 87 (16). arXiv:quant-ph/0102001. doi:10.1103/PhysRevLett.87.167902.CS1 maint: multiple names: authors list (link)