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:
- The empirical probability of each string = count ÷ total shots.
- 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])fromqiskit.resultdoes 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$.
- 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$ andk[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
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
| Line | Explanation |
|---|---|
| 1 | Import sine to compute the theoretical value. |
| 2-4 | Import the circuit, the simulator and the marginal distribution function. |
| 6 | A circuit with three qubits and three bits. |
| 7-8 | A Bell state on q0 and q1. |
| 9 | An RY(1) rotation on q2, independent of the rest. |
| 10 | Measure the three qubits. |
| 12-13 | Run 4000 shots with seed 21. |
| 14 | Total number of shots. |
| 15 | Convert the counts into sorted empirical probabilities. |
| 16-17 | Print the counts and probabilities. |
| 19 | Marginal distribution of bit 2 only. |
| 20 | Print it. |
| 21 | Empirical probability that q2 = 1. |
| 22 | Compare it with the theoretical value sin²(0.5). |
| 24 | Estimate ⟨Z1 Z0⟩: +1 if the two bits are equal and −1 if they differ. |
| 25 | Print 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
- IBM Quantum (2025). Qiskit SDK API documentation. IBM Quantum Documentation quantum.cloud.ibm.com
- Qiskit contributors (2025). Qiskit Aer documentation. GitHub Pages qiskit.github.io
