Code exampleIntermediateQiskit labLesson 9

Analyzing Results

Turn counts into probabilities, extract a single-qubit marginal with marginal_counts, and estimate ⟨Z1Z0⟩ from samples, comparing with theory.

Execution outputs are Counts, i.e. a dictionary from strings to numbers. The usual analysis:

  1. The empirical probability of each string = count ÷ total shots.
  1. The Marginal distribution: to study one qubit we sum the counts over the values of the other qubits. The function marginal_counts(counts, indices=[2]) from qiskit.result does this and keeps only bit 2. In the example, $q_2$ is independent of the rest and prepared with $R_Y(1)$, so the theoretical value is $P(q_2=1)=\sin^2(0.5)\approx0.2298$, and the estimate from 4000 shots is close to it within the statistical error $\sqrt{p(1-p)/N}\approx0.0067$.
  1. Expectation Values: for a diagonal operator such as $Z_1Z_0$ we give each string the value $(-1)^{b_1\oplus b_0}$ and take the weighted mean. Because $q_0,q_1$ are in the Bell state $|\Phi^+\rangle$, $\langle Z_1Z_0\rangle=1$ exactly, and every sample confirms it. In the string $k$, k[2] is $q_0$ and k[1] is $q_1$ because of the little-endian ordering.

For non-diagonal operators (such as $X$) a change of basis is needed before measurement, or the StatevectorEstimator primitive can be used. When comparing with theoretical values it is better to compute the standard error of each estimate than to judge by eye. Execution here is on a classical simulator (AerSimulator or the Statevector primitives), not on a real quantum computer.

Requirements: pip install qiskit qiskit-aer. The code runs on your machine or in Colab, not inside WordPress.

Code

Qiskit
from math import sin
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator
from qiskit.result import marginal_counts

qc = QuantumCircuit(3, 3)
qc.h(0)
qc.cx(0, 1)
qc.ry(1.0, 2)
qc.measure(range(3), range(3))

sim = AerSimulator()
counts = sim.run(transpile(qc, sim), shots=4000, seed_simulator=21).result().get_counts()
shots = sum(counts.values())
probs = {k: v / shots for k, v in sorted(counts.items())}
print("counts:", dict(sorted(counts.items())))
print("probabilities:", probs)

q2 = marginal_counts(counts, indices=[2])
print("marginal q2:", q2)
p1 = q2.get("1", 0) / shots
print("P(q2=1) =", p1, "| theory sin^2(0.5) =", round(sin(0.5) ** 2, 4))

zz = sum((1 if k[1] == k[2] else -1) * v for k, v in counts.items()) / shots
print("<Z1 Z0> from counts =", zz)

Line-by-line code explanation

LineExplanation
1Import sine to compute the theoretical value.
2-4Import the circuit, the simulator and the marginal distribution function.
6A circuit with three qubits and three bits.
7-8A Bell state on q0 and q1.
9An RY(1) rotation on q2, independent of the rest.
10Measure the three qubits.
12-13Run 4000 shots with seed 21.
14Total number of shots.
15Convert the counts into sorted empirical probabilities.
16-17Print the counts and probabilities.
19Marginal distribution of bit 2 only.
20Print it.
21Empirical probability that q2 = 1.
22Compare it with the theoretical value sin²(0.5).
24Estimate ⟨Z1 Z0⟩: +1 if the two bits are equal and −1 if they differ.
25Print the estimate.

Expected output (from an actual run)

counts: {'000': 1539, '011': 1551, '100': 446, '111': 464}
probabilities: {'000': 0.38475, '011': 0.38775, '100': 0.1115, '111': 0.116}
marginal q2: {'0': 3090, '1': 910}
P(q2=1) = 0.2275 | theory sin^2(0.5) = 0.2298
<Z1 Z0> from counts = 1.0

Counts from AerSimulator with seed_simulator=21; the value ⟨Z1Z0⟩ = 1 is deterministic, while P(q2=1) fluctuates around 0.2298.

Related topics

References

  1. IBM Quantum (2025). Qiskit SDK API documentation. IBM Quantum Documentation quantum.cloud.ibm.com
  2. Qiskit contributors (2025). Qiskit Aer documentation. GitHub Pages qiskit.github.io