Visualizing bits and qubits
By: Dr James Wootton
Tags: Programming, Gamification, Quantum Computing, Science, Computers

For the last few years, I’ve been a bit obsessed with two things: programming prototype quantum computers and gamification. These interests have now inevitably collided, resulting in a gamified tutorial for quantum programming.

An important part of making this tutorial was working out how to visualize qubits. This is an intractable problem in general. If it were easy to do it for large quantum computations, it would follow that it is easy to simulate them. Since that is not the case, our visualization will focus on just a pair of qubits.

The visualization is the main way that the user of the tutorial will understand and relate to their pair of qubits. But we don’t want its usefulness to end there. Ideally, we should base everything on principles that also apply to larger numbers of qubits. The user can then carry on thinking in terms of those principle when they go on to design much larger quantum programs.

In this article, I’ll explain what I and my collaborators came up with.

Visualizing a pair of bits

Let’s start with something nice and simple: the bit. Each bit is either a 0 or a 1 . Let’s represent this in picture form.

The 0 and 1 states of a bit, with an ASCII art circle for some reason.

Here we have a blue bar that is is light blue, or off, to represent 0 , and dark blue, or on, to represent 1 .

Sometimes we need random bits. These are still just 0 or 1 , of course, but we don’t know which. Let’s represent our ignorance with a bar that is half on (or half off).

Is it 0 or is it 1? Yes! We just don’t know which yet.

One bit isn’t useful for much, so let’s look at two. Obviously we can represent this with two bars: one for each bit.

Two bits next to each other. The jaunty angle is thanks to the limitations of ASCII art.

There are four possible states of two bits. The one shown above is 01 , but there are also 00 , 10 and 11 . These are represented by different combinations of on and off.

Using just two bars is a fine way to visualize the state of two qubits as long as we know exactly what both are doing. But what if they are both random?

There are multiple different things that could be going on here. For one, it could be that the bits were generated by the same random process, and therefore always have the same value. Here’s a Python snippet that would do just that (note that False and True are Python for 0 and 1 ).

random_bit = random.choice([False, True])

bit[0] = random_bit

bit[1] = random_bit

Or they could’ve been generated such that they always have opposite value.

rand_bit = random.choice([False, True])

bit[0] = random_bit

bit[1] = !random_bit # 0 whenever the above is 1, and vice-versa

They could also be completely unrelated random bits.

bit[0] = random.choice([False, True])

bit[1] = random.choice([False, True])

These different possibilities have very different behaviour. So our visualization will be incomplete if it doesn’t tell us which we have.

To add this information into our visuals, we’ll add another bar. This extra bar will have the job of telling us about these correlations. It will be off if the random bits are destined to always agree.

And on if they always disagree.

If they are unrelated, and their agreements and disagreements are just random, the extra bar will be half on.

Now we have a way to represent everything that two bits could be up to.

Visualizing a pair of qubits

Bits are quite simple and familiar. They don’t really need a fancy visualization like this. But their quantum cousins do. Let’s move on to qubits.

A qubit is a quantum object from which we can extract one bit of information. There are multiple ways we can choose to implement that extraction, and the choice we make can lead to different results. To visualize a qubit, we need to keep track of all these possible outputs at once.

We’ll focus on two ways to extract a bit from each qubit, and represent this with two bars. One blue (and vertical), and one red (and horizontal).

This is just one qubit

If we take the blue output, we can see that the state shown here would definitely give the output 1 . If we take the red output, the result will be random.

In fact, the randomness here is quite special. The qubit is so concentrated on definitely giving a 1 if asked for a blue output, it really doesn’t know what it would do if asked for a red one. It would have to make up its mind on the spot if the red output was needed, and does so in a completely unpredictable way. It’s not the pseudorandomness that computers usually give us. It is true randomness. This is one of the things that make qubits unique and useful: something we’ll discuss more in another article.

The second qubit is also represented with two bars, but we’ll stack these vertically.

In this example it is the red output whose result is definite, and the blue one left to be random.

Since there is always randomness for qubits, we again need something extra to keep track of correlations. For two bits, we saw that just one extra bar was enough. But for two qubits we have more possible correlations to keep track of.

Specifically, there are four ways that we can extract two bits from two qubits:

A blue output from both qubits (let’s call this BB);

A red output from both qubits (RR);

A blue output from the one qubit, and a red one from the the other (BR);

A red output from the one qubit, and a blue one from the the other (RB).

For each of these we need an extra bar to tell us what correlations to expect.

Here we have one qubit represented by the two bars on top (we’ll call this the top qubit) and another by the two on the left (the left qubit). The remaining bars, with a grey background here for clarity, describe the correlations.

Correlations between blue outputs are shown by the BB bar, which is also blue. Correlations between the red outputs are shown by red RR bar.

For the two cases with one red output and one blue, we have RB and RR. As a compromise between blue and red, these bars are purple (and diagonal).

In the example shown above, we know that a blue output from the top qubit will give 1 , and a red one from other qubit will give 0 . The BR bar is therefore full, acknowledging that they will definitely disagree. Everything else is just random.

Now we’ve worked out how to visualize qubits, let’s start manipulating them.

Manipulating qubits

A computer doesn’t just look at bits, it processes them. Bits are constantly changed from 0 to 1 and back again while the computer charts a course from input to output. A quantum computer will do much the same, but with qubits.

The simplest thing that can be done to a bit is the NOT operation. This flips 0 to 1 and 1 to 0 . With our visualization, the effect is

A NOT does not stop a random bit being random, so it has no effect on a bar that’s half on.

Qubits have operations with similar effects. There is one called x , which affects the blue output of a qubit. And there is one called z that affects the red output.

All these operations, NOT for bits and x and z for qubits, also affect the bars that describe correlations. The reason for this is quite straightforward: If you have two bits that are going to agree, and then you flip one, they will disagree instead.

The result is that x and z on the top qubit affect whole columns of the grid. Here’s the effect of z as an example.

A simple animation of a z applied to the top qubit

Note that, in general, the purple bar in this column is also affected by the z . It just doesn’t do much in this particular example, because that bar is half on.

The x and z operations will instead affect rows when applied to the qubit on the left. Here’s an example with x .

A simple animation of an x applied to the left qubit

These operations, x and z , are examples of Clifford operations. This fancy name just tells us that they are operations with a relatively straightforward effect on the bars. In fact, this bar based visualization comes precisely from the way we like to think about Clifford operations and their effects.

There are other Clifford operations too. There is h , which swaps the two outputs of a qubit, making red output what the blue would have been, and vice-versa. There are also operations that act on pairs of qubits, called controlled operations, which are crucial for the way we process information in quantum computers. If you want to learn more about these, your best option is to play the tutorial.

Once you’ve played enough of the tutorial to fully internalize the effects of Cliffords on two qubits, you’ll be well prepared to write Clifford-based programs for any number of qubits. This will be a great skill for developers of quantum software, since Clifford operations are an extremely useful part of quantum computation. In fact, the vast majority of any quantum program will be made up of Clifford operations

Even so, the applications that can be achieved with Cliffords alone is limited. To justify actually building quantum computers, we need something more. We need the unimaginatively named non-Clifford operations.

We’ll only look at two in this article. One is usually called u3(pi/4,0,0) . But since that’s not very catchy, we’ll call it q . The other is the equally cumbersome u3(-pi/4,0,0) , which we call qdg .

The q operation is going to allow us to move beyond bars that are simply on, off or half on. We will start getting other fractions too. For example, let’s start with a qubit whose blue box is fully on.

Now let’s hit it with a q .

The blue box is still mostly on, which means it will most likely output 1 . But it isn’t certain. The loss of certainly for the blue is a gain for the red. It goes from being completely random, to having a strong bias toward 1 .

To explain the full effect of a q, it helps to draw the two bars on top of each other. If we look at where their levels meet, we can draw a point.

The effect of q is simply to rotate the point, dragging the levels of the bars with it.

The effects of 8 sequential q operations

The q operation does a clockwise rotation 1/8 of the way around the circle, and qdg does the same but anticlockwise.

Now it’s time to see how q affects our whole two qubit grid. We’ll start with a q on the top qubit. To do this, we’ll have to draw the two columns on top of each other.

This creates three circles. The top one is just the red and blue bars for the top qubit, as we looked at before.

The middle pair is made of the BB bar and the RB bar. The BB bar is always blue and vertical, and so plays the part of the vertical axis. Now we need a horizontal axis to complete the circle. This is done by the nieghbouring RB bar. Though usually purple and diagonal, it becomes red and horizontal to make the circle.

For the bottom circle, the RR box is the clear choice for the horizontal axis. This means that the BR bar gets the job of being blue and vertical in this case.

For each of these circles, the q operation performs its rotation. The rotation on the top circle shows how the q affects the top qubit itself. The others determine how the correlations are affected.

To see how q affects the left qubit, we instead need to draw the two rows on top of each other.

Everything goes much the same. The difference is that the purple bars play the opposite roles as for when we squashed together the columns.

These circles are not our own invention: they are Bloch circles. These are simplified forms of the Bloch sphere that pretty much every popular science article on quantum computing uses as an infographic.

The Bloch sphere

How many ways are there to extract an output from a qubit? It is true to say that there are an infinite number. It is also true to say that there is only one. But the truest answer of all is that there are three. Two of these are the red and the blue that we have already met. So let’s call the final one green.

When we combine the red and blue bars, we get a circle. When we add in the green one, we get a sphere. This is the Bloch sphere.

The Bloch sphere is the best way to visualize non-Clifford operations on a single qubit. It’s no too bad with two qubits either. We just need to add a green row and column to our grid. Then we can combine triplets of rows of columns into collections of spheres, just as we did above with circles.

For three qubits, you’ll need a 3D cube instead of just a grid. For more than three qubits you’ll end up trying to compile spheres from rows and columns of an n-dimensional hypercube. At that point it’s probably best to ditch the Bloch sphere and develop a visualization more in tune with whatever quantum program you are trying to write.

The green bars were not included in our tutorial for reasons of simplicity. Without them, there is already enough to start exploring the world of quantum programming. With them, everything becomes just a little more bloated. Even us scientists working on quantum computing like to forget about them whenever possible. If you want to learn how to think like us, this is an important lesson to learn.

Conclusions

Many people think of quantum systems as weird, random, multiple-things-at-the-same-time philosophical conundrums. That’s not the best approach for a quantum programmer, who needs to be more practically minded. To train newcomers to the field to hack better than us old hacks, we developed a way of visualizing two qubit states and a tutorial around it.

To do this we blending existing methods. Specifically, we combine the standard way of thinking about Clifford operations (flipping and swapping things) with the standard visualization for single qubit non-Clifford operations (the Bloch sphere).

If you want to see a prettier visualization, which we made for the app version of Hello Quantum, check out the sister article below.