Module 2: The Deutsch-Jozsa Algorithm
Learn the quintessential quantum algorithm that proved quantum computers can solve specific computational problems exponentially faster than any classical algorithm.
1. The Problem for Software Engineers
Imagine you are testing a third-party microservice API that accepts an $n$-bit binary payload and returns a single boolean:
def evaluate_flag(x: int) -> bool:
# Black-box implementation
...
The provider promises that this service adheres to one of two behaviors:
- Constant: It returns the exact same value (always
0or always1) for every single possible input. - Balanced: It returns
0for exactly half of the possible inputs, and1for the other half.
| Computational Model | Worst-Case Queries Required | For $n=32$ bits |
|---|---|---|
| Classical Computing | $$\frac{2^n}{2} + 1 = 2^{n-1} + 1$$ | $$2,147,483,649\text{ calls}$$ |
| Quantum Computing (Deutsch-Jozsa) | $$1\text{ query}$$ | $$1\text{ call}$$ |
In classical computing, even if you test 100 inputs and all return 0, you cannot be certain the 101st input won't return 1. In the worst case, you must check more than half the search space.
The Deutsch-Jozsa algorithm determines whether the function is constant or balanced in exactly 1 single evaluation, regardless of whether $n=3$ or $n=1,000,000$!
2. How the Quantum Magic Works (Without Physics Jargon)
How can a single call know the global property of an entire function? The secret is a technique called Phase Kickback combined with Hadamard Interference.
Think of noise-canceling headphones. If two sound waves of equal volume meet in phase (+1 and +1), they amplify. If one wave is inverted (-1 and +1), they completely destroy each other to total silence (0). Deutsch-Jozsa encodes the function's output into the phase (positive or negative sign) of each possible input.
Step-by-Step Execution:
- Initialize in Superposition: All $n$ input qubits start in $\vert 0\rangle$ and pass through a Hadamard ($H$) gate, creating an equal superposition of all $2^n$ possible inputs simultaneously.
- The Ancilla Trick (Phase Kickback): A helper qubit (ancilla) is initialized to $\vert 1\rangle$ and flipped with $H$ into $\vert-\rangle = \frac{\vert 0\rangle - \vert 1\rangle}{\sqrt{2}}$. When the oracle gate $U_f$ evaluates $\vert x\rangle \vert y \oplus f(x)\rangle$, the value $f(x)$ is "kicked back" as a phase factor $(-1)^{f(x)}$ onto the input state!
- Inverse Hadamard Transform: A second layer of $H$ gates converts the phase differences back into measurable bit amplitudes.
-
The Decisive Readout Rule:
- If $f$ is Constant: Every input received the same phase. All paths constructively interfere onto $\vert 00\dots0\rangle$. You will measure all zeros with 100% certainty.
- If $f$ is Balanced: Exactly half the inputs received a $+1$ phase and half received a $-1$ phase. They completely cancel each other out at $\vert 00\dots0\rangle$! You will measure any non-zero bitstring.
3. Running Deutsch-Jozsa on Quanifi
In Quanifi, this entire workflow runs as an automated data pipeline inside NiFi 2.11:
Pipeline Components:
DJ — Trigger(GenerateFlowFile): Emits a trigger FlowFile to initiate the test run.Qrisp Deutsch-Jozsa(QrispDeutschJozsa): High-level quantum processor. It synthesizes the oracle, compiles the circuit into OpenQASM 2.0, executes the statevector simulation, and attaches verdict attributes.DJ — Report(QuanifiReport): Takes the completed FlowFile, generates a full visual HTML audit card with circuit diagram and probability distributions, and writes it toreports/qrisp-deutsch-jozsa.html.
Triggering the Flow:
- Make sure Docker is running (
docker compose up -d). - Open NiFi at https://localhost:8443/nifi and enter the
Qrisp Components & Textbook Algorithmsgroup. - Right-click on
DJ — Triggerand click Run Once (or start and immediately stop it). - Watch the FlowFile move across the queue into
Qrisp Deutsch-Jozsaand then intoDJ — Report. In ~2 seconds, all queues return to 0.
4. How to Read and Interpret the Results
After the flow finishes, open the generated HTML report file in your browser:
open reports/qrisp-deutsch-jozsa.html
Look at the resulting metadata attributes card:
| Attribute | Observed Value | Meaning for Engineers |
|---|---|---|
deutsch_jozsa.verdict |
balanced |
The algorithm's verified classification of the oracle function. |
deutsch_jozsa.is_constant |
false |
Boolean flag indicating if function was constant. |
sim.top_result |
100 |
The measured binary state. Because this is not 000, the function is provably balanced! |
sim.top_probability |
1.0000 |
Deterministic probability of measuring the top state. |
circuit.format |
qasm2 |
The circuit was exported in standard OpenQASM 2.0, ensuring framework portability. |
Testing a Constant Oracle
Right now, the processor is configured to test a balanced oracle. What happens when we point it at a constant function?
Your Task:
- In the NiFi canvas (https://localhost:8443/nifi), right-click on
Qrisp Deutsch-Jozsaand click Stop. - Right-click it again, select Configure, and switch to the PROPERTIES tab.
- Change the Function Type property from
balancedtoconstant_zero. Click Apply. - Right-click the processor and click Start (it should show a green play icon).
- Trigger a new run: right-click
DJ — Triggerand select Run Once. - Refresh
reports/qrisp-deutsch-jozsa.html. What is the newsim.top_resultanddeutsch_jozsa.verdict?
👉 Click to Reveal Exercise Solution & Explanation
Solution:
When you inspect the newly appended run card in reports/qrisp-deutsch-jozsa.html, you will see:
sim.top_result:000(all zeros).sim.top_probability:1.0000(100% of the 1024 shots measured000).deutsch_jozsa.verdict:constant.deutsch_jozsa.is_constant:true.
Why did this happen? In a constant function, $f(x) = 0$ for every input $x$. Because the oracle imparts no relative phase flips across the input states, the inverse Hadamard transform perfectly reconstructs the initial state $\vert 000\rangle$. If even a single shot had measured a non-zero state, we would know with mathematical certainty that the function is not constant.