8. The Toric Code (part 4)

Dr James Wootton
9 min readJan 6, 2022


This is part of a series on quantum error correction. For other parts, check out the link at the bottom.

Last time we saw how the toric code protects against bit flip errors. These errors that change the values of the qubits in the grid, flipping them from 0 to 1 or vice-versa. This is exactly the kind of error we see in normal data, where corruptions cause spurious changes. By implementing the toric code with normal bits instead of qubits, we could use everything we know so far to correct errors in conventional computers.

But that’s not what the toric code is optimized for. There are other forms of error that it can also protect against. Ones that are more subtle, and which only affect quantum data. Now it’s time to look at them.

Protecting against measurements

Qubits can exist in a superposition state: something which is neither simply 0 nor simply 1. But if we measure the qubit, it is forced to choose to become either 0 or 1, and the superposition property is gone.

For our physical qubits, this is something we need to live with. They are physical objects that exist in a world with other physical objects. We can try to make them interact as little as possible, by doing things such as cooling them to almost absolute zero and shielding them from radiation, but some degree of interaction will always happen. The effect of these interactions is like a little gremlin sitting next to the qubit, occasionally randomly deciding to measure its value.

But how about the logical qubit? Last time we discovered how to measure whether the logical qubit is 0 or 1: by measuring all the physical qubits around certain loops. So once all the physical qubits around one of these loops have been measured by their gremlins, the logical qubit will have effectively been measured as well. Any superposition in the logical qubit would therefore be destroyed.

So how can the code stop this? We need to find a way to detect when a gremlin has made a measurement. Then we need to perform some sort of scrambling operation, so that the result of that measurement can no longer be used to deduce the logical bit value.

The way we detect bit flips is with a rule. Specifically, we have a rule for each white square: each must have an even number of 1s around it. By looking at which of these rules are broken, we can work out how to get rid of the bit flip errors. But let’s forget about these rules for a while. Instead we will come up with some new rules that can be used to detect the measurements. Once we’ve worked those out, we can try to get the two sets of rules play well together.

The X basis measurement

When we’ve discussed measurement so far, it’s always been in terms of the states 0 and 1. These are the two possible outcomes that a qubit must choose between. If the qubit is in the state 0, obviously it will choose 0. If it’s 1 it’ll choose 1. If it’s a superposition of the two then it will choose with some degree of randomness.

This is the most common kind of measurement that we apply to qubits. It’s known (among other names) as the Z basis measurement. There is also the X basis measurement, which is defined in terms of two states called + and -. If we do an X basis measurement, the two possible outcomes are + and -. If the qubit is in state +, the qubit will choose+. If it’s -, it’ll choose -. If the qubit state is anything else, we can think of it as a superposition of + and -, and so the outcome will be chosen with some degree of randomness.

So the X measurement just another version of the kind of measurement we know so far. It just chooses between a different pair of possible qubit states.

We won’t go into all the maths behind this, and how the qubit states can be represented. For that you can consult the Qiskit textbbook.

All we need to know is how X and Z measurements play together.

So what is the + state. As we’ve said, qubit states can be 0, 1 or some superposition of these. So since + is not 0 or 1, it is therefore one example of a superposition state. The state is similarly another. The important thing to note is that they are different superposition states. In fact, + and are just as different from each other as 0 and 1 are.

For that reason, we could have equally said that qubit can be either in the + state, the state, or some superposition of the two. With this interpretation, 0 and 1 can be thought of as superpositions of + and -.

Because of this, a Z measurement of a qubit in state + or will yield a random answer. Similarly, an X measurement of a qubit in state0 or 1 will yield a random answer.

Now we know enough about measurements to define a new set of rules.

Rules for blue plaquettes

We’ll now think about the code using the + and states. With these we can declare a rule for each blue squares: when looked at in terms of + and -, each must have an even number of -’s (and therefore an even number of +’s).

There are many possible patterns of +’s and -’s that obey these rules. Here’s a few.

These should look pretty familiar. They are the same ones as before, but with 0 replaced with +, 1 to , white squares with blue and everything turned 90 degrees.

Now we go off and get a cup of tea, and a gremlin sees an opportunity for some mischief. It measures one of the qubits to see if it is 0 or 1. Every qubit here is + or -, both of which are a superposition of 0 and 1. So the measured qubit will randomly choose one or the other. Let’s say, for sake of argument, it chooses 0.

When we get back from our tea break, we check whether the blue squares are following the rules. Most are, but what about the two near the measured qubit?

When we measure those squares, we are asking them to look at how many of the qubits are in state +, and how many are in state -, and tell us whether the number of -’s is odd or even. For most of the qubits, which are simply in state + or -, this is an easy job. But the measured one is in state 0, a superposition of + and -. This has to decide whether to be + or before the question can be answered.

If it decides to be +, which is what this qubit was originally, the rules are obeyed on these squares. The qubit has also been forced into a state that is nothing like the 0 state that the Gremlin measured, so that information is now out of date and the Gremlin knows nothing. All is well.

If it decides to be the opposite of what it was originally, the effect is similar to a bit flip. Except this kind of flip turns + to - and - to +. We call these phase flips. The phase flip causes the rules to be broken on the two blue squares. But, just like with bit flips and white squares, this allows us to detect and correct the errors. Also, the qubit is again forced into a state that has nothing to do with the one that the Gremlin measured. So its information is again out of date, and all is well.

With these rules, protecting the code from measurements is exactly the same as protecting it from bit flips. So a few measurements here and there don’t cause a big problem. We can just detect them, remove their effects and update the state to make sure their results can’t tell anyone anything about our logical qubit. The only way for an unwanted measurement to happen is if a load of measurements happen all at the same time, and do so on a loop that goes through the doughnut. That’s unlikely, so we’re mostly okay.

Combining the rules

We now have a set of rules for dealing with bit flips, and a set for dealing with phase flips. But we really need them to work together, so we can deal with both at the same time.

Getting things to work together in quantum mechanics is not always possible. Suppose we have a single qubit, and set two rules:

  • (i) When we do a Z measurement, the answer must always come out 0.
  • (ii) When we do an X measurement, the answer must always come out +.

The only way to make a qubit always give us the result 0 for a Z measurement, as rule (i) requires, is to have it in state 0. But this is a superposition of + and -. This means it gives the result - half the time when we do an X measurement, breaking rule (ii). Similarly, the + state is the only one that always obeys rule (ii), but it breaks (i) half the time.

If the same was true for the rules for the squares, we would get loads of broken rules all the time. It would be impossible to get enough information to detect and correct the errors, because of all the random nonsense that comes from the rules fighting each other.

But there is a big difference between rules (i) and (ii), and the rules we use in the toric code. There is not just one state that satisfies the rules for the white squares, but there are many patterns of 0s and 1s that do it. And any superposition of these will obey them too. There are also many patterns of +s and -s that obey the blue square rules, as do their superpositions. With more possibilities, there is hope.

Any state can be thought of either in terms of 0s and 1s or in terms of +s and -s. Just like a single qubit in state 0 can be thought of as just a 0, or as a superposition of + and -. So it could be that some superposition of white rule abiding 0s and 1s turns out to be the same state as some superposition of blue rule abiding +s and -s. If so, states like this would obey both the rules for white squares and blue squares at once.

Fortunately, it turns out that there are states like this. They are not just nice simple patterns of +s and -s as in the figure above, or nice simple patterns of 0s and 1s as in the figures for previous posts. We need a big superposition to make sure that the rules can work well together. In fact, for a logical 0 we need a superposition of every possible set of 0s and 1s that could be used for a logical 0. Making such a complicated set of correlated qubits is going to be quite hard, and is the main reason that you can’t buy a toric code from the shops at the moment. But experimentalists are working hard towards this goal.

With both sets of rules working well together, we just need to keeping checking on both types of square. When we see white rules broken, we know that a bit flip has happened, and can correct it. When blue rules are broken, we detect and correct phase flips. As long as unlikely things don’t happen, or logical qubit will keep nice and safe.

Phase flips and Xmeasurements

So far we’ve assumed that the only kind of bad things that happen are bit flips and unwanted Z measurements. But what about phase flips, or unwanted X measurements? Our code naturally sorts those out too. What about other crazy weird quantum stuff? It turns out that anything that could ever happen can be thought of as some combination of bit and phase flips. So whatever happens, once we measure what rules are being obeyed, the noise gets forced to decide what kind of bit and phase flips it wants to be. The we detect and correct those, and all is well. The toric code (as well as other quantum error correcting codes) protects us from everything.


Well, perhaps it doesn’t protect us from everything: big and horrible errors that hit us all at once can still get under the radar. But as long as they are rare, our logical qubit will last a long time. And if they are not rare enough, we just make our code bigger so they need to be ever bigger and more horrible before they can mess us up.

So that’s the toric code in 5 parts. Next time we’ll move on to its flat sibling, the planar code.

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



Dr James Wootton

Helping to make quantum computers IBM Research . Occasionally misusing them for fun and/or science. Two Ts and no Es. All nonsense here is my own doing