Skip to the content
Kets & Gates homeOpen the app

Lesson 9: Parallelism, Deutsch and Deutsch–Jozsa

One query, one global answer: phase kickback and interference

about 45 min15 partsbest after lesson 6 and lesson 7

Start the interactive lesson

The interactive version checks every answer, gives hints and saves your progress in this browser. This page is the same lesson on one page, with the answers folded away.

1 / 15 · Learn

Before you start

A measurement returns one bit per qubit (Lesson 1), and a relative phase becomes visible only after a gate such as HH makes the branches interfere (Lessons 4 and 5). This lesson puts both facts to work in the first quantum algorithms, with the phase kickback of Lessons 6 and 7 as the key step.

Part A: the oracle, quantum parallelism and Deutsch’s algorithm. Part B: nn input bits, the Deutsch–Jozsa algorithm, and how many queries each method needs.

By the end of this lesson you can:

  • apply Uf∣x,y⟩=∣x,y⊕f(x)⟩U_f\ket{x,y} = \ket{x, y\oplus f(x)} to basis states and to superpositions;
  • say what one measurement after a query on a superposition reveals, and what it doesn’t;
  • derive phase kickback and trace Deutsch’s algorithm for all four one-bit functions;
  • compute H⊗n∣x⟩H^{\otimes n}\ket x and the ∣0…0⟩\ket{0\ldots0} amplitude in Deutsch–Jozsa;
  • compare query counts: quantum, deterministic classical and randomized classical.

Check yourself

A warm-up: make a guess before reading on. The lesson comes back to it.

A quantum computer applies UfU_f once to a superposition of all eight 3-bit inputs, producing 18∑x∣x⟩∣f(x)⟩\tfrac{1}{\sqrt{8}}\sum_x\ket{x}\ket{f(x)}. Then every qubit is measured in the 0/1 basis. What does this one run tell you about ff?

  1. AAll eight values f(x)f(x): the whole table of ff
  2. BNothing: measuring a superposition gives meaningless random bits
  3. COnly f(000)f(000), because the inputs started in ∣000⟩\ket{000}
  4. DThe value f(x)f(x) at one input xx, chosen at random
Show the answer

Answer: D. The value f(x)f(x) at one input xx, chosen at random

Each branch ∣x⟩∣f(x)⟩\ket{x}\ket{f(x)} has amplitude 18\tfrac{1}{\sqrt{8}}. The measurement picks one branch at random, with probability 1/81/8 each, and returns its input and its value: one f(x)f(x) per run, which is no more than one classical query gives.

Why not the others

  • A. The state involves all eight values, but a 0/1 measurement of 4 qubits returns 4 bits: one input xx and its value f(x)f(x). The other branches are gone once you have measured.
  • B. The bits are not meaningless. Every branch is ∣x⟩∣f(x)⟩\ket{x}\ket{f(x)}, so in every result the last bit equals ff of the first ones: you learn one genuine value.
  • C. Every input has the same amplitude, 18\tfrac{1}{\sqrt{8}}, so the measured xx is random: 000000 turns up only one time in 8.

Check yourself

A warm-up: make a guess before reading on. The lesson comes back to it.

A black box computes ff on 3-bit strings (eight inputs). You are promised that ff is either constant or balanced (0 on exactly 4 inputs and 1 on the other 4). Querying one input at a time, and needing to be certain, how many queries might you need in the worst case?

  1. A22
  2. B33
  3. C55
  4. D88
Show the answer

Answer: C. 55

In the worst case the first 4 answers agree, which a balanced ff allows. Query number 5 decides: 2n−1+1=52^{n-1} + 1 = 5 for n=3n = 3.

Why not the others

  • A. Two queries settle it only if they disagree. If they agree, a balanced ff is still possible: 4 inputs share each value.
  • B. The count depends on the number of inputs, not on the number of bits in xx. A balanced ff can give the same value on any 4 inputs, so 4 agreeing answers prove nothing.
  • D. You never need all 8. A balanced ff has only 4 inputs of each value, so once 5 answers agree, ff must be constant.

2 / 15 · Learn

Part A · The oracle and quantum parallelism

Part A: the oracle, what a query on a superposition gives you, and Deutsch’s algorithm.

A function ff from one bit to one bit is either constant (f(0)=f(1)f(0) = f(1)) or balanced (f(0)≠f(1)f(0) \ne f(1)). To use ff in a circuit you need a gate, and a gate that simply replaced xx by f(x)f(x) fails for a constant ff: both inputs would land on one output, which no reversible gate allows. The fix is the Toffoli trick from Lesson 6: keep the input, and XOR the value into a second qubit,

Uf∣x,y⟩=∣x, y⊕f(x)⟩.U_f\ket{x, y} = \ket{x,\ y\oplus f(x)}.

Here xx is called the data register and yy the target. Applying UfU_f twice gives back y⊕f(x)⊕f(x)=yy\oplus f(x)\oplus f(x) = y, so UfU_f undoes itself: it only permutes the basis states.

Check yourself

For f(x)=xˉf(x) = \bar x, so f(0)=1f(0) = 1 and f(1)=0f(1) = 0, what is Uf∣0,1⟩U_f\ket{0, 1}?

  1. A∣0,0⟩\ket{0, 0}
  2. B∣1,1⟩\ket{1, 1}
  3. C∣0,1⟩\ket{0, 1}
  4. D∣1,0⟩\ket{1, 0}
Show the answer

Answer: A. ∣0,0⟩\ket{0, 0}

f(0)=1f(0) = 1, so the target becomes y⊕f(x)=1⊕1=0y\oplus f(x) = 1\oplus1 = 0, and the data qubit is untouched: ∣0,0⟩\ket{0, 0}. The truth table above shows the same row.

Why not the others

  • B. That replaces xx by f(x)f(x). On basis inputs UfU_f never changes the data qubit; the value goes into the target, by XOR.
  • C. That writes f(0)=1f(0) = 1 into the target, replacing yy. UfU_f adds it mod 2 instead: y⊕f(x)=1⊕1=0y\oplus f(x) = 1\oplus1 = 0. Overwriting would erase yy, and UfU_f could not be undone.
  • D. Only the target can change on a basis input. The data qubit stays ∣0⟩\ket0, and the target becomes 1⊕f(0)=01\oplus f(0) = 0.

Now give UfU_f a superposition. With the data qubit in ∣+⟩\ket+ and the target in ∣0⟩\ket0, UfU_f acts on each branch separately (linearity):

Uf(∣0⟩+∣1⟩2 ∣0⟩)=12(∣0,f(0)⟩+∣1,f(1)⟩)\begin{aligned}&U_f\Big(\tfrac{\ket0 + \ket1}{\sqrt2}\,\ket0\Big) \\ &\qquad = \tfrac{1}{\sqrt2}\big(\ket{0, f(0)} + \ket{1, f(1)}\big)\end{aligned}

One use of UfU_f, and the state involves both f(0)f(0) and f(1)f(1). With nn data qubits, each in ∣+⟩\ket+, there are 2n2^n branches, and one use of UfU_f gives 12n∑x∣x,f(x)⟩\tfrac{1}{\sqrt{2^n}}\sum_x\ket{x, f(x)}. This is called quantum parallelism. Whether you can get the values out is the next question.

Check yourself

Let f(x)=xˉf(x) = \bar x. Enter Uf(∣+⟩∣0⟩)U_f\big(\ket+\ket0\big) as four amplitudes, ∣00⟩\ket{00} to ∣11⟩\ket{11} (data qubit first).

Hints
  1. Apply UfU_f to each branch, ∣0,0⟩\ket{0,0} and ∣1,0⟩\ket{1,0}, separately.
  2. Uf∣x,0⟩=∣x,f(x)⟩U_f\ket{x, 0} = \ket{x, f(x)}, with f(0)=1f(0) = 1 and f(1)=0f(1) = 0.
Show the answer

Answer: 12∣01⟩+12∣10⟩\frac{1}{\sqrt{2}}|01\rangle + \frac{1}{\sqrt{2}}|10\rangle

Branch by branch: Uf∣0,0⟩=∣0,1⟩U_f\ket{0,0} = \ket{0,1} and Uf∣1,0⟩=∣1,0⟩U_f\ket{1,0} = \ket{1,0}. So the result is (∣01⟩+∣10⟩)/2(\ket{01} + \ket{10})/\sqrt2, which is the Bell state ∣β01⟩\ket{\beta_{01}}: the data and the target are now entangled.

3 / 15 · Explore

Part A · What one run reveals

Predict, then see

Two data qubits, each in ∣+⟩\ket+, a target in ∣0⟩\ket0, and one use of UfU_f give

12(∣00,f(00)⟩+∣01,f(01)⟩+∣10,f(10)⟩+∣11,f(11)⟩).\begin{aligned}\tfrac12\big(&\ket{00, f(00)} + \ket{01, f(01)} \\ + &\ket{10, f(10)} + \ket{11, f(11)}\big).\end{aligned}

You measure all three qubits in the 0/1 basis. Then you measure the same three qubits again, without preparing anything new. What do the two results show?

  1. AAll four pairs at once; the second measurement adds nothing new
  2. BTwo different pairs (x,f(x))(x, f(x)): each measurement reads out another branch
  3. CTwo random 3-bit strings with no link to ff
  4. DThe same pair (x,f(x))(x, f(x)) both times, with xx random

Why did the second measurement repeat the first, and what does it cost to see another pair (x,f(x))(x, f(x))? A sentence or two is enough.

Show what happens

What happens: D. The same pair (x,f(x))(x, f(x)) both times, with xx random

Each measurement returns one branch ∣x,f(x)⟩\ket{x, f(x)}: a random xx (probability 14\tfrac14 each) with its correct value. Measuring again repeats it, because the measurement left the register in that basis state. Learning more needs fresh copies, each costing another use of UfU_f: one value per query, as on a classical computer, except that you don’t get to choose xx.

Why not the others

  • A. Three qubits measured in the 0/1 basis give three bits: two for xx and one for f(x)f(x). The state involves all four values, but a measurement returns a single branch, picked at random.
  • B. With all four branches in the state, it seems a second look should find another one. But in the widget the second result repeated the first: one measurement keeps one branch and discards the rest.
  • C. The bits are not noise. In every result the last bit equals ff of the first two, because every branch is ∣x,f(x)⟩\ket{x, f(x)}. Check any result in the widget against the values listed above it.
Why not just read off the amplitudes?

The circles show the amplitudes because this is a simulation. A real register offers no such readout. The only access is measurement, and a 0/1-basis measurement of three qubits returns three bits (Lesson 6). Estimating amplitudes from statistics takes many fresh copies, and so many uses of UfU_f.

Check yourself

Three data qubits, each in ∣+⟩\ket+, and a target in ∣0⟩\ket0; one use of UfU_f; then all four qubits are measured in the 0/1 basis. What is the probability that this run tells you f(101)f(101)?

P=  ?P = \;?

Hints
  1. What decides which value of ff the run shows?
  2. The data register gives each of its eight strings with the same probability.
Show the answer

Answer: P=18P = \frac{1}{8}

The data register gives each of the eight inputs with probability ∣1/8∣2=18\abs{1/\sqrt8}^2 = \tfrac18, and the target then shows f(x)f(x) for that input. So f(101)f(101) appears with probability 18\tfrac18.

Common slips

  • 11: That assumes the run reveals every value. The measurement returns one branch ∣x⟩∣f(x)⟩\ket{x}\ket{f(x)}, and x=101x = 101 is one of eight equally likely inputs.
  • 116\frac{1}{16}: That treats all four measured bits as random. Only the three data bits are: once xx is found, the target bit is f(x)f(x) for certain. So the chance is that of finding x=101x = 101, which is 18\tfrac18.
  • 12\frac{1}{2}: Either you learn f(101)f(101) or you don’t, but the two cases are not equally likely. You learn it only when the data register gives 101101, one of eight equally likely results.

4 / 15 · Learn

Part A · Phase kickback

Change one thing: put the target in ∣−⟩=(∣0⟩−∣1⟩)/2\ket- = (\ket0 - \ket1)/\sqrt2. If f(x)=0f(x) = 0, UfU_f leaves ∣x⟩∣−⟩\ket x\ket- alone. If f(x)=1f(x) = 1, it flips the target, and flipping swaps the two terms of ∣−⟩\ket-: (∣1⟩−∣0⟩)/2=−∣−⟩(\ket1 - \ket0)/\sqrt2 = -\ket-. Both cases in one line:

Uf∣x⟩∣−⟩=(−1)f(x)∣x⟩∣−⟩U_f\ket x\ket- = (-1)^{f(x)}\ket x\ket-

The target comes out unchanged, and the value f(x)f(x) has become a sign on the data branch. On a basis state that sign is a global phase. On a superposition each branch gets its own sign, a relative phase. This is the phase kickback you met with CNOT in Lessons 6 and 7: CNOT is UfU_f for f(x)=xf(x) = x.

Check yourself

Let f(x)=xˉf(x) = \bar x, so f(0)=1f(0) = 1 and f(1)=0f(1) = 0. What is Uf∣+⟩∣−⟩U_f\ket+\ket-?

  1. A−∣−⟩∣−⟩-\ket-\ket-
  2. B∣+⟩∣−⟩\ket+\ket-: the data qubit acts like a control, and controls don’t change
  3. C∣+⟩∣+⟩\ket+\ket+: the target flips
  4. D−∣+⟩∣−⟩-\ket+\ket-
Hints
  1. Write ∣+⟩\ket+ as two branches and apply UfU_f to each.
  2. A branch gains −1-1 exactly when f(x)=1f(x) = 1.
Show the answer

Answer: A. −∣−⟩∣−⟩-\ket-\ket-

Branch by branch: f(0)=1f(0) = 1 gives −∣0⟩∣−⟩-\ket0\ket- and f(1)=0f(1) = 0 gives +∣1⟩∣−⟩+\ket1\ket-. Together 12(−∣0⟩+∣1⟩)∣−⟩=−∣−⟩∣−⟩\tfrac{1}{\sqrt2}(-\ket0 + \ket1)\ket- = -\ket-\ket-. The overall minus is a global phase; the relative sign between the branches is what turned ∣+⟩\ket+ into ∣−⟩\ket-.

Why not the others

  • B. “The control never changes” holds only for basis inputs. Split ∣+⟩\ket+ into its branches: the ∣0⟩\ket0 branch has f(0)=1f(0) = 1 and picks up −1-1, the ∣1⟩\ket1 branch doesn’t. A minus sign on just one branch turns ∣+⟩\ket+ into ∣−⟩\ket- (up to an overall sign).
  • C. Flipping ∣−⟩\ket- doesn’t give ∣+⟩\ket+: XX swaps the two terms of (∣0⟩−∣1⟩)/2(\ket0 - \ket1)/\sqrt2 and so gives −∣−⟩-\ket-. The target keeps its state; the sign lands on the data branch.
  • D. That would need both branches to pick up −1-1, which happens for f(x)=1f(x) = 1. Here only the x=0x = 0 branch has f(x)=1f(x) = 1, so the two branches end with opposite signs.

5 / 15 · Learn

Part A · Deutsch’s algorithm

Deutsch’s problem: is a one-bit ff constant or balanced? A classical computer has to evaluate ff twice. Deutsch’s circuit uses UfU_f once. Follow the state:

  • ∣ψ0⟩=∣0⟩∣1⟩\ket{\psi_0} = \ket0\ket1, the input;
  • HH on both qubits gives ∣ψ1⟩=∣+⟩∣−⟩\ket{\psi_1} = \ket+\ket-;
  • UfU_f then acts with kickback on each branch:
∣ψ2⟩=12((−1)f(0)∣0⟩+(−1)f(1)∣1⟩)∣−⟩\ket{\psi_2} = \tfrac{1}{\sqrt2}\big((-1)^{f(0)}\ket0 + (-1)^{f(1)}\ket1\big)\ket-

Think of each input as carrying a chip, +1+1 or −1-1. If ff is constant the two chips match, and the data qubit is ±∣+⟩\pm\ket+. If ff is balanced they differ, and it is ±∣−⟩\pm\ket-.

Check yourself

For which functions is the data qubit in ∣−⟩\ket- (up to an overall sign) right after UfU_f?

Select all that apply.

  1. Af(x)=xˉf(x) = \bar x
  2. Bf(x)=0f(x) = 0
  3. Cf(x)=1f(x) = 1
  4. Df(x)=xf(x) = x
Hints
  1. Find the two chips (−1)f(0)(-1)^{f(0)} and (−1)f(1)(-1)^{f(1)} for each function.
  2. Matching chips leave ±∣+⟩\pm\ket+; different chips give ±∣−⟩\pm\ket-.
Show the answer

Answers: A. f(x)=xˉf(x) = \bar x; D. f(x)=xf(x) = x

The data qubit is ±∣−⟩\pm\ket- exactly when the chips differ, that is when f(0)≠f(1)f(0) \ne f(1): the two balanced functions f(x)=xf(x) = x (giving ∣−⟩\ket-) and f(x)=xˉf(x) = \bar x (giving −∣−⟩-\ket-).

Why not the others

  • B. With f(x)=0f(x) = 0 both chips are +1+1, so the data qubit stays ∣+⟩\ket+.
  • C. With f(x)=1f(x) = 1 both chips are −1-1: the data qubit is −∣+⟩-\ket+. An overall sign is a global phase, so this is still ∣+⟩\ket+, not ∣−⟩\ket-.

6 / 15 · Learn

Part A · The last Hadamard

The last HH acts on the data qubit only. It turns ∣+⟩\ket+ into ∣0⟩\ket0 and ∣−⟩\ket- into ∣1⟩\ket1:

∣ψ3⟩=±∣f(0)⊕f(1)⟩∣−⟩\ket{\psi_3} = \pm\ket{f(0)\oplus f(1)}\ket-

In chip terms, the data qubit’s ∣0⟩\ket0 amplitude is the average of the two chips, and its ∣1⟩\ket1 amplitude is half their difference. Matching chips add on ∣0⟩\ket0 and cancel on ∣1⟩\ket1; opposite chips do the reverse. So a 0/1-basis measurement of the top qubit gives f(0)⊕f(1)f(0)\oplus f(1) with certainty: 0 for constant, 1 for balanced. One query settles a question that no single classical evaluation can, and yet you learn neither f(0)f(0) nor f(1)f(1).

Check yourself

Deutsch’s algorithm runs with f(x)=xˉf(x) = \bar x. What does the 0/1-basis measurement of the top qubit give?

  1. A1, with certainty
  2. B0, with certainty
  3. Cf(0)f(0) first; a second measurement then gives f(1)f(1)
  4. D0 or 1, each with probability 12\tfrac12: Hadamards randomize
Show the answer

Answer: A. 1, with certainty

The chips are (−1)1=−1(-1)^1 = -1 and (−1)0=+1(-1)^0 = +1. They differ, so after UfU_f the data qubit is −∣−⟩-\ket-, and the last HH makes it −∣1⟩-\ket1. The measurement gives 1=f(0)⊕f(1)1 = f(0)\oplus f(1): balanced.

Why not the others

  • B. 0 is the constant answer. Here f(0)=1f(0) = 1 and f(1)=0f(1) = 0, so the chips are −1-1 and +1+1: they cancel on ∣0⟩\ket0 and add on ∣1⟩\ket1.
  • C. The circuit never makes f(0)f(0) or f(1)f(1) readable on its own. Kickback turns them into two signs, and the last HH combines the signs into the single bit f(0)⊕f(1)f(0)\oplus f(1). Measuring again just repeats that bit.
  • D. A Hadamard can also undo randomness: H∣−⟩=∣1⟩H\ket- = \ket1 exactly. After the query the data qubit is −∣−⟩-\ket-, a definite state, and the last HH turns it into −∣1⟩-\ket1. Nothing is left to chance.

Check yourself

After Deutsch’s circuit someone measures the bottom (target) qubit in the 0/1 basis. What do they get?

  1. Af(0)⊕f(1)f(0)\oplus f(1), because UfU_f writes into the target
  2. B0 or 1 with probability 12\tfrac12 each, whatever ff is
  3. Cf(0)f(0), the value at the first input
  4. D1, because the target started in ∣1⟩\ket1
Show the answer

Answer: B. 0 or 1 with probability 12\tfrac12 each, whatever ff is

The target is ∣−⟩\ket- at the end for every ff, so its 0/1 measurement gives 0 or 1 with probability 12\tfrac12 each. The answer is in the top qubit.

Why not the others

  • A. With the target in ∣−⟩\ket-, UfU_f only multiplies the target by ±1\pm1, so it stays ∣−⟩\ket- for every ff. A 0/1 measurement of ∣−⟩\ket- is a fair coin, with no information about ff.
  • C. No value of ff is stored in the target. It is ∣−⟩\ket- for every ff; the effect of ff is a sign on the data branches.
  • D. The first HH turned the target’s ∣1⟩\ket1 into ∣−⟩\ket-, and it stays ∣−⟩\ket- to the end. Measured in the 0/1 basis, ∣−⟩\ket- gives 0 or 1 with probability 12\tfrac12 each.

7 / 15 · Worked example

Part A · Worked example: all four functions

Enter states as four amplitudes, ∣00⟩\ket{00} to ∣11⟩\ket{11}, top qubit first. These steps want the exact vector, signs included, even where an overall sign would be a harmless global phase.

Worked example

The two balanced functions

Every run starts from the same ∣ψ1⟩=∣+⟩∣−⟩=12(∣00⟩−∣01⟩+∣10⟩−∣11⟩)\ket{\psi_1} = \ket+\ket- = \tfrac12(\ket{00} - \ket{01} + \ket{10} - \ket{11}). First f(x)=xf(x) = x, worked for you; then f(x)=xˉf(x) = \bar x, which you do.

  1. Query

    Take f(x)=xf(x) = x. f(0)=0f(0) = 0, so the x=0x = 0 terms are untouched. f(1)=1f(1) = 1, so in the x=1x = 1 terms the target flips: ∣10⟩\ket{10} and ∣11⟩\ket{11} swap amplitudes, 12∣10⟩−12∣11⟩→−12∣10⟩+12∣11⟩\tfrac12\ket{10} - \tfrac12\ket{11}\to -\tfrac12\ket{10} + \tfrac12\ket{11}. So

    ∣ψ2⟩=12(∣00⟩−∣01⟩−∣10⟩+∣11⟩)=∣−⟩∣−⟩\begin{aligned}\ket{\psi_2} &= \tfrac12\big(\ket{00} - \ket{01} \\ &\qquad - \ket{10} + \ket{11}\big) \\ &= \ket-\ket-\end{aligned}

    Chips: +1+1 for x=0x = 0, −1-1 for x=1x = 1.

  2. Last H

    H∣−⟩=∣1⟩H\ket- = \ket1, so ∣ψ3⟩=∣1⟩∣−⟩=12(∣10⟩−∣11⟩)\ket{\psi_3} = \ket1\ket- = \tfrac{1}{\sqrt2}(\ket{10} - \ket{11}). With chips: the top ∣0⟩\ket0 amplitude is 12(1+(−1))=0\tfrac12(1 + (-1)) = 0 and the top ∣1⟩\ket1 amplitude is 12(1−(−1))=1\tfrac12(1 - (-1)) = 1. The measurement gives 1=f(0)⊕f(1)=0⊕11 = f(0)\oplus f(1) = 0\oplus1.

  3. Your turn: query

    Now f(x)=xˉf(x) = \bar x: f(0)=1f(0) = 1 and f(1)=0f(1) = 0. Start again from ∣ψ1⟩\ket{\psi_1}.

    Your turn

    Run Deutsch’s algorithm with f(x)=xˉf(x) = \bar x. Enter ∣ψ2⟩\ket{\psi_2}, the state right after UfU_f, as four amplitudes. Give the exact vector, signs included.

    Hints
    1. Which branches have f(x)=1f(x) = 1?
    2. In those branches the target flips, and X∣−⟩=−∣−⟩X\ket- = -\ket-: the branch changes sign, the target doesn’t.
    3. Here (−1)f(0)=−1(-1)^{f(0)} = -1 and (−1)f(1)=+1(-1)^{f(1)} = +1. Multiply each branch of ∣+⟩∣−⟩\ket+\ket- by its chip.
    Show the answer

    Answer: −12∣00⟩+12∣01⟩+12∣10⟩−12∣11⟩-\frac{1}{2}|00\rangle + \frac{1}{2}|01\rangle + \frac{1}{2}|10\rangle - \frac{1}{2}|11\rangle

    Chips: (−1)f(0)=−1(-1)^{f(0)} = -1 and (−1)f(1)=+1(-1)^{f(1)} = +1. So ∣ψ2⟩=−∣−⟩∣−⟩=−12∣00⟩+12∣01⟩+12∣10⟩−12∣11⟩\ket{\psi_2} = -\ket-\ket- = -\frac{1}{2}|00\rangle + \frac{1}{2}|01\rangle + \frac{1}{2}|10\rangle - \frac{1}{2}|11\rangle.

  4. Your turn: last H

    Your turn

    Run Deutsch’s algorithm with f(x)=xˉf(x) = \bar x. Enter ∣ψ3⟩\ket{\psi_3}, the state after the final HH, as four amplitudes. Give the exact vector, signs included.

    Hints
    1. Find ∣ψ2⟩\ket{\psi_2} first: which sign does each branch carry?
    2. Then apply HH to the top qubit only: H∣+⟩=∣0⟩H\ket+ = \ket0, H∣−⟩=∣1⟩H\ket- = \ket1.
    3. Here ∣ψ2⟩=−∣−⟩∣−⟩\ket{\psi_2} = -\ket-\ket-.
    Show the answer

    Answer: −12∣10⟩+12∣11⟩-\frac{1}{\sqrt{2}}|10\rangle + \frac{1}{\sqrt{2}}|11\rangle

    Chips: (−1)f(0)=−1(-1)^{f(0)} = -1 and (−1)f(1)=+1(-1)^{f(1)} = +1, so ∣ψ2⟩=−∣−⟩∣−⟩\ket{\psi_2} = -\ket-\ket-. HH on the top qubit gives ∣ψ3⟩=−∣1⟩∣−⟩=−12∣10⟩+12∣11⟩\ket{\psi_3} = -\ket1\ket- = -\frac{1}{\sqrt{2}}|10\rangle + \frac{1}{\sqrt{2}}|11\rangle. The top qubit reads 1=f(0)⊕f(1)1 = f(0)\oplus f(1): balanced.

One more, fully worked, with a constant function.

Another worked example

Another example: a constant function

  1. Query

    Take f(x)=0f(x) = 0. No branch has f(x)=1f(x) = 1, so UfU_f does nothing: ∣ψ2⟩=∣ψ1⟩=∣+⟩∣−⟩=12(∣00⟩−∣01⟩+∣10⟩−∣11⟩)\ket{\psi_2} = \ket{\psi_1} = \ket+\ket- = \tfrac12(\ket{00} - \ket{01} + \ket{10} - \ket{11}). Both chips are +1+1.

  2. Last H

    H∣+⟩=∣0⟩H\ket+ = \ket0, so ∣ψ3⟩=∣0⟩∣−⟩=12(∣00⟩−∣01⟩)\ket{\psi_3} = \ket0\ket- = \tfrac{1}{\sqrt2}(\ket{00} - \ket{01}). With chips: 12(1+1)=1\tfrac12(1 + 1) = 1 on the top ∣0⟩\ket0 and 12(1−1)=0\tfrac12(1 - 1) = 0 on the top ∣1⟩\ket1.

  3. Read out

    The measurement gives 0: constant. For f(x)=1f(x) = 1 both chips are −1-1, which gives −∣0⟩∣−⟩-\ket0\ket-: the same physical state with a global sign, and the same reading.

Check yourself

Run Deutsch’s algorithm with f(x)=xf(x) = x. Enter ∣ψ3⟩\ket{\psi_3}, the state after the final HH, as four amplitudes. Give the exact vector, signs included.

Hints
  1. Find ∣ψ2⟩\ket{\psi_2} first: which sign does each branch carry?
  2. Then apply HH to the top qubit only: H∣+⟩=∣0⟩H\ket+ = \ket0, H∣−⟩=∣1⟩H\ket- = \ket1.
  3. Here ∣ψ2⟩=∣−⟩∣−⟩\ket{\psi_2} = \ket-\ket-.
Show the answer

Answer: 12∣10⟩−12∣11⟩\frac{1}{\sqrt{2}}|10\rangle - \frac{1}{\sqrt{2}}|11\rangle

Chips: (−1)f(0)=+1(-1)^{f(0)} = +1 and (−1)f(1)=−1(-1)^{f(1)} = -1, so ∣ψ2⟩=∣−⟩∣−⟩\ket{\psi_2} = \ket-\ket-. HH on the top qubit gives ∣ψ3⟩=∣1⟩∣−⟩=12∣10⟩−12∣11⟩\ket{\psi_3} = \ket1\ket- = \frac{1}{\sqrt{2}}|10\rangle - \frac{1}{\sqrt{2}}|11\rangle. The top qubit reads 1=f(0)⊕f(1)1 = f(0)\oplus f(1): balanced.

8 / 15 · Learn

Part B · Hadamards on n qubits

Part B: nn input bits, the Deutsch–Jozsa algorithm, and how many queries each method needs.

H⊗nH^{\otimes n} means a Hadamard on each of nn qubits. On ∣0…0⟩\ket{0\ldots0} every qubit becomes ∣+⟩\ket+, and multiplying out the product gives each nn⁠-bit string exactly once:

H⊗n∣0⟩⊗n=12n∑x∈{0,1}n∣x⟩H^{\otimes n}\ket{0}^{\otimes n} = \frac{1}{\sqrt{2^n}}\sum_{x\in\{0,1\}^n}\ket x

For n=2n = 2 that is 12(∣00⟩+∣01⟩+∣10⟩+∣11⟩)\tfrac12(\ket{00} + \ket{01} + \ket{10} + \ket{11}). So nn gates prepare an equal superposition of 2n2^n strings, each with amplitude 1/2n1/\sqrt{2^n}, so that the 2n2^n probabilities add up to 1. H⊗nH^{\otimes n} is also called the Walsh–Hadamard transform.

Check yourself

What is the amplitude of ∣101⟩\ket{101} in H⊗3∣000⟩H^{\otimes3}\ket{000}?

Hints
  1. How many strings appear, and do they all have the same amplitude?
  2. Each qubit contributes a factor 1/21/\sqrt2.
Show the answer

Answer: 24\frac{\sqrt{2}}{4}

H⊗3∣000⟩=∣+⟩∣+⟩∣+⟩H^{\otimes3}\ket{000} = \ket+\ket+\ket+ has 8 terms, each with amplitude (1/2)3=1/(22)=2/4(1/\sqrt2)^3 = 1/(2\sqrt2) = \sqrt2/4. That includes ∣101⟩\ket{101}.

Common slips

  • 18\frac{1}{8}: 18\tfrac18 is the probability of each string. The amplitude is its square root, 1/8=2/41/\sqrt8 = \sqrt2/4, so that eight probabilities of 18\tfrac18 add up to 1.
  • 13\frac{1}{3}: Three qubits don’t give three terms: the product of three factors (∣0⟩+∣1⟩)/2(\ket0 + \ket1)/\sqrt2 has 23=82^3 = 8 terms, each with amplitude (1/2)3(1/\sqrt2)^3.
  • 13\frac{1}{\sqrt{3}}: That shares the probability among three strings. There are 23=82^3 = 8 strings, so each amplitude is 1/81/\sqrt8.
  • 12\frac{1}{2}: 12\tfrac12 is the amplitude for two qubits. Each extra qubit multiplies by another 1/21/\sqrt2: (1/2)3=1/8(1/\sqrt2)^3 = 1/\sqrt8.

Other inputs give signs. For one qubit, H∣0⟩=(∣0⟩+∣1⟩)/2{H\ket0 = (\ket0 + \ket1)/\sqrt2} and H∣1⟩=(∣0⟩−∣1⟩)/2{H\ket1 = (\ket0 - \ket1)/\sqrt2}: the term ∣z⟩\ket z gets a minus sign exactly when the input and zz are both 1. Multiply the nn factors and the signs multiply:

H⊗n∣x⟩=12n∑z(−1)x⋅z∣z⟩H^{\otimes n}\ket x = \frac{1}{\sqrt{2^n}}\sum_z(-1)^{x\cdot z}\ket z

Here x⋅z=x1z1⊕⋯⊕xnznx\cdot z = x_1z_1\oplus\cdots\oplus x_nz_n is the dot product mod 2. So ∣z⟩\ket z is negative when xx and zz share an odd number of 1s. Check with n=2{n = 2}: H⊗2∣01⟩=∣+⟩∣−⟩=12(∣00⟩−∣01⟩+∣10⟩−∣11⟩)H^{\otimes2}\ket{01} = \ket+\ket- = \tfrac12(\ket{00} - \ket{01} + \ket{10} - \ket{11}), negative where zz ends in 1.

Refresher: Bits, XOR and bit strings The dot product x⋅zx\cdot z mod 2, sums over bit strings and signs (−1)f(x)(-1)^{f(x)}, with practice.

Check yourself

Enter H⊗2∣10⟩H^{\otimes2}\ket{10} as four amplitudes.

Hints
  1. Which bit of x=10x = 10 is 1?
  2. With x=10x = 10, x⋅z=z1x\cdot z = z_1: minus exactly where the first bit of zz is 1.
Show the answer

Answer: 12∣00⟩+12∣01⟩−12∣10⟩−12∣11⟩\frac{1}{2}|00\rangle + \frac{1}{2}|01\rangle - \frac{1}{2}|10\rangle - \frac{1}{2}|11\rangle

x=10x = 10, so x⋅z=z1x\cdot z = z_1, and the sign is minus exactly where zz starts with 1: 12(∣00⟩+∣01⟩−∣10⟩−∣11⟩)\tfrac12(\ket{00} + \ket{01} - \ket{10} - \ket{11}). That is ∣−⟩∣+⟩\ket-\ket+, as H∣1⟩⊗H∣0⟩H\ket1\otimes H\ket0 should be.

9 / 15 · Learn

Part B · The Deutsch–Jozsa algorithm

Now ff takes nn bits to one bit, and you are promised that it is constant (the same value on all 2n2^n inputs) or balanced (0 on exactly half of them, 1 on the other half). Which is it? The circuit is Deutsch’s with a wider data register: nn qubits in ∣0⟩\ket0 and a target in ∣1⟩\ket1; HH on every qubit; one UfU_f; then H⊗nH^{\otimes n} on the data and a 0/1-basis measurement of the data. After the query, kickback has put a chip on every input:

∣ψ2⟩=12n∑x(−1)f(x)∣x⟩ ∣−⟩\ket{\psi_2} = \frac{1}{\sqrt{2^n}}\sum_x(-1)^{f(x)}\ket x\,\ket-

Check yourself

Let n=2n = 2 and f(00)=0f(00) = 0, f(01)=1f(01) = 1, f(10)=1f(10) = 1, f(11)=0f(11) = 0, a balanced function. Enter the data register’s part of ∣ψ2⟩\ket{\psi_2}, that is 12∑x(−1)f(x)∣x⟩\tfrac12\sum_x(-1)^{f(x)}\ket x, as four amplitudes.

Hints
  1. What is the chip (−1)f(x)(-1)^{f(x)} for each of the four inputs?
  2. Each amplitude is 12\tfrac12 times its chip.
Show the answer

Answer: 12∣00⟩−12∣01⟩−12∣10⟩+12∣11⟩\frac{1}{2}|00\rangle - \frac{1}{2}|01\rangle - \frac{1}{2}|10\rangle + \frac{1}{2}|11\rangle

Chips (−1)f(x)(-1)^{f(x)}: +1,−1,−1,+1+1, -1, -1, +1. So the data register is 12(∣00⟩−∣01⟩−∣10⟩+∣11⟩)\tfrac12(\ket{00} - \ket{01} - \ket{10} + \ket{11}), which happens to be ∣−⟩∣−⟩\ket-\ket-.

The last H⊗nH^{\otimes n} sends each ∣x⟩\ket x to 12n∑z(−1)x⋅z∣z⟩\tfrac{1}{\sqrt{2^n}}\sum_z(-1)^{x\cdot z}\ket z. Collect the terms for one output zz; its amplitude is

12n∑x(−1)x⋅z+f(x).\frac{1}{2^n}\sum_x(-1)^{x\cdot z + f(x)}.

For z=0…0z = 0\ldots0 every x⋅zx\cdot z is 0, so the amplitude of ∣0…0⟩\ket{0\ldots0} is simply the average of the chips. If ff is constant, all chips are equal, the average is ±1\pm1, and the measurement gives all zeros with certainty. If ff is balanced, half the chips are +1+1 and half −1-1, the average is 0, and all zeros never appears. The rule: all zeros means constant; anything else means balanced.

Check yourself

Deutsch–Jozsa with n=3n = 3 and some balanced ff. What is the probability that the 0/1-basis measurement of the data register gives 000000?

P(000)=  ?P(000) = \;?

Hints
  1. What is the ∣000⟩\ket{000} amplitude in terms of the chips?
  2. It is the average of the eight chips (−1)f(x)(-1)^{f(x)}. How many are +1+1 for a balanced ff?
Show the answer

Answer: P(000)=0P(000) = 0

The ∣000⟩\ket{000} amplitude is 18∑x(−1)f(x)=18(4−4)=0\tfrac18\sum_x(-1)^{f(x)} = \tfrac18(4 - 4) = 0 for every balanced ff, so P(000)=0P(000) = 0. That is why the read-out rule never fails.

Common slips

  • 18\frac{1}{8}: That treats the last Hadamards as spreading the probability evenly over the eight strings. They add the eight chips into the ∣000⟩\ket{000} amplitude, and four +1+1⁠s cancel four −1-1⁠s exactly.
  • 12\frac{1}{2}: Half the inputs have f(x)=0f(x) = 0, but probabilities don’t follow that count. The ∣000⟩\ket{000} amplitude is the average of the chips, 18(4−4)=0\tfrac18(4 - 4) = 0.
  • 164\frac{1}{64}: Square the average of the chips, not one chip over 8. For a balanced ff the average is 18(4−4)=0\tfrac18(4 - 4) = 0.
  • 11: All zeros with certainty is what a constant ff gives, when all eight chips are equal. For a balanced ff, four +1+1 chips and four −1-1 chips cancel on ∣000⟩\ket{000}: its amplitude is 18(4−4)=0\tfrac18(4 - 4) = 0.

10 / 15 · Learn

Part B · Where a balanced f sends the amplitude

For a balanced ff, the probability that leaves ∣0…0⟩\ket{0\ldots0} has to land on other strings, and which ones depends on ff. A tidy family is f(x)=s⋅xf(x) = s\cdot x for a fixed string s≠0…0s \ne 0\ldots0 (such an ff is balanced). Its chips (−1)s⋅x(-1)^{s\cdot x} are exactly the signs H⊗nH^{\otimes n} gives when it acts on ∣s⟩\ket s. Since H⊗nH^{\otimes n} undoes itself, the last Hadamards turn the data register back into ∣s⟩\ket s, with certainty. For n=2n = 2 and f(x)=x2f(x) = x_2, the last bit of xx, that string is s=01s = 01.

Check yourself

Deutsch–Jozsa with n=2n = 2 and f(x)=x1⊕x2f(x) = x_1\oplus x_2. What does the 0/1-basis measurement of the two data qubits give?

  1. A0000, with certainty
  2. B1111, with certainty
  3. CEach of the four strings with probability 14\tfrac14
  4. D0101 or 1010, the inputs where f(x)=1f(x) = 1
Hint
  1. x1⊕x2=x⋅sx_1\oplus x_2 = x\cdot s for which string ss?
Show the answer

Answer: B. 1111, with certainty

x1⊕x2=11⋅xx_1\oplus x_2 = 11\cdot x, so s=11s = 11 and the output is ∣11⟩\ket{11} with certainty. Check: for z=11z = 11, x⋅z=f(x)x\cdot z = f(x) for every xx, so all four signs (−1)x⋅z+f(x)(-1)^{x\cdot z + f(x)} are +1+1 and the amplitude is 1.

Why not the others

  • A. All zeros means constant, and x1⊕x2x_1\oplus x_2 is balanced: f(01)=f(10)=1f(01) = f(10) = 1, f(00)=f(11)=0f(00) = f(11) = 0. Its chips +,−,−,++, -, -, + cancel on ∣00⟩\ket{00}.
  • C. The last Hadamards don’t spread the amplitude out evenly. For each zz they add four signed terms; here the terms add up fully for one string and cancel for the other three.
  • D. The output is not a list of inputs. It is the string zz where the signs (−1)x⋅z+f(x)(-1)^{x\cdot z + f(x)} all agree. For z=01z = 01 they are +,+,−,−+, +, -, -, which cancel.
A preview beyond this course

Finding the hidden string ss in f(x)=s⋅xf(x) = s\cdot x with one query is called the Bernstein–Vazirani problem. A classical computer needs nn queries for it, one per bit of ss. This course doesn’t cover it, but it uses exactly the circuit you just ran.

11 / 15 · Learn

Part B · Counting queries

A classical method that must be certain can be unlucky. A balanced ff gives the same value on half its inputs, so the first 2n−12^{n-1} answers can all agree. One more query settles it: 2n−1+12^{n-1} + 1 queries in the worst case. Deutsch–Jozsa uses UfU_f once, whatever nn is. For n=1n = 1, Deutsch’s problem, that is 2 against 1; for n=10n = 10 it is 513 against 1.

Check yourself

For n=6n = 6, how many queries does a deterministic classical method need in the worst case to be certain?

Hints
  1. How many inputs can share the same value when ff is balanced?
  2. There are 26=642^6 = 64 inputs, half of them with each value.
Show the answer

Answer: 3333

A balanced ff has 25=322^5 = 32 inputs of each value, so 32 answers can agree; the 33rd settles it: 2n−1+1=332^{n-1} + 1 = 33.

Common slips

  • 6464: You never need all 26=642^6 = 64 inputs. Half of them, 32, can share a value under a balanced ff, so 33 agreeing answers already prove that ff is constant.
  • 3232: Thirty-two agreeing answers are still possible for a balanced ff: it has 32 inputs of each value. The 33rd query is the one that decides.
  • 66: The count depends on the number of inputs, 26=642^6 = 64, not on the number of bits. Up to 32 answers can agree under a balanced ff.

What changes if a small chance of error is allowed? Query kk inputs chosen at random and answer “constant” if all kk values agree, “balanced” otherwise. A constant ff is never misjudged. A balanced ff fools you only if all kk values agree. For independent random inputs each value is 0 or 1 with probability 12\tfrac12, so all kk agree with probability

2⋅(12)k=21−k2\cdot\big(\tfrac12\big)^k = 2^{1-k}

(picking distinct inputs only lowers it). This does not depend on nn: eleven queries keep the error below 1 in 1000 for any nn. Deutsch–Jozsa’s exponential advantage holds only against classical methods that must never err.

Check yourself

You use the random method with k=4k = 4 queries on a balanced ff. What is the probability that it wrongly answers “constant”?

P(error)=  ?P(\text{error}) = \;?

Hints
  1. Which outcomes of the four queries lead to the answer “constant”?
  2. All 0s, or all 1s. Each has probability (12)4(\tfrac12)^4.
Show the answer

Answer: P(error)=18P(\text{error}) = \frac{1}{8}

The method says “constant” when all four values agree: all 0s or all 1s, each with probability (12)4=116(\tfrac12)^4 = \tfrac{1}{16}. So the error is 21−4=182^{1-4} = \tfrac18.

Common slips

  • 116\frac{1}{16}: That is the chance that all four values are 0. They could also all be 1, which fools you just as well, so double it: 2⋅116=182\cdot\tfrac{1}{16} = \tfrac18.
  • 78\frac{7}{8}: That is the chance of the right answer, balanced. The error happens when all four values agree: 2⋅(12)4=182\cdot(\tfrac12)^4 = \tfrac18.
  • 12\frac{1}{2}: One query gives 0 or 1 with probability 12\tfrac12, but the error needs all four to agree, and each extra query halves that chance: 2⋅(12)4=182\cdot(\tfrac12)^4 = \tfrac18.

Check yourself

A headline reads: “Deutsch–Jozsa shows that a quantum computer beats every classical computer exponentially.” Take n=50n = 50. Which verdict is accurate?

  1. AExaggerated: the gain is exponential only against classical methods that must never err
  2. BFalse: Deutsch–Jozsa needs many runs too, so it has no advantage
  3. CFalse: a careful classical method needs only 50 queries, one per bit
  4. DAccurate: every classical method needs 249+12^{49} + 1 queries
Hint
  1. How many queries does the random method need to push its error below 10−610^{-6}, and does that number depend on nn?
Show the answer

Answer: A. Exaggerated: the gain is exponential only against classical methods that must never err

A classical method that must be certain needs 249+12^{49} + 1 queries in the worst case, and against it one query is an exponential gain. The random method errs with probability 21−k2^{1-k}, independent of nn: 21 queries make that less than 10−610^{-6}. So “exponentially faster” holds only against error-free classical methods.

Why not the others

  • B. One run settles it with certainty: ∣0…0⟩\ket{0\dots0} has probability 1 for a constant ff and 0 for a balanced one. The advantage is real; it is exponential only against methods that must never err.
  • C. A query reveals one value f(x)f(x), not one bit of anything, and a balanced ff can agree on 2492^{49} inputs. So a method that must be certain can need 249+12^{49} + 1 queries. The headline fails for another reason: it forgets methods allowed a tiny error.
  • D. That count is for a method that must be certain. The random method errs with probability 21−k2^{1-k} whatever nn is, so 21 queries bring the error below one in a million even for n=50n = 50. Against that, one quantum query is a modest gain. The headline is tempting because the certain count really does grow exponentially.

12 / 15 · Practice

Practice

Three items, each with new numbers when it comes back. Hints are there if you need them.

Check yourself

What is the amplitude of ∣0001⟩\ket{0001} in H⊗4∣0110⟩H^{\otimes 4}\ket{0110}?

Hints
  1. What sign does (−1)x⋅z(-1)^{x\cdot z} give for x=0110x = 0110 and z=0001z = 0001?
  2. Every amplitude of H⊗n∣x⟩H^{\otimes n}\ket x has size 1/2n1/\sqrt{2^n}; its sign is minus when xx and zz share an odd number of 1s.
  3. They have no 1 in the same place, so x⋅z=0x\cdot z = 0. The size is 1/161/\sqrt{16}.
Show the answer

Answer: 14\frac{1}{4}

xx and zz have no 1 in the same place, so x⋅z=0x\cdot z = 0 and the sign is ++. The size is 1/24=141/\sqrt{2^{4}} = \frac{1}{4}, so the amplitude is 14\frac{1}{4}.

Line up x=0110x = 0110 and z=0001z = 0001 and multiply bit by bit: 0⋅0=00\cdot0 = 0, 1⋅0=01\cdot0 = 0, 1⋅0=01\cdot0 = 0, 0⋅1=00\cdot1 = 0. The products add to 0, so x⋅z=0 mod 2=0x\cdot z = 0 \bmod 2 = 0 and the sign (−1)x⋅z(-1)^{x\cdot z} is +1+1. Every amplitude of H⊗4∣0110⟩H^{\otimes 4}\ket{0110} has size 1/24=141/\sqrt{2^{4}} = \frac{1}{4}, so the amplitude of ∣0001⟩\ket{0001} is 14\frac{1}{4}.

Common slips

  • −14-\frac{1}{4}: Right size, wrong sign. The sign is (−1)x⋅z(-1)^{x\cdot z}: x=0110x = 0110 and z=0001z = 0001 have no 1 in the same place, so x⋅z=0x\cdot z = 0 and the sign is plus.
  • 116\frac{1}{16}: 1/161/16 is the size of a probability here. Each amplitude has size 1/241/\sqrt{2^{4}}, so that the 16 squared magnitudes add up to 1.

Check yourself

Deutsch–Jozsa with n=3n = 3. The balanced function ff is 1 on the inputs 010010, 100100, 110110, 111111 and 0 on the other four. What is the amplitude of ∣111⟩\ket{111} in the data register just before the measurement?

Hints
  1. Which two signs does each input xx contribute to the amplitude of ∣111⟩\ket{111}?
  2. The amplitude is the average of the eight signs (−1)x⋅z+f(x)(-1)^{x\cdot z + f(x)}: one from the last Hadamards, one from the chip.
  3. List x⋅111x\cdot 111 for x=000,…,111x = 000, \ldots, 111: 0, 1, 1, 0, 1, 0, 0, 1. A term is −1-1 where this differs from f(x)f(x).
Show the answer

Answer: 12\frac{1}{2}

The eight signs (−1)x⋅z+f(x)(-1)^{x\cdot z + f(x)} are +, -, +, +, +, +, -, +; they add up to 44, so the amplitude is 4/8=124/8 = \frac{1}{2}.

Go through x=000,001,…,111x = 000, 001, \ldots, 111. The values x⋅111x\cdot 111 are 0, 1, 1, 0, 1, 0, 0, 1; the values f(x)f(x) are 0, 0, 1, 0, 1, 0, 1, 1. Each term (−1)x⋅z+f(x)(-1)^{x\cdot z + f(x)} is +1+1 where they agree and −1-1 where they differ: +, -, +, +, +, +, -, +. The sum is 44, so the amplitude is 4/8=124/8 = \frac{1}{2}, and ∣111⟩\ket{111} has probability 14\frac{1}{4}.

Common slips

  • 00: The chips alone, (−1)f(x)(-1)^{f(x)}, average to 0 for a balanced ff: that is the ∣000⟩\ket{000} amplitude. For ∣111⟩\ket{111} every term also carries the sign (−1)x⋅z(-1)^{x\cdot z} from the last Hadamards.
  • 14\frac{1}{4}: That is the probability, the squared amplitude. The amplitude itself is 12\frac{1}{2}.
  • −12-\frac{1}{2}: Right size, wrong sign. Recount the signs (−1)x⋅z+f(x)(-1)^{x\cdot z + f(x)}: a term is −1-1 exactly when x⋅zx\cdot z and f(x)f(x) differ.

Check yourself

A balanced ff on 2-bit inputs (four of them). You query two different inputs, chosen at random. What is the probability that the two values agree, so that the random method wrongly says “constant”?

Hints
  1. After the first answer, how many of the remaining inputs give the same value?
  2. Each value belongs to 2 inputs, and one of them has been used. 3 inputs remain.
  3. So the chance is 1 out of 3.
Show the answer

Answer: 13\frac{1}{3}

Whatever the first value, 1 of the 3 remaining inputs shares it, so the probability is 13\tfrac{1}{3}. In general it is 2n−1−12n−1<12\tfrac{2^{n-1}-1}{2^n-1} < \tfrac12: already better than independent queries, which give 12\tfrac12.

A balanced ff on 4 inputs has 2 inputs with each value. The first query gives some value; 1 other input has that value, and the second query picks one of the 3 remaining inputs at random. So the chance that the two agree is 13\tfrac{1}{3}. With independent queries (repeats allowed) it would be 12\tfrac12.

Common slips

  • 12\frac{1}{2}: That is the answer when the second query may repeat the first input: 2⋅(12)2=122\cdot(\tfrac12)^2 = \tfrac12. Here the inputs are different, and one input of the first value is already used up.
  • 14\frac{1}{4}: The numerator is right: 1 unused input shares the first value. But the second query chooses among the 3 inputs that are left, not all 4.

13 / 15 · Explain

Explain it

Explain it

One measurement can’t read both f(0)f(0) and f(1)f(1). How, then, does a single use of UfU_f in Deutsch’s algorithm decide whether ff is constant or balanced?

Show a model answer

With the target in ∣−⟩\ket-, UfU_f leaves the target alone and multiplies each branch ∣x⟩\ket x of the data qubit by (−1)f(x)(-1)^{f(x)}: phase kickback. So after the query the data qubit is ±∣+⟩\pm\ket+ if f(0)=f(1)f(0) = f(1) and ±∣−⟩\pm\ket- if not. The last HH makes the two branches interfere: matching signs add up on ∣0⟩\ket0, opposite signs on ∣1⟩\ket1. A 0/1-basis measurement then gives f(0)⊕f(1)f(0)\oplus f(1) with certainty. You learn only that one global bit, never f(0)f(0) or f(1)f(1) themselves.

A good answer covers

  • Kickback: with the target in ∣−⟩\ket-, UfU_f puts the sign (−1)f(x)(-1)^{f(x)} on each branch of the data qubit
  • The last HH makes the branches interfere: equal signs give ∣0⟩\ket0, opposite signs give ∣1⟩\ket1, with certainty
  • Only the global bit f(0)⊕f(1)f(0)\oplus f(1) is learned, not f(0)f(0) or f(1)f(1)
  • The target stays ∣−⟩\ket-; the answer ends up in the data qubit

14 / 15 · Mastery check

Mastery check

Check yourself

A quantum computer applies UfU_f once to a superposition of all four 2-bit inputs, producing 12∑x∣x⟩∣f(x)⟩\tfrac12\sum_x\ket{x}\ket{f(x)}. Then every qubit is measured in the 0/1 basis. What does this one run tell you about ff?

  1. AThe value f(x)f(x) at one input xx, chosen at random
  2. BNothing: measuring a superposition gives meaningless random bits
  3. COnly f(00)f(00), because the inputs started in ∣00⟩\ket{00}
  4. DAll four values f(x)f(x): the whole table of ff
Show the answer

Answer: A. The value f(x)f(x) at one input xx, chosen at random

Each branch ∣x⟩∣f(x)⟩\ket{x}\ket{f(x)} has amplitude 12\tfrac12. The measurement picks one branch at random, with probability 1/41/4 each, and returns its input and its value: one f(x)f(x) per run, which is no more than one classical query gives.

Why not the others

  • B. The bits are not meaningless. Every branch is ∣x⟩∣f(x)⟩\ket{x}\ket{f(x)}, so in every result the last bit equals ff of the first ones: you learn one genuine value.
  • C. Every input has the same amplitude, 12\tfrac12, so the measured xx is random: 0000 turns up only one time in 4.
  • D. The state involves all four values, but a 0/1 measurement of 3 qubits returns 3 bits: one input xx and its value f(x)f(x). The other branches are gone once you have measured.

Check yourself

A student builds Deutsch’s circuit but prepares the bottom qubit in ∣0⟩\ket0 instead of ∣1⟩\ket1, so it enters UfU_f as ∣+⟩\ket+. Everything else is the same. What does the 0/1-basis measurement of the top qubit give?

  1. A0 or 1 at random, with probability 12\tfrac12 each
  2. B0 for every ff, so the circuit no longer tells constant from balanced
  3. Cf(0)⊕f(1)f(0)\oplus f(1), as before: the target doesn’t matter
  4. DNothing on top; the answer now appears on the bottom qubit
Show the answer

Answer: B. 0 for every ff, so the circuit no longer tells constant from balanced

Uf∣x⟩∣+⟩=∣x⟩ 12(∣f(x)⟩+∣1⊕f(x)⟩)=∣x⟩∣+⟩U_f\ket x\ket+ = \ket x\,\tfrac{1}{\sqrt2}\big(\ket{f(x)} + \ket{1\oplus f(x)}\big) = \ket x\ket+: no sign, for any ff. The state stays ∣+⟩∣+⟩\ket+\ket+, the last HH gives ∣0⟩∣+⟩\ket0\ket+, and the top qubit reads 0 whatever ff is. The ∣−⟩\ket- target is what turns f(x)f(x) into a sign.

Why not the others

  • A. With no signs kicked back, the data qubit is still ∣+⟩\ket+ after UfU_f, and H∣+⟩=∣0⟩H\ket+ = \ket0 exactly. Two Hadamards in a row undo each other; nothing here randomizes the top qubit.
  • C. The target matters. Kickback needs the flip to change the target’s sign, and X∣+⟩=+∣+⟩X\ket+ = +\ket+. With ∣+⟩\ket+ no branch picks up a sign, so the top qubit returns to ∣0⟩\ket0 for every ff.
  • D. The bottom qubit is ∣+⟩\ket+ before and after UfU_f, since Uf∣x⟩∣+⟩=∣x⟩∣+⟩U_f\ket x\ket+ = \ket x\ket+ for every ff. Measured in the 0/1 basis it gives a random bit that says nothing about ff.

Check yourself

What is the amplitude of ∣111⟩\ket{111} in H⊗3∣110⟩H^{\otimes 3}\ket{110}?

Hints
  1. What sign does (−1)x⋅z(-1)^{x\cdot z} give for x=110x = 110 and z=111z = 111?
  2. Every amplitude of H⊗n∣x⟩H^{\otimes n}\ket x has size 1/2n1/\sqrt{2^n}; its sign is minus when xx and zz share an odd number of 1s.
  3. They share a 1 in 2 places, so x⋅z=0x\cdot z = 0. The size is 1/81/\sqrt{8}.
Show the answer

Answer: 24\frac{\sqrt{2}}{4}

xx and zz share a 1 in 2 places, so x⋅z=0x\cdot z = 0 and the sign is ++. The size is 1/23=241/\sqrt{2^{3}} = \frac{\sqrt{2}}{4}, so the amplitude is 24\frac{\sqrt{2}}{4}.

Line up x=110x = 110 and z=111z = 111 and multiply bit by bit: 1⋅1=11\cdot1 = 1, 1⋅1=11\cdot1 = 1, 0⋅1=00\cdot1 = 0. The products add to 2, so x⋅z=2 mod 2=0x\cdot z = 2 \bmod 2 = 0 and the sign (−1)x⋅z(-1)^{x\cdot z} is +1+1. Every amplitude of H⊗3∣110⟩H^{\otimes 3}\ket{110} has size 1/23=241/\sqrt{2^{3}} = \frac{\sqrt{2}}{4}, so the amplitude of ∣111⟩\ket{111} is 24\frac{\sqrt{2}}{4}.

Common slips

  • −24-\frac{\sqrt{2}}{4}: Right size, wrong sign. The sign is (−1)x⋅z(-1)^{x\cdot z}: x=110x = 110 and z=111z = 111 share a 1 in 2 places, so x⋅z=0x\cdot z = 0 and the sign is plus.
  • 18\frac{1}{8}: 1/81/8 is the size of a probability here. Each amplitude has size 1/231/\sqrt{2^{3}}, so that the 8 squared magnitudes add up to 1.

Check yourself

Deutsch–Jozsa with n=2n = 2 and f(00)=0f(00) = 0, f(01)=0f(01) = 0, f(10)=1f(10) = 1, f(11)=1f(11) = 1. What does the 0/1-basis measurement of the two data qubits give?

  1. A∣00⟩\ket{00} with certainty
  2. B∣01⟩\ket{01} with certainty
  3. C∣10⟩\ket{10} with certainty
  4. D∣11⟩\ket{11} with certainty
  5. EEach of the four strings with probability 14\tfrac14
Show the answer

Answer: C. ∣10⟩\ket{10} with certainty

For z=10z = 10 the four signs (−1)x⋅z+f(x)(-1)^{x\cdot z + f(x)} are +, +, +, +: they all agree, so ∣10⟩\ket{10} has amplitude 11 and every other string has amplitude 0. (Here f(x)=10⋅xf(x) = 10\cdot x.)

Why not the others

  • A. All zeros would mean constant, but this ff is balanced: two 0s and two 1s, so the four chips +, +, -, - cancel on ∣00⟩\ket{00}.
  • B. For ∣01⟩\ket{01} the four signs (−1)x⋅z+f(x)(-1)^{x\cdot z + f(x)} are +, -, -, +: they cancel, so this outcome never occurs.
  • D. For ∣11⟩\ket{11} the four signs (−1)x⋅z+f(x)(-1)^{x\cdot z + f(x)} are +, -, +, -: they cancel, so this outcome never occurs.
  • E. The Hadamards don’t just spread amplitude around. For each outcome the last H⊗2H^{\otimes 2} adds four signed contributions, and here they add up fully on one string and cancel on the other three.

Check yourself

A black box computes ff on 4-bit strings (sixteen inputs). You are promised that ff is either constant or balanced (0 on exactly 8 inputs and 1 on the other 8). Querying one input at a time, and needing to be certain, how many queries might you need in the worst case?

  1. A22
  2. B44
  3. C99
  4. D1616
Show the answer

Answer: C. 99

In the worst case the first 8 answers agree, which a balanced ff allows. Query number 9 decides: 2n−1+1=92^{n-1} + 1 = 9 for n=4n = 4.

Why not the others

  • A. Two queries settle it only if they disagree. If they agree, a balanced ff is still possible: 8 inputs share each value.
  • B. The count depends on the number of inputs, not on the number of bits in xx. A balanced ff can give the same value on any 8 inputs, so 8 agreeing answers prove nothing.
  • D. You never need all 16. A balanced ff has only 8 inputs of each value, so once 9 answers agree, ff must be constant.

15 / 15 · Recap

Recap

From memory

Before reading the summary, write down what you remember. Pulling it back from memory is what makes it last.

  1. What does one run of UfU_f on a superposition, followed by a 0/1 measurement, tell you? Why is that no speedup?

    Show the key points
    • One pair (x,f(x))(x, f(x)), with xx random
    • Measuring the same register again repeats it; a new pair needs a fresh copy and another use of UfU_f
    • An advantage needs interference that answers a global question about ff
  2. Write the four states of Deutsch’s algorithm, ∣ψ0⟩\ket{\psi_0} to ∣ψ3⟩\ket{\psi_3}.

    Show the key points
    • ∣ψ0⟩=∣0⟩∣1⟩\ket{\psi_0} = \ket0\ket1, then ∣ψ1⟩=∣+⟩∣−⟩\ket{\psi_1} = \ket+\ket-
    • Kickback: ∣ψ2⟩=12((−1)f(0)∣0⟩+(−1)f(1)∣1⟩)∣−⟩\ket{\psi_2} = \tfrac{1}{\sqrt2}\big((-1)^{f(0)}\ket0 + (-1)^{f(1)}\ket1\big)\ket-; the target stays ∣−⟩\ket-
    • ∣ψ3⟩=±∣f(0)⊕f(1)⟩∣−⟩\ket{\psi_3} = \pm\ket{f(0)\oplus f(1)}\ket-; measure the top qubit
  3. Why does all zeros mean constant in Deutsch–Jozsa, and how well can a randomized classical method do?

    Show the key points
    • The ∣0…0⟩\ket{0\ldots0} amplitude is the average of the chips, 12n∑x(−1)f(x)\tfrac{1}{2^n}\sum_x(-1)^{f(x)}: ±1\pm1 if constant, 0 if balanced
    • A deterministic method can need 2n−1+12^{n-1} + 1 queries
    • kk random queries err with probability at most 21−k2^{1-k}, whatever nn is

Key ideas

  1. Uf∣x,y⟩=∣x,y⊕f(x)⟩U_f\ket{x,y} = \ket{x, y\oplus f(x)} is reversible. On 12n∑x∣x⟩∣0⟩\tfrac{1}{\sqrt{2^n}}\sum_x\ket x\ket0 it gives 12n∑x∣x,f(x)⟩\tfrac{1}{\sqrt{2^n}}\sum_x\ket{x, f(x)}, but a measurement returns one random pair.

  2. Phase kickback: Uf∣x⟩∣−⟩=(−1)f(x)∣x⟩∣−⟩U_f\ket x\ket- = (-1)^{f(x)}\ket x\ket-. The target is unchanged; f(x)f(x) becomes a sign on the data branch.

  3. Deutsch: ∣ψ3⟩=±∣f(0)⊕f(1)⟩∣−⟩\ket{\psi_3} = \pm\ket{f(0)\oplus f(1)}\ket-, so one query reveals whether f(0)=f(1)f(0) = f(1), and nothing more.

  4. H⊗n∣x⟩=12n∑z(−1)x⋅z∣z⟩H^{\otimes n}\ket x = \tfrac{1}{\sqrt{2^n}}\sum_z(-1)^{x\cdot z}\ket z. In Deutsch–Jozsa the ∣0…0⟩\ket{0\ldots0} amplitude is the average of the chips: ±1\pm1 if ff is constant, 0 if balanced.

  5. One quantum query against 2n−1+12^{n-1} + 1 for a certain classical answer, but only a few for a nearly certain one: kk random queries err with probability at most 21−k2^{1-k}.

Go further

Free resources for when you want more than this lesson covers.

Math refreshers

The toolkit topics this lesson leans on.

Start the interactive lesson