DeepPractise
DeepPractise

Bernstein–Vazirani Algorithm

Track: Quantum Algorithms · Difficulty: Intermediate · Est: 14 min

Bernstein–Vazirani Algorithm

Overview

Bernstein–Vazirani (BV) is a “hidden string” algorithm.

You are given oracle access to a function

fs(x)=sx    (mod 2),f_s(x) = s\cdot x \;\;\text{(mod 2)},

where:

  • x{0,1}nx\in\{0,1\}^n is your query,
  • s{0,1}ns\in\{0,1\}^n is a fixed hidden bitstring,
  • sxs\cdot x means the bitwise dot product mod 2 (XOR of the ANDs).

Goal: recover ss.

Why it’s interesting:

  • Classically, you typically need nn queries to learn all bits of ss.
  • Quantumly, BV finds ss with one oracle query, using phase kickback and interference.

Intuition

In the oracle model, the function output is one bit. So it seems like you only learn one bit per query.

BV bypasses that “one-bit bottleneck” by using phase kickback:

  • the oracle imprints a structured phase pattern (1)fs(x)(-1)^{f_s(x)} across a superposition of all xx.
  • a final layer of Hadamards converts that phase pattern into the computational basis state s|s\rangle.

So the algorithm extracts the entire hidden string in a single query because the oracle’s phase pattern is globally structured.

Algorithm Structure

High-level steps:

  1. Prepare nn input qubits in 0n|0\rangle^{\otimes n} and a target qubit in 1|1\rangle.
  2. Apply H to all qubits to create a uniform superposition on the input and | - \rangle on the target.
  3. Query the oracle once.
  4. Apply H to the input register.
  5. Measure the input register; the result is ss.

Formal Description

Oracle

Use the standard reversible oracle:

Ufsxy=xyfs(x).U_{f_s}|x\rangle|y\rangle = |x\rangle|y\oplus f_s(x)\rangle.

As in Deutsch–Jozsa, preparing the target as | - \rangle turns the XOR into a phase:

x(1)fs(x)x.|x\rangle| - \rangle \mapsto (-1)^{f_s(x)}|x\rangle| - \rangle.

So after the oracle, the input register is

12nx(1)sxx.\frac{1}{\sqrt{2^n}}\sum_x (-1)^{s\cdot x}|x\rangle.

Why the final Hadamards reveal ss

Hadamards transform phase patterns into basis states. In BV, the particular phase pattern (1)sx(-1)^{s\cdot x} is exactly the one that maps to s|s\rangle after HnH^{\otimes n}.

Conceptual takeaway:

  • the hidden string is not stored in probabilities yet;
  • it is stored in the phase relationship across all xx;
  • the final mixing step turns that phase into a sharp measurement outcome.

Worked Example

Let n=3n=3 and hidden string s=101s=101. So

fs(x1x2x3)=x1x3.f_s(x_1x_2x_3) = x_1 \oplus x_3.

After the oracle (with phase kickback), each basis state x|x\rangle gets a sign of (1)x1x3(-1)^{x_1\oplus x_3}:

  • if x1x3=0x_1\oplus x_3=0, the sign is +1
  • if x1x3=1x_1\oplus x_3=1, the sign is −1

This sign pattern is globally consistent with the hidden string. After applying H3H^{\otimes 3}, the input register collapses to the basis state 101|101\rangle. Measuring yields 101 with certainty in the ideal model.

Turtle Tip

Turtle Tip

BV is “phase kickback + one final interference step.” The oracle writes ss into phase; Hadamards read it out as a basis state.

Common Pitfalls

Common Pitfalls
  • Don’t interpret BV as violating information limits. The oracle is a structured black box, and the algorithm exploits that structure.
  • Don’t confuse sxs\cdot x with ordinary real-number dot product. Here it’s mod 2: XOR of bitwise ANDs.

Quick Check

Quick Check
  1. What is the problem BV solves in one sentence?
  2. Where is the hidden string stored right after the oracle call: in probabilities or in phase?

What’s Next

BV shows how phase can encode a hidden linear structure. Next we study Simon’s algorithm, which is an early example of period-finding: the oracle hides a secret XOR-period ss, and measurement reveals constraints that let you reconstruct it.