Quantum Teleportation in Q#

[Note: This is the second post in the series 'A Beginner's Guide to Quantum Computing' and assumes you've read the first post here.]

First things first, quantum teleportation isn’t going to get my grandma in Yorkshire to my flat in London, although that would be nice. Now, she’s heard I like this thing called ‘quantum’ and has decided that instead of sending me money and socks for my birthday, she wants to send me a quantum state instead.

But unfortunately, she can’t just email me this qubit or pop it in the post, no. She needs a quantum channel to send it through, and Yorkshire only just got internet, so chances are slim they’ve got one of those.

If my grandma has no means to send quantum information, then maybe she can communicate it classically instead – for example over the internet, or over the phone. The trouble is, a quantum state is continuous, so she’d need to tell me an infinite number of classical bits to describe the state. My grandma is a busy lady, she doesn’t have time for that.

Just when she was beginning to lose all hope, I say it’s okay grandma. Remember when you came round for tea last Wednesday and we made that Bell state together? You can use it to mail me the state! This technique is called quantum teleportation, it’s a way of communicating a quantum state using classical means, and we’re going to learn how that works in theory and in code.

Contents

Sending Grandma's quantum mail

Getting the code up and running

Making sense of the code

Sending Grandma's quantum mail

Step 0 – "Here's what you should've prepared earlier"

For this to work, my grandma and I need to have pre-prepared a Bell state, where her qubit |G⟩, is entangled with my qubit, |F⟩. When I leave my grandma's (taking my half of the Bell state with me), no matter what distance we are separated - whether that's from London to Yorkshire, across the globe, or across the universe - our qubits will still be entangled.

Remember the great property about entanglement - that measurement outcomes are correlated? If my grandma measures her qubit, it's going to affect my qubit (again, despite the distance). We're going to exploit this property in the following steps.

Step 1 – Entangle grandma’s qubit with |ψ⟩

This is a quantum circuit which we can use to describe our initial starting point. At the top left we’ve got the source of all this effort – a qubit my grandma wishes to send me. It’s in an unknown state which we can represent in the following way,

Also in her possession is her qubit, |G⟩, which as we just described is now entangled with my qubit, |F⟩ (illustrated by the lovely squiggly line). They’re now both in the following Bell state,
We’re going to begin by entangling my grandma’s qubit |G⟩, with the state |ψ⟩. Why we do this will become apparent shortly, so you’ll just have to trust me for now. In the previous blog post we learned that we can entangle two qubits first by applying a CNOT gate and then a Hadamard gate. Let’s take a look at how we can describe this using a quantum circuit.

Applying a CNOT

This is our circuit once we’ve added the CNOT gate into the mix. The input state of the CNOT gate can be written as |ψ⟩|G⟩, where the first input corresponds to the upper wire, and the second input corresponds to the middle wire. Replacing |ψ⟩ and |G⟩ with their definitions, we have
We can multiply out the brackets, as shown below:

This gives us the input . Each of these terms is operated on by the CNOT gate. We talked about the CNOT gate in the first post, and we said how it works on two qubits. The eagle-eyed will have noticed that each of our terms |000〉, |011〉, |100〉 and |111〉 have three qubits. In this instance, the first two qubits are used as the control and the target, shown below.

When the control value is |1〉, the target qubit is flipped. This means the |100〉 state becomes |110〉, and |111〉 becomes |101〉. All the other states remain the same.

So once the CNOT gate has acted, we have the result .

Applying a Hadamard

The second step to entangle |G⟩ with the state |ψ⟩ is to apply the Hadamard gate to the first qubit. We can see what that looks like as a quantum circuit below

Just to remind you of the Hadamard transformation, it takes |0〉 and |1〉 to these states:

The output of the CNOT gate was . Time to apply the Hadamard gate to the first qubit of each term. What’s coming up doesn’t look pretty, but take a deep breath, this is our output:

So our result is now

Spring cleaning

It’s in a right old state. Let’s tidy this up. If we look at the first term , you’ll notice we have two √2 factors, we’re going to pull these out of their fractions, and multiply them like so,
Applying the same logic to the remaining terms, we obtain , and . You’ll notice the 1/2 factor is common to each of them, so we can take it outside the brackets giving the collective result:This is already looking better. Now we’re going to expand out the brackets (this will make it easier for our next step). I’m going to expand as an example.
I’d leave the remaining ones “as an exercise” but if you’re anything like me that means you won’t do them. So here’s the expansions of the remaining terms:

Collecting them all together, we get the following result:

Step 2 - Taking a measurement

Now that we have entangled my grandma’s qubit |G⟩ with the qubit |ψ⟩, we’re going to measure them both. But before we do that, we need to get our results into a format that is easier to interpret.

More tidying up

This is the last time we do this, I promise. When we measure the first and second qubit, we’re going to get one of four results, either 00, 01, 10, or 11. Currently, it’s not all too clear what our third qubit would be, for each of these measurements. We’re going to regroup our result so that we know what state the third qubit, my qubit |F⟩, is in.

Currently we have the result

Let’s highlight the first and second qubit of each term:
Consider the cases where the first and second qubit equal |00〉. If we look at this case in isolation, we have . We can refactor this term as shown below,

And we can do similarly for the |01〉, |10〉, and |11〉 cases.

In summary, our regrouping looks like this:
Perfect.

Making a measurement

This is what our circuit looks like with the measurement symbols added. I just want to point out the double lines after the measurement symbols – that’s not an accident, it means they’re carrying classical bits, which is the result we get after measurement.

My grandma is going to measure her two qubits |G⟩ and |ψ⟩. As we said, she can obtain one of four results, either 00, 01, 10 or 11. Remember, when we measure a qubit, the superposition for that particular qubit collapses. So let's say we measure the first qubit |ψ⟩ and get a result of 0. The superposition collapses, so we now have only terms where the first qubit is consistent with a measurement of 0. That’s these ones:

Now say we measure the second qubit, |G⟩ and get a result of 0. The superposition collapses, and we now are left with the only term consistent with the first and second qubit having a measurement of 0. That’s this one:
The lovely thing about this is, we now know what state the third qubit is in (that’s my qubit |F⟩). I can now see it’s in the state .

What if we have a measurement result of ‘01’? Let’s look at our factored result:
The only term consistent with a measurement of ‘01’ is (using the same logic we just used) which means my qubit must be . Again, applying that logic, if my grandma measures ‘10’, I know the third qubit is . And if my grandma measures ‘11’, then the third qubit is .

We can summarise this like so:

Step 3 - Performing an operation on my qubit (F) to get the state |ψ⟩

Currently we’re in a position where my grandma has measured her two qubits, and called me up and told me the result, so I now know what state my qubit is in. Remember that the unknown state we began with |ψ⟩ was equal to . We need to apply some operations to my qubit so that it matches this state, the state my grandma wanted to send me (otherwise I’ve got something very different).

That’s a simple thing for us to do now. Look at the table above. If we’ve got a measurement of ‘00’, and my qubit in the state , then you can see this is exactly the same as |ψ⟩ - so we don’t need to perform any operations on it. That’s the simplest case.

Now look at the situation where our measurement is ‘01’, and my qubit is in the state . Note that in comparison to |ψ⟩, |0〉 and |1〉 are flipped. Here, we just need to apply a quantum NOT gate to my qubit, i.e an X gate, to get the desired result .
If we look at the ‘10’ case, my qubit would be in the state . If we compare this to |ψ⟩, we have but we want . All we need to do is apply the Z gate, which flips the sign.

Finally, we have the ‘11’ case, where by qubit is . Here, |0〉 and |1〉 are flipped, and also we have , but we’d like this term to be positive. We need to apply an X gate, followed by a Z gate to get the desired result . Let’s update our table with these operations.

Notice that if the first bit of our measurement result is '1', we apply Z, and if the second bit is '1', we apply X. Our final circuit therefore looks like this, where we add the Z gate to use the upper wire as the control, and the X gate to use the second wire as the control, with my qubit being the target for both (because that's the qubit we want to apply the gates to).

We have shown that my grandma needs to communicate just two bits of classical information to me to share the unknown state, these are the results of the measurements of |G⟩ and |ψ⟩. Through entanglement my grandma can communicate the values of α and β to me. She has done this without a quantum communications channel, which is why it is termed ‘quantum teleportation’.

Getting the code up and running

Now that we understand the theory behind quantum teleportation we can dive into some code!

If at any point you get stuck you can find the code here (just clone or download the repo, open 'QuantumTeleportation.sln' in Visual Studio and follow Step 5).

Step 1: Create a new Project

Create a new ‘Q# Application’ in Visual Studio and name it something sensible like ‘QuantumTeleportation’.

Step 2: Get a copy of the code

Step 3: Edit Operation.qs

Next, you want to replace all of the code in ‘Operation.qs’ with the code in ‘TeleportationSample.qs’, which you’ll find under Quantum > Samples > Teleportation here.

Step 4: Edit Driver.cs

Similarly, you want to replace all of the code in ‘Driver.cs’ with the contents of ‘Program.cs’.

Step 5: Save and Run

Run the code by pressing ‘start’. The output should print results for rounds 0-8 which look like the below:

Making sense of the code

I say ‘making sense’ which isn’t fair, the code is commented beautifully 🙌. What we’re going to do is relate the code back to the theory we’ve just learned, which won’t be too difficult.

Step 1: Teleport

Throughout the post we’ve talked about three qubits, the first being the qubit in an unknown state |ψ⟩, the second being my grandma’s half of the Bell state, and the third being my half. If we look at the ‘Teleport’ operation, we can see that these three qubits correspond to msg (the unknown state grandma is sending me), here (my grandma’s qubit), and there (my qubit F, where |ψ⟩ is being sent).

As we discussed, my grandma and I share a Bell state which we had to prepare ahead of time. So we need to entangle her qubit, here, with mine, there before we can do anything else. This is done in the code by applying the Hadamard gate and CNOT like this:

Now we’re going to refer to our circuit diagram which I’m going to annotate with the naming as it appears in the code.

Walking through the steps we’ve gone through in the blog post, we first entangled my grandma’s qubit here with the unknown quantum state msg. In our diagram we have the CNOT gate and the Hadamard gate, which corresponds to these lines:

Then we measured the first and second qubit, msg and here. This is the measurement that my grandma calls me up and tells me. You can see that in the code here, as conditions for the if statements:

As we discussed in the final part of the blog, once we’ve got our measurement, we want to alter my qubit so that it matches with the qubit |ψ⟩, msg. We said that if the first qubit’s measurement was ‘1’, then we apply the Z gate to my qubit (there). You can see that above. Likewise, if the second qubit’s measurement was ‘1’, then we apply the X gate to my qubit.
Done! My qubit is now in the state my grandma wished to send me 😊

Step 2: TeleportClassicalMessage

Next we’re going to look at how we can use our Teleport operation we’ve just defined to send a message. Grandma’s finally got the hang of quantum teleportation now, and instead of sending me an unknown quantum state, she wants to send me known one – in this case it’s going to be a Boolean message. I’ve asked Grandma if she’s put the kettle on, and she’s going to send me a message saying either true or false.

We begin by allocating a register with two qubits, the Boolean message we will send (msg), and my qubit (there) where my grandma will send it. Remember that we index the register like an array, beginning at 0. So that looks like this:

The msg qubit we have allocated is prepared in the state |0〉 (that’s the default when you allocate a new qubit). My grandma’s message, whether the kettle is on, is either true or false. If it’s false, we don’t need to do anything as this is equivalent to |0〉, which is the state the msg qubit is already in. If my grandma’s message is true, that is |1〉, we need to perform a quantum NOT on the msg to get it in this state which you can see below.

We use our Teleport operation to send the state from my grandma to me.

We then measure my qubit’s state there.

This gives us the Boolean result – an important use of decades of research into quantum mechanics and computation – of whether a warm brew is in sight.
Running the code shows the result of sending different Boolean states, which you can find in the Driver.cs file.

And we’re done!

Next steps

If you've got any questions about this blog, or the previous one, please comment below or ask me on twitter @frances_tibble. Next up we'll be looking at measurement in a bit more detail. If you'd like to go into more depth on some of the gates that we've seen in this post then do check out Anita's blog here.