Code exampleIntermediateQuantum protocols

Superdense Coding

Send two classical bits with one qubit of a shared Bell pair; all four messages decode without error.

For each message $b_1b_0$, Alice applies $X^{b_0}$ then $Z^{b_1}$ to $q_0$, and Bob decodes with CNOT and H. After decoding, $q_0$ holds bit $b_1$ and $q_1$ holds bit $b_0$, so the printed string ($q_1q_0$) appears reversed; we therefore reverse it with [::-1] to read $b_1b_0$. The example shows why attention to bit order matters when interpreting results.

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

Code

Qiskit
from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler

sampler = StatevectorSampler(seed=8)
for msg in ["00", "01", "10", "11"]:
    qc = QuantumCircuit(2)
    qc.h(0)
    qc.cx(0, 1)
    if msg[1] == "1":
        qc.x(0)
    if msg[0] == "1":
        qc.z(0)
    qc.cx(0, 1)
    qc.h(0)
    qc.measure_all()
    counts = sampler.run([qc], shots=100).result()[0].data.meas.get_counts()
    decoded = max(counts, key=counts.get)
    print("sent", msg, "-> counts", counts, "-> read as b1b0 =", decoded[::-1])

Line-by-line code explanation

LineExplanation
1-2Imports.
4A primitive with seed 8.
5Loop over the four messages.
6-8A shared Bell pair.
9-10X if b0 = 1.
11-12Z if b1 = 1.
13-14Bob's decoding: CNOT then H.
15Measurement.
16100 shots.
17The only outcome.
18Reverse the string to read b1b0.

Expected output (from an actual run)

sent 00 -> counts {'00': 100} -> read as b1b0 = 00
sent 01 -> counts {'10': 100} -> read as b1b0 = 01
sent 10 -> counts {'01': 100} -> read as b1b0 = 10
sent 11 -> counts {'11': 100} -> read as b1b0 = 11

The results are deterministic; seed=8.

Try it yourself

Related topics

References

  1. Charles H. Bennett; Stephen J. Wiesner (1992). Communication via one- and two-particle operators on Einstein-Podolsky-Rosen states. Phys. Rev. Lett. 69(20):2881–2884 doi:10.1103/PhysRevLett.69.2881
  2. IBM Quantum (2025). Qiskit SDK API documentation. IBM Quantum Documentation quantum.cloud.ibm.com