How To Program A Quantum Computer
How To Program A Quantum Computer
5) COMPLEX NUMBERS
The phase of Hilbert space
6) DECOHERENCE
Environmental decoherence
The coldest place in the universe
The race against time
7) THE QUBIT
Boolean logic
Reversible computing
The CNOT gate
The Toffoli gate is your friend
Copying a qubit
Entanglement and the CNOT gate
The Oracle
Inversion around the average
Programming a real quantum algorithm on a real quantum computer
Some final thoughts
By the rules of the vector space, we can apply the parallelogram rule to
add those two vectors, thereby producing a new, third vector :
As you can see in the previous diagram, the parallelogram rule has been
applied, thereby adding the two state vectors. As a result, a third vector has
been generated which represents the addition of the red and blue state
vectors. It is as though the particle has a mix of properties: as though it is
both red and blue at the same time.
Initially, this might not seem like a particularly remarkable result. After
all, colours can be mixed together (when mixing paint, for example).
However, we have to remember that other properties of a particle — for
example, its position or momentum — are also described by state vectors in
vector space. In that case, how on Earth could a particle have two values of
position which are "mixed together"? Can a particle really be in two places
at once? This appears to be what the mathematics of vector spaces is telling
us.
Well, yes, amazing though it may appear, sometimes a particle can
behave as though it is in two places at once. The most famous experiment
revealing this behaviour is the double-slit experiment in which a beam of
light is directed towards two narrow slits, with a screen positioned on the
other side of the slits. The light beam passes through both of the slits,
generating an interference pattern on the screen due to the wavelike nature
of light:
|red 〉
or some other common examples:
term on the right-hand side of the equation represents the rate of change
of the wavefunction with respect to time, the units of which are "inverse
time", or "one divided by time". Therefore, it appears the Schrödinger
equation is mistakenly comparing apples with oranges! The solution comes
by multiplying the right-hand side by the reduced Planck constant, which
has just the right units to convert the right-hand side of the equation into a
value with units of energy. The resultant Schrödinger equation is then
correctly comparing apples with apples, and has therefore become
dimensionally correct.
This powerful type of analysis comparing the units on both sides of an
equation is called dimensional analysis .
You will see, though, that if you just replace the left-hand side of the
equation (the sum of the kinetic and potential energy) with the total energy,
H , you get back to the form of the Schrödinger equation presented earlier .
In their book Quantum Mechanics: The Theoretical Minimum , Leonard
Susskind and Art Friedman describe this as the "iconic form of the
Schrödinger equation that appears on T-shirts":
With reference to the logo on the T-shirt, hopefully you should now
understand all of the parts of the equation on the T-shirt .
The complete predictability of quantum mechanics
Now, here's a strange thing.
If you have read popular science books about quantum mechanics,
probably the first thing you are told is that quantum mechanics leads to
inevitable fundamental indeterminacy, or randomness. Einstein referred to
"God playing dice". A quantum world is a random world.
In contrast to this indeterminacy of quantum mechanics, Newton's laws
of motion are presented as the ultimate in determinacy: once the initial
conditions of a system are set, the system proceeds in an entirely
predictable manner according to Newton's laws. Applied to the universe as
a whole, this predictability due to Newton is called the "clockwork
universe".
So, according to many accounts, the contrast could not be greater: on one
side we have the predictability of Newton, and on the other side there is the
randomness of quantum mechanics.
But, hold on.
You can see that if we consider the Schrödinger equation which describes
the evolution of the quantum state, we find not a hint of unpredictability in
it. There are no undefined variables in the equation, no random number
generators. In fact, it would appear that the behaviour of the quantum state
is completely deterministic — not random at all, and not what you might
expect from "fundamentally random" quantum mechanics. Roger Penrose
describes this surprising feature of the Schrödinger equation in his book The
Road To Reality : "One thing that we note is that it is a deterministic
equation (the time-evolution being completely fixed once the state is known
at any one time). This may come as a surprise to some people, who may
well have heard of quantum uncertainty."
In fact, the equation appears to imply that the evolution of the quantum
state is just as predictable as the evolution due to Newton's laws. So, what's
up?
Well, it is completely true: the evolution of the quantum state as
described by the Schrödinger equation is just as predictable and
deterministic as Newton's laws. If you set the initial state, then the quantum
state will evolve in an entirely deterministic manner — according to the
equation. In fact, it seems like a perfect analogue to the determinism of
Newton's laws.
So, once again, we find that the quantum world is not "weird" at all.
It is this determinism and predictability of the Schrödinger equation
which allows the precise manipulation of the quantum state in quantum
computers: if you change the energy, you know precisely how the quantum
state will change. You have complete control.
It therefore appears that quantum behaviour is completely predictable. So
why do hear about the "randomness" of quantum mechanics?
That is what we will discover in the next chapter …
4
OBSERVING A QUANTUM
SYSTEM
In the previous chapter, the Schrödinger equation was introduced and it
was explained how it describes the evolution of the quantum state. It was
also explained how the Schrödinger equation was completely deterministic
and predictable. So why do hear about the "randomness" of quantum
mechanics?
Well, we have to remember that the quantum state vector we have
considered so far represents the state of the system before it is observed ,
before we measure the system and obtain the value for some property of the
system. As explained earlier, before observation, the quantum state can be
in a mix of states: both red and blue at the same time. However, when we
observe the system, we only ever observe a particle to be in a single, well-
defined state: either red or blue — but not both. For example, in the
double-slit experiment, we would only ever observe the particle to be in a
single position — not in two places at once.
So, when we observe a quantum system, it is as if the state vector in a
superposition state "collapses" to just a single value. This process has been
called the "collapse of the wavefunction", and it is illustrated in the
following diagram :
In the previous diagram, you can see what happens during the act of
observation of a quantum system. You will obtain an observed value (either
red or blue) but, as described earlier, the measurement operation also has
the effect of modifying the state vector so that the vector moves from its
superposition state to point at the value which has been observed (either red
or blue).
So there is a rotation of the state vector from its superposition state to
point to a well-defined single state.
Some new terminology needs to be introduced at this point. Firstly, the
"well-defined" states — the states which we can possibly observe when we
make a measurement — are called eigenstates . Secondly, the state vector
which represents a particular eigenstate is called an eigenvector . So, when
we make a measurement, the state vector rotates to become an eigenvector,
and the system is then said to be in an eigenstate .
Now we get to the crucial question: what determines which of the
possible states we will observe when we measure the system? As an
example, in the previous diagram, it seems that the state vector could just as
easily have rotated to become a blue state. In that case, why was red
selected?
The answer is that the observed value is determined by the projection of
the state vector onto the relevant eigenvector. This is perhaps easiest
explained by the following diagram:
You can see from the previous diagram that — to calculate the projection
— a dotted line is first drawn from the tip of the state vector to make a right
angle with the eigenvector under consideration (in this particular diagram,
the eigenvector is the horizontal line). The projection is then defined as the
distance along the eigenvector (along the bottom horizontal) until you reach
the dotted line. This is all clearly shown on the previous diagram.
Crucially, the value of the projection then represents the probability of
that particular eigenstate being selected. So it is at this observation stage
that probability enters quantum mechanics .
Let us consider an example of how to calculate the value of the
projection. Consider the following diagram. A state vector is shown with
the tip of the vector at the coordinates (0.6, 0.8):
The vector is 1.0 units in length, in which case it is called a unit vector
(state vectors are usually defined as unit vectors).
We would like to know what value we will obtain when we observe this
particular quantum system. Will the state vector rotate so that it points in
the vertical direction (towards red), or will it rotate so that it points in the
horizontal direction (towards blue)? In other words, will we observe the
particle described by this quantum state vector to be either red or blue?
Well, as stated earlier, the selection of the observed value is determined
by the projection of the state vector onto the relevant eigenvector. From the
diagram, it is easy to see the relevant projection values from the coordinates
of the state vector. You can see the projection along the red eigenvector is
0.8 units, and the projection along the blue eigenvector is 0.6 units .
Remember, these projection values merely give you the probabilities of
a particular value being obtained after observation. However, in that case,
then these particular values of 0.8 and 0.6 cannot be quite correct. We see
that there are only two possible outcomes from the observation: either we
observe red, or we observe blue. With only two possible outcomes, we
know that the sum of the probabilities of these two outcomes must be 1.0
(as probabilities always sum to 1.0). So, to calculate the correct
probabilities, we have to take the square of the projection values, which
gives us 0.64 (which is 0.8×0.8) and 0.36 (which is 0.6×0.6). You can see
that these new values do, indeed, sum to 1.0. So these are the correct
probabilities: the probability of observing red will be 0.64, and the
probability of observing blue will be 0.36.
If you consider the previous diagram, you can easily see why this
"squaring" rule will always lead to the sum of the probabilities being equal
to 1.0. It is due to Pythagoras's theorem: the length of the state vector has
been set to 1.0. The state vector is on the hypotenuse of a right-angled
triangle, so the sum of the squares of the other two sides of the triangle (the
sum of the squares of the two projection values) must therefore be equal to
1.0. [3]
So it is during the act of observation that probabilities enter quantum
mechanics, and that is the reason why quantum mechanics has obtained its
reputation for randomness and indeterminism .
In the chapters so far, we have seen that the quantum state vector exists in
a "vector space", and it can rotate in that vector space. The particular type
of vector space which is used in quantum mechanics is called a Hilbert
space (named after the great German mathematician David Hilbert, who we
have encountered in my previous books).
The important feature of a Hilbert space is that the coordinates of points
can be complex numbers. Therefore, the coordinates describing quantum
state vectors can be complex numbers .
Environmental decoherence
In 1970, Dieter Zeh of the University of Heidelberg published a paper
which suggested a mechanism which could explain the mysterious
"collapse of the wavefunction". [6] To understand Dieter Zeh's idea, we
need to return to Chapter Two of this book in which it was explained that
quantum mechanics inevitably arises when we take a more realistic and
sophisticated picture of how the world must work.
In Chapter Two, two fundamental principles were presented:
It is the second of these two principles which formed the basis for Dieter
Zeh's explanation of the apparent "collapse of the wavefunction". It is the
principle that objects are never completely isolated from the rest of the
universe. The idea that our world is split into entirely discrete, isolated
objects is a completely human invention. Think of yourself sitting in your
chair. You probably think of yourself as being a separate object from the
chair. But the atoms of your backside are pressed against the atoms of the
chair, in constant contact. So where is the division? Does that not represent
one joined unit? And we could continue the process: your chair is in direct
contact with the floor. The floor is directly connected to the Earth. Again,
where is the division? How can you justify thinking that you are an entirely
separate object from the chair or the planet?
As another example, you breathe air. The air is part of a continuous
substance which surrounds the planet and places you in contact with every
other living person. In that case, how can you consider yourself to be an
entirely separate object?
Of course, you should not think of yourself as being isolated. As has just
been described, this was the belief of the Futurist modern art movement.
And it is this principle which led Dieter Zeh to propose a mechanism
behind the apparent "collapse of the wavefunction". In particular, Dieter
Zeh suggested that it was the ever-present environment surrounding an
object — constantly "observing" the object — which leads to the object
being taken out of its superposition state.
The mechanism is called decoherence .
And, once again, we encounter the idea that quantum mechanics is more
of a "state of mind", representing a more sophisticated picture of how the
world must work. In this case, the principle is that no object is ever
completely isolated from the rest of the world.
Unfortunately, Dieter Zeh passed away earlier this year. He should surely
have received the Nobel Prize in his lifetime for his work in proposing
decoherence, which has now been firmly experimentally verified.
We can use a simple thought experiment to explain how decoherence
works, and the thought experiment we will use is the double-slit experiment
which was described in Chapter Two.
In the double-slit experiment, a beam of light is directed towards two
narrow slits, with a screen positioned on the other side of the slits. The light
beam passes through both of the slits.
If, at the screen, a peak in the wave from one slit coincides with a peak in
the wave from the other slit, then a bright point appears on the screen.
Conversely, if a peak in the wave from one slit coincides with a trough in
the wave from the other slit, the two waves will cancel at that point and no
bright point will appear on the screen. Therefore, as shown in the following
diagram, a regular pattern of dark and bright bands will appear on the
screen:
The light intensity is then reduced so that only a single photon of light is
in transit at a time. Over time, a pattern develops on the screen as the
individual photons strike the screen and accumulate. Amazingly, though,
the interference pattern is still produced — even though there is only one
photon in the system at a time! It is as if the single photon passes through
both slits and interferes with itself !
Let us now make the experiment rather more realistic. We must realise
that particles, in general, are never completely isolated from their
environment, and that environment will tend to be rather messy and
random. Let us consider the effect of the messy, random environment on the
single photon.
In a more realistic picture, on its way from the light source to the screen,
the photon has to pass through a considerable distance of air (representing
the "environment"). That air is likely to be turbulent, moving randomly. As
it moves along its path, the photon interacts with the atmosphere, and it, in
return, is affected by the atmosphere. The effect of the random, turbulent
atmosphere will be to slightly increase or decrease the length of the
particle's path to the screen.
Chad Orzel describes the situation in an article about decoherence on his
website: "If we're talking about a long distance in a turbid medium, there's
going to be a phase shift. If you think in terms of waves, there are going to
be interactions along the way that slow down or speed up the waves on one
path or the other. This will cause a shift in the interference pattern,
depending on exactly what happened along the way. Those shifts are really
tiny, but they add up. If you're talking about a short interferometer in a
controlled laboratory setting, there won't be enough of a shift to do much,
but if you're talking about a really long interferometer, passing through
many kilometers of atmosphere, it'll build up to something pretty
significant." [7]
The effect of this shortening and lengthening of the path is to very
slightly move the position of the peaks and troughs of the light wave.
Because the positions become random, the effect is to destroy the
interference pattern on the screen.
And, if the interference pattern is destroyed on the screen, that makes it
appear as though the photon only went through one slit. Remember, the
interference pattern is caused by a photon apparently going through two
slits at once. So if the interference pattern is destroyed then the photon no
longer appears to be in a superposition state, no longer going through two
slits at once.
So we can treat the interference pattern as being an indirect signature of
the photon being in a superposition state. And the presence or absence of
the interference pattern indicates whether or not the particle is in a
superposition state.
At this point, we can put our Sherlock Holmes deerstalker hat on and
deduce what is responsible for the apparent "collapse of the wavefunction",
taking the particle out of its superposition.
The initial arrangement of the double-slit experiment produced an
interference pattern on the screen. From that interference pattern, we
deduce that the particle is in a superposition state.
The second form of the experiment was more realistic, introducing a
random environment. The result was that the interference pattern was
destroyed. We therefore deduce that the particle is not longer in a
superposition state.
So what was responsible for taking the particle out of its superposition
state? In other words, what was responsible for the apparent "collapse of the
wavefunction" which seems to have occurred ?
Well, with our Sherlock Holmes hats on, we observe that the only factor
that has changed in the two versions of the experiment has been the
introduction of a noisy, messy, random environment. Sherlock Holmes once
famously said: "when you have eliminated the impossible, whatever
remains, however improbable, must be the truth". On that basis, we have all
the information we need to make our final deduction: The apparent
"collapse of the wavefunction" is caused by a particle interacting with
the noisy, messy, random environment.
And that is the principle behind decoherence.
But what is actually going on at the microscopic level? How is the
random environment capable of taking a single particle out of its
superposition state? Well, if all we are interested in is building a quantum
computer, or programming a quantum computer, we really don't care. All
we have to do is make sure we isolate our quantum computer from its
surrounding environment in order to maintain particles in their
superposition state. And it is quite fortunate that we don't need to know
precisely what is happening at the quantum level because the science of
decoherence is not yet fully understood. [8]
However, we do have a fair idea of the sort of process which must be
occurring at the microscopic level. Let us consider a particle hitting a screen
(with the screen representing the "environment"). It must be realised that
the screen is already in a well-defined, classical state. In other words, the
screen is not in a superposition state — it is in an eigenstate, meaning the
screen only exists in one position. When the particle hits the screen
(interacts with the environment), the state vector of the particle finds itself
in competition with the state vector of the much larger environment. This
means the state vector of the particle — in its superposition state — is
going to be affected more than the state vector of the screen.
There will be components of the particle state vector which do not agree
with the state vector of the screen. Those components disperse into the
wider, noisy environment, much like the splash from a stone thrown into a
rough, turbulent ocean. The Schrödinger equation continues to apply to all
those components, controlling their evolution in time. In other words, there
is no new sudden mystical "collapse" process. However, once those
components have dispersed into the "ocean", it becomes effectively
impossible to regenerate the original superposition state (this explains why
the apparent "collapse of the wavefunction" is an irreversible process).
Those superposition elements — to all intents and purposes — disappear.
The particle is then detected in only one position: on the screen.
In his book, The Fabric of the Cosmos , Brian Greene describes the
process: "Decoherence forces much of the weirdness of quantum physics to
leak from large objects since, bit by bit, the quantum weirdness is carried
away by the innumerable impinging particles from the environment."
So it is almost impossible to completely isolate an object from the rest of
the world. But if we want to maintain a superposition state inside our
quantum computer, we need to find some way of achieving that isolation,
and thereby preventing that onset of decoherence. Let us now examine how
we can achieve that isolation .
Entanglement
Now we have come to the end of this chapter on the qubit, it seems like a
good time to introduce the important concept of quantum entanglement .
In order to understand entanglement, we shall consider a qubit which is
formed from the property of particle spin. Particles can have the property of
spin, which has the units of angular momentum just like classical spin —
the spin of a ball, for example. [10] However, when we measure the
quantum spin of a particle we find it can only have two values: either spin-
up , or spin-down . As these are the only two states we will find after
measurement, this makes particle spin ideal to be used as a qubit. As an
〉
example, we might interpret the |0 state of our qubit as being spin-up, and
〉
the |1 state as being spin-down.
If a pair of photons is released by a common source, then the properties
of those two photons will be related. This is because of the conservation
laws. For example, the law of conservation of momentum states that the
momentum before the photons were emitted must be equal to the total
momentum after the photons were emitted. As the momentum before the
photons was zero (nothing was moving), the total momentum after the
photons are emitted must also be zero. This requirement can be satisfied if
the photons are emitted in precisely opposite directions, because
momentum is a vector quantity (like an arrow) and if we add two arrows
pointing in precisely opposite directions then we get zero.
The law of conservation of angular momentum imposes a similar
constraint of the spin of the photons. As the angular momentum before the
photons were emitted was zero (nothing was spinning), then the angular
momentum after the spin of the two photons is measured must also be zero.
This requirement can be satisfied if the spin of the two particles is opposite:
one of the particles being spin-up, and the other particle being spin-down.
When the properties of two separate particles are correlated in this
manner, the particles are said to be entangled .
When the two particles are emitted, however, they are both in a
〉 〉
superposition state, an equal mix of state |0 (spin-up) and state |1 (spin-
down). Let us imagine what happens when we measure the spin of one of
the particles and find it to be in the spin-down state. That implies that the
state vector of that particle has rotated from its superposition state (an equal
mix of states) to point toward the spin-down state. At that point, we then
know with 100% certainty that when the spin of the second particle is
measured it will be found to be spin-up (it must have the opposite spin
value). This implies that the state vector of the second particle has also
rotated — to point to the spin-up state. When the spin of the second particle
is then measured, it will be found to be spin-up with 100% certainty.
These two rotations of the two state vectors (from the superposition state
to spin-down and spin-up) are shown in the following diagram :
In this Composer window, you can see the five horizontal lines,
reminiscent of a piece of sheet music (hence the name "Composer").
Each of the five horizontal lines represents one qubit (because the
quantum processor available to you currently has five qubits — see the
photograph of the actual five-qubit IBM quantum processor in Chapter
Seven). You can see on the left side of each of the lines that each qubit
〉
starts in state |0 .
Individual processing units which modify the state of each qubit are
called quantum gates . You can see the gates listed as coloured square boxes
on the right-hand side of the Composer:
As the qubits pass along the horizontal lines from left to right, the qubits
are processed by these gates. The final structure then forms what is known
as a quantum circuit . The combination of gates provided for you by IBM
form the standard set of gates with the whole package being known as the
standard circuit model . Because of this, even if IBM one day discontinue
the Q Experience, your experience of using it — and the standard circuit
model described in this book — should remain relevant.
Let us now consider a few of these quantum gates, and show how they
can be used to modify the state of a single qubit .
X: The bit-flip
The first quantum gate we will be considering is the X gate. The X gate
is listed as one of the square boxes in the IBM Q Experience (see the
previous diagram).
As stated earlier in this book, these quantum gates work by rotating the
quantum state vector of a qubit. These rotations can be understood by
considering the Bloch sphere as having three axes, as shown in the
following diagram:
The X gate performs a rotation of 180° around the X-axis, as shown in
the following diagram :
As you can see from the previous diagram, the effect of this rotation
〉 〉
around the X-axis is to invert (or "flip") the state vector from |0 to |1
〉 〉
(or vice versa from |1 to |0 ).
The use of the X gate is shown in the following video which I recorded
using the IBM Q Experience. You might want to sign-in to the Q
Experience and perform the same experiment, or else you can just watch the
video.
Here is a link to the video on YouTube:
http://tinyurl.com/quantumxgate
In the video, you will see that I actually use the simulator rather than the
real quantum computer. You might find it more convenient to use the
excellent simulator for development purposes. This is because a real
quantum processor has qubits on a two-dimensional plane — the surface of
the chip — so not every qubit can be connected to every other qubit (refer
back to the photograph of the actual IBM processor in Chapter Seven). This
places some limitations on which qubit can be connected to which other
qubit, and you might find this rather frustrating when you are developing on
the real quantum computer (it will sometimes not give you the option of
connecting certain qubits to certain other qubits). In contrast, if you use the
simulator, there are no restrictions on connectivity. Also, if you use the
simulator, you get your results immediately, rather than having to wait for
your experiment to be queued before it is run on the real quantum computer.
However, for our next experiment, we will be using the real quantum
computer …
The Z gate performs a rotation of 180° around the Z-axis (just as the X
gate performs a rotation of 180° around the X-axis, and the Y gate performs
a rotation of 180° around the Y-axis).
Additional quantum gates in the IBM Q Experience include the S gate
(which performs a rotation of 90° around the Z-axis — as shown in the
previous diagram), and the T gate (which performs a rotation of 45° around
the Z-axis).
So all three of these gates — Z, S, and T — act to change the phase of
the qubit.
9
MULTIPLE-QUBIT QUANTUM
GATES
In the previous chapter we considered the simple quantum gates which
take a single qubit as input, and output a single qubit. However, in this
chapter, we will see that if we want to do anything interesting with our
quantum computer we will require more complex gates which can take two
qubits as input, allowing us to compare and combine the values of those
qubits.
These type of multiple-input gates are the simple logical units which
form the basis of today's conventional classical computers. So let us start
this chapter by examining those conventional logic gates.
Boolean logic
Since the time of Aristotle in 350 B.C., logical reasoning had been the
preserve of the philosophers, with their reasoned arguments. For example:
Socrates is a man.
All men are mortal.
Therefore, Socrates is mortal .
In the case of this argument, logical reasoning is used to prove that the
statement "Socrates is mortal" is a true statement.
In 1847, the British mathematician George Boole wrote a book entitled
Mathematical Analysis of Logic which attempted to place logic on a firm
mathematical footing. George Boole realised that he could create a
mathematical system of logic by substituting the number 1 for TRUE and
the number 0 for FALSE. These TRUE and FALSE numerical values could
then be combined according to strict mathematical laws. The ambiguous
arguments of the philosophers could then be replaced by clear, unarguable,
mathematical operations.
George Boole's method is now called Boolean logic .
But what George Boole could never have anticipated was the role that his
Boolean logic would play in digital computers (which, of course, were not
around in the mid-19th century). At their core, digital computers are based
on binary arithmetic, with data bits which can only take the values of 0 or 1.
These values, therefore, were a perfect match for the TRUE and FALSE
values of George Boole's logical system. When digital computers were
invented, George Boole's system was seamlessly integrated. As a result,
Boolean logic and Boolean logic gates now form the basis of today's digital
computers. The logical thought processes of philosophers such as Aristotle
could now be replicated by an automated process — with Boolean logic
essentially forming the "thought processes" of the modern computer.
So let us now consider the range of available Boolean logic gates. A
logic gate is a device which takes one or more inputs (which can be either 0
or 1), performs some logical operation on those inputs, and produces a
single output (which will be either 0 or 1) .
Firstly, there is the NOT gate. The NOT gate is unique among Boolean
gates in that it only takes one input (which would be either 0 or 1). The
output of the gate is then the inverse of the input (the output is 1 if the input
is 0, and vice versa).
Here is the symbol for the NOT gate:
And here is the truth table for the AND gate. Note that the output is 1
only if both of its inputs are equal to 1:
Now let us consider the OR gate. The OR gate gives an output of 1 if
either input A or input B is equal to 1. Otherwise, the output of the OR
gate is 0.
Here is the symbol for the OR gate:
And here is the truth table for the OR gate. Note that the output is 1 only
if at least one of the inputs is equal to 1 :
Next, there are the NAND and NOR gates. The NAND gate is similar to
the AND gate except its output is inverted (the opposite of the AND gate
output). While the NOR gate is similar to the OR gate except its output is
inverted (the opposite of the OR gate output).
Here is the symbol for the NAND gate:
And here is the truth table for the NAND gate. Note that the output is the
opposite of the output of the AND gate :
Finally, here is the symbol for the NOR gate:
And here is the truth table for the NOR gate. Note that the output is the
opposite of the output of the OR gate:
The NAND and NOR gates are particularly important because they are
universal gates. The term "universal" is used when an object can do
anything, it can behave like any other object. A universal logic gate can
behave like any other logic gate, and that makes it very useful and very
powerful.
Let us consider examples of how a NAND gate or a NOR gate can act
like a different gate. Firstly, if both of the inputs to a NAND or NOR gate
are the same, then the gate will act like a NOT gate. You might like to
check that this is the case by looking back to the truth table for the NAND
or NOR gate and seeing that when both inputs are 1 then the output will be
0, and when both inputs are 0 then the output will be 1. Hence, the
(inverting) behaviour is the same as a NOT gate. This is shown in the
following diagram :
The previous diagram shows how a NAND gate with both its inputs tied
together will behave like a NOT gate. This is one example of the
universality of the NAND gate.
A more complex example of universality is presented in the following
diagram. You might like to check from the truth tables that the following
circuit of three NAND gates performs like an OR gate:
And here is the truth table for the XOR gate. Note that the output is the 0
when both of the inputs are equal to 1:
Reversible computing
Our next task is to implement these Boolean logic gates on a quantum
computer. We would then have a guarantee that our quantum computer
would be able to solve the problems which could be solved using a
conventional classical computer. However, in this section we will see that
implementing these Boolean gates on a quantum computer is rather tricky.
Let us first consider the AND gate. Here is the truth table for the AND
gate which was presented earlier:
As you can see from the previous table, the AND gate takes two inputs
but has only one output. As a consequence, if you were only presented with
the output of the gate, you would not be able to say what that the inputs had
been with complete certainty. As an example, you can see from the previous
table that if you knew the output of an AND gate was 0, you would not be
able to tell if the inputs had been 0 and 1, or both inputs had been 0. In both
of those cases, the output of the AND gate would have been 0. In other
words, some knowledge — some information — would have been lost
through the application of the gate.
This type of two-input Boolean gate is therefore not reversible. If all we
know is the output, we cannot "wind the gate back" and get the input
values.
This might not seem like a very big deal for a conventional computer.
After all, logic gates in a conventional computer only ever have to work in
the forward direction. However, it represents a serious problem for a
quantum computer.
The problem is due to the nature of the Schrödinger equation which we
considered in Chapter Three. If you remember, the Schrödinger equation
determines the behaviour of the quantum state vector over time, and the
Schrödinger equation states that this motion is proportional to the total
energy of the system (the Hamiltonian). As a result, the rotation of the state
vector is linear: smooth and predictable. This is called unitary evolution .
This also means that means the motion of the state vector is completely
reversible: you could theoretically "wind back" the rotation of the state
vector to an earlier state. Because of this reversibility of the Schrödinger
equation, all quantum logic gates must be reversible .
However, as we have just seen in the AND gate example, if information
is lost then the gate is not reversible (the inputs cannot be regenerated from
the outputs). Therefore, we can deduce that no information can be lost in
unitary evolution. [11 ]
This means that, because the AND gate loses information and is therefore
not reversible, it would appear it is not possible to implement an AND gate
as a quantum gate.
Let us examine the truth table for this circuit, now with the two outputs:
We can see from the previous truth table that it is still not possible to
uniquely identify the inputs from the outputs. For example, you can see that
if the outputs of the circuit are 0 and 0, you would not be able to tell if the
inputs had been 0 and 0, or 0 and 1. So information has been lost and that
means that this AND circuit cannot be implemented as a quantum gate.
This is not only the case for the AND gate — you might like to check
that this problem also applies to the OR gate, the NOR gate, and the NAND
gate.
But let us now consider the exclusive-OR (XOR) gate which was
described earlier in this chapter. The following diagram shows an XOR gate
with one of the inputs carried over to the outputs:
Let us examine the truth table for this circuit, now with the two outputs:
In this case, you can see that the two outputs uniquely identify the two
inputs. For example, if the output pair is 1 and 1 then we know that the
input pair was definitely 1 and 0. So no information is lost, which means
this gate is reversible (the inputs can be regenerated from the outputs).
So the XOR gate with one of the inputs carried-over can be implemented
as a quantum gate. That means we have managed to discover a reversible
two-input quantum gate we can use in our quantum computer! Hurrah !
Here is a diagram of this XOR gate circuit as drawn on a quantum circuit
diagram. You will see I have replaced the written "XOR" gate symbol with
the standard symbol for the XOR gate which is a circle with a cross in the
middle:
You will also see that qubit A has been labelled as the "control" qubit,
and qubit B has been labelled the "target" qubit. The reason for this will
now be explained, and we shall see it is because of the way the behaviour of
this gate is interpreted in quantum computing.
This quantum version of the XOR gate is not called an XOR gate in
quantum computing terminology. To see why that is the case, let us examine
the truth table for the XOR gate in this circuit, and see if we can interpret it
in a different way :
From the previous truth table, we can see that if qubit A is equal to 0,
then the output is equal to the value of qubit B. But if qubit A is equal to 1,
then the output is equal to the inverse of qubit B (the NOT operation
applied to qubit B). In other words, the NOT operation is only applied if
qubit A is equal to 1. Therefore, we could interpret the operation as a NOT
operation which is controlled by qubit A.
And, indeed, this is how this XOR circuit is described in quantum
computing: it is called the controlled-NOT gate, or CNOT gate. To recap, in
a CNOT gate, the NOT operation is only applied on a qubit when the value
of another qubit is equal to 1.
The qubit which controls the operation (qubit A in this case) is called the
control qubit, and the qubit which is inverted (qubit B in this case) is called
the target qubit (as shown on the earlier circuit diagram of the CNOT gate).
Here is a diagram of the CNOT gate as shown on a quantum circuit
diagram:
So we have discovered that the CNOT gate is the only reversible two-
input Boolean logic gate which can be implemented as a quantum gate. This
explains the layout of the available gates in the IBM Q Experience :
You can see in the previous image that the range of gates available to you
in the IBM Q Experience (the standard circuit model) include the full range
of single-input quantum gates, plus the only possible two-input quantum
gate: the CNOT gate .
〉
In other words, both input qubits must be in state |1 for the NOT
operation to be performed on the third (target) qubit. If the third qubit is in
〉
initially in state |0 you can see that the Toffoli gate acts like an AND
gate. That is because the third qubit will be inverted — changed to |1 — 〉
〉
only if the other two qubits are both set to |1 . And that is the
characteristic behaviour of an AND gate (output equal to 1 when both
inputs are 1).
What is more, it is easy to see that if the third qubit was initially in state
〉 〉
|1 rather than state |0 then the Toffoli gate would act like a NAND gate
rather than an AND gate. This is because if both input qubits were in state
〉 〉
|1 then the target qubit would be flipped to be in state |0 — and that is
the characteristic behaviour of a NAND gate (output equal to 0 when both
inputs are 1).
That's great, because we know the NAND gate is universal, so we now
have a universal set of quantum gates.
And what makes that such great news is that if we have a universal set of
quantum gates then we have a guarantee that we can calculate anything on
our quantum computer which can be calculated on a classical
computer!
The Toffoli gate is not available as one of the gates within the IBM
Composer. However, it is possible to program it using the available gates as
shown in the following diagram: [12 ]
Now, to finish this chapter, the final two short sections will consider
some of the useful side-products of the CNOT gate.
Copying a qubit
Sometimes while writing a quantum computer program you might find it
useful to copy the value of a variable or qubit. You might imagine it would
be easy to copy the value of a qubit in a quantum circuit diagram simply by
using a T-junction:
However, that is not the case: T-junctions such as the one shown in the
previous diagram cannot exist on quantum circuit diagrams. This is due to
the quantum no-cloning theorem which states that a quantum state cannot
simply be duplicated as shown in the previous diagram.
However, it is possible to use the CNOT gate to copy the value of a qubit
to another qubit, and you will surely find that very useful when you come to
writing your own quantum computing programs.
The qubit-copying process is simple. You need an additional qubit (the
qubit to which the value is going to be copied). You then set the state of that
〉
qubit initially to be |0 , and apply a CNOT gate — as shown in the
following diagram :
〉
You can see from the diagram that if the input qubit A is equal to |0 ,
then the NOT operation will not be applied (because that is how the CNOT
gate works), and so the output copy on the second qubit will remain at its
〉 〉
initial state of |0 . But if the input qubit A is equal to |1 then the state of
〉
the second qubit will be inverted by the CNOT gate to become |1 . So, in
both cases, the value of the input qubit is copied to the output qubit.
The contents of a register represents the piece of data which the computer
is currently processing. Various operations can be applied to the data in the
register as the microprocessor steps through a computer program line-by-
line. These operations would generally be Boolean logic operations such as
AND and NOR which we considered in the previous chapter. In this way,
even complicated computer programs are translated into very many simple
Boolean operations which are then applied to the data stored in a register. In
a modern computer, these operations can be applied in nanoseconds. That is
incredibly quick, but we are still dealing with only one piece of data at a
time. The only reason our computers are as fast as they are is because of
their incredibly fast clock speed (speed at which a single operation is
performed). Otherwise, conventional computers have a processing
bottleneck, only being able to perform one instruction at a time.
However, a quantum computer promises to eliminate that processing
bottleneck by holding multiple copies of its data in a superposition state.
The inputs of a quantum computer are not composed of classical bits —
they are composed of qubits. As an example, you will remember that the
IBM Q Experience quantum processor we have been using so far in this
book has five input qubits. If you look carefully in the composer window
you will see that these are labelled from q[0] to q[4], so we might interpret
those qubits as making a five-qubit register.
And, of course, each of those qubits is capable of being in a superposition
state which effectively means it is holding both the value zero and one at
the same time. We now start to see how a quantum computer can overcome
the processing bottleneck of a conventional computer.
To explain the advantage of a quantum computer, let us first consider an
8-bit classical register. With eight bits, the register is capable of holding 256
different binary numbers (28 equals 256). However, the register can only
hold one of those 256 numbers at a time. In contrast, each of the qubits in a
quantum computer can be both zero and one at the same time, and so a
quantum register can consider all 256 different numbers at the same time.
As in a classical register, we would then apply logic gates to the qubits is
order to process their values (stepping through each step of a computer
program). But, of course, in a quantum computer we would apply our
quantum gates (such as the X, H, and CNOT gates described in the previous
two chapters) instead of the Boolean logic gates (AND, OR, and NOT) we
would apply to a classical register. And those quantum gates would be
being applied to multiple copies of the input data at the same time — you
get so much more processing power for your buck.
And, because the number of possible input combinations rises
exponentially (according to 2N , where N is the number or qubits), a
quantum computer gets very powerful very quickly. As just described, with
eight input qubits there would be 28 different input combinations, which is
equal to 256. And the quantum computer would be able to consider all of
those 256 different input combinations at the same time. With sixteen qubits
there would be 65,536 different input combinations. With 24 qubits there
would be over 16 million different input combinations. And with just 300
qubits, a quantum computer would have more input combinations than there
are atoms in the universe.
Let us now consider the problems we could solve using that tremendous
quantum computing parallelism .
Shor's algorithm
But there is one particular inverse problem which has generated a great
deal of attention and has made quantum computing a front page story. It is
the inverse problem which involves finding the factors of a number.
It is a relatively simple process to multiply two integers (whole numbers)
together, even very large numbers, to produce a larger number. We might
think of the initial two numbers as representing the input to our process, and
the act of multiplying the two numbers together as being the "forward
transformation". It is clearly an easy task for a computer to multiply two
large numbers together. However, the inverse problem then arises when we
consider the resultant large number and want to work backward to
determine the initial two numbers which were multiplied together. The
initial two numbers are called the factors of the larger number, and the
process of determining the two factors is called factorization .
It turns out that there is no quick and easy method to factorize a large
number. In fact, it is incredibly hard and time-consuming — even for the
most powerful classical computer. So, while the forward transformation is
easy (multiplying two numbers together), we do not have access to a
practical inverse transformation. And that should immediately make you
think: "Aha! This sounds like an inverse problem which could be
successfully tackled by a quantum computer!"
Indeed, in 1994, the mathematician Peter Shor developed a quantum
algorithm for factoring large numbers. Shor's algorithm, utilising the power
of quantum parallelism, was far faster than any existing classical algorithm .
For this reason, Shor's algorithm generated a lot of attention and
newspaper column space because it threatened to crack the encryption
technique — known as RSA encryption — used to secure internet
communications. Every time you type "https:" into your internet browser
address bar, you are using RSA encryption to encode your credit card
details (the "s" in "https" stands for "secure"). If Shor's algorithm could
crack RSA encryption, the consequences would be huge.
RSA encryption was first explained in a 1978 scientific paper which
introduced us to two fictional characters named Alice and Bob. The names
"Alice" and "Bob" (instead of just using A and B, as was usual up to that
point) have since become the accepted names which are used in many
thought experiments in physics.
To understand RSA encryption, imagine that Alice and Bob live a
considerable distance apart. Now imagine Bob wants to send an encrypted
message to Alice which only she can read. The conventional method of
sending a coded message would require Alice and Bob to share a key ,
which is known only to themselves. Bob would use the key to encode his
message, and Alice would then use the key to decode Bob's message.
However, transmitting the key between Bob and Alice in advance of the
message is inconvenient and a security risk.
Rather amazingly, RSA encryption does not require the secret sharing of
a key in advance. To understand how RSA encryption works, first Alice
picks two large prime numbers, say, p and q . These two numbers then
represent Alice's private key which she keeps to herself. Alice then
multiplies those two numbers together to form her public key . Alice must
then publish her public key in a publicly-available directory — anyone can
read it. However, Alice's private key still remains completely secure
because of the difficulty of factoring large integers: no eavesdropping third
party can work out what p and q are — Alice's private key — even if they
have access to her public key. Even though the public key contains both p
and q multiplied together!
When Bob wants to send a message to Alice, he encodes it using her
public key, and then sends the message to Alice. This is entirely secure even
if the message is intercepted because the resultant encoded message can
only be decoded using Alice's private key. When Alice receives the
message from Bob, she decodes it using her private key and reads the
message. So encrypted messages using RSA encryption can be sent
between Alice and Bob without the requirement for a secret key to be
exchanged in advance.
It is clear that if Shor's algorithm could factor large numbers, it could
reveal Alice's private key, and RSA encryption — the backbone of internet
security — would no longer be secure.
Peter Shor's algorithm is clearly a tremendous achievement. However, it
is specific to the problem of factoring numbers. The aim of this book,
however, is to present general techniques which IT professionals might use
for their own applications, and thereby take quantum computing into the
mainstream. Though Peter Shor's algorithm is impressive, I suspect most IT
professionals will not be interested in cracking internet security.
With that aim in mind, in the next chapter we will be examining a general
technique for solving inverse problems which might be used in a wide
range of applications .
The Oracle
The structure of Grover's algorithm can be split into two stages. Here is a
block diagram of Grover's algorithm:
You can see from the previous diagram that Grover's algorithm is
composed of two main blocks called "The Oracle" and "Invert around the
average". I am going to jump slightly ahead at this point and show you how
we will be coding these two blocks in the IBM Q Experience later in this
chapter. The two blocks are going to look like this:
We are going to be using Grover's algorithm to find the position of the
queen of spades in the previous playing cards example. As described earlier,
this is a search operation — we have to search to find the queen .
There are four possible positions for the queen, with the positions
numbered 0, 1, 2, or 3. The binary equivalent of those decimal numbers
would be 00, 01, 10, and 11. So the binary numbers have two bits. Hence,
our quantum algorithm is going to have two input qubits which will be
capable of representing all possible positions of the queen (the previous
block diagram of Grover's algorithm can be seen to be based on two qubits).
In this section we will consider the first part of Grover's algorithm,
which, as you can see, is called the Oracle.
The first part of Grover's algorithm is a self-contained "black box" of
code, a code block which — in computer science terminology — is often
referred-to as an "oracle". The term "oracle" seems rather strange, but this is
chosen because the unit is defined only in terms of what it does: its inputs,
and its outputs. We are not particularly interested in the internal workings,
how it derives those outputs from its inputs (this is why it is often called a
"black box" — we do not care what happens inside it). Or, to be more
specific, its internal workings are not defined, and it is left to the
programmers to design how it works internally.
So this "black box" is rather similar to a human "oracle", a visionary or
seer with a crystal ball, who you ask a question and who miraculously gives
you the correct answer (I seem to remember one of the characters in the
Matrix movie was called The Oracle — a lady who provided the answers
for Keanu Reeves). You don't know how the oracle has done it, you don't
know the internal workings — you are just glad it works.
So the first part of Grover's algorithm is an oracle. Let us now consider
what the oracle has to do.
The oracle is presented with the n input qubits in a superposition state,
which represents all possible input combinations (for two qubits, this would
be the four possible states 00, 01, 10, and 11). The oracle then has to
perform the following two tasks:
Oracle Task Number One
The first task for the oracle is that it has to consider the input value (00 or
01, etc.) and perform a calculation to see if that value satisfies the particular
search problem. In this playing card example, the oracle has to decide if the
number represents the correct position of the queen of spades. So the key
task of the oracle is that it has to recognise when a specific combination
of input qubit values solves a problem.
If you wanted to adapt Grover's algorithm to solve your own
problems, you would have to adapt this oracle for your own problem.
You would have to perform some calculation on the input value in order to
test to see if the input value satisfies your search problem. Is it the number
you are looking for? For example, is the number a factor of a larger
number? Does it divide perfectly into the larger number? If so, you've
cracked the code! And, of course, because you are being presented with
your input qubits in a superposition state, your calculation will be working
on all possible combinations at once. Like magic!
Oracle Task Number Two
The second task for the oracle is that when it finds the solution it is
looking for, it has to "mark" the solution. According to Grover's algorithm,
this marking is performed by inverting the phase of the state by rotating it
by 180°. If you rotate anything by 180°, you are making it point in the
opposite direction. So another way of interpreting this "marking" operation
is that it puts a minus sign in front of the state vector .
Let us now consider how we can create a quantum oracle on the IBM Q
Experience.
In our example of finding the queen of spades, we note that in our earlier
diagram we placed the queen at the extreme right-hand position of the four
cards. So we want to create our oracle to detect when this numbered
position is input. You will notice on the earlier diagram of the playing cards
that the right-hand position was given the number 3, which is equivalent to
the binary number 11. So we want our quantum oracle to mark the input
〉
state when both input qubits are in state |1 — and not when the qubits are
in any other state. In other words, we need to invert the phase only when
〉
both input qubits are in state |1 .
It turns out that this phase inversion can be performed by a very simple
circuit. It can, in fact, be done with a single CNOT gate and a couple of
Hadamard gates. The following diagram is the actual circuit diagram for the
Grover's algorithm oracle:
In the example shown in the previous diagram, both inputs are set to state
〉
|1 . As just explained, this is the condition which we want to result in the
phase inversion.
The details of how this oracle circuit was designed are fairly
complicated, so they has been moved to Appendix One at the back of this
book .
But we now have the design for our quantum oracle circuit, so we are
getting closer to finding our queen …
Inversion around the average
It is now time to move to the second stage of Grover's algorithm. And, as
we shall see, it is quite ingenious.
To recap, the queen of spades has been found and the state representing
the position of the queen (11) has been marked. But what do we do next
with this marked state? How do we ensure that when we look at the output
of our Grover's algorithm, the marked state will appear as the most likely
solution with high probability? We need to find some way of amplifying the
marked state.
Remember, we marked the state by inverting the phase of that state so it
points in the opposite direction to the phases of all the other states. This
situation is shown in the following diagram showing the phases of all the
states. You can see that all the phases point up except for the single phase of
the marked state which was inverted and therefore points down :
In the top part of the diagram you will also see a thin dashed line. This
represents the average value of all the phases. Because almost all the
phases are pointing upward, this average value is quite large, almost
precisely aligned with the tips of all the lines which are pointed upward.
The only thing which is dragging the average value down very slightly is
the value of the single marked state which is pointed downward.
The next step is very clever. We rotate all the states by 180° around the
average value, the dashed line. You can see the effect in the lower part of
the previous diagram. Because the value of the upward-pointing states was
very close to the average value, the effect of the rotation does not affect
them very much. However, as you can see in the previous diagram, the tip
of the downward-pointing state was very far away from the average value
— approximately twice as far away as all the other upward-pointing states.
So when we rotate all the states around the average value, the downward-
pointing state now gets amplified to more than twice its original length, and
now points upward — as shown in the lower part of the previous diagram.
Moving on to consider how to implement this rotation, it turns out that
the final circuit for performing the "rotation around the average" takes the
following form:
Once again, the details of how this circuit was designed are quite
complicated and so they have been moved to Appendix Two at the back of
this book.
And that is how the second part of Grover's algorithm works. The
amplification boosts the marked state so that when the output of the
quantum computer is observed, it is more likely to collapse to the correct
state, revealing the position of the queen of spades. Usually several
iterations have to be performed (the square root of n times) until the correct
state receives sufficient amplification to ensure that it will be selected with
high probability .
The revealing of the queen of spades at the end of the video represents
perhaps the most technologically-advanced magic trick in history .
Like all quantum circuit diagrams, we read the diagram from left to right.
You can see that the target qubit — the lower qubit — passes through the
first Hadamard gate. The qubit state vector then gets rotated according to
the usual behaviour of the Hadamard gate (see Chapter Eight for a
reminder). So, as you can see in the following diagram, the qubit state
vector gets rotated around the diagonal dashed line and thereby enters a
superposition state :
But you can see from the previous diagram that the direction of the state
vector in the superposition state is different depending on whether the initial
〉 〉 〉
state was |0 or |1 . If the initial state was |0 (state vector pointing up),
then the state vector gets rotated to a position pointing toward the left and to
the front (see the previous diagram). This is called superposition state |+〉
〉
(the "plus state"). However, if the initial state was |1 (state vector
pointing down), then the state vector gets rotated to a position pointing
〉
toward the right and to the back. This is called superposition state |- (the
"minus state") .
In this playing cards example, we are passing a target qubit in state |1〉
to the Hadamard gate. So, in this case, the qubit enters the superposition
〉
state |- .
〉
Expressed mathematically, the superposition state |+ is the usual equal
〉 〉 〉
mix of |0 and |1 . However, the superposition state |- is an equal mix
〉 〉 〉
of |0 and -|1 (the |1 now has a negative sign in front it). That
explains why this superposition state vector is pointing in the opposite
direction to the usual direction.
For the next stage of the oracle, you can see that the CNOT gate is then
applied to this qubit. Because the control qubit of the CNOT gate is also in
〉
state |1 , the CNOT gate performs its usual "flipping" operation on the
〉
target qubit. This has the effect of flipping the values of the |0 and |1 〉
〉 〉
terms of the state vector, changing |0 to |1 , and vice versa. For a state
〉
vector in the superposition state |+ , both of those terms are positive so
〉
flipping the terms has no effect. But for the superposition state |- we have
〉
in this example, as mentioned earlier, the |0 term is positive but the |1 〉
term has a negative sign in front of it. Therefore, flipping the two terms has
the effect of placing a negative sign in front of the entire qubit state, as
explained in the following diagram :
And that is just what we have been looking for! If you remember, we
wanted to find a way of inverting the state, which, as stated earlier, is the
same as putting a negative sign in front of the state. And that is just what we
have done with our CNOT gate.
So we have achieved the phase inversion we desired. And this phase
inversion is caused when the following two conditions are true:
1. The lower of the two qubits in the circuit diagram must be in state |1〉
〉
so that the qubit enters the superposition state |- .
2. The higher of the two qubits in the circuit diagram must also be in
〉
state |1 so that the CNOT gate does its "bit-flipping" operation.
〉
So both input qubits must be in state |1 for the phase inversion to take
place. And, if you remember, that is precisely how we wanted our quantum
oracle to behave: we wanted our quantum oracle to perform a phase
inversion ("marking" the state) when both of the input qubits are in state
〉
|1 .
We have therefore found our quantum oracle!
APPENDIX TWO: ROTATION
AROUND THE AVERAGE
This appendix describes how the "rotation around the average" quantum
circuit in Grover's algorithm was designed. Once again, this is tricky stuff,
so only read it if you need to.
We know how to do rotations, as we have just used the method in the
quantum oracle. We will be reusing the same piece of code, and it will form
the centrepiece of our new "rotation around the average". Here, once again,
is the circuit diagram which performs the rotation:
Let us now extend this circuit diagram to perform the "rotation about the
average".
You might think it would be a tricky calculation to find the average value
of all the states, but it is actually extremely easy. An average might be
considered as being halfway between two extreme values. Building on that
〉 〉
idea, if we have two qubits, one in state |0 and the other in state |1 , the
half-way value would be the superposition state, halfway between state |0 〉
〉
and state |1 . So the average, in this case, is defined by the superposition
state. In fact, it can be shown that — no matter how many states you have
— the average is always the equally-weighted superposition state.
So that makes things a lot easier: to perform the "inversion around the
〉
average" we need to rotate all the states around the superposition state |+ .
But there is an easier way of achieving that. Remember, if a Hadamard gate
is applied to a superposition state |+ 〉, it gets rotated to state |0〉, as shown
in the following diagram:
Z-Access
https://wikipedia.org/wiki/Z-Library
ffi