0% found this document useful (0 votes)
5 views6 pages

Quantum Computing for Programmers-5

The document provides an introduction to programming quantum computers, focusing on the concept of quantum bits (qubits) and their operations. It explains how quantum computing leverages the principles of quantum physics to perform computations differently than classical computers, including the use of quantum gates and superposition. The workshop aims to demonstrate these principles through practical exercises using the Quantum Playground platform.

Uploaded by

ojmi54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Quantum Computing for Programmers-5

The document provides an introduction to programming quantum computers, focusing on the concept of quantum bits (qubits) and their operations. It explains how quantum computing leverages the principles of quantum physics to perform computations differently than classical computers, including the use of quantum gates and superposition. The workshop aims to demonstrate these principles through practical exercises using the Quantum Playground platform.

Uploaded by

ojmi54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Programming a Quantum Computer

Dominic Moylett
Quantum Engineering Centre for Doctoral Training,
University of Bristol
[email protected]
February 27, 2016

Worksheet Details 2 Quantum Bits

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 + " >"
 

This seems to produce the same result as before: The qubit is


5 Superposition in a superposition, and when we measure it the particle collapses
into |0i or |1i. So does this produce the same result as H|0i?
Remove the last two lines of code and look at the image. Now
Another example of a single qubit gate is the Hadamard gate, the square for the |0i state is white with an amplitude of √1 ,
2
H. Let’s see what this gate does to a qubit in the |0i state:
 but the |1i state is light blue, with an amplitude of − √12 . Thus
1 VectorSize 6 this state is:
2 Hadamard 0
 
|0i − |1i
This now produces white squares for the states |0i and |1i, H|1i = √
both with Re numbers of 0.707107 ' √12 . Let’s measure this 2
state a few times to see what happens:
  This explains why the results when measuring look so similar,
1 VectorSize 6 as the probability of getting |0i or |1i is still 21 4 .
2 Hadamard 0
MeasureBit 0 Like the X gate, applying the Hadamard  gate twice will pro-
3
4 P r i n t " Qubit 0 is | " + m e a s u r e d v a l u e + " >" 
 duce the original qubit: HH|0i = H |0i+|1i
√ = |0i and
  2
|0i−|1i
Running this multiple times, we see that sometimes we get |0i HH|1i = H √
2
= |1i. You can check this yourself using
and other times we get |1i. This is called a superposition, where the Quantum Playground, or if you are more mathematically in-
measuring the qubit could give |0i or |1i, and it is impossible to clined, look at Section 10.1 for the mathematics behind this. In
predict which result it will be. This is already a very useful reason fact, all quantum gates are reversible: After applying a quantum
for utilising quantum effects in technology, as classical computers gate to a qubit, there is always another gate we can apply to get
are not good at generating numbers purely at random. And this back to the original qubit. It is only after measuring that our
technology is already being used for applications from security operations cannot be reversed.
and simulations to gambling and gaming.
You might wonder at this point what other quantum states we
But how likely are we to measure |0i or |1i? This is decided can possibly make with a single qubit. The answer is that we can
by the Re and Im2 numbers we have seen. These numbers form make any qubit of the form a|0i + b|1i, where |a|2 + |b|2 = 1.
the amplitudes of the state. When we measure a qubit, the 3 The absolute function |x| converts any negative numbers to positive

2 For numbers: | − 1| = |1| = 1


this workshop, we will only focus on the Re numbers and the Im 2  2
4 − √1 = √1 = 12
numbers will be 0. 2 2

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

VectorSize 6 7 P r i n t " Qubit 1 is | " + m e a s u r e d v a l u e + " >"


1
2 Hadamard 0
 
3 Hadamard 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 + " >"
This produces a very different result. It now appears that
6 MeasureBit 1 our measurement of qubit 0 is random, but our measurement of
7 P r i n t " Qubit 1 is | " + m e a s u r e d v a l u e + " >" qubit 1 is always the same state as qubit 0. If we measure qubit
 
1 first, then our measurement of qubit 1 will be random, but our
Like single qubits, we can produce any superposition over two measurement of qubit 0 will always be the same state as qubit
qubits. Thus our state can be any form of a|00i+b|01i+c|10i+ 1. It seems that our qubits are in a superposition together, and
d|11i where |a|2 + |b|2 + |c|2 + |d|2 = 1. Some example states if one collapses then the other collapses with it. We write this
|00i+|11i
|00i+|10i |00i+|01i+|10i+|11i state as √ , as shown mathematically in Section 10.2.
include |01i, √
2
and 2 . 2

This effect is called entanglement, and is one of the strangest


results of quantum mechanics. Entangled states collapse at ex-
7 Entanglement actly the same time, regardless of distance between the two
qubits. One qubit could be at the At-Bristol Science Centre
and the other could be at the Digimakers Moon Base5 , and their
Not all classical gates are single bit. The classical AND gate collapse would still be at the same time. Classically, this simul-
for example, takes two classical bits as input and produces one taneous collapse should be impossible, as it would require one
classical bit as output. qubit to send a message describing its measurement result to the
other faster than the speed of light, breaking the laws of relativ-
We also have multiple qubit quantum gates. One of particular ity. This led Albert Einstein to describe quantum mechanics as
importance is the Controlled-NOT gate, or CN OT , which takes “Spooky action at a distance.”
a control qubit as the first argument and a target qubit as the
second. In the quantum playground, the first argument of the While we haven’t been able to entangle qubits between the At-
CN OT command is the control qubit, and the second argument Bristol Science Centre and the Digimakers Moon Base, physicists
is the target qubit. Let’s see what happens when the control at a number of institutions – most famously Delft University
qubit is |0i:
 of Technology – have managed to successfully entangle qubits
1 VectorSize 6 and witness these effects before light could have travelled from
2 CNot 0 , 1 one qubit to the other. So there must be something more to
3 MeasureBit 0
quantum.
4 P r i n t " Qubit 0 is | " + m e a s u r e d v a l u e + " >"
5 MeasureBit 1
6 P r i n t " Qubit 1 is | " + m e a s u r e d v a l u e + " >" 5 Construction pending.
 

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:

Case 1: If you want f (0) = 0 and f (1) = 0, then delete line 5.


9 Where is my Quantum Computer?
Case 2: If you want f (0) = 0 and f (1) = 1, then replace line 5
with:
 
1 CNot 0 , 1 You can buy one right now! D-Wave Systems already sells quan-
 tum computers, though these computers are only built for solving
a very specific problem called quantum annealing. But they are
Case 3: If you want f (0) = 1 and f (1) = 0, then replace line 5 on the market, and groups ranging from Google to NASA are
with:
 currently seeing how useful the quantum annealers they bought
1 CNot 0 , 1 from D-Wave are for their problems.
2 SigmaX 1
  But what is more interesting in the Physics community is when
will we see a universal quantum computer – a computer that
Case 4: Or if you want f (0) = 1 and f (1) = 1, then replace can perform any quantum computation – on the market. And
line 5 with:
 the answer is we don’t know, but you can use one right now!
1 SigmaX 1 The Centre for Quantum Photonics at the University of Bristol
 connected their two qubit universal quantum computer to the
Internet so that anyone can have a go at programming on it,
in what they call the Quantum in the Cloud project. You can
If we run the full program, we find that if f (0) = f (1) (cases 1 find out more about this work, and run a simulator of it your-
and 4) then qubit 0 is in the |0i state, and if f (0) 6= f (1) (cases self, here: http://www.bristol.ac.uk/physics/research/
2 and 3) then qubit 0 is in the |1i state. Thus we have solved quantum/outreach/qcloud/
the problem with only one query of f . See section 10.3 for a
mathematical explanation of this. David Deutsch and Richard But two qubits is not very useful; when are we going to get uni-
Jozsa6 later showed that a similar problem requires many queries versal quantum computers with many qubits? Again, we don’t
on a classical computer, but just one on a quantum computer. know, but potentially soon: John Martinis at the University of
California, Santa Barbara is working with Google to build a 100
Of course, in this case we can figure out if f (0) = f (1) by qubit universal quantum computer in 2-3 years; Microsoft Re-
just looking at the code. But Deutsch’s algorithm also works if search predicts a quantum computer in ten years; and others
we cannot see the code. If we are just given a black box which have predicted between five and forty years. Regardless of when
6 Richard Jozsa is another famous physicist who was previously a professor it comes, one thing is certain: There’s a lot of excitement sur-
in the Department of Computer Science at the University of Bristol. rounding quantum computers right here and now.

4
10 More Mathematics 10.3 The Mathematics of Deutsch’s Algorithm

Our aim with Deutsch’s Algorithm, as described in Section 8,


This section contains some of the more challenging mathematics
is to produce the state |0i when f (0) = f (1) and the state |1i
behind the concepts here and is aimed at those who are more
when f (0) 6= f (1).
mathematically experienced. If you are not as interested in the
more formal theory of these concepts then feel free to skip ahead After applying the Hadamard
to Section 11.  in line
 4 toeach qubit, our two
qubits are in the state |0i+|1i

2
⊗ |0i−|1i

2
.
One important property with all the mathematics shown in
this section is that all quantum gates are linear, so applying a Now, note that in cases 1 and 4 where f (0) = f (1), we do
gate to a superposition of states is equivalent to applying a gate nothing to qubit 0. So after applying our quantum version of
|0i+|1i
to each of those states individually. For example, if we apply an f , qubit 0 is still in the state √2 . Once we apply our final
X gate to the state (a|0i + b|1i) then we get the state: Hadamard gate, we get the state |0i, as H( |0i+|1i√ ) = |0i as
2
shown in Section 10.1.

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:

10.1 Hadamards are Reversible


   
|0i + |1i |0i − |1i
CN OT √ ⊗ √
2 2
 
In Section 5, I stated that H reverses itself: HH|0i = |0i. Let’s |00i − |01i + |10i − |11i
= CN OT
check this: 2
CN OT |00i − CN OT |01i + CN OT |10i − CN OT |11i
=
2
  |00i − |01i + |11i − |10i
|0i + |1i H|0i + H|1i =
HH|0i = H √ = √
2 2  2 
|0i − |1i |0i − |1i
1

|0i + |1i |0i − |1i

= √ ⊗ √
=√ √ + √ 2 2
2 2 2
|0i + |1i + |0i − |1i 2|0i
= = = |0i Qubit 0 is now in the state ( |0i−|1i

2
). When we apply a
2 2
Hadamard to this state, we get the state |1i as desired!

A similar proof can be done for showing that HH|1i = |1i. 11 Further Reading

I first heard about quantum computing from a booklet written


10.2 The Mathematics Behind Entanglement by the University of Bristol’s Ashley Montanaro, which explains
many of the concepts discussed here from a theoretical perspec-
tive. That booklet is available here: http://www.cs.bris.ac.
I stated in Section 7 that the entangled state we get after using uk/ montanar/gameshow.pdf
~
the CN OT operator is |00i+|11i

2
. The mathematical proof of
this is below: Ashley also recently gave a guest lecture on quantum com-
puting in general, the slides of which are available here:http:
//www.maths.bris.ac.uk/~csxam/teaching/history.pdf
 
|0i + |1i CN OT |00i + CN OT |10i Juan Miguel Arrazola at the University of Waterloo has been
CN OT √ ⊗ |0i = √
2 2 writing a collection of blog posts explaining quantum mechanics
|00i + |11i to everyone:
= √
2
• Part one is here: https://uwaterloo.ca/
institute-for-quantum-computing/blog/post/
More generally, any state is entangled if it cannot be written as anyone-can-understand-quantum-mechanics-part-1
the tensor product of individual qubits. For example, ( |00i+|01i
√ )
2 • Part two is here: https://uwaterloo.ca/
is not entangled as it can be written as |0i⊗ |0i+|1i

2
, but |01i+|10i

2 institute-for-quantum-computing/blog/post/
is entangled. anyone-can-understand-quantum-mechanics-part-2

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:

• Trigonometry (sin, cos, trigonometric identities...)

• Complex numbers (eiθ = cos θ + i sin θ, complex conju-


gates...)
• Linear algebra (Matrices, vectors, eigenvalues & eigenvec-
tors...)

All of these concepts come up on a regular basis in quantum


physics, so if you want more of a reason to study these concepts,
now is your chance!

You might also like