Quantum Computing for Programmers-5
Quantum Computing for Programmers-5
Dominic Moylett
Quantum Engineering Centre for Doctoral Training,
University of Bristol
[email protected]
February 27, 2016
A copy of this worksheet is available online at https://djmylt. A classical computer is made up of bits: 1s and 0s that represent
github.io/static/quantum_playground.pdf. If anyone data. Quantum computers store data in quantum bits, or qubits.
wants to modify this worksheet for their own workshop, they We write qubits as |1i and |0i. These symbols are called kets1 ,
can download the original material at https://github.com/ and are used to describe quantum states.
djmylt/quantum_playground.
In the Quantum Playground, we define qubits using the com-
Have any questions or comments? Find any errors? Then mand VectorSize, which we write at the top of our code. This
feel free to get in touch! My email address is at the top of this specifies the number of qubits we will be using, and the play-
worksheet. ground initialises these qubits all to the value |0i Try running the
code below by typing it into the text box, pressing “Compile”
and then pressing “Run”:
VectorSize 6
1 What is Quantum Computing? 1
This line initialises 6 qubits, all in the |0i state. We can see
Quantum physics is a collection of bizarre behaviours that hap- this on the left, where we see a black image with one white
pen to really small particles. The nature of quantum physics square in the bottom left hand corner. This image describes our
makes simulations of these particles really hard, even on today’s state. Each square of this image represents a quantum state, and
fastest computers. hovering over a square with the mouse will show us the quantum
state, and numbers labelled Re and Im. The number Re is 0 for
Quantum computing came about after a number of proposals
all quantum states except for the state |0i, where Re is 1. We
by physicists such as Richard Feynman. They suggested that in
will see what these numbers mean in the next sections.
order to make computers able to simulate quantum systems, we
should build computers that take advantage of the same effects
that makes quantum physics hard to simulate. These computers
run on a very different model of computation to our current lap- 3 Measurement
tops and smart phones, and can lead to more powerful machines
in turn. In the decades that have followed since then, academics
at many institutions – including the University of Bristol – have While images like this showing off our complete quantum state
studied how quantum physics can benefit our technology, and are nice, we cannot look at quantum states this way in practice.
how we can realise these benefits in practice. Instead, we have to measure our qubits. We can do this using
the MeasureBit command:
In this workshop, we aim to highlight some of the strange
1 VectorSize 6
effects of quantum physics, and how they can allow our comput- 2 M e a s u r e B i t 0
ers to be faster and more powerful than otherwise. To do so, 3 P r i n t " Qubit 0 is | " + m e a s u r e d v a l u e + " >"
we will simulate a small quantum computer using the Quantum
Computing Playground.
The second line measures qubit 0 and puts the result in the
All you need to get started is a laptop with Google Chrome measured value variable. The third line displays the result.
installed. Open up Google Chrome and go to the website http:
//quantumplayground.net/. Click “Playground” in the menu. From this we get the output “Qubit 0 is |0i”. So, if we set
This will load the playground with a default script. Delete this all our qubits to |0i and measure one of them, we find that the
script and click on the “2D+Phase” icon in the top left of the measured qubit is in the |0i state.
screen. We are now ready to explore the world of quantum 1 This notation was invented by Paul Dirac, one of the best-known quan-
computers! tum physicists and born and raised in Bristol.
1
4 Quantum Gates probability of measuring a state with amplitude a is |a|2 , where
|a| is the absolute function3 . In our case, the probability of
2 2
But data is only one half of computation. We also need be able measuring |0i and |1i are both √12 = √12 = 12 , so half the
to perform operations. Classical computers do this with logic time we see a |0i and half the time we see a |1i. Because the
gates, such as the N OT gate, which flips a bit: N OT (0) = 1 amplitude of each state is √12 , we say that the state is:
and N OT (1) = 0.
In quantum computing, we also have logic gates, which are |0i + |1i
H|0i = √
called quantum gates. One example is the X gate, which is the 2
quantum equivalent of a classical N OT gate: X|0i = |1i and
X|1i = |0i. Let’s apply an X gate to qubit 0 in the playground: So we can generate a random bit based on the result of this
state. Can we generate more random bits by measuring it mul-
1 VectorSize 6
2 SigmaX 0
tiple times? Sadly,this is not the case. This is because once we
have measured a qubit in a superposition, that qubit has col-
lapsed into either |0i or |1i, and the randomness has now been
Now our image is black with one white square denoting an Re lost. So we need to generate a new superposition for each ran-
number of 1 for the |1i state. So qubit 0 has flipped from |0i to dom bit we want. Measuring a superpositon is like opening a
|1i. We can also check by measuring: mysterious box; we don’t know what the box contains when it is
1 VectorSize 6
closed, but once we open it we cannot unlearn what is inside.
2 SigmaX 0
3 MeasureBit 0 But we have only been looking at the |0i state. What happens
4 P r i n t " Qubit 0 is | " + m e a s u r e d v a l u e + " >" when we try applying H to a qubit in the |1i state?
1 VectorSize 6
Now when we measure our qubit, we find that the result is |1i. 2 SigmaX 0
3 Hadamard 0
We can also check the other direction by applying X to our qubit 4 MeasureBit 0
twice before measuring it, and find that XX|0i = X|1i = |0i. 5 P r i n t " Qubit 0 is | " + m e a s u r e d v a l u e + " >"
2
6 Multiple Qubits It seems that CN OT |00i = |00i. If we were to apply
an X gate to qubit 1 before the CN OT we would find that
CN OT |01i = |01i. So when the control qubit is |0i, the CN OT
Multiple qubits act similarly to multiple classical bits, where we gate does nothing. What if the control qubit is |1i?
can apply quantum gates to the individual qubits. We represent
these qubits in the tensor product ⊗. For example, suppose the 1 V e c t o r S i z e 6
first qubit is |0i and the second is |1i. We write this state as 2 SigmaX 0
|0i ⊗ |1i, and often shorten it to |01i. 3 CNot 0 , 1
4 MeasureBit 0
5 P r i n t " Qubit 0 is | " + m e a s u r e d v a l u e + " >"
So for example, we can apply an X gate to the first and second 6 M e a s u r e B i t 1
qubits as follows: 7 P r i n t " Qubit 1 is | " + m e a s u r e d v a l u e + " >"
1 VectorSize 6
2 SigmaX 0
3 SigmaX 1 Now our target qubit has been flipped, so we can see that
4 MeasureBit 0
5 P r i n t " Qubit 0 is | " + m e a s u r e d v a l u e + " >"
CN OT |10i = |11i. And again, if we were to apply an X gate
6 MeasureBit 1 to our target qubit too, we would find that CN OT |11i = |10i.
7 P r i n t " Qubit 1 is | " + m e a s u r e d v a l u e + " >" So if our control qubit is |0i, then our CN OT gate does nothing,
but if our control qubit is |1i, then CN OT applies an X gate
to the target qubit.
And we find that they are in the |11i state, similarly to when
we were acting only on individual qubits. Likewise, if we applied But these aren’t the only possible states our control qubit can
X to only qubit 0, then we would find that they were in the |10i take. What if our control qubit is in a superposition?
state. So applying a quantum gate to one qubit doesn’t affect
the other. 1 VectorSize 6
2 Hadamard 0
If we apply a Hadamard gate to both qubits, we would find 3 CNot 0 , 1
that they were in their own superpositions and would produce 4 M e a s u r e B i t 0
5 P r i n t " Qubit 0 is | " + m e a s u r e d v a l u e + " >"
random measurements:
6M e a s u r e B i t 1
3
8 Deutsch’s Algorithm we are told implements the quantum version of f , then we can
still figure out if f (0) = f (1).
We are now ready to see one problem where quantum comput- This problem doesn’t sound very useful, but there are many
ers can truly outperform classical ones, first proposed by David more practical problems which quantum computers can do better
Deutsch. Suppose we have a classical circuit f , which takes one than classical computers:
bit as input and returns one bit as output. In other words, f (0)
is either 0 or 1 and f (1) is either 0 or 1. Deutsch’s problem is
to determine if f (0) = f (1).
• Charles Bennett and Stephen Wiesner showed that you can
On a classical computer, we would need two queries to the send two classical bits from one person to another by just
function f : One to figure out what f (0) is and one to figure out sending one qubit from one to the other using entanglement
what f (1) is. But can we do better with a quantum version of in a technique called superdense coding.
this circuit?
Deutsch’s Algorithm can be implemented as follows: • Searching through an unsorted list of items for a particular
item classically requires you to look at every item in that
1 VectorSize 6 list. Lov Grover showed that we could do this on a quantum
2 SigmaX 1
3 Hadamard 0
computer by looking at the list much fewer times.
4 Hadamard 1
5 //Insert quantum version of circuit here
6 Hadamard 0 • Peter Shor showed that you can efficiently find the prime
7 MeasureBit 0 factors of a number on a quantum computer. This prob-
8 P r i n t " Qubit 0 is | " + m e a s u r e d v a l u e + " >"
lem is believed to be hard classically, as finding an efficient
solution would break the RSA encryption scheme, thus po-
For line 5, we pick what operation to run based on what we tentially giving hackers access to everything from your Face-
want f (0) and f (1) to be. The four possibilities are outlined book account to your online banking details.
below:
4
10 More Mathematics 10.3 The Mathematics of Deutsch’s Algorithm
X(a|0i + b|1i) = aX|0i + bX|1i = a|1i + b|0i But in cases 2 and 3 where f (0) 6= f (1), we apply a CN OT .
Let’s look at what a CN OT does to our two states:
A similar proof can be done for showing that HH|1i = |1i. 11 Further Reading
5
• And part three will be coming soon, so make sure to keep
an eye on their blog.
Note that while these are the easier aspects of quantum me-
chanics to understand, a lot of quantum mechanics in general
relies on topics that are taught at GCSE or A Level Mathematics,
so be careful diving straight in. But if you want to try and get
ahead, some very useful mathematical concepts to learn include: