ProductBlogAlgorithmsAboutSign inSign up

Quick Example

Bernstein-Vazirani: Quantum Circuit Example

Find a hidden bitstring

What this example teaches

Recover the fixed hidden string 101.

Expected result

Input wires q0q1q2 read 101. Browser outcomes are 1010 and 1011, each with ideal probability 1/2; the last bit is an auxiliary wire.

Browser bit strings list physical wires q0 first, including auxiliary wires. Native classical-register strings can use another order; inspect the declared measurements.

These are ideal probabilities. Sampled frequencies fluctuate when more than one outcome is possible; they need not match percentages exactly. An ideal outcome with probability 100% has no finite-shot variation. Hardware noise is a separate effect.

Try the example

Open Run in Marqov →

The link opens the editor. Save existing work, then choose Examples → Bernstein-Vazirani → framework. Click Run, choose an available browser simulator, then click Run in browser. The link does not preselect or execute the example.

Scope and limitations

  • The oracle encodes the fixed three-bit string 101; auxiliary wires are not part of the recovered string.

These expected answers describe the fixed circuit. This page does not establish a completed hosted or hardware run. Read the user guide for this example.

Inspect and download the circuit

Inspect the same fixed circuit in five frameworks. Browser execution translates supported circuit definitions into OpenQASM; it does not run general Python. Outside Marqov, Qiskit, Cirq, and Marqov snippets need a separate simulator invocation. PennyLane invokes its QNode and stores sampled counts in result.

OpenQASM
// Bernstein-Vazirani — find hidden string s = 101
OPENQASM 2.0;
include "qelib1.inc";
qreg q[4];
creg c[3];

// Initialize output qubit in |->
x q[3];
h q[3];

// Apply Hadamard to input qubits
h q[0];
h q[1];
h q[2];

// Oracle for s = 101: CNOT where s has a 1
cx q[0],q[3];
cx q[2],q[3];

// Interference
h q[0];
h q[1];
h q[2];

// Measure input qubits — reveals s
measure q[0] -> c[0];
measure q[1] -> c[1];
measure q[2] -> c[2];
Download OpenQASM circuit
Qiskit
# Bernstein-Vazirani — find hidden string s = 101
from qiskit import QuantumCircuit

qc = QuantumCircuit(4, 3)

# Initialize output qubit in |->
qc.x(3)
qc.h(3)

# Hadamard on input qubits
qc.h([0, 1, 2])

# Oracle for s = 101: CNOT where s has a 1
qc.cx(0, 3)
qc.cx(2, 3)

# Interference
qc.h([0, 1, 2])

# Measure — reveals s
qc.measure([0, 1, 2], [0, 1, 2])
Download Qiskit circuit
Cirq
# Bernstein-Vazirani — find hidden string s = 101
import cirq

q = cirq.LineQubit.range(4)
circuit = cirq.Circuit([
    # Initialize output qubit in |->
    cirq.X(q[3]),
    cirq.H(q[3]),
    # Hadamard on input qubits
    cirq.H.on_each(q[0], q[1], q[2]),
    # Oracle for s = 101
    cirq.CNOT(q[0], q[3]),
    cirq.CNOT(q[2], q[3]),
    # Interference
    cirq.H.on_each(q[0], q[1], q[2]),
    # Measure — reveals s
    cirq.measure(q[0], q[1], q[2], key="result"),
])
Download Cirq circuit
PennyLane
# Bernstein-Vazirani — find hidden string s = 101
import pennylane as qml

dev = qml.device("default.qubit", wires=4, shots=1000)

@qml.qnode(dev)
def bernstein_vazirani():
    # Initialize output qubit in |->
    qml.PauliX(wires=3)
    qml.Hadamard(wires=3)
    # Hadamard on input qubits
    for i in range(3):
        qml.Hadamard(wires=i)
    # Oracle for s = 101
    qml.CNOT(wires=[0, 3])
    qml.CNOT(wires=[2, 3])
    # Interference
    for i in range(3):
        qml.Hadamard(wires=i)
    return qml.counts(wires=[0, 1, 2])

result = bernstein_vazirani()
Download PennyLane circuit
Marqov
# Bernstein-Vazirani — find hidden string s = 101
from marqov import Circuit

circuit = (
    Circuit()
    .x(3).h(3)             # Output qubit in |->
    .h(0).h(1).h(2)        # Input superposition
    .cnot(0, 3).cnot(2, 3) # Oracle for s = 101
    .h(0).h(1).h(2)        # Interference
)
Download Marqov circuit

Sources and provenance

These references explain the concept and gate conventions. The snippets originate in Marqov's internal example collection; an upstream code attribution has not been established.

Snapshot identity
Source revision
df5a80cd474144684bf69dbda237b28ce40cc48c
Example version
3f05fc4ef6376a5cd0554a36920f3aefe1c652fe6ede0de2caeaed1575ffed79

The source revision identifies the editor collection used for this snapshot. The example version identifies its five exact code variants; neither is evidence of a hosted run.

Go deeper

For a paper-based experiment with scientific comparisons and execution evidence, read Research Replications. Its published scope and availability are separate from this small teaching circuit.

Explore all twelve Quick Examples →