Skip to content

Algorithm lab · reviewed 2026-10-25

Bernstein–Vazirani s=110

Recover a three-bit secret in one oracle query and watch it appear on every shot.

  • Qiskit
  • OpenQASM 3

Recorded simulator example · open starter templates to create your own run. Open starter templates →

A modular teaching board with two glass paths, removable gates and two output tiles.
A small circuit. The whole experiment.
Qubits
4
Operations
13
Shots
4,096
  1. Hide

    Choose the secret and build the oracle from CNOT gates.

    oracle note
  2. Query

    Run one oracle query between two layers of Hadamard gates.

    Qiskit + OpenQASM
  3. Simulate

    Execute 4,096 seeded shots on local Aer.

    complete counts + trace
  4. Review

    Compare the measured string with the secret.

    reviewer note

Workflow question

What this sample is testing.

Hide the secret bitstring 110 in an oracle built from controlled-X gates, run the Bernstein–Vazirani circuit once, and verify that measuring the three input qubits returns the secret on every shot.

Route rationale

Local Qiskit Aer simulator, no provider token required

Secret 110 recovered on all 4,096 shots in a reviewer-safe result packet

Method

Prepare the ancilla q3 in the minus state and the three input qubits in superposition. The oracle applies a controlled-X from every input position where the secret has a 1: q1 and q2 for 110, read as q2 q1 q0. Hadamard gates on the inputs then turn the phase pattern into the secret string, and the three inputs are measured.

Run the generated Qiskit circuit on the local Qiskit Aer simulator for 4,096 shots with simulator and transpiler seed 2048. Store the circuit, source, complete counts, route note, and export manifest together.

How to read the result

The ideal circuit returns the secret exactly, so all 4,096 shots read 110. The same circuit with the secret changed to 101 was run on the same simulator and returned 101 for all 4,096 shots, which checks that the output follows the oracle rather than the circuit layout.

A classical method learns the string by querying the oracle on one-hot inputs, once per secret bit (three queries here); the quantum circuit uses one query. Bits are read as c2 c1 c0, so the leftmost digit belongs to q2.

Measured distribution

Complete seeded count table

4,096 shots

1104,096100.00%

Complete ideal-simulator distribution. Engine Qiskit Aer 0.17.0; simulator and transpiler seed 2048. This static public fixture is not a live provider run.

Proof boundary

  1. At three bits the classical task is trivial, so this sample illustrates the algorithm and is not evidence of a practical speedup.
  2. The result is from a noise-free ideal simulator. On hardware, noise would flip bits of the answer, and the number of controlled-X gates grows with the number of ones in the secret.
  3. This is a seeded local simulation, not a live provider job or hardware benchmark.

Generated representations

Inspect the source behind the counts.

The public code is a review fixture. Provider credentials, workspace members, billing context, and private notes stay excluded.

Qiskit circuit

Python
from qiskit import QuantumCircuit

qc = QuantumCircuit(4, 3)
qc.x(3)
qc.h([0, 1, 2, 3])
qc.cx(1, 3)
qc.cx(2, 3)
qc.h([0, 1, 2])
qc.measure([0, 1, 2], [0, 1, 2])

OpenQASM circuit

OpenQASM 3
OPENQASM 3;
include "stdgates.inc";
bit[3] c;
qubit[4] q;
x q[3];
h q[0];
h q[1];
h q[2];
h q[3];
cx q[1], q[3];
cx q[2], q[3];
h q[0];
h q[1];
h q[2];
c[0] = measure q[0];
c[1] = measure q[1];
c[2] = measure q[2];

Evidence manifest

JSON
{
  "workflowId": "QF-SAMPLE-BERNSTEIN-VAZIRANI-S110",
  "runner": "qiskit-aer",
  "artifacts": [
    "oracle-notes.md",
    "generated-qiskit.py",
    "generated-openqasm.qasm",
    "counts.json",
    "reviewer-note.md"
  ],
  "privateFieldsExcluded": [
    "providerCredentialId",
    "userEmail",
    "billingState",
    "adminNotes"
  ]
}

What the reviewer receives

  • oracle-notes.md
  • generated-qiskit.py
  • generated-openqasm.qasm
  • counts.json
  • reviewer-note.md

Explicitly excluded

  • providerCredentialId
  • userEmail
  • billingState
  • adminNotes

Questions this record answers

Workflow FAQ

What is the Bernstein–Vazirani problem?

An oracle computes f(x) = s · x (mod 2) for a hidden bitstring s, and the task is to recover s. The quantum circuit finds every bit of s with one oracle call.

Why does the output equal the secret on every shot?

In the ideal circuit the final Hadamard layer maps the register onto the secret string with probability one, so there is no spread over other strings.

How is this different from Deutsch–Jozsa?

Both use phase kickback and Hadamard layers, but Deutsch–Jozsa decides a yes-or-no property of the oracle, while Bernstein–Vazirani reads out a full hidden string.