Swap test

From HandWiki
Short description: Technique for comparing quantum states
Circuit implementing the swap test between two states [math]\displaystyle{ |\phi\rangle }[/math] and [math]\displaystyle{ |\psi\rangle }[/math]

The swap test is a procedure in quantum computation that is used to check how much two quantum states differ, appearing first in the work of Barenco et al.[1] and later rediscovered by Harry Buhrman, Richard Cleve, John Watrous, and Ronald de Wolf.[2] It appears commonly in quantum machine learning, and is a circuit used for proofs-of-concept in implementations of quantum computers.[3][4]

Formally, the swap test takes two input states [math]\displaystyle{ |\phi\rangle }[/math] and [math]\displaystyle{ |\psi\rangle }[/math] and outputs a Bernoulli random variable that is 1 with probability [math]\displaystyle{ \textstyle\frac{1}{2} - \frac{1}{2} {|\langle \psi | \phi\rangle|}^2 }[/math] (where the expressions here use bra–ket notation). This allows one to, for example, estimate the squared inner product between the two states, [math]\displaystyle{ {|\langle \psi | \phi\rangle|}^2 }[/math], to [math]\displaystyle{ \varepsilon }[/math] additive error by taking the average over [math]\displaystyle{ O(\textstyle\frac{1}{\varepsilon^2}) }[/math] runs of the swap test.[5] This requires [math]\displaystyle{ O(\textstyle\frac{1}{\varepsilon^2}) }[/math] copies of the input states. The squared inner product roughly measures "overlap" between the two states, and can be used in linear-algebraic applications, including clustering quantum states.[6]

Explanation of the circuit

Consider two states: [math]\displaystyle{ |\phi\rangle }[/math] and [math]\displaystyle{ |\psi\rangle }[/math]. The state of the system at the beginning of the protocol is [math]\displaystyle{ |0, \phi, \psi\rangle }[/math]. After the Hadamard gate, the state of the system is [math]\displaystyle{ \frac{1}{\sqrt{2}}(|0, \phi, \psi\rangle + |1, \phi, \psi\rangle) }[/math]. The controlled SWAP gate transforms the state into [math]\displaystyle{ \frac{1}{\sqrt{2}}(|0, \phi, \psi\rangle + |1, \psi, \phi\rangle) }[/math]. The second Hadamard gate results in [math]\displaystyle{ \frac{1}{2}(|0, \phi, \psi\rangle + |1, \phi, \psi\rangle + |0, \psi, \phi\rangle - |1, \psi, \phi\rangle) = \frac{1}{2}|0\rangle(|\phi, \psi\rangle + |\psi, \phi\rangle) + \frac{1}{2}|1\rangle(|\phi, \psi\rangle - |\psi, \phi\rangle) }[/math]

The measurement gate on the first qubit ensures that it's 0 with a probability of

[math]\displaystyle{ P(\text{First qubit} = 0) = \frac{1}{2} \Big( \langle \phi | \langle \psi | + \langle \psi | \langle \phi | \Big) \frac{1}{2} \Big( | \phi \rangle | \psi \rangle + | \psi \rangle | \phi \rangle \Big) = \frac{1}{2} + \frac{1}{2} {|\langle \psi | \phi\rangle|}^2 }[/math]

when measured. If [math]\displaystyle{ \psi }[/math] and [math]\displaystyle{ \phi }[/math] are orthogonal [math]\displaystyle{ ({|\langle \psi | \phi\rangle|}^2 = 0) }[/math], then the probability that 0 is measured is [math]\displaystyle{ \frac{1}{2} }[/math]. If the states are equal [math]\displaystyle{ ({|\langle \psi | \phi\rangle|}^2 = 1) }[/math], then the probability that 0 is measured is 1.[2]

In general, for [math]\displaystyle{ P }[/math] trials of the swap test using [math]\displaystyle{ P }[/math] copies of [math]\displaystyle{ |\phi\rangle }[/math] and [math]\displaystyle{ P }[/math] copies of [math]\displaystyle{ |\psi\rangle }[/math], the fraction of measurements that are zero is [math]\displaystyle{ 1 - \textstyle\frac{1}{P} \textstyle\sum_{i = 1}^{P} M_i }[/math], so by taking [math]\displaystyle{ P \rightarrow \infty }[/math], one can get arbitrary precision of this value.

Below is the pseudocode for estimating the value of [math]\displaystyle{ |\langle \psi | \phi \rangle |^2 }[/math] using P copies of [math]\displaystyle{ |\psi\rangle }[/math] and [math]\displaystyle{ |\phi\rangle }[/math]:

Inputs P copies each of the n qubits quantum states [math]\displaystyle{ |\psi\rangle }[/math] and [math]\displaystyle{ |\phi\rangle }[/math]
Output An estimate of [math]\displaystyle{ |\langle \psi | \phi \rangle |^2 }[/math]

for j ranging from 1 to P:
    initialize an ancilla qubit A in state [math]\displaystyle{ |0\rangle }[/math]
    apply a Hadamard gate to the ancilla qubit A
    for i ranging from 1 to n: 
        apply CSWAP to [math]\displaystyle{ \psi_i }[/math] and [math]\displaystyle{ \phi_i }[/math] (the ith qubit of the jth copy of [math]\displaystyle{ |\psi\rangle }[/math] and [math]\displaystyle{ |\phi\rangle }[/math]), with A as the control qubit
    apply a Hadamard gate to the ancilla qubit A
    measure A in the [math]\displaystyle{ Z }[/math] basis and record the measurement Mj as either a 0 or 1
compute [math]\displaystyle{ s = 1 - \textstyle\frac{2}{P} \textstyle\sum_{i = 1}^{P} M_i }[/math].
return [math]\displaystyle{ s }[/math] as our estimate of [math]\displaystyle{ |\langle \psi | \phi \rangle |^2 }[/math]

References

  1. Adriano Barenco, André Berthiaume, David Deutsch, Artur Ekert, Richard Jozsa, Chiara Macchiavello (1997). "Stabilization of Quantum Computations by Symmetrization". SIAM Journal on Computing 26 (5): 1541–1557. doi:10.1137/S0097539796302452. 
  2. 2.0 2.1 Harry Buhrman, Richard Cleve, John Watrous, Ronald de Wolf (2001). "Quantum Fingerprinting". Physical Review Letters 87 (16): 167902. doi:10.1103/PhysRevLett.87.167902. PMID 11690244. Bibcode2001PhRvL..87p7902B. 
  3. Schuld, Maria; Sinayskiy, Ilya; Petruccione, Francesco (2015-04-03). "An introduction to quantum machine learning". Contemporary Physics 56 (2): 172–185. doi:10.1080/00107514.2014.964942. ISSN 0010-7514. Bibcode2015ConPh..56..172S. https://doi.org/10.1080/00107514.2014.964942. 
  4. 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): 6167. doi:10.1038/s41598-019-42662-4. PMID 30992536. Bibcode2019NatSR...9.6167K. 
  5. de Wolf, Ronald (2021-01-20). "Quantum Computing: Lecture Notes". pp. 117–119, 122. arXiv:1907.09415 [quant-ph].
  6. Wiebe, Nathan; Kapoor, Anish; Svore, Krysta M. (1 March 2015). "Quantum Algorithms for Nearest-Neighbor Methods for Supervised and Unsupervised Learning". Quantum Information and Computation (Rinton Press, Incorporated) 15 (3–4): 316–356. doi:10.26421/QIC15.3-4-7. https://dl.acm.org/doi/10.5555/2871393.2871400.