Reader small image

You're reading from  A Practical Guide to Quantum Machine Learning and Quantum Optimization

Product typeBook
Published inMar 2023
PublisherPackt
ISBN-139781804613832
Edition1st Edition
Right arrow
Authors (2):
Elías F. Combarro
Elías F. Combarro
author image
Elías F. Combarro

Elías F. Combarro holds degrees from the University of Oviedo (Spain) in both Mathematics (1997, award for second highest grades in the country) and Computer Science (2002, award for highest grades in the country). After some research stays at the Novosibirsk State University (Russia), he obtained a Ph.D. in Mathematics (Oviedo, 2001) with a dissertation on the properties of some computable predicates under the supervision of Prof. Andrey Morozov and Prof. Consuelo Martínez. Since 2009, Elías F. Combarro has been an associate professor at the Computer Science Department of the University of Oviedo. He has published more than 50 research papers in international journals on topics such as Computability Theory, Machine Learning, Fuzzy Measures and Computational Algebra. His current research focuses on the application Quantum Computing to algebraic, optimisation and machine learning problems. From July 2020 to January 2021, he was a Cooperation Associate at CERN openlab. Currently, he is the Spain representative in the Advisory Board of CERN Quantum Technology Initiative, a member of the Advisory Board of SheQuantum and one of the founders of the QSpain, a quantum computing think tank based in Spain.
Read more about Elías F. Combarro

Samuel González-Castillo
Samuel González-Castillo
author image
Samuel González-Castillo

Samuel González-Castillo holds degrees from the University of Oviedo (Spain) in both Mathematics and Physics (2021). He is currently a mathematics research student at the National University of Ireland, Maynooth, where he works as a graduate teaching assistant. He completed his physics bachelor thesis under the supervision of Prof. Elías F. Combarro and Prof. Ignacio F. Rúa (University of Oviedo), and Dr. Sofia Vallecorsa (CERN). In it, he worked alongside other researchers from ETH Zürich on the application of Quantum Machine Learning to classification problems in High Energy Physis. In 2021, he was a summer student at CERN developing a benchmarking framework for quantum simulators. He has contributed to several conferences on quantum computing.
Read more about Samuel González-Castillo

View More author details
Right arrow

Preface

See that the imagination of nature is far, far greater than the imagination of man.
— Richard Feynman

Many people believe that quantum computing is very difficult to learn, assuming that it requires knowledge of arcane and obscure branches of mathematics, and that it can only be mastered with a strong background in physics. We couldn't disagree more. In fact, the set of prerequisites that you need for a journey into the depths of quantum computing is surprisingly small: just some basic linear algebra, a few notions from probability, and some familiarity with computer programming. All of these are acquired by students of mathematics, physics, computer science, and engineering in their first year of college.

For this reason, a few years ago, one of us started designing quantum computing courses that focused only on the essentials, trying to demystify the subject and make it accessible to as many students as possible. Prominent among these courses is the series of lectures titled “A Practical Introduction to Quantum Computing: From Qubits to Quantum Machine Learning and Beyond" taught online from CERN in 2020 (you can find the materials and recordings at https://indico.cern.ch/event/970903/).

The seed of this book was already present in that series of lectures (and in others taught at the University of Oviedo, the University of Castilla-La Mancha and the University of Almería, among other places). Some of the material (greatly adapted and expanded) comes from what was prepared for the CERN course and, more importantly, the main guiding principles behind the design of the lectures (discussed in detail in [26]) have remained unchanged in the preparation of the book that you are now reading.

The first of those principles is our firm belief that quantum computing should be about computing. Let us clarify that a little bit. This means that our end goal will be to enable you to run quantum algorithms on quantum computers and solve problems with them. To achieve that, you will need to know how to write code that implements some quantum algorithms. And you cannot do that if you only study quantum computing from a purely theoretical perspective. Indeed, you need to get your hands dirty, learn (at least!) one quantum programming language, and be able to translate abstract quantum operations into executable instructions.

This is why a big part of this book is devoted to introducing different quantum programming frameworks (Qiskit, PennyLane, D-Wave's Ocean) and explaining how to run different quantum algorithms with them. Contrary to other (excellent!) quantum computing books out there, here you will find code. Lots of code. Code that you will be able to run straightaway on simulators and actual quantum computers. Code that you can modify, experiment with, and adapt to your own problems and projects.

But this is not just a quantum programming book. Our goal is not to give you recipes for solving particular instances of particular problems. Our goal is for you to understand quantum computing. So our second guiding principle is a commitment to discuss all the mathematics behind every quantum algorithm covered in the book and behind each line of code in our examples — at least in a reasonable amount of detail.

This is important for a couple of reasons. On the one hand, quantum computing is still a young field. New, improved algorithms are proposed in scientific publications every day and some of them will become standard in the short or medium term. Eventually, you will want to understand and use those algorithms. But you cannot do it if you do not fully understand the algorithms they come from.

On the other hand, successfully programming and running an algorithm is the ultimate test to know if you really understand its principles. Computers are merciless. They do not tolerate imprecision or ambiguities. You really need to know each and every detail of an algorithm in order to implement it. We completely agree with Donald Knuth when he says that “a person does not really understand something until he can teach it to a computer."

So those are the two main pillars upon which we have built this book: code and the mathematics behind it. Enough mathematics to understand the code and enough code to make the mathematics clear and useful. We won't lie to you: it was difficult to find a balance between the two. Sometimes you may feel that our mathematical explanations run for too long. But just be patient and we promise that it will pay off when you see the formulas coming alive in the examples.

These two pillars were already present in the CERN lectures. But there is a difference between the topics covered there and the ones that we have selected for this book. We have decided not to include basic methods such as quantum teleportation [14, 19] and quantum key distribution with the BB84 protocol [13], or canonical algorithms like the ones by Deutsch and Jozsa [29] and Shor [87], for instance.

Fortunately, there is much more quantum computing material out there than four or five years ago, when we started developing our introductory courses. We think that there is no longer an urgent need to explain methods that are perfectly discussed in other books such as the (highly recommended) one by Sutor [92].

However, we do feel that there exists a need for a unified, detailed and practice-oriented explanation of many algorithms that are central to modern quantum computing and that are difficult to find together in a sole source. This includes a lot of methods that have been developed to solve optimization problems with quantum computers, and most algorithms (especially the ones based on variational circuits) from the field of quantum machine learning.

Many of these algorithms have been proposed fairly recently and have been designed to run on the kind of quantum computers available today (small, not fully connected, and susceptible to noise) as opposed to on idealized, fault-tolerant quantum processors. For this reason, these algorithms are currently the subject of intense research, because their true capabilities are not yet completely understood. There is some evidence that they may surpass classical algorithms in certain tasks, but this is still not so well-established as with other, older quantum algorithms such as Shor's.

Does this mean that this book is advanced or only for people already experienced in quantum computing? Not at all! It is true that, traditionally, one used to start studying quantum computing by going through protocols with just a few qubits, and then learning about Deutsch-Jozsa's, Simon's [89], and Bernstein-Vazirani's [29] algorithms, climbing all the way up to Shor's and Grover's [48] methods. If you know about those algorithms, that knowledge will certainly be useful, but it is not, by any means, necessary or expected to understand the topics that we will cover.

With this book, we want to provide you with a solid understanding of the principles behind modern quantum algorithms that have been proposed for the fields of optimization and machine learning, as well as to show you how to implement them and run them on quantum simulators and on real quantum hardware. This will allow you to start experimenting on problems of your own right away. We strongly believe that this is the perfect moment to start searching for use cases with current quantum computers. The algorithms that we present in this book are strong candidates to be among the first to be applied in practical situations in the near future, because most of them need much fewer resources than other earlier quantum algorithms (such as Shor's) and do not require error correction. Moreover, they can be understood and used without the need to know about previous developments in the field.

In fact, we have designed this book assuming that you have had no previous experience with quantum computing at all (we do assume that you have a working knowledge of complex numbers and linear algebra, although we also provide a refresher of both topics in the appendices).

The style of our exposition is mainly informal, without following the usual structure of definition-theorem-proof-corollary of many mathematical texts, but without sacrificing rigor at any point in the book. Whenever possible, we give detailed derivations that justify the mathematical properties that we use in our developments and analyses (or, at least, we provide an argument that may be extended to a full proof by just adding some small technical details). In the cases that proving a particular fact is beyond the scope of the book, we provide references in which a full treatment can be found.

Throughout all the text, we propose exercises that will help you understand important concepts and develop practical skills for manipulating formulas and writing your own quantum code. They are intended to be readily solved (we try to give useful hints for those exercises that are a lit bit more challenging), but, at the end of the book, we provide full, detailed solutions so that you check your understanding of the subject.

Quantum computing is a field in constant evolution, so we feel that it is especially important to give pointers to new developments, to variants of the algorithms that we present in the book, and to alternative approaches to solve the kind of problems that we study. We do this by including numerous boxes with the label “To learn more…". You can skip these boxes if you wish, as they are not necessary to follow the main text. However, we strongly recommend reading them, since they help to situate in a wider context the topics under study. Other boxes that we use throughout the book serve to highlight important facts, to give warnings about subtle points, or to remind you of central definitions and formulas. These should not be skipped. They are labeled “Important note" for a reason!

We've had a great time writing this book and we hope that it shows. But, above all, we hope that you find it useful. If it helps you in understanding the fascinating field of quantum computers a little bit better, we will consider our mission to be fulfilled.

Who this book is for

This book is for professionals from a wide variety of backgrounds, including computer scientists and programmers, engineers, physicists, chemists, and mathematicians. Basic knowledge of linear algebra and some programming skills (for instance, in Python) are assumed, although all mathematical prerequisites will be covered in the appendices.

What this book covers

This book is organized in three parts, an afterword, and some appendices as follows:

Part I, I, for One, Welcome our New Quantum Overlords

Chapter 1, Foundations of Quantum Computing, briefly reviews the key ideas behind the quantum circuit model, fixing the notation that we will use throughout the book. It explores core ideas and notions, discussing quantum states, quantum gates and measurements; all starting completely from scratch. It also makes the book more self-contained and accessible for readers with different backgrounds.

Chapter 2, The Tools of the Trade in Quantum Computing, presents different quantum programming libraries that you can use to implement and run quantum methods, focusing especially on Qiskit and PennyLane. This chapter will guide you through the process of implementing quantum circuits and running them on simulators and actual quantum computers.

Part II, When Time is Gold: Tools for Quantum Optimization

Chapter 3, Working with Quadratic Unconstrained Binary Optimization Problems, introduces a mathematical framework that will help us formulate combinatorial optimization problems in a way that will allow us to solve them with quantum algorithms. It also provides many examples of how to use this formalism in practice.

Chapter 4, Adiabatic Quantum Computing and Quantum Annealing, is devoted to quantum annealing, our first quantum optimization method. It starts by explaining all the mathematical details behind this algorithm and then covers all the practical aspects of using it to solve optimization problems. It also introduces Ocean, the library that we use to run programs on quantum annealers.

Chapter 5, QAOA: Quantum Approximate Optimization Algorithm, shows how to adapt the ideas behind quantum annealing to the quantum circuit model. It introduces QAOA, one of the most popular modern quantum algorithms, and studies its mathematical properties. This chapter also explains in detail how to use this algorithm with both Qiskit and PennyLane.

Chapter 6, GAS: Grover Adaptive Search, introduces Grover's algorithm and explains how to use it solve optimization problems. It focuses on designing oracles for optimization problems and on running the method with Qiskit.

Chapter 7, VQE: Variational Quantum Eigensolver, expands the applicability of the quantum optimization methods studied in the previous chapters to problems that are not combinatorial, including tasks from fields such as physics or quantum chemistry. It also shows how to run VQE with Qiskit and PennyLane, including how to use important techniques such as noise simulation and error mitigation.

Part III, A Match Made in Heaven: Quantum Machine Learning

Chapter 8, What is Quantum Machine Learning?, gives a self-contained introduction to (classical) machine learning. It also explains the ways in which quantum computing can be used to define new machine learning methods.

Chapter 9, Quantum Support Vector Machines, studies our first quantum machine learning model: a quantum version of the famous Support Vector Machines. It explains how they can be derived mathematically and shows how to use them to solve classification problems with Qiskit and PennyLane.

Chapter 10, Quantum Neural Networks, shows how to construct quantum versions of neural networks by using variational circuits with different roles in the model. It also provides detailed examples of how to define and run these models using PennyLane and Qiskit.

Chapter 11, The Best of Both Worlds: Hybrid Architectures, is a very practical and hands-on chapter. It shows how to mix quantum and classical neural networks to create hybrid models. It also guides you through all the steps needed to implement these architectures in both PennyLane and Qiskit.

Chapter 12, Quantum Generative Adversarial Networks, shows how to create quantum generative models that are the quantum version of classical Generative Adversarial Networks (or GANs). In addition to explaining the architecture of the model, it also provides detailed examples, both in PennyLane and Qiskit, of how to use them in practice.

Afterword and Appendices

Chapter 13, Afterword: The Future of Quantum Computing, wraps up everything discussed in the book and hints at some possible developments for quantum computing in the short and medium term.

Appendix A, Complex Numbers, gives a quick recap of the most relevant properties of complex numbers and how to operate with them.

Appendix B, Basic Linear Algebra, is a refresher on the fundamentals of linear algebra, including vectors and matrices, important notions such as bases and eigenvalues, and even some concepts from modular arithmetic.

Appendix C, Computational Complexity, serves as a quick introduction to measuring the resources needed to solve problems with algorithms. It defines important concepts such as big O notation and reductions, and complexity classes such as the famous P and NP.

Appendix D, Installing the Tools, guides you through the process of installing the libraries needed in order to run the source code included in this book.

Appendix E, Production Notes, gives a glimpse of the process of writing a technical book like this one, including the software used to typeset formulas and create figures.

Assessments contains the solutions to all the exercises proposed in the main text.

Parts II and III are mostly self-contained and independent of each other (although, in the main text, we point out the connections between them whenever they exist). They can be used for self-study of quantum optimization and quantum machine learning, or to teach two short, independent courses on these topics or one full course on modern quantum algorithms. The strongest dependencies between chapters are shown in Figure 1, so you can know which chapters you may skip without losing track of the explanation.

Figure 1: Strongest dependencies between chapters

To get the most out of this book

The concepts explained in this book are better understood by implementing algorithms that solve practical problems and by running them on simulators and actual quantum computers. You will learn how to do all that starting from the very beginning of the book, but in order to run the code you will need to install some tools.

We recommend that you download the Jupyter notebooks from the link provided in the following section and that you follow the instructions given in Appendix D, Installing the Tools, to get your environment ready to rock!

Download the example code files

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/A-Practical-Guide-to-Quantum-Machine-Learning-and-Quantum-Optimization. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/FtU9t.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, and user input. Here is an example: "We could create a GroverOptimizer object and directly use its solve method with qp."

A block of code is set as follows:

import dimod 
 
J = {(0,1):1, (0,2):1} 
 
h = {} 
 
problem = dimod.BinaryQuadraticModel(h, J, 0.0, dimod.SPIN) 
 
print("The problem we are going to solve is:") 
 
print(problem)

Any command-line input or output is written as follows:

$ python3 script.py

Important ideas are highlighted in boxes like the following:

Important note

I am a box. I feel important. That's because I am important.

We sometimes include material for those of you who want to learn more. We format it as follows:

To learn more

You don't have to read me if you don't want to.

There are a few exercises in the text, which are displayed as follows:

Exercise 0.1

Prove that every even number greater than two can be written as the sum of two prime numbers.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at customercare@packtpub.com.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packtpub.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Share your thoughts

Once you've read A Practical Guide to Quantum Machine Learning and Quantum Optimization, we'd love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we're delivering excellent quality content.

Download a free PDF copy of this book

Thanks for purchasing this book!

Do you like to read on the go but are unable to carry your print books everywhere? Is your eBook purchase not compatible with the device of your choice?

Don't worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.

The perks don't stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily.

Follow these simple steps to get the benefits:

  1. Scan the QR code or visit the link below:

    PIC

  2. Submit your proof of purchase.

  3. That's it! We'll send your free PDF and other benefits to your email directly.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
A Practical Guide to Quantum Machine Learning and Quantum Optimization
Published in: Mar 2023Publisher: PacktISBN-13: 9781804613832
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Elías F. Combarro

Elías F. Combarro holds degrees from the University of Oviedo (Spain) in both Mathematics (1997, award for second highest grades in the country) and Computer Science (2002, award for highest grades in the country). After some research stays at the Novosibirsk State University (Russia), he obtained a Ph.D. in Mathematics (Oviedo, 2001) with a dissertation on the properties of some computable predicates under the supervision of Prof. Andrey Morozov and Prof. Consuelo Martínez. Since 2009, Elías F. Combarro has been an associate professor at the Computer Science Department of the University of Oviedo. He has published more than 50 research papers in international journals on topics such as Computability Theory, Machine Learning, Fuzzy Measures and Computational Algebra. His current research focuses on the application Quantum Computing to algebraic, optimisation and machine learning problems. From July 2020 to January 2021, he was a Cooperation Associate at CERN openlab. Currently, he is the Spain representative in the Advisory Board of CERN Quantum Technology Initiative, a member of the Advisory Board of SheQuantum and one of the founders of the QSpain, a quantum computing think tank based in Spain.
Read more about Elías F. Combarro

author image
Samuel González-Castillo

Samuel González-Castillo holds degrees from the University of Oviedo (Spain) in both Mathematics and Physics (2021). He is currently a mathematics research student at the National University of Ireland, Maynooth, where he works as a graduate teaching assistant. He completed his physics bachelor thesis under the supervision of Prof. Elías F. Combarro and Prof. Ignacio F. Rúa (University of Oviedo), and Dr. Sofia Vallecorsa (CERN). In it, he worked alongside other researchers from ETH Zürich on the application of Quantum Machine Learning to classification problems in High Energy Physis. In 2021, he was a summer student at CERN developing a benchmarking framework for quantum simulators. He has contributed to several conferences on quantum computing.
Read more about Samuel González-Castillo