Swap test
Appearance
![]() | This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these messages)
|

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.[1]
Example
An implementation of the Swap test in Cirq.
"""Demonstrates Swap test.
=== EXAMPLE OUTPUT ===
0: ───H───@───H───M('Results')───
│
1: ───H───×──────────────────────
│
2: ───H───×──────────────────────
Results are all 0 because the states are equal.
Results=0000000000
0: ───H───────@───H───M('Results')───
│
1: ───X───H───×──────────────────────
│
2: ───H───────×──────────────────────
Not all the results are 0 because the states are not equal.
Results=1111000011
"""
import cirq
def swap_test(q0, q1, q2, circuit):
circuit.append([
cirq.H(q0),
cirq.CSWAP(q0, q1, q2),
cirq.H(q0),
cirq.measure(q0, key='Results'),
])
simulator = cirq.Simulator()
print(circuit)
result = simulator.run(circuit, repetitions=10)
return result
def main():
q0, q1, q2 = cirq.LineQubit.range(3)
equal_states = cirq.Circuit.from_ops(
cirq.H(q1),
cirq.H(q2),
)
results = swap_test(q0, q1, q2, equal_states)
print("Results are all 0 because the states are equal.")
print(results)
non_equal_states = cirq.Circuit.from_ops(
cirq.X(q1),
cirq.H(q1),
cirq.H(q2),
)
results = swap_test(q0, q1, q2, non_equal_states)
print("Not all the results are 0 because the states are not equal.")
print(results)
if __name__ == '__main__':
main()
References
- ^ a b
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.
{{cite journal}}
: CS1 maint: multiple names: authors list (link)
This article needs additional or more specific categories. (August 2019) |