We translate the teleportation protocol into executable Python code. Alice wants to send a qubit state $|\psi\rangle$ to Bob. Direct copying of the state is not possible due to the no-cloning theorem; instead, an entangled pair of qubits is consumed to transfer the information.To simulate this, we need a system with three qubits and two classical bits.Qubit 0 ($q_0$): The "payload" qubit containing the message $|\psi\rangle$.Qubit 1 ($q_1$): Alice's half of the entangled pair.Qubit 2 ($q_2$): Bob's half of the entangled pair.Classical Bits ($c_0, c_1$): To store the measurement results from Alice's side.Initializing the CircuitWe begin by setting up the quantum register and the classical register. In a Python environment using a standard SDK like Qiskit, we define these registers to hold our data. We also need to prepare $q_0$ in a distinct state. If we leave $q_0$ in the default ground state $|0\rangle$, verifying teleportation is difficult because the target is already initialized to $|0\rangle$.To make the test valid, we apply a rotation to $q_0$. For this example, we apply a Rotation-X gate to change the state to something specific, such as a localized probability amplitude.import numpy as np from qiskit import QuantumCircuit, Aer, execute # Define the circuit with 3 qubits and 2 classical bits circuit = QuantumCircuit(3, 2) # Step 0: Prepare the payload state |psi> on q0 # We apply a rotation so the state is not just |0> or |1> # This makes it easier to verify the transfer later circuit.rx(np.pi/3, 0) circuit.barrier() Creating the Entangled ResourceTeleportation requires a shared resource. Alice and Bob must share a Bell pair before the protocol begins. We generate this pair between $q_1$ (Alice) and $q_2$ (Bob). As learned in the previous chapter, we achieve this using a Hadamard gate followed by a CNOT gate.# Step 1: Create a Bell Pair between q1 and q2 circuit.h(1) circuit.cx(1, 2) circuit.barrier()At this stage, $q_1$ and $q_2$ are maximally entangled. Alice holds $q_0$ (the message) and $q_1$ (her resource). Bob holds $q_2$.Alice's OperationsAlice needs to entangle her message qubit ($q_0$) with her resource qubit ($q_1$). She performs a CNOT gate using the message as the control and the resource as the target. Immediately after, she applies a Hadamard gate to the message qubit.These operations change the basis of the qubits, preparing them for measurement. This effectively "spreads" the information of $|\psi\rangle$ across the three-qubit system.# Step 2: Alice applies CNOT and Hadamard circuit.cx(0, 1) circuit.h(0) circuit.barrier()Measurement and CommunicationAlice now measures her two qubits. The results are stored in classical bits. This action destroys the quantum state on Alice's side. However, due to the entanglement established earlier, the state of Bob's qubit ($q_2$) instantly collapses into one of four possible variations of the original message $|\psi\rangle$.# Step 3: Alice measures her qubits circuit.measure([0, 1], [0, 1]) circuit.barrier()The diagram below illustrates the flow of information through these gates. Note how the quantum information interacts locally at Alice's end before becoming classical data.digraph G { rankdir=LR; node [style=filled, fontname="Helvetica", shape=box]; subgraph cluster_alice { label = "Alice's Location"; style=filled; color="#e9ecef"; q0 [label="Payload (q0)", fillcolor="#a5d8ff"]; q1 [label="Resource (q1)", fillcolor="#a5d8ff"]; ops [label="CNOT + H", fillcolor="#bac8ff"]; measure [label="Measurement", fillcolor="#ffc9c9"]; } subgraph cluster_bob { label = "Bob's Location"; style=filled; color="#e9ecef"; q2 [label="Resource (q2)", fillcolor="#b2f2bb"]; correction [label="Correction Gates", fillcolor="#ffec99"]; final [label="Output State", fillcolor="#b2f2bb"]; } q0 -> ops; q1 -> ops; q2 -> correction [label="Entanglement Link", style=dashed, color="#868e96"]; ops -> measure; measure -> correction [label="Classical Bits (00, 01, 10, 11)", color="#495057"]; correction -> final; }The standard data flow in a teleportation circuit. The dashed line represents the initial quantum correlation, while the solid arrow represents the transmission of classical bits.Bob's Correction StepBob's qubit ($q_2$) now holds the message, but it might be flipped or phase-shifted depending on Alice's random measurement outcomes. Bob must apply "correction" gates based on the classical bits he receives.If the first classical bit is 1, Bob applies an X gate (bit flip).If the second classical bit is 1, Bob applies a Z gate (phase flip).In programming terms, we use conditional logic. The simulator checks the classical register value and applies the gate dynamically.# Step 4: Bob applies correction gates based on classical bits # If classical bit 0 is 1, apply Z circuit.z(2).c_if(circuit.clbits[0], 1) # If classical bit 1 is 1, apply X circuit.x(2).c_if(circuit.clbits[1], 1)The order of operations is mathematically specific. If both bits are 1, we apply $X$ then $Z$ (or $Z$ then $X$, though standard convention usually implies $ZX$). The logic ensures that whatever random transformation occurred during measurement is perfectly undone.Verifying the ResultTo prove teleportation was successful, we compare the state of Bob's qubit ($q_2$) at the end of the circuit with the initial state of Alice's qubit ($q_0$). Since we cannot "see" a quantum state without destroying it, simulation offers a unique advantage. We can use a statevector simulator to inspect the mathematical probability amplitudes directly.The following chart displays the probability distribution of the original payload versus the final teleported state. In a perfect simulation, they overlap completely.{"layout": {"title": {"text": "State Probability Comparison"}, "xaxis": {"title": "Basis State"}, "yaxis": {"title": "Probability"}, "barmode": "group", "plot_bgcolor": "#f8f9fa", "font": {"family": "Helvetica"}}, "data": [{"x": ["|0⟩", "|1⟩"], "y": [0.75, 0.25], "type": "bar", "name": "Original Payload (q0)", "marker": {"color": "#4dabf7"}}, {"x": ["|0⟩", "|1⟩"], "y": [0.75, 0.25], "type": "bar", "name": "Teleported State (q2)", "marker": {"color": "#69db7c"}}]}Comparison of probability amplitudes. The blue bars represent the initialization of Alice's qubit, and the green bars represent Bob's qubit after the protocol.Analyzing the OutputIf you run this experiment on a real quantum computer, you will not get the perfect distribution shown above in a single shot. You would need to run the circuit thousands of times (shots) and build a histogram of the results. This is due to quantum noise and gate errors.However, in our ideal Python simulation, you will observe that the mathematical vector describing $q_2$ is identical to the vector we initialized on $q_0$.$$ |\psi\rangle_{q2} = \alpha|0\rangle + \beta|1\rangle $$This confirms that the state has been transferred. Note that $q_0$ and $q_1$ collapsed into $|0\rangle$ or $|1\rangle$ during measurement. The information did not duplicate; it moved. This satisfies the no-cloning theorem while achieving information transport.Summary of ConstraintsWhen implementing this in production environments, keep these limitations in mind:Destructive Read: The original state on Alice's side is destroyed. You cannot use teleportation to back up data.Classical Speed Limit: Because Bob must wait for Alice's classical bits to know which correction gate to apply, the information transfer cannot happen faster than the speed of light. Teleportation is not instantaneous communication.Resource Cost: Every qubit teleported requires the consumption of one Bell pair and two bits of classical bandwidth.By mastering this circuit, you understand the primary mechanism for moving data in a quantum network. In the next section, we will look at "Superdense Coding," which uses the same resource to achieve the opposite effect: sending two classical bits using one qubit.