Week 5 | Block II begins: the first problems a quantum computer solves with fewer questions | reading: Watrous §5.1–5.3; de Wolf Ch. 2
Block I built the machinery. Block II asks what it is good for. The cleanest place to compare quantum and classical computers is the query model, where a function is hidden in a black box and we count how many times we must consult it. By the end you should be able to state the query model and its limits, use the formula for the Hadamard transform on \(n\) qubits, analyse Deutsch's, the Deutsch–Jozsa and the Bernstein–Vazirani algorithms, and say honestly how large their advantage over classical algorithms is.
In a query problem an algorithm must compute some property of a function \(f:\{0,1\}^n\to\{0,1\}^m\) that it can access only through an oracle. A classical query asks for \(f(x)\) at one input \(x\). A quantum query applies the standard oracle of Lecture 7,
$$U_f\,|x\rangle|y\rangle=|x\rangle|y\oplus f(x)\rangle ,$$to any state, superpositions included. The query complexity of the problem is the number of queries needed in the worst case over all allowed \(f\); all other gates are free.
Recall from Lecture 7 (Proposition 7.3) that for one-bit outputs a query with the output qubit in \(|-\rangle\) acts as a phase oracle:
$$U_f\,|x\rangle|-\rangle=(-1)^{f(x)}\,|x\rangle|-\rangle .$$So one query writes the whole truth table of \(f\) into the signs of a superposition. Every algorithm in this lecture is built on that sentence.
Let \(n=2\) and let \(f(x_1x_0)=1\) only for \(x=10\). The input register starts in the uniform superposition \(\tfrac12\sum_x|x\rangle\) and the output qubit in \(|-\rangle\). After one query, the amplitude of \(|10\rangle\) is .
Counting queries is the main setting in which we can prove large separations between quantum and classical computation; in the ordinary circuit model nobody can yet prove that factoring, say, is classically hard. The price is realism. A real \(f\) is not a sealed box: it is a circuit you can read, and a classical algorithm may exploit that structure. So a query speedup shows what quantum interference can do, but it becomes a real-world speedup only if \(f\) genuinely behaves like a black box (as a cipher is designed to), and only after counting the gates that implement each query (Lecture 7).
Every algorithm here starts and ends with \(H\) applied to each of \(n\) qubits. We need a formula for \(H^{\otimes n}=H\otimes\cdots\otimes H\). For bit strings \(x,y\in\{0,1\}^n\) write
$$x\cdot y=x_{n-1}y_{n-1}\oplus\cdots\oplus x_1y_1\oplus x_0y_0 ,$$the dot product mod 2. It is 1 exactly when \(x\) and \(y\) share an odd number of 1s.
Compute \(x\cdot y\) for \(x=1011\) and \(y=1101\).
For every \(x\in\{0,1\}^n\),
$$H^{\otimes n}|x\rangle=\frac{1}{2^{n/2}}\sum_{y\in\{0,1\}^n}(-1)^{x\cdot y}\,|y\rangle .$$In particular \(H^{\otimes n}|0^n\rangle\) is the uniform superposition of all \(2^n\) strings, and \(H^{\otimes n}\) is its own inverse.
Proof. HW5. For \(n=1\) the formula says \(H|x\rangle=\tfrac{1}{\sqrt2}(|0\rangle+(-1)^x|1\rangle)\), which is the definition of \(H\); Example 9.1 checks a case with \(n=2\).
Use Proposition 9.1 to find the amplitude of \(|110\rangle\) in \(H^{\otimes3}|011\rangle\).
Compute \((H\otimes H)|01\rangle\) directly and from Proposition 9.1.
Directly, \((H\otimes H)|0\rangle|1\rangle=|+\rangle|-\rangle=\tfrac12\big(|00\rangle-|01\rangle+|10\rangle-|11\rangle\big)\). From the formula with \(x=01\): \(x\cdot y=y_0\), so the sign is \(-\) exactly when \(y_0=1\), i.e. for \(y=01\) and \(11\). Same answer. It is also column 01 of the left grid in the figure, read top to bottom: \(+,-,+,-\).
The smallest interesting query problem has \(n=1\). There are four functions \(f:\{0,1\}\to\{0,1\}\): two constant (\(f=0\), \(f=1\)) and two balanced, taking each value once (\(f(x)=x\), \(f(x)=1-x\)). Deutsch's problem: decide whether \(f\) is constant or balanced, i.e. compute \(f(0)\oplus f(1)\).
Classically one query is useless: \(f(0)\) alone is consistent with both a constant and a balanced \(f\). Two queries are necessary and sufficient.
Prepare \(|0\rangle|1\rangle\) (input, output), apply \(H\) to both, make one query, apply \(H\) to the input and measure it. The result is \(f(0)\oplus f(1)\) with certainty.
After the Hadamards the input is \(|+\rangle\) and the output is \(|-\rangle\). By the phase-oracle identity, the query turns the input into
$$\frac{1}{\sqrt2}\Big((-1)^{f(0)}|0\rangle+(-1)^{f(1)}|1\rangle\Big)=(-1)^{f(0)}\,\frac{1}{\sqrt2}\Big(|0\rangle+(-1)^{f(0)\oplus f(1)}|1\rangle\Big).$$Up to the global sign this is \(|+\rangle\) when \(f(0)\oplus f(1)=0\) and \(|-\rangle\) when it is 1. The final \(H\) maps these to \(|0\rangle\) and \(|1\rangle\).
| \(f\) | input after the query (up to sign) | measured | verdict |
|---|---|---|---|
| \(f=0\) | \(|+\rangle\) | 0 | constant |
| \(f=1\) | \(-|+\rangle\) | 0 | constant |
| \(f(x)=x\) | \(|-\rangle\) | 1 | balanced |
| \(f(x)=1-x\) | \(-|-\rangle\) | 1 | balanced |
The quantum algorithm never learns \(f(0)\) or \(f(1)\): measuring gives one bit, and it spends that bit on the global property \(f(0)\oplus f(1)\). This is the pattern for everything that follows: a quantum algorithm wins by computing a joint property of many values without computing any of them.
Deutsch's algorithm is run once on an unknown \(f:\{0,1\}\to\{0,1\}\) and outputs 1. Tick every statement that is now certain.
Given \(f:\{0,1\}^n\to\{0,1\}\) that is promised to be either constant or balanced (equal to 1 on exactly half of the \(2^n\) inputs), decide which.
A deterministic classical algorithm needs \(2^{n-1}+1\) queries in the worst case.
Enough. Among any \(2^{n-1}+1\) inputs a balanced function takes both values, so if all answers agree, \(f\) is constant. Necessary. Answer the first \(2^{n-1}\) queries of any algorithm with 0. Both the constant function 0 and a balanced function that is 0 on some set of \(2^{n-1}\) inputs containing all the queried ones (and 1 elsewhere) are consistent with those answers, so an algorithm that stops now is wrong for one of them.
For \(n=5\), how many queries does a deterministic classical algorithm need in the worst case to solve the Deutsch–Jozsa problem?
Run the circuit above. The probability that the measured string is \(0^n\) is
$$P(0^n)=\Big|\frac{1}{2^n}\sum_{x\in\{0,1\}^n}(-1)^{f(x)}\Big|^2 ,$$which is 1 if \(f\) is constant and 0 if \(f\) is balanced. So one query decides the problem with certainty: output "constant" exactly when the result is \(0^n\).
After the first layer of Hadamards and the query, the input register is in the phase state
$$|\varphi_f\rangle=\frac{1}{2^{n/2}}\sum_x(-1)^{f(x)}|x\rangle .$$We only need the amplitude of \(|0^n\rangle\) after the last \(H^{\otimes n}\). By Proposition 9.1 with \(y=0^n\), every \(|x\rangle\) contributes \(2^{-n/2}\) to that amplitude (the first row of the sign grid is all \(+\)). So the amplitude is \(2^{-n/2}\cdot2^{-n/2}\sum_x(-1)^{f(x)}\), which gives the formula. If \(f\) is constant the sum is \(\pm2^n\) and the probability is 1. If \(f\) is balanced the sum has \(2^{n-1}\) terms \(+1\) and \(2^{n-1}\) terms \(-1\), so it is 0.
Suppose \(n=3\) and \(f\) is 1 on exactly one of the eight inputs, so it breaks the promise. Use the formula of Theorem 9.4 to find \(P(000)\).
Let \(n=3\) and \(f=\mathrm{MAJ}\), the majority of three bits. Check that \(f\) is balanced and find the full output distribution of the algorithm.
\(\mathrm{MAJ}(x)=1\) for the four inputs with at least two 1s, \(011,101,110,111\), and 0 for the other four, so it is balanced and \(P(000)=0\). For the rest, the amplitude of \(|y\rangle\) is, by Proposition 9.1 applied to each term,
$$a_y=\frac18\sum_x(-1)^{\mathrm{MAJ}(x)\oplus x\cdot y}.$$For \(y=001\) we have \((-1)^{x\cdot y}=(-1)^{x_0}\). The eight products \((-1)^{\mathrm{MAJ}(x)}(-1)^{x_0}\), for \(x=000,\dots,111\) in order, are
$$+1,\;-1,\;+1,\;+1,\;+1,\;+1,\;-1,\;+1 ,$$whose sum is 4, so \(a_{001}=\tfrac12\). By the symmetry of MAJ in its three bits, \(a_{010}=a_{100}=\tfrac12\) as well. Those three already use up probability \(\tfrac34\). The same computation for \(y=111\), where \((-1)^{x\cdot y}\) is the parity sign, gives \(a_{111}=-\tfrac12\), the last \(\tfrac14\). So the output is \(001,010,100\) or \(111\), each with probability \(\tfrac14\), and never \(000\). The Qiskit run in Section 6 shows exactly these four strings.
Run the algorithm with \(n=2\) on \(f=\mathrm{AND}\), which is neither constant nor balanced. What happens?
\(\sum_x(-1)^{f(x)}=1+1+1-1=2\), so \(P(00)=\big|\tfrac{2}{4}\big|^2=\tfrac14\). The algorithm answers "constant" a quarter of the time and "balanced" the rest. That is not a flaw: the promise is part of the problem, and the guarantee of Theorem 9.4 only covers functions that satisfy it. In general \(P(0^n)\) measures how far \(f\) is from balanced.
One quantum query against \(2^{n-1}+1\) classical queries looks like an exponential speedup, but only against deterministic algorithms. A randomised classical algorithm can query \(k+1\) uniformly random inputs and answer "constant" if all answers agree. It is never wrong on a constant \(f\), and on a balanced \(f\) all answers agree with probability \(2\cdot2^{-(k+1)}=2^{-k}\). Eleven queries give an error below \(0.1\%\), whatever \(n\) is. So Deutsch–Jozsa separates quantum from deterministic computation, not from randomised computation. Simon's algorithm (Lecture 10) is the first to beat randomised algorithms exponentially.
The randomised classical algorithm above makes 8 independent uniformly random queries and answers "constant" if all 8 answers agree. If \(f\) is balanced, what is the probability that it answers wrongly?
Given \(f:\{0,1\}^n\to\{0,1\}\) promised to have the form \(f(x)=s\cdot x\) for some secret string \(s\in\{0,1\}^n\), find \(s\).
Here the answer is \(n\) bits, not one. The quantum algorithm is the Deutsch–Jozsa circuit, unchanged; only the interpretation of the output differs. An oracle for \(f(x)=s\cdot x\) is easy to build from Lecture 7: one CNOT from each input qubit \(k\) with \(s_k=1\) into the target.
A Bernstein–Vazirani oracle on input qubits \(q_0,q_1,q_2\) with target \(q_3\) consists of cx(0, 3) and cx(1, 3). Written as a Qiskit bit string \(s_2s_1s_0\), the secret is
.
011, and the algorithm's output will print as 011 too.Run the Deutsch–Jozsa circuit with the oracle in the figure. Show that the output is \(101\) with certainty.
After the query the input register holds \(\tfrac{1}{2\sqrt2}\sum_x(-1)^{x_2\oplus x_0}|x\rangle\). Its sign pattern, over \(x=000,\dots,111\), is
$$+,\;-,\;+,\;-,\;-,\;+,\;-,\;+ ,$$By Proposition 9.1, the amplitude of \(|101\rangle\) after \(H^{\otimes3}\) is \(\tfrac18\sum_x(-1)^{x_2\oplus x_0}(-1)^{x\cdot101}\). Since \(x\cdot101=x_2\oplus x_0\), each of the eight terms is \((+1)\), and the amplitude is \(\tfrac18\cdot8=1\). The output state has length 1, so every other amplitude is 0, and the output is \(101\) with certainty. (You can also spot the answer in the figure: the sign pattern above is row \(101\) of the \(n=3\) grid.)
For \(f(x)=s\cdot x\), the Deutsch–Jozsa circuit outputs \(s\) with certainty, using one query.
Proof. HW5, which also asks how many queries a classical algorithm needs.
Bernstein–Vazirani extracts a hidden linear structure from a single superposition query. Simon's algorithm (Lecture 10) does the same for a hidden XOR-period, and that has been turned into attacks on real symmetric constructions. Kuwakado and Morii (2010, 2012) broke the 3-round Feistel construction (as a pseudorandom permutation) and the Even–Mansour cipher, and Kaplan, Leurent, Leverrier and Naya-Plasencia (CRYPTO 2016) broke CBC-MAC and several authenticated-encryption modes, all in polynomial time. The catch is the model: the attacker must be able to query the keyed primitive in superposition, which a remote server encrypting classical messages does not allow. Whether such "quantum superposition queries" are ever realistic is debated, and the area is a good source of course projects.
from qiskit import QuantumCircuit from qiskit.primitives import StatevectorSampler n = 3 qc = QuantumCircuit(n + 1, n) # inputs q0..q2, target q3 qc.x(n); qc.h(n) # target in |-> qc.h(range(n)) qc.ccx(0, 1, n); qc.ccx(0, 2, n); qc.ccx(1, 2, n) # U_f for MAJ = x0x1 xor x0x2 xor x1x2 qc.h(range(n)) qc.measure(range(n), range(n)) counts = StatevectorSampler(seed=5).run([qc], shots=2000).result()[0].data.c.get_counts() print(dict(sorted(counts.items())))
{'001': 514, '010': 520, '100': 483, '111': 483}
Majority is \(x_0x_1\oplus x_0x_2\oplus x_1x_2\) (check the eight inputs), so its oracle is three Toffolis XORing into the target, as in Lecture 7. The counts match Example 9.2: four outcomes near \(\tfrac14\) each, and never 000, so the function is balanced. With QuantumCircuit(n + 1, n) the classical register is named c.
from qiskit import QuantumCircuit from qiskit.primitives import StatevectorSampler s = "1011" # the secret, written s3 s2 s1 s0 n = len(s) qc = QuantumCircuit(n + 1, n) qc.x(n); qc.h(n) # target qubit n in |-> qc.h(range(n)) for k in range(n): if s[n - 1 - k] == "1": # s_k is the k-th character from the RIGHT qc.cx(k, n) # oracle for f(x) = s.x qc.h(range(n)) qc.measure(range(n), range(n)) # qubit k -> classical bit k counts = StatevectorSampler(seed=5).run([qc], shots=100).result()[0].data.c.get_counts() print(counts)
{'1011': 100}
All 100 shots return the secret. The ordering works out because both the secret string and Qiskit's printed outcome put bit \(k\) at position \(k\) from the right. Index the string from the left by mistake (s[k]) and you recover the reversed secret 1101, a bug that is easy to miss when \(s\) is a palindrome.
Next time: Simon's algorithm, the first exponential separation from randomised classical algorithms, and the complexity classes P, BPP, BQP and NP. HW5 covers Lectures 9 and 10.
all numbers and figures in these notes are computed in python (verify_L09.py) and follow the page theme