Lesson 14: Queries and Bernstein–Vazirani
Counting oracle calls, and finding a hidden string with one query
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 / 14 · Learn
Before you start
Lesson 9 counted uses of an oracle: one for Deutsch–Jozsa, up to for a classical method that must be certain. This lesson makes that count a model of computation, then uses it on a new problem: finding a hidden string with a single query. You need the Hadamard formula and phase kickback from Lesson 9, and the habit from Lesson 13 of asking what an oracle leaves behind.
By the end of this lesson you can:
- say what the query model counts, why it is useful, and what it leaves out;
- explain why a classical algorithm needs queries to find in ;
- run Bernstein–Vazirani by hand and explain why the last Hadamards return ;
- build its oracle from CNOTs, and write and read the circuit in Qiskit;
- state honestly how large the quantum advantage is.
Check yourself
A warm-up: make a guess before reading on. The lesson comes back to it.
A black box hides a 5-bit string . Ask it about any 5-bit input and it answers with one bit, , the dot product mod 2. You ask about one input at a time and must be certain of at the end. How many questions do you need?
- A, if it is well chosen
- B
- C
- D: every input
Show the answer
Answer: B.
Ask the 5 inputs with a single 1: the one with its 1 in place returns . Fewer can’t do it: 4 one-bit answers form at most patterns, but has 32 possible values, so two strings would give the same answers.
Why not the others
- A. One answer is one bit, and could be any of strings. A single bit splits them into at most two groups, so many strings would still fit the answer.
- C. is the Deutsch–Jozsa count, where many agreeing answers prove little. Here a well-chosen question gives one bit of directly: the input with a single 1 in place returns .
- D. You never need every input. The input returns , the input returns , and so on: 5 questions.
Check yourself
A warm-up: make a guess before reading on. The lesson comes back to it.
The same box, hiding a 5-bit string , is now a quantum oracle, , which you may call on a superposition of inputs, with any gates before and after. What is the fewest calls that can find with certainty?
- A
- BAbout : a quantum computer gives a square-root saving
- C: each call still answers with one bit
- DNo number of calls gives certainty: quantum measurements are random
Show the answer
Answer: A.
One call. Hadamards on the inputs, one call with the target in , and Hadamards again leave the inputs in , so measuring them gives all 5 bits of with certainty.
Why not the others
- B. A square-root saving is what Grover’s search gives on problems with no structure to exploit. Here is linear in , and that structure lets one call reveal all of .
- C. The answer bit isn’t where the information goes. With the target in , one call writes the sign onto every input branch, and Hadamards turn that sign pattern into : the 5 input qubits carry all of out.
- D. A measurement is certain when the state is a basis state, as at the end of Deutsch–Jozsa for a constant . Here the circuit ends with the inputs in , so the result is every time.
2 / 14 · Learn
The query model
In the query model a function sits inside a black box, the oracle. An algorithm may do anything it likes between calls, but it learns about only by calling the oracle, and its cost is the number of calls, or queries. Every other gate is free in this count.
Why count only that? First, lower bounds become provable: each call returns limited information, so one can show that no algorithm gets by with fewer calls. For ordinary running time such proofs are mostly out of reach. Second, the model shows where a speed-up comes from: the structure of , and interference between the branches of one call.
The catch: a real problem comes with no box. Someone has to build from gates, as in Lesson 13, and every call costs that whole circuit again. Lesson 18 builds real oracles.
Check yourself
Two algorithms solve the same problem with the same oracle. Algorithm A calls it 4 times and uses 60,000 other gates. Algorithm B calls it 50 times and uses 500 other gates. Built from gates, one call costs 800 gates. Which algorithm wins in the query model, and which uses fewer gates in all?
- AA in the query model; B in total gates
- BA on both counts: the number of calls is the whole cost
- CB on both counts
- DB in the query model; A in total gates
Hints
- In the query model, what is counted and what is free?
- For total gates, each call costs the oracle’s gates; add the other gates.
Show the answer
Answer: A. A in the query model; B in total gates
Calls: A makes 4, B makes 50, so A wins the query count. Gates: A uses 4 × 800 + 60,000 = 63,200 and B uses 50 × 800 + 500 = 40,500, so B is cheaper to run. The query model is a clean way to compare algorithms, but a real oracle is a circuit, and its gates count too.
Why not the others
- B. The query count leaves out every gate outside the oracle, and treats each call as one step although it costs 800 gates. In all, A uses 4 × 800 + 60,000 = 63,200 gates and B uses 50 × 800 + 500 = 40,500.
- C. In the query model only calls count, and A makes 4 against B’s 50. B wins only once every gate is counted: 40,500 against 63,200.
- D. That swaps the two counts. Calls: A 4, B 50. Gates in all: A 63,200, B 40,500.
Query algorithms come in three kinds.
- Deterministic: fixed rules choose each query from the answers so far, and the result must always be right.
- Randomised: it may flip coins, and may be wrong with a small probability you choose, such as 1 in 1000.
- Quantum: it calls on superpositions of inputs, with gates in between, and measures at the end.
The kind matters. For Deutsch–Jozsa on -bit inputs, Lesson 9 found that a deterministic algorithm can need queries, random queries err with probability at most whatever is, and the quantum algorithm needs one. An exponential gap against one kind of algorithm can shrink to a small one against another.
Check yourself
Deutsch–Jozsa with 30-bit inputs: is constant or balanced? Match each kind of algorithm with the number of queries that is enough for it, using the methods from Lesson 9.
Match each of these…
- Deterministic, always right
- Randomised, wrong at most 1 time in 1000
- Quantum, always right
…with one of these
Show the answer
| Deterministic, always right | |
| Randomised, wrong at most 1 time in 1000 | |
| Quantum, always right |
Deterministic: the first answers can agree, so . Randomised: the error after random queries is at most , and is below 1 in 1000, so 11. Quantum: one call. The exponential gap holds only against the deterministic kind.
3 / 14 · Learn
The hidden-string problem
The Bernstein–Vazirani problem: a black box hides an -bit string and computes
the dot product mod 2 from Lesson 9. Find .
So is the parity of the bits of in the places where has a 1. With , and .
Every such except is balanced, so it is also a valid Deutsch–Jozsa input; you saw there. The question is new: not “constant or balanced?” but “which ?”
Check yourself
. What is ?
Hints
- Multiply the two strings bit by bit, then add the products mod 2.
- Both strings have a 1 in 2 places.
Show the answer
Answer:
Bit by bit: , , , . The products add up to 2, and , so .
Common slips
- : 2 is the number of places where both strings have a 1. The dot product mod 2 keeps only whether that number is odd: .
- : Line the strings up and multiply bit by bit: , , , . The products add up to 2, which is even, so .
4 / 14 · Explore
Finding s one bit at a time
Each answer is one bit, but a well-chosen question makes that bit a bit of . Write for the input with a single 1, in place . Then : for , ask , and and read off , , . That is queries.
No deterministic method does better. Its answers trace a path down a tree like the one below: each one-bit answer splits the candidates into at most two groups. After queries there are at most groups, and certainty needs one candidate per group, so and . This is the query model at work: a lower bound that holds for every possible algorithm.
The single-1 strategy for . Each question is answered with one bit, so three answers lead to one of leaves, one for each . The path for is highlighted: , , .
Check yourself
A 10-bit string is chosen at random, all strings equally likely. A deterministic algorithm asks 7 questions , one bit each, and then names one string. What is the highest possible probability that it names ?
Hints
- After the answers come in, how many groups can the strings be split into?
- 7 one-bit answers have at most patterns; the algorithm names one string per pattern.
Show the answer
Answer:
7 one-bit answers split the 1,024 strings into at most groups, and the algorithm names one string per group. So it is right for at most 128 of the 1,024 strings: probability . Asking 7 single-1 inputs and guessing the other 3 bits achieves it.
Common slips
- : That gives credit for each bit found, but the answer counts only if all 10 bits are right. 3 bits are still unknown, and a guess gets all of them right with probability .
- : It can’t be certain, but it can be lucky. Asking 7 single-1 inputs fixes 7 bits, and a guess at the other 3 bits is right with probability . (A blind guess, with no questions asked, would be right with probability , close to 0.)
5 / 14 · Learn
One query
Quantumly, use the oracle in phase form. With the target in , kickback (Lesson 9) gives
so the target stays put and each call multiplies the input branch by a sign. The circuit is Deutsch–Jozsa’s; only the reading of the result is new.
- Start the inputs in and the target in , and apply to every qubit.
- Call the oracle once. The inputs become .
- Apply to each input qubit again and measure the inputs in the 0/1 basis.
The claim is that the result is , every time.
Predict, then see
Bernstein–Vazirani with and . What will the 0/1-basis measurement of the three input qubits give?
- AOne bit of per run, so three runs to see all of it
- B, every time
- CA random input , with in the target
- DEach of the eight strings, with probability
Where did the information about sit just after the call, and what did the last Hadamards do with it? A sentence or two is enough.
Show what happens
What happens: B. , every time
After the first Hadamards the inputs hold all eight strings with equal amplitude, and the target is . The call gives each branch the sign . The last Hadamards make the eight branches interfere: they add up on and cancel on every other string. So the inputs read every time; the target, still , gives a random bit.
Why not the others
- A. One run measured all three inputs, and all three bits matched . The call wrote a sign on every branch, and the Hadamards turned the whole sign pattern into .
- C. With the target in , nothing is written into it: it stays and reads 0 or 1 at random. The inputs are not random at all; they read .
- D. It seemed the last Hadamards would spread the amplitude evenly. Stepping through showed the eight branches recombining: they add up on and cancel everywhere else.
6 / 14 · Learn
Why the last Hadamards return s
Lesson 9 gave the Hadamard transform of a basis state:
That is exactly the state of the inputs after the call. The oracle has prepared without anyone knowing . And undoes itself, since on every qubit, so the last Hadamards give .
Each leaves its own sign pattern on the inputs. The figure shows three for . In the shaded columns, the inputs with a single 1, the sign is : the classical strategy reads exactly these, one query each.
The sign on each input after the call, for three hidden strings. Every bar is an amplitude of size . The pattern of is the pattern of times the pattern of , column by column.
Check yourself
Bernstein–Vazirani with . Right after the call, the eight input branches carry the signs , in that order. Which string is hidden?
- A
- B
- C
- D
Hints
- The sign on is . Which inputs make equal to a single bit of ?
- At the sign is ; at , ; at , .
Show the answer
Answer: B.
The signs at the inputs with a single 1 spell out : has , has , has , so . Check one more: at the sign is , as listed.
Why not the others
- A. That reads the bits from right to left. The sign at is , so it gives the first bit of ; the sign at gives the last.
- C. A minus sign means . At the sign is , so : read as 1 and as 0.
- D. The pattern of has the sign at , but this one has there. The signs at , and give , , .
The same fact, seen from the end of the circuit. After the last Hadamards the amplitude of an outcome is
Each term is where the two sign patterns agree and where they differ. For they agree everywhere, and the amplitude is 1. For any other the product of the two patterns is the pattern of , a balanced function, so the terms cancel to 0. The last Hadamards compare the pattern with every candidate at once, and only survives.
Two comparisons for . Against every product is , so the amplitude is . Against four products are and four are , so the amplitude is 0. The bars at the bottom show every outcome’s amplitude: all of it is on .
Check yourself
Bernstein–Vazirani with and . Just before the measurement, what is the amplitude of in the input register?
Hints
- Compare the sign patterns of and : where do they agree?
- The product of the two patterns is the pattern of , which is balanced.
Show the answer
Answer:
The product of the two patterns is the pattern of : four s and four s. Their average is 0, so has amplitude 0. Only survives.
Common slips
- : That spreads the probability evenly over the eight strings. The patterns of and agree on four inputs and differ on four, so the eight terms cancel: .
- : Half the terms are , but the other half are and cancel them: .
- : is each amplitude right after the call. The last Hadamards then add up eight signed terms for each outcome, and for they cancel.
7 / 14 · Learn
Building the oracle
So far the oracle was a box. For this you can build it, and cheaply. is the XOR of the bits with , and a CNOT from into the target adds to it mod 2. So one CNOT from each such input computes
For that is three CNOTs, from , and . There is no work qubit and no garbage, so nothing has to be uncomputed. The whole oracle costs at most CNOTs.
Check yourself
Build for : the top three wires are and the bottom wire is the target. A control dot and an in the same column make a CNOT.
Build it in the circuit builder (gates: control, X).
Hints
- Which bits does add up when ?
- Each CNOT from an input into the target adds that bit to the target, mod 2.
- Every gate in a column obeys every dot in that column, so two dots in one column make a Toffoli, which adds (AND). Give each CNOT its own column.
Show the answer
One circuit that works: CNOT q0→q3, then CNOT q2→q3.
For , : a CNOT from and a CNOT from , in separate columns, add those two bits to the target. Their order doesn’t matter.
Compare a careless build. Lesson 13 showed that a work qubit left holding an intermediate value wrecks interference. The next question puts that to the test on this circuit.
Check yourself
A student builds the oracle for with a spare work qubit that starts in : CNOTs from and into the work qubit, which then holds , and one CNOT from the work qubit into the target. The work qubit is left as it is. What does the Bernstein–Vazirani measurement of give?
- A every time: the target still kicks back the right sign
- B every time: the work qubit takes the answer
- CEach of the eight strings, with probability
- D or , with probability each
Show the answer
Answer: D. or , with probability each
After the call the state is : the work qubit is entangled with the inputs. Each of its two values keeps four branches, and the last Hadamards turn each group into an even mix of and . Uncomputing the work qubit (running its two CNOTs again) would restore the certain answer.
Why not the others
- A. The sign does land on each branch, but the work qubit now holds , a record of which half of the inputs each branch belongs to. Branches with different records can’t interfere, so the cancellation that leaves only fails.
- B. The work qubit does hold , but that doesn’t send every branch to . Each group of branches, with or , ends in an even mix of and .
- C. The record splits the eight branches into two groups of four, one for each value of . Within a group they still interfere, so most outcomes still cancel: only and survive.
8 / 14 · Worked example
Worked example: qubit by qubit
The state after the call factors into one qubit at a time, which makes Bernstein–Vazirani easy to trace by hand.
Bernstein–Vazirani for , with the oracle built from CNOTs. At slice 1 every input is ; at slice 2 the inputs with have turned into ; at slice 3 each input is .
Worked example
Bernstein–Vazirani for
The sign splits into one factor per qubit, , so the state stays a product throughout. Take , , and the oracle built from CNOTs.
Before the call
After the first Hadamards each input is and the target is . The state is .
The call
The oracle is a CNOT from and a CNOT from into the target. A CNOT into a target multiplies its control’s branch by (Lessons 6 and 7), which turns into . So after the call the state is : the inputs with are , the others still .
Last H
and , so the inputs become and the measurement reads . No qubit was ever entangled with another: each carried its own bit of as a sign.
Your turn: the call
Now . Start again from .
Your turn
Which input qubits are in right after the call?
Select all that apply.
- A
- B
- C
- DNone of them: a CNOT never changes its control
Hint
- Which inputs does the oracle for connect to the target?
Show the answer
Answers: B. ; C.
For the oracle is a CNOT from and one from . Each kicks a minus sign onto its control’s branch: .
Why not the others
- A. , so the oracle has no CNOT from : nothing kicks back, and it stays .
- D. That holds for controls in or . Here the target is , and puts a minus sign on the control’s branch, which turns into .
Your turn: read out
Your turn
What does the measurement of give?
- A, every time
- B, every time
- C0 or 1 at random on and
- D, every time
Show the answer
Answer: D. , every time
and , so becomes : the measurement reads .
Why not the others
- A. , not : the qubits that are after the call read 1.
- B. That reverses the order. is the first bit; it stayed and reads 0.
- C. Measuring directly would be a coin toss, but the last comes first, and exactly.
One more, fully worked.
Another worked example
Another example:
Before the call
As always: .
The call
For the oracle is a CNOT from and one from . Each turns its control into : .
Last H
becomes . The measurement reads .
9 / 14 · Learn
Bernstein–Vazirani in Qiskit
In Qiskit the circuit takes a few lines, with one detail to watch: Qiskit prints qubit 0 as the rightmost bit of a result (Lesson 12). Put on qubit 0, on qubit 1 and so on, as in the circuits here, and a run for prints , reversed.
The program below sidesteps this by writing the string s the way Qiskit prints: its last character belongs to qubit 0. That is why the loop runs over reversed(s): qubit i gets a CNOT when the i-th character from the right is 1, and the counts then show s exactly as written. Run it, then change s.
Check yourself
A Bernstein–Vazirani program on 4 input qubits puts a CNOT from qubits 0, 1 and 3 into the target, qubit 4, and measures qubit into classical bit for . It runs 1000 shots. What do Qiskit’s counts show?
- A
{'0100': 1000} - B
{'1011': 1000} - C
{'1101': 1000} - DAbout 63 shots on each of the 16 strings
Hints
- Which qubits end in ?
- Qiskit writes qubit 0 as the last character of the string.
Show the answer
Answer: B. {'1011': 1000}
Qubits 0, 1 and 3 get a CNOT, so they read 1 and the rest read 0. In lesson order, qubit 0 first, that is . Qiskit prints qubit 0 rightmost, so every shot shows 1011.
Why not the others
- A. The qubits with a CNOT read 1, not 0. Each CNOT into the target turns its control’s into , and .
- C. That writes qubit 0 first, as the kets in these lessons do. Qiskit prints qubit 0 as the rightmost bit, so the string comes out the other way round.
- D. The last Hadamards don’t spread the shots out. They turn the sign pattern from the call into one basis state, so every shot gives the same string.
Check yourself
The program below is missing its oracle. The hidden string is s = '110100', written the way Qiskit prints results. Add the oracle so that every shot reads 110100 in the counts.
from qiskit import QuantumCircuit
s = '110100' # the hidden string, as Qiskit will print it
n = len(s)
qc = QuantumCircuit(n + 1, n)
qc.x(n) # target (qubit n) in |1>
qc.h(range(n + 1))
# the oracle: one CNOT into qubit n for each 1 in s
qc.h(range(n))
qc.measure(range(n), range(n))Write the program in the app’s code editor (Python with Qiskit’s circuit methods), run it, and check it.
Hints
- Qiskit prints qubit 0 as the rightmost bit. Which character of
sbelongs to qubit 0? - The last character,
s[5], belongs to qubit 0, and the first,s[0], to qubit 5. Loop overenumerate(reversed(s)). - For each
i, bitinenumerate(reversed(s)), addqc.cx(i, n)whenbit == '1'.
Show the answer
One program that works:
from qiskit import QuantumCircuit
s = '110100' # the hidden string, as Qiskit will print it
n = len(s)
qc = QuantumCircuit(n + 1, n)
qc.x(n) # target (qubit n) in |1>
qc.h(range(n + 1))
for i, bit in enumerate(reversed(s)):
if bit == '1':
qc.cx(i, n)
qc.h(range(n))
qc.measure(range(n), range(n))Qiskit writes qubit 0 as the rightmost bit, so the last character of s decides qubit 0’s CNOT. Looping over reversed(s) lines them up: CNOTs from qubits 2, 4 and 5, and the counts read 110100.
10 / 14 · Learn
How big is the gap?
Count honestly. Classically, Bernstein–Vazirani needs queries, and randomness doesn’t help much: a randomised algorithm is a mixture of deterministic ones, so with queries it is right with probability at most on a random . The quantum algorithm needs one query. That is against 1: a polynomial gap, not an exponential one. It survives randomness, unlike Deutsch–Jozsa’s, but it was never large.
Count gates and it shrinks further. The quantum circuit spends up to CNOTs on its one call, plus one and Hadamards. The classical method makes calls. Both cost about steps.
So why study it? It is the cleanest case of Hadamards, one call and Hadamards beating classical queries, and Lesson 15 uses the same pattern on a problem where classical algorithms need exponentially many queries.
Check yourself
Bernstein–Vazirani for the 20-bit string (all ones), with the oracle built from CNOTs. Counting every , and CNOT, how many gates does the whole circuit use?
Hints
- List the layers: the target’s , the first Hadamards, the oracle, the last Hadamards.
- The first Hadamards act on all 21 qubits, the last ones on the 20 inputs; the oracle is one CNOT per 1 in .
Show the answer
Answer:
One , 21 Hadamards on all qubits, 20 CNOTs for the oracle, and 20 Hadamards on the inputs: . The classical method makes 20 calls; in gates the two are about the same size.
Common slips
- : One is the number of calls. Built from gates, the call is 20 CNOTs, and the circuit around it has an and 41 Hadamards.
- : That leaves out the oracle. For it is one CNOT from each of the 20 inputs: .
- : Almost: the target also needs its , to start in .
- : The second layer of Hadamards acts on the 20 inputs only, not on the target: .
11 / 14 · Practice
Practice
Three items, each with new numbers when it comes back. Hints are there if you need them.
Check yourself
. What is ?
Hints
- Multiply the two strings bit by bit, then add the products mod 2.
- Both strings have a 1 in 2 places.
Show the answer
Answer:
Bit by bit: , , , , . The products add up to 2, and , so .
Common slips
- : 2 is the number of places where both strings have a 1. The dot product mod 2 keeps only whether that number is odd: .
- : Line the strings up and multiply bit by bit: , , , , . The products add up to 2, which is even, so .
Check yourself
Bernstein–Vazirani with and . Right after the call, what is the amplitude of in the input register?
Hints
- Before the call every input has the same amplitude. What does the call change?
- The call multiplies by . Work out .
Show the answer
Answer:
Before the call every input has amplitude . The call multiplies by with , so the amplitude is .
Common slips
- : Right size, wrong sign. The sign is , and .
- : is the size of a probability here. Each of the 16 amplitudes has size , so that their squares add up to 1.
- : Only after the last Hadamards does the amplitude gather on . Right after the call, every input still has amplitude of size ; the call only sets its sign.
Check yourself
You build the oracle for with from CNOTs into the target. Which input wires get a CNOT?
Select all that apply.
- A
- B
- C
- D
- E
Hint
- adds up, mod 2, the bits where .
Show the answer
Answers: A. ; B. ; C. ; D.
is the XOR of the bits with : here , , , . One CNOT from each of them into the target adds that bit to it, so together they add .
Why not the others
- E. , so doesn’t appear in and needs no CNOT.
12 / 14 · Explain
Explain it
Explain it
A classical query to this box returns one bit, yet one quantum call reveals all bits of . Explain how, and why this doesn’t break the rule that each answer is one bit.
Show a model answer
With the target in , the call writes no bit into the target at all: kickback multiplies each input branch by . Because the inputs are in a superposition of all strings, one call writes a whole sign pattern, and that pattern is exactly . The last Hadamards undo the transform and leave , so measuring the inputs gives every bit of . The one-bit rule is about the answer register; a quantum call also acts on the input qubits, and they carry the information out.
A good answer covers
- Kickback: the call puts the sign on every input branch, not a bit in the target
- The sign pattern equals , so the last Hadamards turn it into
- The information leaves through the input qubits, which is how one call gives bits
13 / 14 · Mastery check
Mastery check
Check yourself
A black box hides a 6-bit string . Ask it about any 6-bit input and it answers with one bit, , the dot product mod 2. You ask about one input at a time and must be certain of at the end. How many questions do you need?
- A, if it is well chosen
- B
- C
- D: every input
Show the answer
Answer: B.
Ask the 6 inputs with a single 1: the one with its 1 in place returns . Fewer can’t do it: 5 one-bit answers form at most patterns, but has 64 possible values, so two strings would give the same answers.
Why not the others
- A. One answer is one bit, and could be any of strings. A single bit splits them into at most two groups, so many strings would still fit the answer.
- C. is the Deutsch–Jozsa count, where many agreeing answers prove little. Here a well-chosen question gives one bit of directly: the input with a single 1 in place returns .
- D. You never need every input. The input returns , the input returns , and so on: 6 questions.
Check yourself
Two algorithms solve the same problem with the same oracle. Algorithm A calls it 6 times and uses 120,000 other gates. Algorithm B calls it 60 times and uses 4,000 other gates. Built from gates, one call costs 1,200 gates. Which algorithm wins in the query model, and which uses fewer gates in all?
- AA in the query model; B in total gates
- BA on both counts: the number of calls is the whole cost
- CB on both counts
- DB in the query model; A in total gates
Show the answer
Answer: A. A in the query model; B in total gates
Calls: A makes 6, B makes 60, so A wins the query count. Gates: A uses 6 × 1,200 + 120,000 = 127,200 and B uses 60 × 1,200 + 4,000 = 76,000, so B is cheaper to run. The query model is a clean way to compare algorithms, but a real oracle is a circuit, and its gates count too.
Why not the others
- B. The query count leaves out every gate outside the oracle, and treats each call as one step although it costs 1,200 gates. In all, A uses 6 × 1,200 + 120,000 = 127,200 gates and B uses 60 × 1,200 + 4,000 = 76,000.
- C. In the query model only calls count, and A makes 6 against B’s 60. B wins only once every gate is counted: 76,000 against 127,200.
- D. That swaps the two counts. Calls: A 6, B 60. Gates in all: A 127,200, B 76,000.
Check yourself
Bernstein–Vazirani with . Right after the call, the eight input branches carry the signs , in that order. Which string is hidden?
- A
- B
- C
- D
Hints
- The sign on is . Which inputs make equal to a single bit of ?
- At the sign is ; at , ; at , .
Show the answer
Answer: B.
The signs at the inputs with a single 1 spell out : has , has , has , so . Check one more: at the sign is , as listed.
Why not the others
- A. The pattern of has the sign at , but this one has there. The signs at , and give , , .
- C. A minus sign means . At the sign is , so : read as 1 and as 0.
- D. The pattern of has the sign at , but this one has there. The signs at , and give , , .
Check yourself
The same box, hiding an 8-bit string , is now a quantum oracle, , which you may call on a superposition of inputs, with any gates before and after. What is the fewest calls that can find with certainty?
- A
- BAbout : a quantum computer gives a square-root saving
- C: each call still answers with one bit
- DNo number of calls gives certainty: quantum measurements are random
Show the answer
Answer: A.
One call. Hadamards on the inputs, one call with the target in , and Hadamards again leave the inputs in , so measuring them gives all 8 bits of with certainty.
Why not the others
- B. A square-root saving is what Grover’s search gives on problems with no structure to exploit. Here is linear in , and that structure lets one call reveal all of .
- C. The answer bit isn’t where the information goes. With the target in , one call writes the sign onto every input branch, and Hadamards turn that sign pattern into : the 8 input qubits carry all of out.
- D. A measurement is certain when the state is a basis state, as at the end of Deutsch–Jozsa for a constant . Here the circuit ends with the inputs in , so the result is every time.
Check yourself
Complete Bernstein–Vazirani for . The top three wires are and the bottom wire is the target, which starts in ; the Hadamards are in place. Put the oracle in the empty columns, so that the inputs end in . A control dot and an in the same column make a CNOT; give each CNOT its own column.
Build it in the circuit builder (gates: control, X).
Show the answer
One circuit that works: H on q0, q1, q2 and q3, then CNOT q0→q3, then CNOT q2→q3, then H on q0, q1 and q2.
adds up the bits and , so the oracle is one CNOT from each of them into the target. Each kicks a minus sign back onto its control, which turns its into , and the last Hadamards read .
For the oracle needs 2 CNOTs, from and into the target, each in its own column. With the target in , each CNOT turns its control from into . The last Hadamards give , so the inputs read .
Check yourself
A Bernstein–Vazirani program on 4 input qubits puts a CNOT from qubits 1, 2 and 3 into the target, qubit 4, and measures qubit into classical bit for . It runs 1000 shots. What do Qiskit’s counts show?
- A
{'0001': 1000} - BAbout 63 shots on each of the 16 strings
- C
{'0111': 1000} - D
{'1110': 1000}
Show the answer
Answer: D. {'1110': 1000}
Qubits 1, 2 and 3 get a CNOT, so they read 1 and the rest read 0. In lesson order, qubit 0 first, that is . Qiskit prints qubit 0 rightmost, so every shot shows 1110.
Why not the others
- A. The qubits with a CNOT read 1, not 0. Each CNOT into the target turns its control’s into , and .
- B. The last Hadamards don’t spread the shots out. They turn the sign pattern from the call into one basis state, so every shot gives the same string.
- C. That writes qubit 0 first, as the kets in these lessons do. Qiskit prints qubit 0 as the rightmost bit, so the string comes out the other way round.
Check yourself
A headline reads: “Quantum computer finds a hidden 1000-bit string exponentially faster.” The algorithm is Bernstein–Vazirani. Which verdict is accurate?
- AAccurate: the quantum call returns 1000 bits where a classical one returns 1, an exponential gain
- BExaggerated: one query against 1000 is a factor of 1000, and the oracle itself costs up to 1000 CNOTs
- CAccurate: a classical computer must try all inputs
- DFalse: the quantum algorithm needs 1000 runs as well, one per bit
Show the answer
Answer: B. Exaggerated: one query against 1000 is a factor of 1000, and the oracle itself costs up to 1000 CNOTs
Classically, 1000 queries find (the single-1 inputs), and no deterministic method needs fewer. Quantumly, one call. A factor of 1000 is a polynomial gap, and counting gates, the call itself is up to 1000 CNOTs. The exponential claim is wrong; the advantage is real but modest.
Why not the others
- A. A gain of 1000 bits against 1 per query is a factor of 1000 in the number of queries. An exponential gap would be 1 query against something like .
- C. No need: the 1000 inputs with a single 1 give the 1000 bits of , one query each. The classical cost is 1000 queries, not .
- D. One run measures all 1000 input qubits, and every bit matches . The quantum advantage is real; it just isn’t exponential.
14 / 14 · Recap
Recap
From memory
Before reading the summary, write down what you remember. Pulling it back from memory is what makes it last.
What does the query model count, why is it useful, and what does it leave out?
Show the key points
- Only calls to the oracle; every other gate is free
- Lower bounds can be proved, and it shows where a speed-up comes from
- A real oracle is a circuit, and each call costs its gates
Describe the Bernstein–Vazirani circuit and why it returns .
Show the key points
- on all inputs and on a target in ; one call; on the inputs; measure
- After the call the inputs hold
- The last Hadamards undo the transform: the inputs are , read with certainty
How is the oracle built, and how big is the quantum advantage?
Show the key points
- One CNOT from each input with into the target; no work qubits
- Classically queries, one bit each, and no fewer; quantumly 1
- A polynomial gap, small in gates too; Simon’s algorithm uses the same pattern for an exponential one
Key ideas
The query model counts oracle calls only. It makes lower bounds provable and shows where a speed-up comes from, but a real oracle is a circuit whose gates cost too.
Bernstein–Vazirani: . Classically each query gives one bit, so queries are needed and enough.
Quantumly: Hadamards, one call with the target in , Hadamards. The call leaves on the inputs, and the last Hadamards turn it into .
The oracle is one CNOT per 1 in , into the target. Each turns its control from into , so the circuit can be traced qubit by qubit.
In Qiskit, qubit 0 is the rightmost bit of a result. The gap is queries against 1: polynomial, and a warm-up for Simon’s algorithm.
Go further
Free resources for when you want more than this lesson covers.
- Fundamentals of quantum algorithms: The Deutsch–Jozsa algorithmJohn Watrous, IBM Quantum · Course
Goes on to solve the Bernstein–Vazirani problem with the same Hadamard-transform algebra as this lesson.
- Decision tree modelWikipedia · Article
Defines deterministic, randomised and quantum query complexity, with the decision trees behind lower bounds like this lesson’s.
- Lecture notes on quantum algorithmsAndrew Childs · Lecture notes
Graduate notes whose part on query complexity shows how lower bounds on the number of oracle calls are proved.
- Bernstein–Vazirani algorithmWikipedia · Article
The problem, the classical method with n queries, the one-query circuit, and its original use: separating quantum from classical complexity.