Skip to main content

Module swap_test

Module swap_test 

Source
Expand description

Ancilla-interference primitives: the SWAP test and the Hadamard test.

Both extract a number from the interference of an ancilla qubit prepared in |+⟩, conditionally coupled to a work register, and measured in the X basis. They are the canonical building blocks for overlap estimation and for measuring expectation values of (controlled) unitaries.

  • swap_test estimates the squared state overlap |⟨ψ|φ⟩|² from the ancilla statistics P(0) = (1 + |⟨ψ|φ⟩|²)/2 (Buhrman et al., 2001).
  • hadamard_test estimates Re⟨ψ|U|ψ⟩ (or Im⟨ψ|U|ψ⟩ with the imaginary variant) from P(0) = (1 ± value)/2.

Because this crate stores the full state vector, the ancilla measurement probability is read off exactly (measure_prob) rather than sampled, so the returned values are the analytic results, not shot estimates.

Functions§

hadamard_test
Estimate ⟨ψ|U|ψ⟩ for a single-qubit state ψ and single-qubit gate U via the Hadamard test.
swap_test
Estimate the squared overlap |⟨ψ1|ψ2⟩|² of two k-qubit states via the SWAP test.