GHZ State and Reduced Purity
Prepare a 3-qubit GHZ state, verify that one qubit's reduced state is maximally mixed (purity 1/2), then sample it.
The Purity $\mathrm{Tr}(\rho^2)$ equals 1 for a pure state and $1/2$ for a maximally mixed qubit. Using partial_trace(sv, [1, 2]) we trace out qubits 1 and 2 to get the density matrix of $q_0$: $\rho_0=I/2$. The whole system is in a pure state, but each individual part is mixed; this is the signature of entanglement in pure states.
In the samples only 000 and 111 appear, each about half the time.
Requirements: pip install qiskit qiskit-aer. The code runs on your machine or in Colab, not inside WordPress.
Code
from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler
from qiskit.quantum_info import Statevector, partial_trace
n = 3
qc = QuantumCircuit(n)
qc.h(0)
for i in range(n - 1):
qc.cx(i, i + 1)
sv = Statevector.from_instruction(qc)
print({str(k): round(float(v), 3) for k, v in sv.probabilities_dict().items()})
rho0 = partial_trace(sv, [1, 2])
print("purity of qubit 0:", round(float(rho0.purity().real), 3))
qc.measure_all()
counts = StatevectorSampler(seed=12).run([qc], shots=1000).result()[0].data.meas.get_counts()
print(counts)
Line-by-line code explanation
| Line | Explanation |
|---|---|
| 1-3 | Import the circuit, the primitive, Statevector and the partial trace function. |
| 5-6 | A three-qubit circuit. |
| 7 | Hadamard on q0. |
| 8-9 | A chain of CNOTs spreads the superposition: (|000⟩+|111⟩)/√2. |
| 11 | The exact state vector. |
| 12 | Print the probabilities. |
| 13 | Trace out qubits 1 and 2 to get the density matrix of q0. |
| 14 | Print the purity of q0: 0.5 means maximally mixed. |
| 16 | Add measurement. |
| 17 | Take 1000 samples with seed 12. |
| 18 | Print the counts. |
Expected output (from an actual run)
{'000': 0.5, '111': 0.5}
purity of qubit 0: 0.5
{'000': 503, '111': 497}
Counts from StatevectorSampler(seed=12); the probabilities and purity are deterministic.
Try it yourself
Related topics
References
- Daniel M. Greenberger; Michael A. Horne; Anton Zeilinger (1989). Going Beyond Bell's Theorem. Bell's Theorem, Quantum Theory and Conceptions of the Universe, Springer (Kluwer), pp. 69–72 doi:10.1007/978-94-017-0849-4_10
- Michael A. Nielsen; Isaac L. Chuang (2010). Quantum Computation and Quantum Information (10th Anniversary Edition). Cambridge University Press doi:10.1017/CBO9780511976667The standard reference for quantum computing; the Crossref record gives 2012 for the Cambridge Core online edition.
- IBM Quantum (2025). Qiskit SDK API documentation. IBM Quantum Documentation quantum.cloud.ibm.com
