# A simple Quantum Teleportation exercise with Google Cirq

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:

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 `msg`), and represent this in the circuit we are drawing with the index `0`.

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 cirqmsg = 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 `X` gate and `Y` 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()circuit.append(ranq)print(circuit)0: ───X^0.97───Y^0.296───`

The variable below `original_message` 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, 4), "y: ", np.around(expected, 4),          "z: ", np.around(expected, 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:

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, `1` and `2`.
2. Applying a CNOT gate to `1` and `2`.
3. Applying a Hadamard gate to `1`.

Then, qubit `1` is given to Alice, and qubit `2` is given to Bob.

In the end, this qubit that Bob is holding, `1`, 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 `0`) with her Bell qubit (`1`). 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 `2` 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 `z` (which we call bit `0`) and in the base `x` (which we call bit `1`).

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 `2` in the circuit) becomes identical to whatever Alice reads (measures) in her processed message (the end of line `0` and `1` 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 `0` and `1`). 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 `0` 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, 4), "y: ", np.around(teleported, 4), "z: ", np.around(teleported, 4))x:  -0.7987 y:  -0.0951 z:  -0.5942`

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.

Curiee.com is a not-for-profit 501(c)(3) language-agnostic destination for software engineers and hackers to learn, play, and discuss quantum computing code.

Written by

Written by

## Morgan Val Sartre

#### A computer scientist. CTO of thispersondoesnotexist.com. 