Module 3: The Bernstein-Vazirani Algorithm
Learn how quantum parallelism and the Walsh-Hadamard transform let you crack an $n$-bit secret bitmask in a single query ($O(1)$) instead of querying every single bit classically.
1. The Problem: Finding a Hidden Bitmask
Consider a black-box oracle function that calculates the binary inner product between an input bitstring $x$ and an unknown secret bitmask $s \in \{0,1\}^n$:
$$f(x) = s \cdot x \pmod 2 = (s_0 x_0 \oplus s_1 x_1 \oplus \dots \oplus s_{n-1} x_{n-1})$$In classical software, how do you find $s$?
- To find bit $s_0$, you query with $x = 1000\dots$
- To find bit $s_1$, you query with $x = 0100\dots$
- To find bit $s_i$, you query with the $i$-th unit basis vector.
Therefore, finding an $n$-bit secret classically requires $n$ distinct queries. If the secret has 64 bits, you must query 64 times.
With the Bernstein-Vazirani algorithm, quantum computing extracts all $n$ bits of the secret string $s$ in exactly 1 single evaluation!
2. How It Works: Direct Fourier Reconstruction
Bernstein-Vazirani uses the exact same circuit topology as Deutsch-Jozsa, but the mathematical interpretation is even more striking.
The Mathematical Mechanism:
- Phase Encoding: When the oracle evaluates in superposition with ancilla $\vert-\rangle$, phase kickback assigns each basis state $\vert x\rangle$ a phase $(-1)^{s \cdot x}$: $$\vert\psi_1\rangle = \frac{1}{\sqrt{2^n}} \sum_{x \in \{0,1\}^n} (-1)^{s \cdot x} \vert x\rangle$$
- The Hadamard Property: A remarkable property of the Hadamard transform on an $n$-qubit register is: $$H^{\otimes n} \vert s\rangle = \frac{1}{\sqrt{2^n}} \sum_{x \in \{0,1\}^n} (-1)^{s \cdot x} \vert x\rangle$$ Notice that this is identical to $\vert\psi_1\rangle$!
- Inversion: Because $H$ is its own inverse ($H \cdot H = I$), applying $H^{\otimes n}$ to $\vert\psi_1\rangle$ transforms the state directly into: $$H^{\otimes n} \vert\psi_1\rangle = \vert s\rangle$$ All $2^n - 1$ wrong bitstrings destructively interfere to exactly $0$, while the secret bitstring $\vert s\rangle$ constructively interferes to 100% probability!
In classical computing, we deal with Big-Endian vs Little-Endian bytes. In quantum computing, SDKs vary dramatically:
Qiskit defaults to Little-Endian (qubit $q_0$ is the rightmost least-significant bit).
Qrisp, Cirq, and OpenQASM 2.0 default to Big-Endian (qubit $q_0$ is the leftmost most-significant bit, q0_left).
Quanifi standardizes this across frameworks using the attribute sim.bit_order=q0_left.
3. Running Bernstein-Vazirani in Quanifi
In Quanifi, the Bernstein-Vazirani pipeline is ready on the canvas in NiFi 2.11:
Pipeline Nodes:
BV ā Trigger(GenerateFlowFile): Triggers execution of the pipeline.Qrisp Bernstein-Vazirani(QrispBernsteinVazirani): Builds the inner-product phase oracle for the configured secret string (default1011), compiles the circuit to OpenQASM, executes simulation, and sets recovery verification attributes.BV ā Report(QuanifiReport): Writes the output verification card toreports/qrisp-bernstein-vazirani.html.
Executing the Pipeline:
- Make sure Docker is running (
docker compose up -d). - Log in to NiFi (https://localhost:8443/nifi).
- Navigate to
Qrisp Components & Textbook Algorithms. - Right-click
BV ā Triggerand select Run Once. - In ~2 seconds, the FlowFile traverses through
Qrisp Bernstein-VaziraniintoBV ā Report.
4. Reading and Interpreting the Output
Open the generated report in your browser:
open reports/qrisp-bernstein-vazirani.html
| Attribute | Sample Value | Interpretation |
|---|---|---|
bernstein_vazirani.secret_string |
1011 |
The ground truth secret string configured in the oracle. |
bernstein_vazirani.recovered_string |
1011 |
The string measured from the quantum circuit. |
bernstein_vazirani.success |
true |
Automated assertion verifying exact parity between target and measurement. |
sim.top_probability |
1.0000 |
1024 out of 1024 measurement shots measured the secret string. |
sim.bit_order |
q0_left |
Canonical MSB-first bit-ordering confirmed. |
The Non-Palindromic String Test
A classic bug in quantum software engineering is silent bit-reversal (e.g. treating q0 as LSB instead of MSB). Notice that palindromic strings like 101 or 1001 read the same forwards and backwards, masking bit-reversal bugs!
Your Task:
- In the NiFi canvas (https://localhost:8443/nifi), stop
Qrisp Bernstein-Vazirani. - Configure it and set Secret Bitstring to the asymmetric, non-palindromic string
1100. Apply changes and restart the processor. - Trigger the flow: right-click
BV ā Triggerand choose Run Once. - Inspect the report card in
reports/qrisp-bernstein-vazirani.html. - If there were a bit-reversal bug, what string would have been measured instead of
1100?
š Click to Reveal Exercise Solution & Explanation
Solution:
Upon checking the new report card in reports/qrisp-bernstein-vazirani.html:
sim.top_result:1100.bernstein_vazirani.recovered_string:1100.bernstein_vazirani.success:true.
Bit-Reversal Diagnosis:
If Quanifi or Qrisp had a bit-reversal bug, the output would have measured 0011 (the reversed string). Because Quanifi enforces canonical q0_left MSB-first ordering throughout the wire contracts, non-palindromic inputs pass with 100% fidelity. Always use non-palindromic test vectors when testing quantum software!