# 7. The Toric Code (part 3)

--

*This is the seventh post in a series on quantum error correction. For other parts, check out the link at the bottom.*

## The Toric Code so far

We have a bunch of qubits, which we call *physical qubits*. These are too noisy to do anything useful. To make a useful qubit, which we call a *logical qubit*, we put our physical qubits on a grid.

It looks something like this.

Here all the physical qubits are `0`

, but it’s also possible for some of them to be `1`

. They just need to follow the rules, and the rules state that every white square must have an even number of 1’s around it.

Here are a few examples of the rules being followed.

But we don’t just want a nice looking grid that follows a bunch of arbitrary seeming rules. We want to store some information: our logical qubit. For this we need to find a way to use the grid to store a 0 or 1 value. We also want to make it relatively hard to see what this logical bit value is. Otherwise gremlins could easily come along and measure it, stealing all our lovely quantumness.

So what we do is take the physical qubits shown in blue below. Or the ones shown in green. Or any other loop that passes through blue squares and goes through the loop of the torus on which the grid is wrapped.

Then we look at the physical qubit values along one of these loops, and add them all up. If the answer is odd, the logical qubit value is `1`

. If it is even, the logical qubit is `0`

.

By defining our measurements using these loops, we end up detecting another kind of loop. Specifically, it looks for the kind of loop seen in our examples (c ) and (d): loops of `1`

s that pass through the white squares and around the ring of the torus. The result comes out `0`

when there are an even number of these loops, and `1`

for odd.

We then simply use this result for the logical bit value. A grid with an even number of these loops, like in (a) and (b), are associated with logical `1`

. Those with an odd number, like (c) and (d), are associated with logical `1`

.

There are also a whole bunch of other patterns of `0`

s and `1`

s that satisfy the rules. Whether these correspond to logical `0`

or `1`

just depends on what their loops are doing.

**Errors!**

How much effort would it take to mess everything up? If we’ve make a logical qubit with value `0`

, for example, how could errors make us think it was a `1`

?

Suppose we make our logical qubit using (a), and so all physical qubits are set to `0`

. Then an error happens, flipping one of the physical qubits to `1`

. For example:

Does this correspond to a logical 0 or a logical 1? To answer this we’ll need the lines from earlier that are used to count the relevant loops.

If we were to use the blue line, our count of `0`

s and `1`

s would cross physical qubit on which the error happened. In fact, as far as the blue line is concerned, the physical qubits look exactly the same as the grid patter in (c ). So if we measure whether our logical qubit by looking at these, it will tell us that that its value is `1`

. But that’s wrong! All is lost!

Well, not quite yet. Remember that the `0`

’s and `1`

’s in the grid have to follow the rules, but the error has broken them!

Specifically, the white squares either side of it now have an odd number of `1`

’s. This is why they were give an orange border: to shame them for their rebellion!

To make things more interesting, let’s have a few more errors happen.

Here we have three errors all near each other. They form a little string of `1`

s. For every square, the rules are obeyed. This is because the string contributes two `1`

s: one when it enters and one when it leaves. But the ends of the string are a different story. These squares only get a single `1`

. They have an odd number of `1`

’s. They are breaking the rules.

**Correcting the errors**

Finding the rule-breaking squares gives us a clue about what errors have happened. We can think of them as forming strings across the grid, with the orange squares as the endpoints. Sometimes these strings are so small that it seems odd to call them strings, like the single error in (e). In cases like (f) it makes more sense. Either way, it is a clue as to what happened.

Unfortunately, the clue doesn’t tell us exactly what happened. In both cases above, for the single error in (e) or the longer string of three in (f), the orange squares are in the same places, and so we see the same rules broken. So how can we correct the errors if we don’t know exactly what errors happened?

Let’s think about how we might react if we were ever to see this pair of broken rules. If errors are rare, we could conclude that the single error of (e) is a lot more likely than the three in (f). So we would assume that it was (e) that happened. To correct it, we would flip the corresponding physical qubit.

If we were right, and it really was (e) that happened, the `1`

is flipped back to a `0`

. Everything is back to how it was. All the physical qubits will have the values of as in(a), the rules are all obeyed and our logical qubit is back to having value `0`

.

If we were wrong, and it was (f) that happened, we add to to the string rather than removing it. The addition joins the ends together making a loop. This is just a little loop, not one that goes around the ring of the torus. In fact, it is the same loop as in (b), which also corresponds to a logical qubit with value `0`

. We could have chosen to use these values for our physical qubits in the first place. So everything is fine. The errors have not been removed, but they have been integrated into society as productive and law abiding qubits.

In this example, it seems like the errors still leave a trace: they have added a little loop where none was before. But we can only see this because we are currently letting ourselves look at whether each qubit is `0`

and `1`

. Actually doing these measurements would itself a source of errors. They will be outlawed in a later article, becoming errors themselves just as bit flips are here. So, in the end, the only things we’ll be able to look at are whether the rules are obeyed, and what the loop around the ring of the torus is doing. As long as the errors don’t effect these, any observable effect is completely removed.

**Summary**

When a bunch of bit flips happen on our nice neat code, it’s best to think of them in terms of strings. When we look at the squares, and find out which ones break the rules, it tells us where the endpoints of those strings are. Then we have to try and stick the ends together and make loops.

We might not do this exactly right. We might end up making the loop bigger than it needs to be. We might end up combining a few different strings into the same loop. But that doesn’t matter. All that matters is that none of the loops pass around the doughnut. As long as we achieve this, our logical qubit value will not change. The errors will have failed to have an effect.

It can be that we will get it wrong. If noise creates a string of flips that goes almost all the way around the ring of the torus, we’ll probably end up completing it by mistake. But this needs a lot of errors to happen, and for them all to lie along the same loop. This is pretty unlikely, so we’ll end up nicely correcting our errors most of the time.

**What else do we need to know**

We’ve now seen how to store a logical qubit in the toric code, and how to protect it from errors. So what else is there? Well, we’ve only looked at bit flip errors so far. What if those nasty gremlins were to go around measuring things that they shouldn’t? They’d have to do a lot of it to cause any harm, because you can’t measure the logical qubit values without looking at a whole bunch of qubits. But that’s not the only trick that the toric code has up its sleeve. Next time we’ll take a little look at how to foil this kind of error.

When the next part is out, you’ll find a link in the Twitter thread below.