Chapter 2. One Qubit

Conventional bits have one binary parameter to play with—we can initialize a bit in either state 0 or state 1. This makes the mathematics of binary logic simple enough, but we could visually represent the possible 0/1 values of a bit by two separate empty/filled circles (see Table 2-1).

Table 2-1. Possible values of a conventional bit—a graphical representation
Possible values of a bit Graphical representation

0

prqc 02in01

1

prqc 02in02

Now on to qubits. In some sense, qubits are very similar to bits: whenever you read the value of a qubit, you’ll always get either 0 or 1. So, after the readout of a qubit, we can always describe it as shown in Table 2-1. But characterizing qubits before readout isn’t so black and white, and requires a more sophisticated description. Before readout, qubits can exist in a superposition of states.

We’ll try to tackle just what superposition means shortly. But to first give you an idea of why it might be powerful, note that there are an infinite number of possible superpositions in which a single qubit can exist prior to being read. Table 2-2 lists just some of the different superpositions we could prepare a qubit to be in. Although we’ll always end up reading out 0 or 1 at the end, if we’re clever it’s the availability of these extra states that will allow us to perform some very powerful computing.

Table 2-2. Some possible values of a qubit
Possible values of a qubit Graphical representation

∣0⟩

prqc 02in03

∣1⟩

prqc 02in04

0.707∣0⟩ + 0.707∣1⟩

prqc 02in05

0.95∣0⟩ + 0.35∣1⟩

prqc 02in06

0.707∣0⟩ - 0.707∣1⟩

prqc 02in07
Note

In the mathematics within Table 2-2 we’ve changed our labels from 0 and 1 to ∣0⟩ and ∣1⟩. This is called bra-ket notation, and it’s commonly used in quantum computing. As a casual rule of thumb, numbers enclosed in bra-ket notation denote values that a qubit might be found to have when read out. When referring to a value that a qubit has been read out to have, we just use the number to represent the resulting digital value.

The first two rows in Table 2-2 show the quantum equivalents of the states of a conventional bit, with no superposition at all. A qubit prepared in state ∣0⟩ is equivalent to a conventional bit being 0—it will always give a value of 0 on readout—and similarly for ∣1⟩. If our qubits were only ever in states ∣0⟩ or ∣1⟩, we’d just have some very expensive conventional bits.

But how can we start to get to grips with the more exotic possibilities of superposition shown in the other rows? To get some intuition for the bewildering variations in Table 2-2, it can be helpful to very briefly consider what a qubit actually is.1

A Quick Look at a Physical Qubit

One object that readily demonstrates quantum superposition is a single photon. To illustrate this, let’s take a step back and suppose we tried to use the location of a photon to represent a conventional digital bit. In the device shown in Figure 2-1, a switchable mirror (that can be set as either reflective or transparent) allows us to control whether a photon ends up in one of two paths—corresponding to an encoding of either 0 or 1.

Using a photon as a conventional bit
Figure 2-1. Using a photon as a conventional bit

Devices like this actually exist in digital communication technology, but nevertheless a single photon clearly makes a very fiddly bit (for starters, it won’t stay in any one place for very long). To use this setup to demonstrate some qubit properties, suppose we replace the switch we use to set the photon as 0 or 1 with a half-silvered mirror.

A half-silvered mirror, as shown in Figure 2-2 (also known as a beamsplitter), is a semireflective surface that would, with a 50% chance, either deflect light into the path we associate with 1, or allow it to pass straight through to the path we associate with 0. There are no other options.

A simple implementation of one photonic qubit
Figure 2-2. A simple implementation of one photonic qubit

When a single indivisible photon hits this surface, it suffers a sort of identity crisis. In an effect that has no conventional equivalent, it ends up existing in a state where it can be influenced by effects in both the 0 path and the 1 path. We say that the photon is in a superposition of traveling in each possible path. In other words, we no longer have a conventional bit, but a qubit that can be in a superposition of values 0 and 1.

It’s very easy to misunderstand the nature of superposition (as many popular quantum computing articles do). It’s not correct to say that the photon is in both the 0 and 1 paths at the same time. There is only one photon, so if we put detectors in each path, as shown in Figure 2-2, only one will go off. When this happens, it will reduce the photon’s superposition into a digital bit and give a definitive 0 or 1 result. Yet, as we’ll explore shortly, there are computationally useful ways a QPU can interact with a qubit in superposition before we need to read it out through such a detection.

The kind of superposition shown in Figure 2-2 will be central to leveraging the quantum power of a QPU. As such, we’ll need to describe and control quantum superpositions a little more quantitatively. When our photon is in a superposition of paths, we say it has an amplitude associated with each path. There are two important aspects to these amplitudes—two knobs we can twiddle to alter the particular configuration of a qubit’s superposition:

  • The magnitude associated with each path of the photon’s superposition is an analog value that measures how much the photon has spread into each path. A path’s magnitude is related to the probability that the photon will be detected in that path. Specifically, the square of the magnitude determines the chance we observe a photon in a given path. In Figure 2-2 we could twiddle the magnitudes of the amplitudes associated with each path by altering how reflective the half-silvered mirror is.

  • The relative phase between the different paths in the photon’s superposition captures the amount by which the photon is delayed on one path relative to the other. This is also an analog value that can be controlled by the difference between how far the photon travels in the paths corresponding to 0 and 1. Note that we could change the relative phase without affecting the chance of the photon being detected in each path.2

It’s worth re-emphasizing that the term amplitude is a way of referring to both the magnitude and the relative phase associated with some value from a qubit’s superposition.

Tip

For the mathematically inclined, the amplitudes associated with different paths in a superposition are generally described by complex numbers. The magnitude associated with an amplitude is precisely the modulus of this complex number (the square root of the number multipled by its complex conjugate), while its relative phase is the angle if the complex number is expressed in polar form. For the mathematically uninclined, we will shortly introduce a visual notation so that you need not worry about such complex issues (pun intended).

The magnitude and relative phase are values available for us to exploit when computing, and we can think of them as being encoded in our qubit. But if we’re ever to read out any information from it, the photon must eventually strike some kind of detector. At this point both these analog values vanish—the quantumness of the qubit is gone. Herein lies the crux of quantum computing: finding a way to exploit these ethereal quantities such that some useful remnant persists after the destructive act of readout.

Note

The setup in Figure 2-2 is equivalent to the code sample we will shortly introduce in Example 2-1, in the case where photons are used as qubits.

Okay, enough with the photons! This is a programmer’s guide, not a physics textbook. Let’s abstract away the physics and see how we can describe and visualize qubits in a manner as detached from photons and quantum physics as binary logic is from electrons and semiconductor physics.

Introducing Circle Notation

We now have an idea of what superposition is, but one that’s quite tied up with the specific behavior of photons. Let’s find an abstract way to describe superposition that allows us to focus only on abstract information.

The full-blown mathematics of quantum physics provides such an abstraction, but as can be seen in the lefthand column of Table 2-2, this mathematics is far more unintuitive and inconvenient than the simple binary logic of conventional bits.

Fortunately, the equivalent pictorial circle notation in the righthand column of Table 2-2 offers a more intuitive approach. Since our goal is building a fluent and pragmatic understanding of what goes on inside a QPU without needing to entrench ourselves in opaque mathematics, from now on we’ll think of qubits entirely in terms of this circle notation.

From experimenting with photons we’ve seen that there are two aspects of a qubit’s general state that we need to keep track of in a QPU: the magnitude of its superposition amplitudes and the relative phase between them. Circle notation displays these parameters as follows:

  • The magnitude of the amplitude associated with each value a qubit can assume (so far, ∣0⟩ and ∣1⟩) is related to the radius of the filled-in area shown for each of the ∣0⟩ or ∣1⟩ circles.

  • The relative phase between the amplitudes of these values is indicated by the rotation of the ∣1⟩ circle relative to the ∣0⟩ circle (a darker line is drawn in the circles to make this rotation apparent).

We’ll be relying on circle notation throughout the book, so it’s worth taking a little more care to see precisely how circle sizes and rotations capture these concepts.

Circle Size

We previously noted that the square of the magnitude associated with |0> or |1> determines the probability of obtaining that value on readout. Since a circle’s filled radius represents the magnitude, this means that the shaded area in each circle (or, more colloquially, its size) is directly proportional to the probability of obtaining that circle’s value (0 or 1) if we read out the qubit. The examples in Figure 2-3 show the circle notation for different qubit states and the chance of reading out a 1 in each case.

Probability of reading the value 1 for different superpositions represented in circle notation
Figure 2-3. Probability of reading the value 1 for different superpositions represented in circle notation
Note

Reading a qubit destroys information. In all of the cases illustrated in Figure 2-3, reading the qubit will produce either a 0 or a 1, and when that happens, the qubit will change its state to match the observed value. So even if a qubit was initially in a more sophisticated state, once you read out a 1 you’ll always get a 1 if you immediately try to read it again.

Notice that as the area shaded in the ∣0⟩ circle gets larger, there’s more chance you’ll read out a 0, and of course that means that the chance of getting a 1 outcome decreases (being whatever is left over). In the last example in Figure 2-3, there is a 90% chance of reading out the qubit as 0, and therefore a corresponding 10% chance of reading out a 1.3 We’ll often talk of the filled-area of a circle in our circle notation as representing the magnitude associated with that value in a superposition. Although it might seem like an annoying technicality, it’s important to have in the back of your mind that the magnitude associated with that value really corresponds to the circle’s radius—although often it won’t hurt to equate the two for visual convenience.

It’s also easy to forget, although important to remember, that in circle notation the size of a circle associated with a given outcome does not represent the full superposition amplitude. The important additional information that we’re missing is the relative phase of our superposition.

Circle Rotation

Some QPU instructions will also allow us to alter the relative rotations of a qubit’s ∣0⟩ and ∣1⟩ circles. This represents the relative phase of the qubit. The relative phase of a qubit’s state can take any value from 0° to 360°; a few examples are shown in Figure 2-4.

Probability of reading the value 1 in different situations
Figure 2-4. Example relative phases in a single qubit
Tip

Our convention for rotating the circles in circle notation within this book is that a positive angle rotates the relevant circle counterclockwise, as illustrated in Figure 2-4.

In all the preceding examples we have only rotated the ∣1⟩ circle. Why not the ∣0⟩ circle as well? As the name suggests, it’s only the relative phase in a qubit’s superposition that ever makes any difference. Consequently only the relative rotation between our circles is of interest.4 If a QPU operation were to apply a rotation to both circles, then we could always equivalently reconsider the effect such that only the ∣1⟩ circle was rotated, making the relative rotation more readily apparent. An example is shown in Figure 2-5.

Only relative rotations matter in circle notation. These two states are equivalent because the _relative_ phase of the two circles is the same in both.
Figure 2-5. Only relative rotations matter in circle notation—these two states are equivalent because the relative phase of the two circles is the same in each case

Note that the relative phase can be varied independently of the magnitude of a superposition. This independence also works the other way. Comparing the third and fourth examples in Figure 2-3, we can see that the relative phase between outcomes for a single qubit has no direct effect on the chances of what we’ll read out.

The fact that the relative phase of a single qubit has no effect on the magnitudes in a superposition means that it has no direct influence on observable readout results. This may make the relative phase property seem inconsequential, but the truth could not be more different! In quantum computations involving multiple qubits, we can crucially take advantage of this rotation to cleverly and indirectly affect the chances that we will eventually read out different values. In fact, well-engineered relative phases can provide an astonishing computational advantage. We’ll now introduce operations that will allow us to do this—in particular, those that act only on a single qubit—and we’ll visualize their effects using circle notation.

Note

In contrast to the distinctly digital nature of conventional bits, magnitudes and relative phases are continuous degrees of freedom. This leads to a widely held misconception that quantum computing is comparable to the ill-fated analog computing. Remarkably, despite allowing us to manipulate continuous degrees of freedom, the errors experienced by a QPU can be corrected digitally. This is why QPUs are more robust than analog computing devices.

The First Few QPU Operations

Like their CPU counterparts, single-qubit QPU operations transform input information into a desired output. Only now, of course, our inputs and outputs are qubits rather than bits. Many QPU instructions5 have an associated inverse, which can be useful to know about. In this case a QPU operation is said to be reversible, which ultimately means that no information is lost or discarded when it is applied. Some QPU operations, however, are irreversible and have no inverse (somehow they result in the loss of information). We’ll eventually come to see that whether or not an operation is reversible can have important ramifications for how we make use of it.

Some of these QPU instructions may seem strange and of questionable utility, but after only introducing a handful we’ll quickly begin putting them to use.

QPU Instruction: NOT

NOT gate

NOT is the quantum equivalent of the eponymous conventional operation. Zero becomes one, and vice versa. However, unlike its traditional cousin, a QPU NOT operation can also operate on a qubit in superposition.

In circle notation this results, very simply, in the swapping of the ∣0⟩ and ∣1⟩ circles, as in Figure 2-6.

The `NOT` operation in circle notation
Figure 2-6. The NOT operation in circle notation

Reversibility: Just as in digital logic, the NOT operation is its own inverse; applying it twice returns a qubit to its original value.

QPU Instruction: HAD

HAD gate

The HAD operation (short for Hadamard) essentially creates an equal superposition when presented with either a ∣0⟩ or ∣1⟩ state. This is our gateway drug into using the bizarre and delicate parallelism of quantum superposition! Unlike NOT, it has no conventional equivalent.

In circle notation, HAD results in the output qubit having the same amount of area filled-in for both ∣0⟩ and ∣1⟩, as in Figure 2-7.

Hadamard applied to some basic states
Figure 2-7. Hadamard applied to some basic states

This allows HAD to produce uniform superpositions of outcomes in a qubit; i.e., a superposition where each outcome is equally likely. Notice also that HAD’s action on qubits initially in the states ∣0⟩ and ∣1⟩ is slightly different: the output of acting HAD on ∣1⟩ yields a nonzero rotation (relative phase) of one of the circles, whereas the output from acting it on ∣0⟩ doesn’t.

You might wonder what happens if we apply HAD to qubits that are already in a superposition. The best way to find out is to experiment! Doing so you’ll soon notice that the following occurs:

  • HAD acts on both the ∣0⟩ and ∣1⟩ states separately according to the rules illustrated in Figure 2-7.

  • The ∣0⟩ and ∣1⟩ values this generates are combined, weighted by the amplitudes of the original superpositions.6

Reversibility: Similar to NOT, the HAD operation is its own inverse; applying it twice returns a qubit to its original value.

QPU Instruction: READ

READ

The READ operation is the formal expression of the previously introduced readout process. READ is unique in being the only part of a QPU’s instruction set that potentially returns a random result.

QPU Instruction: WRITE

WRITE

The WRITE operation allows us to initialize a QPU register before we operate on it. This is a deterministic process.


Applying READ to a single qubit will return a value of either 0 or 1 with probabilities determined by (the square of) the associated magnitudes in the qubit’s state (ignoring the relative phase). Following a READ operation, a qubit is left in the state ∣0⟩ if the 0 outcome is obtained and state ∣1⟩ if the 1 outcome is obtained. In other words, any superposition is irreversibly destroyed.

In circle notation an outcome occurs with a probability determined by the filled area in each associated circle. We then shift the filled-in area between circles to reflect this result: the circle associated with the occurring outcome becomes entirely filled in, while the remaining circle becomes empty. This is illustrated in Figure 2-8 for READ operations being performed on two different example superpositions.

The `READ` operation produces random results
Figure 2-8. The READ operation produces random results
Note

In the second example of Figure 2-8, the READ operation removes all meaningful relative phase information. As a result, we reorient the state so that the circle points upward.

Using a READ and a NOT, we can also construct a simple WRITE operation that allows us to prepare a qubit in a desired state of either ∣0⟩ or ∣1⟩. First we READ the qubit, and then, if the value does not match the value we plan to WRITE, we perform a NOT operation. Note that this WRITE operation does not allow us to prepare a qubit in an arbitrary superposition (with arbitrary magnitude and relative phase), but only in either state ∣0⟩ or state ∣1⟩.7

Reversibility: The READ and WRITE operations are not reversible. They destroy superpositions and lose information. Once that is done, the analog values of the qubit (both magnitude and phase) are gone forever.

Hands-on: A Perfectly Random Bit

Before moving on to introduce a few more single-qubit operations, let’s pause to see how—armed with the HAD, READ, and WRITE operations—we can create a program to perform a task that is impossible on any conventional computer. We’ll generate a truly random bit.

Throughout the history of computation, a vast amount of time and effort has gone into developing Pseudo-Random Number Generator (PRNG) systems, which find usage in applications ranging from cryptography to weather forecasting. PRNGs are pseudo in the sense that if you know the contents of the computer’s memory and the PRNG algorithm, you can—in principle—predict the next number to be generated.

According to the known laws of physics, the readout behavior of a qubit in superposition is fundamentally and perfectly unpredictable. This allows a QPU to create the world’s greatest random number generator by simply preparing a qubit in state ∣0⟩, applying the HAD instruction, and then reading out the qubit. We can illustrate this combination of QPU operations using a quantum circuit diagram, where a line moving left to right illustrates the sequence of different operations that are performed on our (single) qubit, as shown in Figure 2-9.

Generating a perfectly random bit with a QPU
Figure 2-9. Generating a perfectly random bit with a QPU

It might not look like much, but there we have it, our first QPU program: a Quantum Random Number Generator (QRNG)! You can simulate this using the code snippet in Example 2-1. If you repeatedly run these four lines of code on the QCEngine simulator, you’ll receive a binary random string. Of course, CPU-powered simulators like QCEngine are approximating our QRNG with a PRNG, but running the equivalent code on a real QPU will produce a perfectly random binary string.

Note

All of the code samples in this book can be found online at http://oreilly-qc.github.io, and can be run either on QPU simulators or on actual QPU hardware. Running these samples is an essential part of learning to program a QPU. For more information, see Chapter 1.

Since this might be your first quantum program (congratulations!), let’s break it down just to be sure each step makes sense:

  • qc.reset(1) sets up our simulation of the QPU, requesting one qubit. All the programs we write for QCEngine will initialize a set of qubits with a line like this.

  • qc.write(0) simply initializes our single qubit in the ∣0⟩ state—the equivalent of a conventional bit being set to the value 0.

  • qc.had() applies HAD to our qubit, placing it into a superposition of ∣0⟩ and ∣1⟩, just as in Figure 2-7.

  • var result = qc.read() reads out the value of our qubit at the end of the computation as a random digital bit, assigning the value to the result variable.

It might look like all we’ve really done here is find a very expensive way of flipping a coin, but this underestimates the power of HAD. If you could somehow look inside HAD you would find neither a pseudo nor a hardware random number generator. Unlike these, HAD is guaranteed unpredictable by the laws of quantum physics. Nobody in the known universe can do any better than a hopeless random guess as to whether a qubit’s value following a HAD will be read to be 0 or 1—even if they know exactly the instructions we are using to generate our random numbers.

In fact, although we’ll properly introduce dealing with multiple qubits in the next chapter, we can easily run our single random qubit program in parallel eight times to produce a random byte. Figure 2-10 shows what this looks like.

Generating one random byte
Figure 2-10. Generating one random byte

This code in Example 2-2 for creating a random byte is almost identical to Example 2-1.

Note that we make use of the fact that QCEngine operations like WRITE and HAD default to acting on all initialized qubits, unless we explicitly pass specific qubits for them to act on.

Note

Although Example 2-2 uses multiple qubits, there are no actual multi-qubit operations that take more than one of the qubits as input. The same program could be serialized to run on only a single qubit.

QPU Instruction: PHASE(θ)

PHASE

The PHASE(θ) operation also has no conventional equivalent. This instruction allows us to directly manipulate the relative phase of a qubit, changing it by some specified angle. Consequently, as well as a qubit to operate on, the PHASE(θ) operation takes an additional (numerical) input parameter—the angle to rotate by. For example, PHASE(45) denotes a PHASE operation that performs a 45° rotation.

In circle notation, the effect of PHASE(θ) is to simply rotate the circle associated with ∣1⟩ by the angle we specify. This is shown in Figure 2-11 for the case of PHASE(45).

Operation of a PHASE(45) operation
Figure 2-11. Action of a PHASE(45) operation

Note that the PHASE operation only rotates the circle associated with the ∣1⟩ state, so it would have no effect on a qubit in the ∣0⟩ state.

Reversibility: PHASE operations are reversible, although they are not generally their own inverse. The PHASE operation may be reversed by applying a PHASE with the negative of the original angle. In circle notation, this corresponds to undoing the rotation, by rotating in the opposite direction.

Using HAD and PHASE, we can produce some single-qubit quantum states that are so commonly used that they’ve been named: ∣+⟩, ∣–⟩, ∣+Y⟩, and ∣–Y⟩, as shown in Figure 2-12. If you feel like flexing your QPU muscles, see whether you can determine how to produce these states using HAD and PHASE operations (each superposition shown has an equal magnitude in each of the ∣0⟩ and ∣1⟩ states).

Four very commonly used single-qubit states
Figure 2-12. Four very commonly used single-qubit states

These four states will be used in Example 2-4, and although one way to produce them is using HAD and PHASE, we can also understand them as being the result of so-called single-qubit rotation operations.

QPU Instructions: ROTX(θ) and ROTY(θ)

We’ve seen that PHASE rotates the relative phase of a qubit, and that in circle notation this corresponds to rotating the circle associated with the ∣1⟩ value. There are two other common operations related to PHASE called ROTX(θ) and ROTY(θ), which also perform slightly different kinds of rotations on our qubit.

Figure 2-13 shows the application of ROTX(45) and ROTY(45) on the ∣0⟩ and ∣1⟩ states in circle notation.

ROTX and ROTY actions on 0 and 1 input states
Figure 2-13. ROTX and ROTY actions on 0 and 1 input states

These operations don’t look like very intuitive rotations, at least not as obviously as PHASE did. However, their rotation names stem from their action in another common visual representation of a single qubit’s state, known as the Bloch sphere. In the Bloch sphere representation, a qubit is visualized by a point somewhere on the surface of a three-dimensional sphere. In this book we use circle notation instead of the Bloch sphere visualization, as the Bloch sphere doesn’t extend well to multiple qubits. But to satisfy any etymological curiosity, if we represent a qubit on the Bloch sphere, then ROTY and ROTX operations correspond to rotating the qubit’s point about the sphere’s y- and x-axes, respectively. This meaning is lost in our circle notation, since we use two 2D circles rather than a single three-dimensional sphere. In fact, the PHASE operation actually corresponds to a rotation about the z-axis when visualizing qubits in the Bloch sphere, so you may also hear it referred to as ROTZ.

COPY: The Missing Operation

There is one operation available to conventional computers that cannot be implemented on a QPU. Although we can make many copies of a known state by repeatedly preparing it (if the state is either ∣0〉 or ∣1〉, we can do this simply with WRITE operations), there is no way of copying some state partway through a quantum computation without determining what it is. This constraint arises due to the fundamental laws of physics governing our qubits.

This is definitely an inconvenience, but as we will learn in the following chapters, other possibilities available to QPUs can help make up for the lack of a COPY instruction.

Combining QPU Operations

We now have NOT, HAD, PHASE, READ, and WRITE at our disposal. It’s worth mentioning that, as is the case in conventional logic, these operations can be combined to realize each other, and even allow us to create entirely new operations. For example, suppose a QPU provides the HAD and PHASE instructions, but NOT is missing. A PHASE(180) operation can be combined with two HADs to produce the exact equivalent of a NOT operation, as shown in Figure 2-14. Conversely, a PHASE(180) instruction can also be realized from HAD and NOT operations.

Building equivalent operations
Figure 2-14. Building equivalent operations

QPU Instruction: ROOT-of-NOT

ROOT-of-NOT

Combining instructions also lets us produce interesting new operations that do not exist at all in the world of conventional logic. The ROOT-of-NOT operation (RNOT) is one such example. It’s quite literally the square root of the NOT operation, in the sense that, when applied twice, it performs a single NOT, as shown in Figure 2-15.

An impossible operation for conventional bits
Figure 2-15. An impossible operation for conventional bits

There’s more than one way to construct this operation, but Figure 2-16 shows one simple implementation.

Recipe for root-of-not
Figure 2-16. Recipe for ROOT-of-NOT

We can check that applying this set of operations twice does indeed yield the same result as a NOT by running a simulation, as shown in Example 2-3.

In circle notation, we can visualize each step involved in implementing an RNOT operation (a PHASE(90) between two HADs). The resulting operation is shown in Figure 2-17.

Circle notation visualization of the ROOT-of-NOT operation
Figure 2-17. Function of the ROOT-of-NOT operation

Following the evolution of our qubit in circle notation helps us see how RNOT is able to get us halfway to a NOT operation. Recall from Figure 2-14 that if we HAD a qubit, then rotate its relative phase by 180°, another HAD will result in a NOT operation. RNOT performs half of this rotation (a PHASE(90)), so that two applications will result in the HAD-PHASE(180)-HAD sequence that is equivalent to a NOT. It might be a bit mind-bending at first, but see if you can piece together how the RNOT operation cleverly performs this feat when applied twice (it might help to remember that HAD is its own inverse, so a sequence of two HADs is equivalent to doing nothing at all).

Reversibility: While RNOT operations are never their own inverse, the inverse of the operation in Figure 2-16 may be constructed by using a negative phase value, as shown in Figure 2-18.

Inverse of RNOT
Figure 2-18. Inverse of RNOT

Although it might seem like an esoteric curiosity, the RNOT operation teaches us the important lesson that by carefully placing information in the relative phase of a qubit, we can perform entirely new kinds of computation.

Hands-on: Quantum Spy Hunter

For a more practical demonstration of the power in manipulating the relative phases of qubits, we finish this chapter with a more complex program. The code presented in Example 2-4 uses the simple single-qubit QPU operations introduced previously to perform a simplified version of Quantum Key Distribution (QKD). QKD is a protocol at the core of the field of quantum cryptography that allows the provably secure transmission of information.

Suppose that two QPU programmers, Alice and Bob, are sending data to each other via a communication channel capable of transmitting qubits. Once in a while, they send the specially constructed “spy hunter” qubit described in Example 2-4, which they use to test whether their communication channel has been compromised.

Any spy who tries to read one of these qubits has a 25% chance of getting caught. So even if Alice and Bob only use 50 of them in the whole transfer, the spy’s chances of getting away are far less than one in a million.

Alice and Bob can detect whether their key has been compromised by exchanging some conventional digital information, which does not need to be private or encrypted. After exchanging their messages, they test a few of their qubits by reading them out and checking that they agree in a certain expected way. If any disagree, then they know someone was listening in. This process is illustrated in Figure 2-19.

The quantum spy hunter program
Figure 2-19. The quantum spy hunter program

Here’s the code. We recommend trying out Example 2-4 on your own, and tweaking and testing like you would with any other code snippet.

In Example 2-4, Alice and Bob each have access to a simple QPU containing a single qubit, and can send their qubits along a quantum communication channel. There might be a spy listening to that link; in the sample code you can control whether or not a spy is present by toggling the spy_is_present variable.

Note

The fact that quantum cryptography can be performed with such relatively small QPUs is one of the reasons why it has begun to see commercial application long before more powerful general-purpose QPUs are available.

Let’s walk through the code one step at a time to see how Alice and Bob’s simple resources allow them to perform this feat. We’ll refer to comments from the code snippet as markers:

// Generate two random bits

Alice uses her one-qubit QPU as a simple QRNG, exactly as we did in Example 2-2, generating two secret random bits known only to her. We denote these send_val and send_had.

// Prepare Alice’s qubit

Using her two random bits, Alice prepares the “spy hunter” qubit. She sets it to value, and then uses send_had to decide whether to apply a HAD. In effect, she is preparing her qubit randomly in one of the states ∣0⟩, ∣1⟩, ∣+⟩, or ∣–⟩, and not (yet) telling anyone which of the states it is. If she does decide to apply a HAD, then if Bob wants to extract whether she intended a 0 or 1, he will have to apply the inverse of HAD (another HAD) before performing a READ.

// Send the qubit!

Alice sends her qubit to Bob. For clarity in this example, we are using another qubit to represent the communication channel.

// Activate the spy

If Alice were transmitting conventional digital data, the spy would simply make a copy of the bit, accomplishing their mission. With qubits, that’s not possible. Recall that there is no COPY operation, so the only thing the spy can do is READ the qubit Alice sent, and then try to carefully send one just like it to Bob to avoid detection. Remember, however, that reading a qubit irrevocably destroys information, so the spy will only be left with the conventional bit of the readout. The spy doesn’t know whether or not Alice performed a HAD. As a result, he won’t know whether to apply a second (inverting) HAD before performing his READ. If he simply performs a READ he won’t know whether he’s receiving a random value from a qubit in superposition or a value actually encoded by Alice. This means that not only will he not be able to reliably extract Alice’s bit, but he also won’t know what the right state is to send on to Bob to avoid detection.

// Receive the qubit!

Like Alice, Bob randomly generates a recv_had bit, and he uses that to decide whether to apply a HAD before applying a READ to Alice’s qubit, resulting in his value bit. This means that sometimes Bob will (by chance) correctly decode a binary value from Alice and other times he won’t.

// If the had setting matches between sender and receiver but the val does not, there’s a spy!

Now that the qubit has been received, Alice and Bob can openly compare the cases in which their choices of applying HADs (or not) correctly matched up. If they randomly happened to agree in both applying (or not applying) a HAD (this will be about half the time), their value bits should match; i.e., Bob will have correctly decoded Alice’s message. If in these correctly decoded messages their values don’t agree, they can conclude that the spy must have READ their message and sent on an incorrect replacement qubit to Bob, messing up his decoding.

Conclusion

In this chapter we introduced a way to describe single qubits, as well as a variety of QPU instructions to manipulate them. The random property of the READ operation was used to construct a quantum random number generator, and control over the relative phase in a qubit was used to perform basic quantum cryptography.

The circle notation used to visualize the state of a qubit is also used extensively in the chapters ahead. In Chapter 3, we will extend circle notation to deal with the behavior of multi-qubit systems, and introduce new QPU operations used to work with them.

1 In this book we’ll try very hard not to think too much about what qubits really are. Although this may seem a little anticlimactic, it’s worth remembering that conventional programming guides almost never revel in the fascinating physical nature of bits and bytes. In fact, it’s precisely the ability to abstract away the physical nature of information that makes the writing of complex programs tractable.

2 Although we introduce the idea of relative phase in terms of relative distances traveled by light, it is a general concept that applies to all flavors of qubits: photons, electrons, superconductors, etc.

3 The sum of the register’s magnitudes must always sum up to 1. This requirement is called normalization, for more on this see “Caveat 2: The requirement for normalized vectors”.

4 That only relative phases are of importance stems from the underlying quantum mechanical laws governing qubits.

5 Throughout the book we will use the terms QPU instruction and QPU operation interchangeably.

6 For details on the mathematics behind HAD and other common operations, see Chapter 14.

7 We will see that being able to prepare an arbitrary superposition is tricky but useful, especially in quantum machine-learning applications, and we introduce an approach for this in Chapter 13.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset