Code exampleAdvancedQuantum protocols

Teleportation with a Dynamic Circuit (if_test)

Teleportation with mid-circuit measurement and classically-conditioned corrections via if_test, verified by undoing the preparation on Bob's qubit.

Dynamic Circuits allow a qubit to be measured mid-circuit and the result to be used later. In modern Qiskit, conditions are written with with qc.if_test((creg, value)): instead of c_if, which has been removed.

Alice measures $q_0$ and $q_1$ into two separate registers; Bob applies $X$ if $m_1=1$ and $Z$ if $m_0=1$. To verify, we apply the inverse of the state preparation, $R_Y(-\theta)R_Z(-\phi)$, to $q_2$; if teleportation is correct the state returns to $|0\rangle$ and the measurement is 0 in every shot. In the printed strings the registers appear separated by spaces in the order “bob m1 m0”, and Alice's four outcomes are roughly equally likely.

The protocol does not copy the state or send information faster than light, because the correction needs the two classical bits.

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

Code

Qiskit
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, transpile
from qiskit_aer import AerSimulator

theta, phi = 1.0472, 0.7854
q = QuantumRegister(3, "q")
m0 = ClassicalRegister(1, "m0")
m1 = ClassicalRegister(1, "m1")
res = ClassicalRegister(1, "bob")
qc = QuantumCircuit(q, m0, m1, res)

qc.ry(theta, 0)
qc.rz(phi, 0)
qc.h(1)
qc.cx(1, 2)
qc.cx(0, 1)
qc.h(0)
qc.measure(0, m0[0])
qc.measure(1, m1[0])
with qc.if_test((m1, 1)):
    qc.x(2)
with qc.if_test((m0, 1)):
    qc.z(2)

qc.rz(-phi, 2)
qc.ry(-theta, 2)
qc.measure(2, res[0])

sim = AerSimulator()
counts = sim.run(transpile(qc, sim), shots=1000, seed_simulator=3).result().get_counts()
print(counts)
bob_ones = sum(v for k, v in counts.items() if k.split()[0] == "1")
print("Bob measured 1 after undoing the preparation:", bob_ones, "times")

Line-by-line code explanation

LineExplanation
1-2Imports: the circuit, the registers, transpile and the simulator.
4The two angles θ and φ of the state being sent.
5A three-qubit quantum register: message, Alice, Bob.
6-8Three classical registers: Alice's two results and Bob's verification result.
9Build the circuit.
11-12Prepare the message state on q0.
13-14A Bell pair between q1 (Alice) and q2 (Bob).
15-16Alice's Bell-basis measurement: CNOT then H.
17-18Mid-circuit measurement.
19-20If m1 = 1, Bob applies X.
21-22If m0 = 1, Bob applies Z.
24-25Undo the preparation on q2 to verify.
26Measure q2: it must be 0.
28-29Run 1000 shots on Aer with seed 3.
30Print the counts (bob m1 m0).
31Count the shots in which Bob got 1.
32Print the count: zero means successful teleportation.

Expected output (from an actual run)

{'0 0 0': 227, '0 1 0': 271, '0 1 1': 246, '0 0 1': 256}
Bob measured 1 after undoing the preparation: 0 times

Counts from AerSimulator with seed_simulator=3; the distribution of Alice's results fluctuates around 250 per outcome, while Bob's result of 0 is deterministic.

Try it yourself

Related topics

References

  1. C. H. Bennett; G. Brassard; C. Crépeau; R. Jozsa; A. Peres; W. K. Wootters (1993). Teleporting an unknown quantum state via dual classical and Einstein-Podolsky-Rosen channels. Phys. Rev. Lett. 70(13):1895–1899 doi:10.1103/PhysRevLett.70.1895
  2. IBM Quantum (2025). Qiskit SDK API documentation. IBM Quantum Documentation quantum.cloud.ibm.com
  3. Qiskit contributors (2025). Qiskit Aer documentation. GitHub Pages qiskit.github.io