Oracle Algorithms Lab
Hide a function inside a black box, then find out its secret with one quantum query. Step through Deutsch–Jozsa, read a hidden string with Bernstein–Vazirani, and recover a hidden period with Simon’s algorithm by collecting measurements and solving a few equations. Build your own function, break the promise, and compare against the classical count. Everything runs in your browser.
Reviewed December 2026. New to this? Start on the Deutsch–Jozsa tab, pick Constant 0, then press Next five times.
Truth table of f (click a cell to flip it). The promise: f is constant or balanced.
This f has 4 ones out of 8: balanced.
Interference: the amplitude of 00…0 is the average of all the signs.
| Input | Amplitude | Chance |
|---|---|---|
| |000⟩ | 0.000 | 0.0% |
| |001⟩ | 0.000 | 0.0% |
| |010⟩ | 0.000 | 0.0% |
| |011⟩ | 0.000 | 0.0% |
| |100⟩ | 0.000 | 0.0% |
| |101⟩ | 0.000 | 0.0% |
| |110⟩ | 0.000 | 0.0% |
| |111⟩ | 1.000 | 100.0% |
Result after the last step
Chance of reading 00…0 on the input qubits: 0.00%
Closed form |average of (−1)^f(x)|² = 0.00%
Verdict: balanced. All zeros never.
20 measurements: 111 × 20
The classical side
If f is balanced, the chance that all 3 answers agree (and you wrongly guess constant) is 14.286%.
A guaranteed answer needs 5 queries in the worst case. The quantum circuit asks once.
A five-minute path
- Deutsch–Jozsa tab: choose Constant 0 and press Next to the last step. The chance of 00…0 is 100 percent.
- Change the function to Balanced: parity. The chance of 00…0 drops to exactly 0. Same circuit, opposite verdict.
- Click a few cells in the truth table to break the promise. The chance lands between 0 and 100, and the verdict says so.
- Move the classical slider. See how few random queries are enough to be almost sure, and how many a guarantee needs.
- Bernstein–Vazirani tab: set a secret and read it back with one query. Flip the offset and see no change.
- Simon tab: press Run once until only one candidate remains. Then press New hidden function.
What the lab does and does not do
- It simulates the full quantum state with perfect gates and an ideal oracle. There is no noise.
- It counts oracle calls, not running time. Building a real oracle costs gates, and that cost is not counted.
- For Simon, the lab ignores the second register at measurement. Measuring it first gives the same distribution for the input register.
- It does not show hardware results. See the Noise Lab for how errors would change the picture.
Reference tables
Computed by the same code as the lab.
| Function f | f(000) … f(111) | Class | Chance of 000 |
|---|---|---|---|
| Constant 0 | 0 0 0 0 0 0 0 0 | constant | 100.0% |
| Constant 1 | 1 1 1 1 1 1 1 1 | constant | 100.0% |
| Balanced: first bit | 0 0 0 0 1 1 1 1 | balanced | 0.0% |
| Balanced: parity of all bits | 0 1 1 0 1 0 0 1 | balanced | 0.0% |
| Balanced: random half (seed 7) | 0 1 1 0 1 0 1 0 | balanced | 0.0% |
| k | n = 3 | n = 4 | n = 5 | n = 6 |
|---|---|---|---|---|
| 1 | 100.000% | 100.000% | 100.000% | 100.000% |
| 2 | 42.857% | 46.667% | 48.387% | 49.206% |
| 3 | 14.286% | 20.000% | 22.581% | 23.810% |
| 4 | 2.857% | 7.692% | 10.122% | 11.319% |
| 5 | 0 (certain) | 2.564% | 4.338% | 5.282% |
| 8 | 0 (certain) | 0.016% | 0.245% | 0.475% |
| 9 | 0 (certain) | 0 (certain) | 0.082% | 0.204% |
| Secret s | Chance of reading s (b = 0) | Chance of reading s (b = 1) |
|---|---|---|
| 0000 | 100.00% | 100.00% |
| 0001 | 100.00% | 100.00% |
| 1010 | 100.00% | 100.00% |
| 1111 | 100.00% | 100.00% |
| x | f(x) | x ⊕ s |
|---|---|---|
| 000 | 110 | 101 |
| 001 | 011 | 100 |
| 010 | 101 | 111 |
| 011 | 100 | 110 |
| 100 | 011 | 001 |
| 101 | 110 | 000 |
| 110 | 100 | 011 |
| 111 | 101 | 010 |
| y | y·s mod 2 | Chance |
|---|---|---|
| 000 | 0 | 25.0% |
| 001 | 1 | 0.0% |
| 010 | 0 | 25.0% |
| 011 | 1 | 0.0% |
| 100 | 1 | 0.0% |
| 101 | 0 | 25.0% |
| 110 | 1 | 0.0% |
| 111 | 0 | 25.0% |
| n | DJ classical (guaranteed) | BV classical (b = 0) | Simon classical (about 2^(n/2)) | Simon quantum (expected runs) |
|---|---|---|---|---|
| 4 | 9 | 4 | 4 | 4.48 |
| 8 | 129 | 8 | 16 | 8.60 |
| 16 | 32,769 | 16 | 256 | 16.61 |
| 24 | 8,388,609 | 24 | 4,096 | 24.61 |
| 32 | 2,147,483,649 | 32 | 65,536 | 32.61 |
Eight guides
What is a quantum oracle, and what does counting queries mean?
8 min read
An oracle is a black box that computes a function. Query complexity counts how often you must call it. That is the setting for all three algorithms in this lab.
Phase kickback: how an oracle writes its answer into signs
8 min read
Put the answer qubit in the minus state and the oracle stops changing it. Instead it flips the sign of every input where f is 1. That one trick powers all three algorithms.
Deutsch–Jozsa step by step: one query to tell constant from balanced
10 min read
Follow the amplitudes through the five steps, see why the chance of 00…0 is exactly 1 or 0 under the promise, and what happens when the promise is broken.
Bernstein–Vazirani step by step: reading a hidden string in one query
9 min read
The same circuit as Deutsch–Jozsa, applied to a different promise, returns a whole n-bit secret with certainty. See why the Hadamard layer is a decoder.
Simon’s algorithm step by step: finding a hidden period
11 min read
Each run of the circuit yields a random string orthogonal to the secret. Collect about n of them, solve a small linear system, and the secret appears. Classically this takes exponentially many queries.
Classical against quantum query counts: reading the scaling table
8 min read
How many oracle calls each problem needs as n grows, with the exact formulas used by the lab and the one place where an estimate replaces an exact count.
What a query advantage does and does not mean
9 min read
Deutsch–Jozsa looks exponential but a randomised classical algorithm solves it in a few queries. Here is the honest reading of all three results.
Running Deutsch–Jozsa and Bernstein–Vazirani in code
7 min read
Short Qiskit sketches of the circuits in this lab, labelled clearly as not run here, and a checklist for verifying them against the lab’s numbers.
Predict, then check
For three input bits and f = the parity of all bits, what is the chance of reading 000?
0%. The function is balanced, so the amplitude of 000 is the average of equal numbers of +1 and −1 signs. The simulator gives 0.0%.
A balanced function on 4 bits is queried at 3 distinct random inputs. How often do all three answers agree?
20.0% of the time. After 9 queries the chance is 0, because only 8 inputs can share a value.
Bernstein–Vazirani with s = 101101 and offset b = 1. What does one query return?
101101 with probability 100%. The offset only changes a global phase.
Simon with n = 3 and s = 110. Which readings y can appear?
Those with y·s = 0 mod 2: 000, 001, 110, 111. Each has probability 25%.
In Deutsch–Jozsa, how large is the chance of 0000 for a 4-bit function with exactly 4 ones among 16 inputs?
((12 − 4)/16)² = 25%. The function is neither constant nor balanced, so the promise is broken. The simulator gives 25.0%.
About how many runs does Simon’s algorithm need on average for n = 8?
About 8.60, to collect 7 independent equations. This page’s own arithmetic from the spanning-time sum.
Related on QuantumAtlas
Frequently asked questions
What is an oracle in quantum computing?
A black-box function you can call but not read. Algorithms are compared by how many calls they need. The three problems in this lab, Deutsch–Jozsa, Bernstein–Vazirani and Simon, are the classic oracle problems.
What does the Deutsch–Jozsa algorithm decide?
Whether a function on n bits is constant or balanced, given a promise that it is one or the other. One quantum query decides it. A classical algorithm needs 2^(n−1) + 1 queries to be certain.
Is Deutsch–Jozsa really exponentially faster?
Only against a classical algorithm that must be certain. A randomised classical algorithm that accepts a tiny error chance needs just a few queries, as the lab’s classical slider shows. The gap is real but not practical.
What is phase kickback?
With the answer qubit in the minus state, the oracle leaves it unchanged and flips the sign of every input where f is 1. The result is a sign pattern on the inputs that a Hadamard layer can read.
What does Bernstein–Vazirani find?
A hidden n-bit string s, from a function f(x) = s·x ⊕ b. One quantum query returns s with certainty. A classical algorithm needs about n queries.
Why does the offset b not matter to the quantum circuit?
It only multiplies the whole state by −1, a global phase that no measurement can detect.
What is Simon’s algorithm for?
Finding a hidden period s of a two-to-one function with f(x) = f(x ⊕ s). Each run gives a random string orthogonal to s. About n runs and a small linear solve recover s. Classically it needs about 2^(n/2) queries.
Why did Simon’s algorithm matter?
It gave an exponential gap against randomised classical algorithms, and its period-finding idea led to Shor’s algorithm.
Is the simulation exact?
Yes. The lab simulates the full state vector, up to 7 qubits for Deutsch–Jozsa, 9 for Bernstein–Vazirani and 10 for Simon, with no approximation. The random samples use a seeded generator, so they repeat.
Where can I build these circuits myself?
In the Circuit Builder, which has Deutsch–Jozsa and Bernstein–Vazirani examples, or in code using the Qiskit lesson.
Sources and further reading
- D. Deutsch, “Quantum theory, the Church–Turing principle and the universal quantum computer” (1985). Introduced the quantum computer as a physical model and posed the first problem where it differs from a classical one.
- D. Deutsch and R. Jozsa, “Rapid solution of problems by quantum computation” (1992). The constant-or-balanced problem and its quantum solution.
- R. Cleve, A. Ekert, C. Macchiavello and M. Mosca, “Quantum algorithms revisited” (1998). The compact one-query circuit that textbooks and this lab use, usually credited to this paper.
- E. Bernstein and U. Vazirani, “Quantum complexity theory” (1993, journal version 1997). The hidden-string problem and the first oracle separation of a quantum class from a classical one.
- D. Simon, “On the power of quantum computation” (1994). The hidden-period problem, an exponential query separation, and the idea that led to Shor’s algorithm.
- M. Nielsen and I. Chuang, Quantum Computation and Quantum Information, section 1.4. The standard textbook treatment of these three algorithms.
References were written from memory of the original works. Check the originals for exact citations.