The Adventures of Alice and Bob

Coding Quantum Teleportation and Superdense coding in Qiskit

Madeline Farina
QubitCo
6 min readOct 21, 2020

--

Is teleportation possible? In the quantum world, yes! And while it may sound like a futuristic (even fictitious) concept only comprehensible to geniuses like Einstein or Newton, in the field of quantum mechanics it’s actually a fairly straightforward idea with practical applications in the world of quantum computing. In the most basic terms, it involves passing encoded information from one qubit to another.

Officially, quantum teleportation is defined as the state of qubit (|𝜓⟩) is being transmitted from one location to another, using two bits of classical communication and a Bell pair. In other words, it is a protocol that destroys the quantum state of a qubit in one location and recreates it on a qubit at a distant location, with the help of shared entanglement.

So imagine two people, Alice and Bob. Alice wants to send quantum information to Bob without having an eavesdropper access the information in any way (for those wondering, these are the standard names chosen because of their alphabetical nature). Specifically, Alice wants to send the qubit state |𝜓⟩=𝛼|0⟩+𝛽|1⟩. This entails passing on information about 𝛼 and 𝛽 to Bob.

Unfortunately, Alice cannot simply make a copy of her qubit and send it to Bob because of the no-cloning theorem: a theorem in quantum mechanics which states that you cannot simply make an exact copy of an unknown quantum state. Because of this, Alice cannot just generate a copy of |𝜓⟩ to give to Bob. Only classical states can be copied, not superpositions.

However, by taking advantage of two classical bits and an entangled qubit pair, Alice can transfer her state |𝜓⟩ to Bob. This is teleportation because, at the end, Bob will have |𝜓⟩ and Alice won’t anymore.

The Quantum Teleportation Protocol

Now that the conceptual explanation has been provided, let’s actually code in Qiskit. I included the necessary installation code below but for any beginners, I suggest first going to my post Coding single qubit circuits in Qiskit to get all the installation and set up code for a Jupyter Notebook.

import numpy as np
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute, BasicAer, IBMQ
from qiskit.visualization import plot_histogram, plot_bloch_multivector
from qiskit.extensions import Initialize
from qiskit_textbook.tools import random_state, array_to_latex

(I suggest just copying, pasting, and running the above code block before proceeding below)

The setup for the teleportation protocol is as follows, using 3 qubits and 2 classical bits in 2 different registers:

# Creates quantum register with 3 qubits
qr = QuantumRegister(3, name="q")
# creates first classical bit and puts it in one classical register
crz = ClassicalRegister(1, name="crz")
# creates second classical bit and puts it in another classical register
crx = ClassicalRegister(1, name="crx")
# creates a quantum circuit called 'teleport'
teleport = QuantumCircuit(qr, crz, crx)
# creates a state psi at random
psi = random_state(1)
# Displays it nicely
array_to_latex(psi, pretext="|\\psi\\rangle =")
plot_bloch_multivector(psi)

Output:

So here we can see the random state psi in an array and plotted on the Bloch sphere. The next step is to initialize the 0 bit with the original state, then create the Bell state Phi on bits 1 and 2:

#initializes the 0 bit with the original state (psi)teleport.initialize(psi, 0) #from now on Alice is in control of this bit
teleport.barrier()
# creates the Bell state Phi on bits 1 and 2:teleport.h(1) # Add a Hadamard gate on qubit 1
teleport.cx(1,2) # Add CNOT with the 1 bit as the control bit and the 2 bit as the target bit
# From now on Alice is in control of bit 1
# From now on Bob is in control of bit 2.
teleport.barrier()
teleport.draw('mpl')

Output:

The next step involves Alice adding her own gates and manipulating her two bits, then performing measurements on the 0th qubit in the crz classical register and 1st qubit in the crx classical register.

# Alice adds her own gates and manipulates her two bits:teleport.cx(0,1)
teleport.h(0)
teleport.barrier()
teleport.measure(0,crz)
teleport.measure(1,crx)
teleport.barrier()teleport.draw('mpl')

Output:

Lastly, Bob applies the unitary transformations on his own qubit depending on the outcomes from Alice’s measurements (i.e. what the two classical bits are).

teleport.x(2).c_if(crx, 1) # Apply gates if the registers
teleport.z(2).c_if(crz, 1) # are in the state '1'
teleport.barrier()
teleport.draw('mpl')

Which will give the final circuit:

And to see the results on the Bloch sphere…

backend = BasicAer.get_backend('statevector_simulator')
out_vector = execute(teleport, backend).result().get_statevector()
plot_bloch_multivector(out_vector)

Output:

And you’re done! With this code you have successfully constructed a quantum teleportation circuit.

Superdense Coding

This concept is similar to quantum teleportation and can be easily confused, which is why I discuss it. Superdense coding is a procedure that allows someone to send two classical bits to another party using just a single qubit of communication. The superdense coding protocol can be thought of as a flipped version of the teleportation protocol, in the sense that Alice and Bob merely “swap their tools of transmission.”

Teleportation vs. Superdense coding

Superdense Coding Protocol

Initially Alice has one of the qubits from the Bell state |Φ+⟩. Then she applies 𝑋 and/or 𝑍 gates depending on her intended message (see the chart below).

Bob’s decoding process is as follows:

The code for the protocol is below, with comments for each step and the final circuit drawn.

## SETUP
# The protocol uses 2 qubits and 2 classical bits
# qubit 0: originally belongs to Alice, and she then passes it along to Bob
# qubit 1: belongs to Bob
# classical bits 0 and 1: used by Bob to decipher Alice's message
# Task: initialize a quantum circuit with 2 quantum bits and 2 classical bitsqc = QuantumCircuit(2, 2)
## STEP 1# Telamon creates the Bell state Phi+ on qubits 0 and 1:
# Task: create the state Phi+ on the qubits
qc.h(0) # Add a Hadamard gate on qubit 0
qc.cx(0,1) # Add CNOT with the 0 bit as the control bit and the 1 bit as the target bit
# Now Alice is in control of qubit 1
# Now Bob is in control of qubit 2.
qc.barrier()
## STEP 2
# Alice creates and enciodes her message
# if her message is '00' do nothing
# if her message is '01' apply a Z gate
# if her message is '10' apply a X gate
# if her message is '11' apply both Z and X gates
# Let's say the message is 11qc.z(0) # Z-gate
qc.x(0) # X-gate
qc.barrier()
## STEP 3# Alice turns over her qubit to Bob
# Nothing to do in the circuit so we just put a barrier
qc.barrier()
# STEP 4
# Bob applies CNOT and H transformations on the qubits
qc.cx(0,1)
qc.h(0)
qc.barrier()# STEP 5
# Bob measures the qubits and deciphers Alice's message
qc.measure(0,0)
qc.measure(1,1)
qc.draw('mpl')
backend = Aer.get_backend('qasm_simulator')
job_sim = execute(qc , backend, shots=1024)
sim_result = job_sim.result()
measurement_result = sim_result.get_counts(qc)print(measurement_result)
plot_histogram(measurement_result)
Results

And you’re done!

Sources

For any additional questions, the documentation for Qiskit can be found here here, and the official Qiskit textbook can be found here, which is what I regularly referenced.

--

--

Madeline Farina
QubitCo

Quantum Physics, InfoSec, and general scientific nonsense