Saturday 23 July 2016

Quantum Computation with the simplest maths possible

This is part of a project to get people involved with quantum error correction. See here for more info.

I and a bunch of my colleagues recently did an AMA on the science part of Reddit. There we answered hundreds of questions that people posed us about quantum technology, and quantum computation in particular. This post is inspired by some of the questions we got. This is quantum computing, explained as simply as I can.

Normal Computers

Before I try to explain simply what a quantum computer does, let's start with normal computers.

Computers do maths. Whether you are browsing Facebook or jumping on Goombas, they are just doing maths.

Maths involves numbers. We, as humans, like to write numbers using a base 10 system, so they come out looking like 42 or 3.14159. For computers it's easier to write them in binary. These same two numbers would then come out as 101010 and 11.00100100011. All numbers in binary are a string of 0s and 1s. We call each digit a bit.

To do complicated maths, computers break the problem down into lots of tiny blocks of maths, which we call gates. These are basic building blocks of mathematics. The ones that computers use are typically just lots of ways of comparing two bits. For example, the AND gate takes two bits and tells you if they are both 1 or not. The XOR gate takes two bits and adds them up, then tells you if the result is even or odd.

Another example is what I call the reversible-(N)AND gate, which we will be getting a bit more intimate with later. This looks at three bits. It does a comparison of the first two, with the third one telling it which comparison to do.

To tell you what the reversible-(N)AND does we have a picture called a circuit diagram

and a table, called a truth table.

These tell the story of three bits that go through this gate. They start on the left. The top one starts off as 0 or 1. Rather than fix it to one or the other, let's just call it a for now. The other two start off and 0 or 1 too. Let's call them b and c.
They each follow their lines through the gate. When the top one comes out, it'll always be the same as it went in. The same is true for the bottom one. But the middle one does something more interesting. If c is 0, the output is only 1 if both a and b are 1. The fancy name for this is an AND gate. If the middle bit starts as 1, it'll come out the opposite. The fancy name for this is a NAND gate.

NAND gates are pretty awesome things. With enough NAND gates you can solve any problem. Some problems need more gates that others. Perhaps you remember adding and multiplying numbers at school. Adding is pretty easy, even if the numbers have lots of digits. You just go along them doing a bunch of little additions. Multiplying, however, becomes a right pain for big numbers.

Computers feel that pain too. The number of gates you need to multiply two numbers is more than you need to add them. As as the numbers get more and more digits, it gets worse and worse.

But there are problems much worse than multplication. Factoring numbers, for example. For big enough numbers, this would need as many gates as there are atoms in the universe! Even if we took a supercomputer, and reused its gates many times, it would take the age of the universe to get all the maths done! But for adding numbers just as big, even your own computer could do it while you make a cup of tea.

Because of this, we can wonder if the gates we use are actually the best ones. Maybe we could find other gates, that could be used to solve problems like factoring much more easily. If gates are the building blocks of maths, perhaps we are currently stuck using Duplo. No wonder our maths is big and bulky if we try to build intricate things frm Duplo! We need to find the Technic.

The problem is that we have to get something to do the gates for us. For the gates in a normal computer, we use transistors. These are magical rocks, that do fun things when you poke them with electricity. This makes them perfect for doing the gates.

For new gates, we need new magic rocks. Where might we find them? Well, one kind of problem that is hard for normal computers is simulating quantum particles. But quantum particles manage to sort themselves out pretty easily. So perhaps we could harness their quantumness for new gates. That is exactly what a quantum computer would do.


A qubit is a little like a ball. Like this ball.

Ignore the eye and stuff. But don't ignore the valve near the top. The valve is an important part of our qubit.

When the valve is at the top, we say that the qubit is in state 0. When it's at the bottom, we say the qubit is in state 1. When it is somewhere else, we say that it is some superposition of 0 and 1. There are an infinite number of places it can be, and so an infinite number of different possible superpositions.

But there is a big difference between a qubit and a ball with a valve on. For the ball, we can ask "where is the valve?", and then look at it and find out exactly where it is. For a qubit, the laws of physics don't let us know that much. All we can ask is something like "is it 0, or is it 1?"

The qubit is not allowed to say "I'm a superposition, leave me alone." It has to choose either 0 or 1. If it is 0, it says 0. If it is 1, it says 1. If it is a superposition, it randomly chooses 0 or 1. If it is near the top, it is more likely to go for 0. Near the bottom will most likely go for 1. For the equator it is 50/50.

A bit is less complicated. It has no need for some wierd story about a ball. It is just 0 or 1. We can ask it which it is, and we can turn 0's into 1's and 1's into 0's if we want. But we can't do anything more.

With a qubit, we can do more. We can rotate it. We can rotate a 0 into a 1 (which we call a NOT gate). We can do the same rotation, but only half-way. Let's call that a halfNOT. And we can also do any other fancy rotation that we could ever imagine. But we won't today. We'll just stick with halfNOTs.

Controlled gates

More interesting than just rotations of single qubits, are controlled gates. These deal with two qubits, with one called the control and the other called the target. They look at whether the control is in state 0 or 1. If 0, they then do nothing. If 1, they do some rotation on the target qubit.

The most popular controlled gate is the controlled-NOT. This does a NOT gate to the target if the control state is 0 Here is our circuit diagram.

Here if the state of the control qubit, a, is 0, the bottom qubit comes out b. Just as it went in. But if a is 1, it comes out the opposite value. It has had a NOT done to it.

There's also another way we can think about this gate.

With this way of writing the story, b is replaced with whether or not a+b is odd. Both versions of the story are useful to keep in mind, and equally valid.

The examples above have the control either in state 0 or 1. So everything is nice and simple. But what if it was a superposition?

The way a controlled operation could be done is to ask the control whether it is 0 or 1. Make it decide one way or the other, and then do whatever is required to the target based on the result. But that's not very quantum.

Instead we can make the qubits interact, so that the controlled gate happens without ever making the control qubit decide. The undecidedness remains, and spreads to the target qubit. The end result is a superposition of the target in state 0 and the control without a NOT, and the target in state 1 and the control with a NOT.

Unfortunately, this is where we can no longer use balls to understand what's going on. A two qubit correlated superposition like this is an example of entanglement. Entanglement is what allows quantum computers to go beyond computers made out of balls, or even transistors.

Since we have to depart from the ball picture, we won't go to far into complicated entanglement for the rest of this post. We'll stick with entry level entanglement, but not too entry level. Rather than a controlled-NOT, let's go for a controlled-halfNOT and see what this gate lets us do.

Doing maths with a controlled-halfNOT

Using a controlled-halfNOT as our basic gate, can we do some maths? Can we find an algorithm that will solve some mathematical problem?

We'll start simple, and just try to do a reversible-(N)AND. But this is actually a pretty important thing. We can build a normal computer just from a bunch of NAND gates, which is something a reversible-(N)AND can do. So if controlled-halfNOTs can make reversible-(N)ANDs, they can do any maths.

First, let's start by taking a look at the circuit diagram for a controlled-halfNOT.

We can start off with a and b being simply 0 or 1. But after a halfNOT, the bottom qubit won't be simply 0 or 1 any more. It will be a quantum superposition. In this post we're not going to worry about the maths needed to write that down. Though you can check it our here.

Also, keep in mind that a controlled-NOT is just two controlled-halfNOTs. Because two halfNOTs are, of course, one NOT. So if we can do controlled half-NOTs, we can do controlled-NOTs too.

Now let's see how to make a reversible-(N)AND from a bunch of controlled-halfNOTs. But first we better remind ourself what it is supposed to do.

Basically, the output is just c, unless both a and b are 1. In that case, the middle bit gets a NOT, to make it 1-c.

Here's how we do that with a bunch of controlled-halfNOTs.

In the first part here, we do a couple of controlled-halfNOTs. The first one has the top qubit as the control and the middle one as target. The second has the bottom as control and the middle as target (that's why it's upside down).

If a and b are both 0, neither controlled-halfNOT does anything. If they are both 1, then both controlled-halfNOTs do a halfNOT on the middle qubit. That makes a full NOT. In both cases, this is exactly what we want from a reversible-halfNOT.

But it's not all good news. For the other two cases, where only a or b is 1, we want nothing to have been done to the middle qubit. Instead, one of the controlled-halfNOTs will do nothing, but the other will do a halfNOT!

To get rid of this, we need to know whether only a or b is 1, and use this knowledge to undo the unwanted halfNOT. We then need to unknow this information, because you can't going around knowing things about quantum systems without upsetting them.

Firstly, note that the cases that get it right (a and b are both 0, and a and b are both 1) are the those where a+b is even. The cases that get it wrong (a=0 and b=1 or vice-versa) are those where a+b is odd. And we know how to find out if a+b is odd or even. We do it with a controlled-NOT.

That's what the second part of the circuit does. There's a controlled not with the top qubit as control and the bottom as target, which results in the bottom one being 0 if a+b is even, and 1 if it is odd. This gate doesn't involve the middle qubit at all (even though it seems to cross it in our picture).

Now the bottom qubit knows whether the unwanted halfNOT happened. We don't ask it to tell us, because our brains have trouble unknowing things once they know them. We just use it to undo the unwanted halfNOT. This is done with three controlled-halfNOTs with the bottom qubit as control and the middle as target. This adds three more halfNOTS to the middle qubit only when that troublesome one is there already. That gives four halfNOTs in total, which is the same as two NOTs, which is the same as nothing. The unwanted halfNOT is gone!

Now the middle qubit is doing exactly what we need. But the bottom qubit isn't. It should come out as b, rather than tell us about whether a+b is odd. To make it unknow this information, we just do the controlled-NOT again. The second NOT undoes the first and returns the bottom qubit to b. Our reversible-(N)AND is now complete!

Here we have taken three qubits, that were definitely in the states 0 or 1. So they were pretty much just bits. At the end we also have three qubits that are definitely 0 or 1, and so are also basically just bits. But inbetween, their quantumness came in to play. Superpositions happened and got shunted around to do the maths we wanted to do. And that, is quantum computation.

Of course, normal computers can do reversible-(N)ANDs just fine. What we really need quantum computers for are problems that normal computers are rubbish at. The approach is the same. We start with our qubits just being bits, and we end with qubits being bits. But in between we do quantum stuff.

To get the full advantage of quantum computation, we'll need to use more than just a simple controlled-halfNOT. This is defintely part of the Lego Technic set of quantum gates, but it's one of the big and bulky ones. We need some of the more intricate gates to make really fancy computations, such all the possible rotations of a qubit. Add those in, and we can factor huge numbers while you make a cup of tea as well.

To find out more about that, a bit more maths is needed. Complex exponentials are pretty essential, but they aren't to everyone's taste. If you think you can handle it, check out my lecture notes here. You can also find someone else's explanation of things like this in a video here.