To follow the interactive canvas steps in this tutorial, ensure the Quanifi NiFi container is started:

docker compose up -d

Check container health using docker compose ps. Then open https://localhost:8443/nifi in your browser (accept the self-signed TLS certificate) and log in with Username: admin, Password: quanifipassword. For full configuration details, see the Docker Quickstart Guide.

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.

|0⟩ q0 |0⟩ q1 |0⟩ q2 |0⟩ q3 |1⟩ anc H H H H H Inner Product Oracle f(x) = s · x (mod 2) H H H H M M M M |s⟩ = |1011⟩
Figure 3.1: Circuit diagram recovering 4-bit secret s = '1011' in 1 pass.

The Mathematical Mechanism:

  1. 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$$
  2. 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$!
  3. 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!
āš ļø Software Engineer Gotcha: Bit-Ordering (Endianness)

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:

Bernstein-Vazirani Canvas Pipeline
Figure 3.2: Bernstein-Vazirani pipeline on the NiFi canvas. ↔ Scroll horizontally to inspect processors • Click to enlarge

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 (default 1011), compiles the circuit to OpenQASM, executes simulation, and sets recovery verification attributes.
  • BV — Report (QuanifiReport): Writes the output verification card to reports/qrisp-bernstein-vazirani.html.

Executing the Pipeline:

  1. Make sure Docker is running (docker compose up -d).
  2. Log in to NiFi (https://localhost:8443/nifi).
  3. Navigate to Qrisp Components & Textbook Algorithms.
  4. Right-click BV — Trigger and select Run Once.
  5. In ~2 seconds, the FlowFile traverses through Qrisp Bernstein-Vazirani into BV — 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.
Hands-on Exercise 3.1

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:

  1. In the NiFi canvas (https://localhost:8443/nifi), stop Qrisp Bernstein-Vazirani.
  2. Configure it and set Secret Bitstring to the asymmetric, non-palindromic string 1100. Apply changes and restart the processor.
  3. Trigger the flow: right-click BV — Trigger and choose Run Once.
  4. Inspect the report card in reports/qrisp-bernstein-vazirani.html.
  5. 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!