Skip to documentation
← Algorithm guides

Algorithm guide · primitive

SWAP test / Hadamard test

Use one measured ancilla to compare two states, or probe the real or imaginary expectation of a unitary.

Circuit size: 2–3 qubits · 1 measured ancilla

Before you begin

Inner products · Complex expectation values · Ancilla interference

Review quantum fundamentals →
Notation used in this lesson
|ψ⟩ and ⟨ψ|
A state vector (ket) and its conjugate transpose (bra). ⟨a|b⟩ is their inner product.
α, β and i
α and β are complex amplitudes; i²=−1. A probability is the squared magnitude |α|².
XOR, · and ⊗
XOR adds bits modulo two. In s·x, multiply corresponding bits and XOR them. ⊗ combines registers or operations on different wires.
U†, Σ and Tr
U† is the adjoint (inverse for a unitary). Σ sums terms. Tr is the matrix trace, the sum of diagonal entries.
Probability, shots and angles
Ideal probabilities describe the model; measured shot frequencies fluctuate. Angles inside trigonometric expressions are in radians unless explicitly labeled turns.

1. The problem

This card has two modes: SWAP estimates the squared overlap of two prepared states; Hadamard estimates the real or imaginary part of ⟨ψ|U|ψ⟩. The ancilla is a probe, not a copy of the answer state.

2. How it works

Prepare two coherent paths for the ancilla. In one path do nothing; in the other swap two states or apply U. Recombining these paths makes their overlap visible in the ancilla’s outcome probabilities.

  1. Choose the task

    Select SWAP or Hadamard mode and the relevant state fields.

  2. Prepare and branch

    Prepare the data and apply H to the ancilla.

  3. Controlled operation

    Apply CSWAP for overlap or controlled-U for an expectation.

  4. Select component and read

    Hadamard imaginary mode adds S† before the final H. Measure only the ancilla.

3. The mathematics, one idea at a time

SWAP: P(0)=[1+|⟨a|b⟩|²]/2

Repeated zeros estimate squared overlap as 2P(0)−1. This does not reveal the phase of ⟨a|b⟩.

Hadamard: P(0)−P(1)=Re⟨ψ|U|ψ⟩ or Im⟨ψ|U|ψ⟩

The selected component is an expectation in [−1,1], not itself a probability.

Work this through by hand

4. Smallest mode: imaginary Hadamard test

Hadamard mode uses two qubits. SWAP requires three; both are supported modes of this card.

Choose Hadamard test, input |1⟩, controlled U=S and Imaginary component.

StageCalculation or stateWhat it means
Compute the expectationS|1⟩=i|1⟩ ⇒ ⟨1|S|1⟩=iIts imaginary part is 1; real part is 0.
Decode the ancillaP(0)−P(1)=1 ⇒ P(0)=1The ideal imaginary-mode result is always zero.
Compare the other modeSWAP(|0⟩,|+⟩): |⟨0|+⟩|²=1/2 ⇒ P(0)=3/4Switching mode changes both the task and the meaning of the zero fraction.

Expected outcomes for this example

These are ideal predictions for the stated configuration, not a result already measured by your browser. Rows describing alternative modes or combined events are labeled explicitly.

Outcome or eventIdeal probabilityInterpretation
0100% for the worked imaginary Hadamard testEstimated imaginary component 1.

5. Load the worked example in Composer

The worked example can use smaller or simpler inputs than the gallery defaults. The button below loads the exact circuit used in this lesson.

Experiment
Hadamard test
Input state
|1⟩
Controlled U
S · phase
Component
Imaginary
  1. Save any current circuit you want to keep, then open the worked example. Loading it does not execute a paid hardware job.
  2. Trace the preparation, main operations and measurements against the worked table. Check the gate editor for controls, targets and angles.
  3. Start with an ideal noiseless simulator compatible with the circuit. Mid-circuit measurement, reset and feed-forward require support from the chosen backend.
  4. Inspect the live preview. For a recorded experiment, open Execute, set the available backend options and shots, and Run. Sign in when prompted to retain it in My Jobs.
  5. Read the output as described below before changing one setting and repeating. The guide describes the original example; edits can change the answer.

6. Read the output correctly

Read c0, the measured ancilla. A zero label does not mean the expectation equals zero; decode using 2f0−1.

For the SWAP extension, roughly 75 zeros out of 100 shots estimate squared overlap 0.5, not 0.75. A single zero shot proves neither equality nor a precise overlap.

A bit label is a register value; its bar height is a probability or count. For S shots and observed count k, the sampled frequency is k/S. A typical sampling scale for probability p is √[p(1−p)/S]; this is not a strict error bound. Finite samples do not have to match the ideal percentages exactly.

7. Common mistakes & limits

  • Taking a real part and taking an absolute square are different operations.
  • Hadamard imaginary mode needs its extra ancilla phase gate; simply relabeling the real-mode histogram is wrong.

8. Check your understanding

What happens if the worked Hadamard example selects Real instead?

Reveal the reasoning

Re(i)=0, so P(0)=P(1)=1/2.

9. Further study

The lesson’s register mappings, supported inputs and loadable example follow Quantket’s implemented circuit. A reference may use different wire names, bit order or resource conventions.

Gallery reference: defaults, definitions & implementation notes

Inputs & default settings

Choose SWAP for state overlap or Hadamard for a controlled-unitary expectation; inspect the ancilla result in Composer.

These are the gallery defaults. The worked example above may override them. Configure the card to change the inputs before loading its editable circuit.

Experiment
Default: swap · Choices for the current settings: SWAP test (Compare two one-qubit states.); Hadamard test (Probe ⟨ψ|U|ψ⟩.).
First state
Default: 0 · Choices for the current settings: |0⟩; |1⟩; |+⟩; |−⟩; |+i⟩; |−i⟩. Shown only for the relevant configuration.
Second state
Default: + · Choices for the current settings: |0⟩; |1⟩; |+⟩; |−⟩; |+i⟩; |−i⟩. Shown only for the relevant configuration.
Input state
Default: 1 · Choices for the current settings: |0⟩; |1⟩; |+⟩; |−⟩; |+i⟩; |−i⟩. Shown only for the relevant configuration.
Controlled U
Default: S · Choices for the current settings: S · phase; X · bit flip; Z · phase flip. Shown only for the relevant configuration.
Component
Default: real · Choices for the current settings: Real; Imaginary. Shown only for the relevant configuration.

Implementation notes

This example uses the gallery's default configuration. Check Run details for the settings used in your execution.

The SWAP test gives P(ancilla=0) = (1 + |⟨a|b⟩|²)/2. Repeated shots estimate squared overlap as 2P(0)−1. One zero outcome alone does not prove the states equal, and the test does not reveal the overlap's phase.

Default example: predictions & circuit facts

These values describe the unedited default setup. They are predictions and circuit properties, not measured results from your saved execution.

Squared overlap
0.5
Ancilla P(0)
75%
Ancilla P(1)
25%

Read your saved result

Check the submitted circuit, measured register and execution settings in Run details. Exact results give computed probabilities; sampled results show frequencies from the recorded shots. Edited circuits and noise can change the expected outcome. The algorithm label records the circuit's origin; it does not certify an edited circuit or a measured result.