Image for post
Image for post
Looking at a brave new world. Illustration by Meghan Rodrigues.

A simple Quantum Teleportation exercise with Google Cirq

Morgan Val Sartre
Jan 3 · 4 min read

In 1993, a group of physicists discovered a type of quantum teleportation that would enable a quantum state to be instantly transported any distance without directly sending the actual qubit (Teleporting an unknown quantum state via dual classical and Einstein-Podolsky-Rosen channels, Phys. Rev. Lett. 70, 1895).

Quantum Teleportation is a process by which a quantum state of a particle (for example, a photon or an electron) can be transmitted by sending only two classical bits of information.

In quantum mechanics, the simplest quantum system is the single qubit:

Image for post
Image for post

The information in this qubit is encoded in the coefficients alpha and beta.

Let’s call this quantum state the message (represented by the variable ), and represent this in the circuit we are drawing with the index .

The teleportation is accomplished by pre-sharing an entangled state between the sender, say Alice, and the receiver, Bob. This entangled state allows the receiver to possess a qubit with the same state as the one held by the sender.

Creating Alice’s Message, which will be Teleported

First, we create an object for a qubit in Google Cirq:

import cirq
msg = cirq.LineQubit(0)
print('message is {0} of type {1}'.format(msg, type(msg)))
message is 0 of type <class 'cirq.devices.line_qubit.LineQubit'>

Let’s fill this object with a random qubit, by applying a gate and gate to it, and multiplying by a random float number:

import randomranX = random.random()
ranY = random.random()
ranq = cirq.X(msg)**ranX, cirq.Y(msg)**ranY

Let’s start a circuit and add this first qubit (the message) to it:

circuit = cirq.Circuit()
print(circuit)0: ───X^0.97───Y^0.296───

The variable below holds Alice's message, which will be compared to Bob's teleported message in the end:

import numpy as npsim = cirq.Simulator()
original_message = sim.simulate(circuit)
expected = cirq.bloch_vector_from_state_vector(original_message.final_state, 0)print("x: ", np.around(expected[0], 4), "y: ", np.around(expected[1], 4),
"z: ", np.around(expected[2], 4))
x: -0.7987 y: -0.0951 z: -0.5942

Creating the Entangled State that will Teleport Alice’s Message

To teleport Alice’s message, we use a special quantum state, called Bell Quantum State.

A Bell Quantum State is composed of two entangled qubits, which are pre-shared between Alice and Bob:

Image for post
Image for post

Remember that the first number in |00> or |11> is the state of the first qubit (Alice’s), and the second number is the state of the second qubit (Bob’s).

We can build this well-defined state by:

  1. Creating two random qubits, and .
  2. Applying a CNOT gate to and .
  3. Applying a Hadamard gate to .

Then, qubit is given to Alice, and qubit is given to Bob.

In the end, this qubit that Bob is holding, , will be the medium where Alice's message will be teleported; like a white sheet of paper waiting to receive ink from a printer.

alice, bob = cirq.LineQubit.range(1, 3)circuit.append([cirq.H(alice), cirq.CNOT(alice, bob)])print(circuit)0: ───X^0.97───Y^0.296───1: ───H────────@─────────

2: ────────────X─────────

Entangling Alice’s message

Now, Alice processes her message (qubit ) with her Bell qubit (). This will entangle her message with her Bell qubit, consequently entangling it to Bob's qubit.

Note that Bob is just sitting there with his Bell qubit, waiting for the magic to happen (line in the circuit).

circuit.append([cirq.CNOT(msg, alice), cirq.H(msg)])print(circuit)0: ───X^0.97───Y^0.296───@───H───

1: ───H────────@─────────X───────

2: ────────────X─────────────────

Alice reads her processed Message

Alice now measures her processed message. The measurement is called Bell measurement:

circuit.append(cirq.measure(msg, alice))print(circuit)0: ───X^0.97───Y^0.296───@───H───M───
│ │
1: ───H────────@─────────X───────M───

2: ────────────X─────────────────────

Alice takes note of the classical outcomes of her measurement, in the base (which we call bit ) and in the base (which we call bit ).

Note that at the moment she makes these two measurements, her (entangled) message is teleported to Bob’s qubit. In quantum mechanics, when you observe a state, you collapse the state to the value that was measured.

That’s the quantum effect! Bob’s entangled qubit (line in the circuit) becomes identical to whatever Alice reads (measures) in her processed message (the end of line and in the circuit).

Bob measures his qubit and confirms that the state was teleported

Bob is ready to measure the teleported state. In order to read the state, he only needs to know the two (classical) bits of information that Alice wrote down from her measurement (bits and ). He can get this information through any classical channel (like a text message).

Bob can now measure his qubit, which prints (or, collapse to) Alice’s original random message (at the beginning of the line in the circuit):

circuit.append([cirq.CNOT(alice, bob), cirq.CZ(msg, bob)])final_results = sim.simulate(circuit)teleported = cirq.bloch_vector_from_state_vector(final_results.final_state, 2)print("x: ", np.around(teleported[0], 4), "y: ", np.around(teleported[1], 4), "z: ", np.around(teleported[2], 4))x:  -0.7987 y:  -0.0951 z:  -0.5942

Voilà! Bob has a qubit that is identical to Alice’s, even though it was never sent to him!

Thank you for reading!

The source code (Juptyer notebooks) for this tutorial is available here. If you enjoyed what you read, check out “How quantum teleportation works” by Andy Matuschak and Michael Nielsen. is a not-for-profit 501(c)(3) language-agnostic destination for software engineers and hackers to learn, play, and discuss quantum computing code.

You can follow us on Twitter, Medium, Instagram, or GitHub.

Quantum Computing for the Utopian

Articles on quantum computing, machine learning, software…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store