|q⟩ Bad Qubits

intermediate · Programming · Noise, Decoherence & Density-Matrix Simulation

Simulating Noisy Circuits

Every real quantum processor is coupled to its environment. Gates are imperfect, qubits leak energy, and stray fields shift phases. A noise model is a mathematical prescription that augments an ideal circuit with probabilistic error operations so that simulated statistics match measured hardware data.

From ideal to noisy: the insertion picture

The simplest simulation strategy is error insertion: after each ideal gate draw from the noise distribution and, if an error occurs, append the corresponding error gate. For a bit-flip channel with error probability pp this means appending XX with probability pp and doing nothing with probability 1p1-p.

Concretely, one simulation trajectory (or "shot") proceeds as:

  1. Initialise all qubits to 0|0\rangle.
  2. Apply each ideal gate in order.
  3. After each gate, sample whether an error occurred and, if so, insert the error gate.
  4. Measure and record the outcome bit-string.

Repeating for many shots produces a sampled distribution whose histogram approximates the true noisy output probabilities.

A worked example: bit-flip noise on a Bell-adjacent circuit

Consider a two-qubit circuit whose ideal action is

ψideal=HI00=+0=12(00+10).|\psi_\text{ideal}\rangle = H \otimes I \, |00\rangle = |{+}\rangle|0\rangle = \tfrac{1}{\sqrt{2}}\bigl(|00\rangle + |10\rangle\bigr).

Measuring this ideal state yields the outcomes 0000 and 1010 each with probability 12\tfrac{1}{2}.

Now suppose qubit 1 suffers a certain bit-flip error (p=1p = 1) after the gates. Inserting the Pauli XX on qubit 1 gives the noisy state

ψnoisy=IXψideal=12(01+11).|\psi_\text{noisy}\rangle = I \otimes X \, |\psi_\text{ideal}\rangle = \tfrac{1}{\sqrt{2}}\bigl(|01\rangle + |11\rangle\bigr).

The measurement distribution shifts from {00,10}\{00, 10\} to {01,11}\{01, 11\}, each still with probability 12\tfrac{1}{2}. The error is detectable: a classically expected distribution has no weight on 0101 or 1111, so any observation in those bins signals a bit-flip on qubit 1.

Reading the sampled histogram

After running the noisy circuit for many shots the histogram shows:

| Outcome | Ideal probability | Noisy probability (p=1p=1) | |---------|-------------------|---------------------------| | 0000 | 12\tfrac{1}{2} | 00 | | 0101 | 00 | 12\tfrac{1}{2} | | 1010 | 12\tfrac{1}{2} | 00 | | 1111 | 00 | 12\tfrac{1}{2} |

The error has shifted every outcome bit-string by flipping qubit 1. In a probabilistic noise model (0<p<10 < p < 1) all four bars would be non-zero, and a syndrome measurement (not a destructive readout) could diagnose the error without collapsing the encoded information.

From Kraus operators to circuit insertion

The bit-flip channel studied in an earlier lesson has Kraus operators

K0=1pI,K1=pX.K_0 = \sqrt{1-p}\,I, \qquad K_1 = \sqrt{p}\,X.

Each shot samples one of these operators: with probability 1p1-p the identity acts (no error), and with probability pp the Pauli XX acts (bit flip). The circuit-insertion picture is a direct Monte Carlo realisation of this Kraus decomposition.

For more complex noise models — such as depolarizing or amplitude-damping — the same principle applies: each Kraus operator is mapped to a circuit fragment, and the stochastic choice between fragments is sampled at run time.

Try it

Build the two-qubit noisy circuit described above. Apply HH to qubit 0, then inject a certain bit-flip error on qubit 1 by applying XX to qubit 1, then measure. The grader checks that the output distribution places equal weight on 01|01\rangle and 11|11\rangle.

Run your code to see the quantum state.

Open the Probabilities tab: the bars for 0000 and 1010 should be absent, and the bars for 0101 and 1111 should each reach 0.50.5. That is the signature of a certain bit-flip error on qubit 1.

Sign in on the full site to ask questions and join the discussion.