Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Quantum interference: making answers cancel

After this lesson: I can show how gates make wrong answers cancel.

55 min lesson3 commandsQuantum simulator lab: qubits, gates and measurement in JupyterLab

Interference makes wrong answers cancel out

Interference is what happens when two routes to the same outcome add up or cancel out. In a quantum program it is the main tool: gates are arranged so the wrong answers cancel and the right one is left.

It matters because it is the real reason a quantum algorithm can work at all. In this lesson you make an answer vanish with a sign, then check it in the lab.

By the end of this lesson

Note

I can show how gates make wrong answers cancel.

Key words

WordWhat it meansExample
Interferencetwo routes to one outcome adding up or cancelling
Circuita list of gates applied one after anotherH, Z, H
Signwhether an amplitude is positive or negative
Reinforceto add up so the amplitude grows
Cancelto add up to zero
Amplitudea number whose square is a chance

The cells students fill in are left blank in this preview.

Two routes to one outcome can cancel

An amplitude can be positive or negative. When a gate gives an outcome two routes, the amplitudes from the routes are added together.

Two positive amplitudes reinforce. A positive and a negative of the same size cancel. You add the amplitudes first and square afterwards. You never add chances.

Worked example: two circuits side by side

StepH, HH, Z, H
Start[1, 0][1, 0]
After the first H[0.707, 0.707][0.707, 0.707]
After Znot used[0.707, -0.707]
After the last H[1, 0][0, 1]
What you readcertainly 0certainly 1

The cells students fill in are left blank in this preview.

Why the answer 1 vanishes in H, H

In H, H the last H sends an outcome of 1 along two routes. One has 0.707 times 0.707, which is +0.5. The other has 0.707 times -0.707, which is -0.5.

They add to zero, so 1 never appears. In H, Z, H the sign in the middle swaps which outcome cancels.

Run the interference script

Command
docker exec jupyter python3 steps/interfere.py

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A hidden sign still changes the result

Before the last H, the states [0.6, 0.8] and [0.6, -0.8] have the same chances. A measurement could not tell them apart.

After the last H they differ a lot. The sign decides which of the two routes reinforce and which cancel. That is why Z, which looks like it does nothing, is useful.

Worked example: the same chances, different results

State before HChance of 0 beforeChance of 0 afterChance of 1 after
[0.6, 0.8]0.360.980.02
[0.6, -0.8]0.360.020.98

The cells students fill in are left blank in this preview.

H on [0.6, 0.8]

Command
docker exec jupyter python3 -c "from qsim import *; print(probabilities(apply(H, np.array([0.6, 0.8], dtype=complex), 0)).round(3))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

H on [0.6, -0.8]

Command
docker exec jupyter python3 -c "from qsim import *; print(probabilities(apply(H, np.array([0.6, -0.8], dtype=complex), 0)).round(3))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think two routes to an outcome can only make it more likely. That is true of chances, but amplitudes can be negative. Two routes can cancel, and then the outcome never appears. Add the amplitudes first, and square only the total.

Run a circuit in plain Python

Code: python
R = 0.5 ** 0.5
H = [[R, R], [R, -R]]
Z = [[1, 0], [0, -1]]

def apply_gate(gate, state):
    result = []
    for row in gate:
        total = 0
        for i in range(len(state)):
            total = total + row[i] * state[i]
        result.append(total)
    return result

state = [1, 0]
for gate in [H, Z, H]:
    state = apply_gate(gate, state)
# A tiny error in the last digits is normal. Treat it as rounding.
print(state)
# Now delete Z from the list and run it again.

About this lab and its source: Quantum simulator lab: qubits, gates and measurement in JupyterLab

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.