Skip to content

iniestarchen/swap-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

SWAP Test

Category: simulation  |  Difficulty: intermediate  |  Qubits: 3  |  Gates: 4  |  Depth: 4

The SWAP test non-destructively estimates the overlap |⟨ψ|φ⟩|² of two quantum states. An ancilla qubit in superposition controls a SWAP between the two state registers. P(ancilla=1) = (1 - |⟨ψ|φ⟩|²)/2. For identical states: P(1)=0, for orthogonal: P(1)=0.5. This circuit tests |ψ⟩=|0⟩ vs |φ⟩=|+⟩, so |⟨0|+⟩|²=0.5 → P(1) = 0.25.

Expected Output

P(1) ≈ 0.25 (|⟨0|+⟩|² = 0.5 → P(1) = (1-0.5)/2 = 0.25)

Circuit

The OpenQASM 2.0 circuit is in circuit.qasm.

OPENQASM 2.0;
include "qelib1.inc";
// SWAP test: estimate |<psi|phi>|^2
// q[0]: ancilla, q[1]: |psi>=|0>, q[2]: |phi>=|+>
qreg q[3];
creg c[1];
// Prepare |phi> = |+>
h q[2];
// SWAP test
h q[0];
cswap q[0],q[1],q[2];
h q[0];
// P(1) = (1 - |<0|+>|^2) / 2 = (1 - 0.5) / 2 = 0.25
measure q[0] -> c[0];

Tags

swap-test state-overlap fidelity ancilla

References

License

MIT — part of the OpenQC Algorithm Catalog.

About

Measure |⟨ψ|φ⟩|² between two quantum states using a Fredkin (controlled-SWAP) gate.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors