PDF Programming Quantum Computers Essential Algorithms and Code Samples 1St Edition Eric R Johnston Ebook Full Chapter
PDF Programming Quantum Computers Essential Algorithms and Code Samples 1St Edition Eric R Johnston Ebook Full Chapter
PDF Programming Quantum Computers Essential Algorithms and Code Samples 1St Edition Eric R Johnston Ebook Full Chapter
https://textbookfull.com/product/supervised-learning-with-
quantum-computers-maria-schuld/
https://textbookfull.com/product/parallel-computers-architecture-
and-programming-v-rajaraman/
https://textbookfull.com/product/ethereum-for-architects-and-
developers-with-case-studies-and-code-samples-in-solidity-1st-
edition-debajani-mohanty/
https://textbookfull.com/product/quantum-computation-and-logic-
how-quantum-computers-have-inspired-logical-investigations-maria-
luisa-dalla-chiara/
Head First Learn to Code 1st Edition Eric Freeman
https://textbookfull.com/product/head-first-learn-to-code-1st-
edition-eric-freeman/
https://textbookfull.com/product/ethereum-for-architects-and-
developers-with-case-studies-and-code-samples-in-solidity-
springerlink-online-service/
https://textbookfull.com/product/algorithms-illuminated-
part-3-greedy-algorithms-and-dynamic-programming-1st-edition-tim-
roughgarden/
https://textbookfull.com/product/quantum-computing-solutions-
solving-real-world-problems-using-quantum-computing-and-
algorithms-1st-edition-bhagvan-kommadi/
https://textbookfull.com/product/foundations-of-quantum-
programming-1st-edition-ying/
Programming Quantum
Computers
Essential Algorithms and Code Samples
Figure P-1. Quantum programs can look a bit like sheet music
How This Book Is Structured
A tried-and-tested approach for getting hands-on with new
programming paradigms is to learn a set of conceptual primitives.
For example, anyone learning Graphics Processing Unit (GPU)
programming should first focus on mastering the concept of
parallelism, rather than on syntax or hardware specifics.
The heart of this book focuses on building an intuition for a set of
quantum primitives—ideas forming a toolbox of building blocks for
problem-solving with a QPU. To prepare you for these primitives, we
first introduce the basic concepts of qubits (the rules of the game, if
you like). Then, after outlining a set of Quantum Processing Unit
(QPU) primitives, we show how they can be used as building blocks
within useful QPU applications.
Consequently, this book is divided into three parts. The reader is
encouraged to become familiar with Part I and gain some hands-on
experience before proceeding to the more advanced parts:
Part I: Programming a QPU
Here we introduce the core concepts required to program a QPU,
such as qubits, essential instructions, utilizing superposition, and
even quantum teleportation. Examples are provided, which can
be easily run using simulators or a physical QPU.
Constant width
Used for program listings, as well as within paragraphs to refer to
program elements such as variable or function names, databases,
data types, environment variables, statements, and keywords.
NOTE
This element signifies a general note.
WARNING
This element indicates a warning or caution.
NOTE
For almost 40 years, O’Reilly Media has provided technology and
business training, knowledge, and insight to help companies succeed.
How to Contact Us
Please address comments and questions concerning this book to the
publisher:
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0104 (fax)
We have a web page for this book, where we list errata, examples,
and any additional information. You can access this page at
http://bit.ly/programming-quantum-computers.
To comment or ask technical questions about this book, send email
to [email protected].
For more information about our books, courses, conferences, and
news, see our website at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
Acknowledgments
This book could not have been possible without a supporting team
of talented people passionate about quantum computing. The
authors would like to thank Michele, Mike, Kim, Rebecca, Chris, and
the technical team at O’Reilly for sharing and amplifying our
enthusiasm for the topic. Although the authors are responsible for all
errors and omissions, this book benefited immensely from the
invaluable feedback and inspiration of a number of technical
reviewers, including Konrad Kieling, Christian Sommeregger,
Mingsheng Ying, Rich Johnston, James Weaver, Mike Shapiro, Wyatt
Berlinic, and Isaac Kim.
EJ would like to thank Sue, his muse. Quantum computation started
to make sense to him on the week that they met. EJ also sends
thanks to friends at the University of Bristol and at SolidAngle, for
encouraging him to color outside the lines.
Nic would like to thank Derek Harrigan for first teaching him to talk
binary, the other Harrigans for their love and support, and Shannon
Burns for her pending Harrigan application.
Mercedes would like to thank José María Gimeno Blay for igniting her
interest on computers early, and Mehdi Ahmadi for being a constant
source of support and inspiration.
But clichéd as it may be, most of all the authors would like to thank
you, the reader, for having the sense of adventure to pick up this
book and learn about something so different and new.
Chapter 1. Introduction
Required Background
The physics underlying quantum computing is full of dense
mathematics. But then so is the physics behind the transistor, and
yet learning C++ need not involve a single physics equation. In this
book we take a similarly programmer-centric approach,
circumventing any significant mathematical background. That said,
here is a short list of knowledge that may be helpful in digesting the
concepts we introduce:
What Is a QPU?
Despite its ubiquity, the term “quantum computer” can be a bit
misleading. It conjures images of an entirely new and alien kind of
machine—one that supplants all existing computing software with a
futuristic alternative.
At the time of writing this is a common, albeit huge, misconception.
The promise of quantum computers stems not from them being a
conventional computer killer, but rather from their ability to
dramatically extend the kinds of problems that are tractable within
computing. There are important computational problems that are
easily calculable on a quantum computer, but that would quite
literally be impossible on any conceivable standard computing device
that we could ever hope to build.1
But crucially, these kinds of speedups have only been seen for
certain problems (many of which we later elucidate on), and
although it is anticipated that more will be discovered, it’s highly
unlikely that it would ever make sense to run all computations on a
quantum computer. For most of the tasks taking up your laptop’s
clock cycles, a quantum computer performs no better.
In other words—from the programmer’s point of view—a quantum
computer is really a co-processor. In the past, computers have used
a wide variety of co-processors, each suited to their own specialties,
such as floating-point arithmetic, signal processing, and real-time
graphics. With this in mind, we will use the term QPU (Quantum
Processing Unit) to refer to the device on which our code samples
run. We feel this reinforces the important context within which
quantum computing should be placed.
As with other co-processors such as the GPU (Graphics Processing
Unit), programming for a QPU involves the programmer writing code
that will primarily run on the CPU (Central Processing Unit) of a
normal computer. The CPU issues the QPU co-processor commands
only to initiate tasks suited to its capabilities.
A Hands-on Approach
Hands-on samples form the backbone of this book. But at the time
of writing, a full-blown, general-purpose QPU does not exist—so how
can you hope to ever run our code? Fortunately (and excitingly),
even at the time of writing a few prototype QPUs are currently
available, and can be accessed on the cloud. Furthermore, for
smaller problems it’s possible to simulate the behavior of a QPU on
conventional computing hardware. Although simulating larger QPU
programs becomes impossible, for smaller code snippets it’s a
convenient way to learn how to control an actual QPU. The code
samples in this book are compatible with both of these scenarios,
and will remain both usable and pedagogical even as more
sophisticated QPUs become available.
There are many QPU simulators, libraries, and systems available.
You can find a list of links to several well-supported systems at
http://oreilly-qc.github.io. On that page, we provide the code
samples from this book, whenever possible, in a variety of
languages. However, to prevent code samples from overwhelming
the text, we provide samples only in JavaScript for QCEngine.
QCEngine is a free online quantum computation simulator, allowing
users to run samples in a browser, with no software installation at
all. This simulator was developed by the authors, initially for their
own use and now as a companion for this book. QCEngine is
especially useful for us, both because it can be run without the need
to download any software and because it incorporates the circle
notation that we use as a visualization tool throughout the book.
A QCEngine Primer
Since we’ll rely heavily on QCEngine, it’s worth spending a little time
to see how to navigate the simulator, which you can find at
http://oreilly-qc.github.io.
Running code
The QCEngine web interface, shown in Figure 1-1, allows you to
easily produce the various visualizations that we’ll rely on. You can
create these visualizations by simply entering code into the
QCEngine code editor.
To run one of the code samples from the book, select it from the
drop-down list at the top of the editor and click the Run Program
button. Some new interactive UI elements will appear for visualizing
the results of running your code (see Figure 1-2).
Quantum circuit visualizer
This element presents a visual representation of the circuit
representing your code. We introduce the symbols used in these
circuits in Chapters 2 and 3. This view can also be used to
interactively step through the program (see Figure 1-2).
Circle-notation visualizer
This displays the so-called circle-notation visualization of the QPU
(or simulator) register. We explain how to read and use this
notation in Chapter 2.
Debugging code
Debugging QPU programs can be tough. Quite often the easiest way
to understand what a program is doing is to slowly step through it,
inspecting the visualizations at each step. Hovering your mouse over
the circuit visualizer, you should see a vertical orange line appear at
a fixed position and a gray vertical line wherever in the circuit your
cursor happens to be. The orange line shows which position in the
circuit (and therefore the program) the circle-notation visualizer
currently represents. By default this is the end of the program, but
by clicking other parts of the circuit, you can have the circle-notation
visualizer show the configuration of the QPU at that point in the
program. For example, Figure 1-3 shows how the circle-notation
visualizer changes as we switch between two different steps in the
default QCEngine program.
Figure 1-3. Stepping through a QCEngine program using the circuit and circle-
notation visualizers
Logical bitwise
NOT (also X) qc.not(t)
NOT
Controlled NOT: if
CNOT qc.cnot(t,c)
(c) then NOT(t)
HAD
qc.had(t) Hadamard gate
(Hadamard)
Relative phase
PHASE qc.phase(angle,c)
rotation
Symbol Name Usage Description
Relative phase
Z qc.phase(180,c)
rotation by 180 °
Relative phase
S qc.phase(90,c)
rotation by 90 °
Relative phase
T qc.phase(45,c)
rotation by 45 °
Conditional phase
CPHASE qc.cphase(angle,c1|c2)
rotation
Conditional phase
CZ qc.cphase(180,c1|c2)
rotation by 180 °
Write
WRITE qc.write(t,val) conventional data
to qubits
Root-of-NOT
ROOTNOT qc.rootnot(t)
operation
Conditional
CSWAP qc.exchange(t1|t2, c) exchange: if (c)
then SWAP(t1,t2)
Hardware Limitations
Conversely, the largest actual QPU hardware available at the time of
writing has around 70 physical qubits, while the largest QPU
available to the public, through the Qiskit open source software
development kit, contains 16 physical qubits.3 By physical, as
opposed to logical, we mean that these 70 qubits have no error
correction, making them noisy and unstable. Qubits are much more
fragile than their conventional counterparts; the slightest interaction
with their surroundings can derail the computation.
Dealing with logical qubits allows a programmer to be agnostic about
the QPU hardware and implement any textbook algorithm without
having to worry about specific hardware constraints. In this book,
we focus solely on programming with logical qubits, and while the
examples we provide are small enough to be run on smaller QPUs
(such as the ones available at the time of publication), abstracting
away physical hardware details means that the skills and intuitions
you develop will remain invaluable as future hardware develops.
1 One of our favorite ways to drive this point home is the following back-of-
the-envelope calculation. Suppose conventional transistors could be made
atom-sized, and we aimed to build a warehouse-sized conventional computer
able to match even a modest quantum computer’s ability to factor prime
integers. We would need to pack transistors so densely that we would create
a gravitational singularity. Gravitational singularities make computing (and
existing) very hard.
2 Despite its popularity in the media as a benchmark for quantum computing,
counting the number of qubits that a piece of hardware can handle is really
an oversimplification, and much subtler considerations are necessary to
assess a QPU’s true power.
3 This figure may become out of date while this book is in press!
Part I. Programming for a
QPU
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the terms
of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth in
paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.