A Practical Guide to Quantum Machine Learning and Quantum Optimization - Elías F. Combarro - E-Book

A Practical Guide to Quantum Machine Learning and Quantum Optimization E-Book

Elías F. Combarro

0,0
38,39 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Work with fully explained algorithms and ready-to-use examples that can be run on quantum simulators and actual quantum computers with this comprehensive guide


Key Features


- Get a solid grasp of the principles behind quantum algorithms and optimization with minimal mathematical prerequisites


- Learn the process of implementing the algorithms on simulators and actual quantum computers

  • Solve real-world problems using practical examples of methods


    Book Description


    This book provides deep coverage of modern quantum algorithms that can be used to solve real-world problems. You’ll be introduced to quantum computing using a hands-on approach with minimal prerequisites.


    You’ll discover many algorithms, tools, and methods to model optimization problems with the QUBO and Ising formalisms, and you will find out how to solve optimization problems with quantum annealing, QAOA, Grover Adaptive Search (GAS), and VQE. This book also shows you how to train quantum machine learning models, such as quantum support vector machines, quantum neural networks, and quantum generative adversarial networks. The book takes a straightforward path to help you learn about quantum algorithms, illustrating them with code that’s ready to be run on quantum simulators and actual quantum computers. You’ll also learn how to utilize programming frameworks such as IBM’s Qiskit, Xanadu’s PennyLane, and D-Wave’s Leap.


    Through reading this book, you will not only build a solid foundation of the fundamentals of quantum computing, but you will also become familiar with a wide variety of modern quantum algorithms. Moreover, this book will give you the programming skills that will enable you to start applying quantum methods to solve practical problems right away.


    What you will learn:


    - Review the basics of quantum computing


    - Gain a solid understanding of modern quantum algorithms


    - Understand how to formulate optimization problems with QUBO


    - Solve optimization problems with quantum annealing, QAOA, GAS, and VQE


    - Find out how to create quantum machine learning models


    - Explore how quantum support vector machines and quantum neural networks work using Qiskit and PennyLane


    - Discover how to implement hybrid architectures using Qiskit and PennyLane and its PyTorch interface


    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.

  • Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

    EPUB

    Seitenzahl: 841

    Veröffentlichungsjahr: 2023

    Bewertungen
    0,0
    0
    0
    0
    0
    0
    Mehr Informationen
    Mehr Informationen
    Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



    Hands-on Approach to Modern Quantum Algorithms

    BIRMINGHAM—MUMBAI

    Copyright © 2023 Packt Publishing

    All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

    Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the , nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

    Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

    Associate Group Product Manager: Gebin George

    Publishing Product Manager: Kunal Sawant

    Content Development Editor: Rosal Colaco

    Technical Editor: Maran Fernandes

    Copy Editor: Safis Editing

    Project Coordinator: Manisha Singh

    Proofreader: Safis Editing

    Indexer: Hemangini Bari

    Production Designer: Vijay Kamble

    Business Development Executive: Kriti Sharma

    Developer Relations Marketing Executive: Sonia Chauhan

    First published: March 2023

    Production reference: 1170323

    Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

    ISBN 978-1-80461-383-2

    www.packtpub.com

    Contributors

    About the authors

    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 carrying out several stays as a visiting researcher at the Novosibirsk State University (Russia), he obtained a PhD 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 a tenured 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 quantum computing, computability theory, machine learning, fuzzy measures, and computational algebra. His current research focuses on the application of quantum computing to algebraic, optimization, and machine-learning problems.

    In 2020 and 2022, he was a Cooperation Associate at CERN openlab. Currently, he is Spain’s representative on the Advisory Board of the CERN Quantum Technology Initiative.

    To Adela, Paula and Sergio. You are my reason to live.

    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 Maynooth University, where he works as a graduate teaching assistant.

    He completed his physics bachelor thesis under the supervision of Prof. Elías F. Combarro, 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 physics. 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 and related fields.

    About the reviewers

    Francisco Orts is a researcher at the Institute of Data Science and Digital Technologies, Vilnius University (Lithuania). He holds a PhD in computer science from the University of Almería (Spain) and is a collaborator of the High-Performance Computing research group of this university. He has worked as a computer scientist at construction, stock exchange, and IT services companies, with more than 15 years of experience in the sector. His research interests are multidimensional scaling, quantum computing, and high-performance computing.

    Guillermo Botella (Senior Member, IEEE) received an MSc degree in physics in 1998, an MSc degree in electronic engineering in 2001, and a PhD degree (in computer engineering) in 2007, all from the University of Granada, Spain. He was an EU research fellow working with the University of Granada, Spain, and University College London, UK. He is currently an associate professor with the Department of Computer Architecture and Automation, Complutense University of Madrid, Spain. He has performed research stays from 2008 to 2012 with the Department of Electrical and Computer Engineering, Florida State University, USA. His current research interests include signal processing for FPGAs, GPGPUs, and novel computing paradigms such as analog and quantum computing.

    Foreword

    “I know you, you were working with Elías Combarro when he gave thatcourse about quantum computing at CERN. That course changed mylife!”

    As I welcome students and more seasoned researchers to the CERN IT Department, it is not unusual to hear this kind of comment about Elías’ lectures. I’ve been working at CERN for 25 years on R&D projects in computing and data science for high-energy physics and still I have not seen this happening that often with other courses.

    When I started building the CERN Quantum Technology Initiative in 2018, quantum computing and its applications had already started growing at an accelerated pace. We were looking for ways of understanding the potential benefits for physics and contributing to the ongoing research in theory, computing, sensing, and communication. A daunting task despite the incredible work that CERN has been doing since 1954 in physics and computing for physics. Where to start? How to build knowledge? How to identify and address realistic problems? Which tools and techniques should we focus on?

    Prof. Combarro joined CERN in 2020 for a short sabbatical, and he immediately became a reference for the team, inspiring students and researchers and helping us lay solid foundations for the work we were doing with the LHC experiments and the theory groups at CERN. Samuel González-Castillo joined the team a year later during his summer internship, building from the ground up our first prototype of a quantum systems benchmark framework.

    This book is the perfect image of how I saw them in action. It will initially guide you across complex concepts with rare clarity, building solid foundations to work comfortably with quantum optimization methods, quantum machine learning, and hybrid architectures without ever losing sight of the goal of providing a realistic, practical, usable approach.

    Chapters 1 and 2 will introduce you to the basics of quantum computing, building a reference of mathematical concepts and notations and a first practical overview of the ”tools of the trade,” the frameworks and platforms used to interact with quantum devices. Once the foundations are set like a sort of teaser of what’s to come, the rest of the book guides you through two complementary paths, quantum optimization methods in Chapters 3 to 7, and quantum machine learning, quantum neural networks, and hybrid architectures in Chapters 8 to 12.

    The authors not only provide clear formal explanations at every step, but also practical instructions and examples on how to implement and execute algorithms and methods on freely accessible actual quantum computers. Exercises (with detailed answers) are given throughout the book to check the progress of the exploration and gently nudge you beyond your comfort zone, always keeping the interest alive.

    Whether you are at the beginning of your discovery of quantum computing or are looking to understand its potential in your ongoing research, this book will be a trustworthy guide on an exciting journey. And I’m sure that the next time I meet you, you will say “I know you, you wrote the foreword for Elias’ andSamuel’s book. That book changed my life!”

    Alberto Di Meglio, MEng, PhD Head of Innovation — Coordinator CERN Quantum Technology Initiative Information Technology Department CERN — European Organization for Nuclear Research

    Acknowledgements

    I would maintain that thanks are the highest form of thought; and thatgratitude is happiness doubled by wonder. — G.K. Chesterton

    There are many people who we are grateful to because their support, help, knowledge, and advice were instrumental in shaping this book. First of all, we would like to thank Tomás Fernández Marcos. He taught both of us — albeit in different millennia! — some of the mathematical notions that, in our future, would be indispensable for our study of quantum computing. Then, he introduced us to each other, because he had the prescient intuition that we would share many interesting projects. It is fair to say that, without him, this book would not exist.

    We would also like to thank Alberto Di Meglio for writing such a wonderful foreword for us. The rest of the book can only go downhill from there!

    Many parts of this book originate from courses on quantum computing taught over the years. Those courses would not have been possible without the help and trust of Enrique Arias, Alberto Di Meglio, Melissa Gaillard, Ester Martín Garzón and José Ranilla, among others.

    We are also indebted to all the colleagues with whom we have discussed different topics on quantum computing and from whom we have learned a lot. We are particularly grateful to those who gave us very useful feedback and comments on preliminary versions of our lectures and of material for this book, including Vasilis Belis, Héctor García Morales, Miguel Hernández-Cáceres, Carla Rieger, Ignacio F. Rúa, Bruno Santidrián Manzanedo, Daniel Setó, Erik Skibinsky Gitlin, and Sofia Vallecorsa. We also want to give heartfelt thanks to Ferdous Khan. He was the first to suggest that our lectures should be collected in book form. We cannot overstate how much we appreciate his constant encouragement and support.

    Of course, we would also like to thank our team at Packt. They believed in our ability to write a whole book on quantum computing much more than we did ourselves! And they provided many useful suggestions and advice, making the complicated process of preparing a technical manuscript as smooth as possible.

    We’ve been extremely lucky to have such wonderful technical reviewers as Guillermo Botella and Francisco Orts. They went well beyond the call of duty to check that everything was correct, they gave us invaluable feedback and suggestions, and they located quite a number of errata and typos that would have been very embarrassing had they made it to the print version. Obviously, all remaining errors are our sole responsibility.

    And last but certainly not least, we would like to thank our friends and families. Writing a book takes a lot of time. And when we say ”a lot,” we really mean ”an incredibly awful lot.” Sadly, we had to steal part of that time from them and, moreover, they had to listen to our complaints and worries when we went through rough spots in the writing process. If it weren’t for them, we could not have made it. This is all because of them and dedicated to them.

    Elías F. Combarro, Samuel González-Castillo Oviedo/Maynooth February 2023

    Table of Contents

    Preface

    I I, for One, Welcome our New Quantum Overlords

    1 Foundations of Quantum Computing

    1.1 Quantum computing: the big picture

    1.2 The basics of the quantum circuit model

    1.3 Working with one qubit and the Bloch sphere

    1.3.1 What is a qubit?

    1.3.2 Dirac notation and inner products

    1.3.3 One-qubit quantum gates

    1.3.4 The Bloch sphere and rotations

    1.3.5 Hello, quantum world!

    1.4 Working with two qubits and entanglement

    1.4.1 Two-qubit states

    1.4.2 Two-qubit gates: tensor products

    1.4.3 The CNOT gate

    1.4.4 Entanglement

    1.4.5 The no-cloning theorem

    1.4.6 Controlled gates

    1.4.7 Hello, entangled world!

    1.5 Working with multiple qubits and universality

    1.5.1 Multi-qubit systems

    1.5.2 Multi-qubit gates

    1.5.3 Universal gates in quantum computing

    Summary

    2 The Tools of the Trade in Quantum Computing

    2.1 Tools for quantum computing: a non-exhaustive overview

    2.1.1 A non-exhaustive survey of frameworks and platforms

    2.1.2 Qiskit, PennyLane, and Ocean

    2.2 Working with Qiskit

    2.2.1 An overview of the Qiskit framework

    2.2.2 Using Qiskit Terra to build quantum circuits

    2.2.3 Using Qiskit Aer to simulate quantum circuits

    2.2.4 Let’s get real: using IBM Quantum

    2.3 Working with PennyLane

    2.3.1 Circuit engineering 101

    2.3.2 PennyLane’s interoperability

    Summary

    II When Time is Gold: Tools for Quantum Optimization

    3 Working with Quadratic Unconstrained Binary Optimization Problems

    3.1 The Max-Cut problem and the Ising model

    3.1.1 Graphs and cuts

    3.1.2 Formulating the problem

    3.1.3 The Ising model

    3.2 Enter quantum: formulating optimization problems the quantum way

    3.2.1 From classical variables to qubits

    3.2.2 Computing expectation values with Qiskit

    3.3 Moving from Ising to QUBO and back

    3.4 Combinatorial optimization problems with the QUBO model

    3.4.1 Binary linear programming

    3.4.2 The Knapsack problem

    3.4.3 Graph coloring

    3.4.4 The Traveling Salesperson Problem

    3.4.5 Other problems and other formulations

    Summary

    4 Adiabatic Quantum Computing and Quantum Annealing

    4.1 Adiabatic quantum computing

    4.2 Quantum annealing

    4.3 Using Ocean to formulate and transform optimization problems

    4.3.1 Constrained quadratic models in Ocean

    4.3.2 Solving constrained quadratic models with dimod

    4.3.3 Running constrained problems on quantum annealers

    4.4 Solving optimization problems on quantum annealers with Leap

    4.4.1 The Leap annealers

    4.4.2 Embeddings and annealer topologies

    4.4.3 Controlling annealing parameters

    4.4.4 The importance of coupling strengths

    4.4.5 Classical and hybrid samplers

    Summary

    5 QAOA: Quantum Approximate Optimization Algorithm

    5.1 From adiabatic computing to QAOA

    5.1.1 Discretizing adiabatic quantum computing

    5.1.2 QAOA: The algorithm

    5.1.3 Circuits for QAOA

    5.1.4 Estimating the energy

    5.1.5 QUBO and HOBO

    5.2 Using QAOA with Qiskit

    5.2.1 Using QAOA with Hamiltonians

    5.2.2 Solving QUBO problems with QAOA in Qiskit

    5.3 Using QAOA with PennyLane

    Summary

    6 GAS: Grover Adaptive Search

    6.1 Grover’s algorithm

    6.1.1 Quantum oracles

    6.1.2 Grover’s circuits

    6.1.3 Probability of finding a marked element

    6.1.4 Finding minima with Grover’s algorithm

    6.2 Quantum oracles for combinatorial optimization

    6.2.1 The quantum Fourier transform

    6.2.2 Encoding and adding integer numbers

    6.2.3 Computing the whole polynomial

    6.2.4 Constructing the oracle

    6.3 Using GAS with Qiskit

    Summary

    7 VQE: Variational Quantum Eigensolver

    7.1 Hamiltonians, observables, and their expectation values

    7.1.1 Observables

    7.1.2 Estimating the expectation values of observables

    7.2 Introducing VQE

    7.2.1 Getting excited with VQE

    7.3 Using VQE with Qiskit

    7.3.1 Defining a molecular problem in Qiskit

    7.3.2 Using VQE with Hamiltonians

    7.3.3 Finding excited states with Qiskit

    7.3.4 Using VQE with molecular problems

    7.3.5 Simulations with noise

    7.3.6 Running VQE on quantum computers

    7.3.7 The shape of things to come: the future of Qiskit

    7.4 Using VQE with PennyLane

    7.4.1 Defining a molecular problem in PennyLane

    7.4.2 Implementing and running VQE

    7.4.3 Running VQE on real quantum devices

    Summary

    III A Match Made in Heaven: Quantum Machine Learning

    8 What Is Quantum Machine Learning?

    8.1 The basics of machine learning

    8.1.1 The ingredients for machine learning

    8.1.2 Types of machine learning

    8.2 Do you wanna train a model?

    8.2.1 Picking a model

    8.2.2 Understanding loss functions

    8.2.3 Gradient descent

    8.2.4 Getting in the (Tensor)Flow

    8.2.5 Training the model

    8.2.6 Binary classifier performance

    8.3 Quantum-classical models

    Summary

    9 Quantum Support Vector Machines

    9.1 Support vector machines

    9.1.1 The simplest classifier you could think of

    9.1.2 How to train support vector machines: the hard-margin case

    9.1.3 Soft-margin training

    9.1.4 The kernel trick

    9.2 Going quantum

    9.2.1 The general idea behind quantum support vector machines

    9.2.2 Feature maps

    9.3 Quantum support vector machines in PennyLane

    9.3.1 Setting the scene for training a QSVM

    9.3.2 PennyLane and scikit-learn go on their first date

    9.3.3 Reducing the dimensionality of a dataset

    9.3.4 Implementing and using custom feature maps

    9.4 Quantum support vector machines in Qiskit

    9.4.1 QSVMs on Qiskit Aer

    9.4.2 QSVMs on IBM quantum computers

    Summary

    10 Quantum Neural Networks

    10.1 Building and training a quantum neural network

    10.1.1 A journey from classical neural networks to quantum neural networks

    10.1.2 Variational forms

    10.1.3 A word about measurements

    10.1.4 Gradient computation and the parameter shift rule

    10.1.5 Practical usage of quantum neural networks

    10.2 Quantum neural networks in PennyLane

    10.2.1 Preparing data for a QNN

    10.2.2 Building the network

    10.2.3 Using TensorFlow with PennyLane

    10.2.4 Gradient computation in PennyLane

    10.3 Quantum neural networks in Qiskit: a commentary

    Summary

    11 The Best of Both Worlds: Hybrid Architectures

    11.1 The what and why of hybrid architectures

    11.2 Hybrid architectures in PennyLane

    11.2.1 Setting things up

    11.2.2 A binary classification problem

    11.2.3 Training models in the real world

    11.2.4 A multi-class classification problem

    11.3 Hybrid architectures in Qiskit

    11.3.1 Nice to meet you, PyTorch!

    11.3.2 Building a hybrid binary classifier with Qiskit

    11.3.3 Training Qiskit QNNs with Runtime

    11.3.4 A glimpse into the future

    Summary

    12 Quantum Generative Adversarial Networks

    12.1 GANs and their quantum counterparts

    12.1.1 A seemingly unrelated story about money

    12.1.2 What actually is a GAN?

    12.1.3 Some technicalities about GANs

    12.1.4 Quantum GANs

    12.2 Quantum GANs in PennyLane

    12.2.1 Preparing a QGAN model

    12.3 Quantum GANs in Qiskit

    Summary

    IV Afterword and Appendices

    13 Afterword: The Future of Quantum Computing

    A Complex Numbers

    B Basic Linear Algebra

    B.1 Vector spaces

    B.2 Bases and coordinates

    B.3 Linear maps and eigenstuff

    B.4 Inner products and adjoint operators

    B.5 Matrix exponentiation

    B.6 A crash course in modular arithmetic

    C Computational Complexity

    C.1 A few words on Turing machines

    C.2 Measuring computational time

    C.3 Asymptotic complexity

    C.4 P and NP

    C.5 Hardness, completeness, and reductions

    C.6 A very brief introduction to quantum computational complexity

    D Installing the Tools

    D.1 Getting Python

    D.2 Installing the libraries

    D.3 Accessing IBM’s quantum computers

    D.4 Accessing D-Wave quantum annealers

    D.5 Using GPUs to accelerate simulations in Google Colab

    E Production Notes

    Assessments

    Chapter 1, Foundations of Quantum Computing

    Chapter 2, The Tools of the Trade in Quantum Computing

    Chapter 3, Working with Quadratic Unconstrained Binary Optimization Problems

    Chapter 4, Adiabatic Quantum Computing and Quantum Annealing

    Chapter 5, QAOA: Quantum Approximate Optimization Algorithm

    Chapter 6, GAS: Grover Adaptative Search

    Chapter 7, VQE: Variational Quantum Eigensolver

    Chapter 8, What is Quantum machine Learning?

    Chapter 9, Quantum Support Vector Machines

    Chapter 10, Quantum Neural Networks

    Chapter 11, The Best of Both Worlds: Hybrid Architectures

    Chapter 12, Quantum Generative Adversarial Networks

    Bibliography

    Other Books You Might Enjoy

    Part II, for One, Welcome our New Quantum Overlords

    This part introduces the main concepts behind the quantum circuit model. You will learn how qubits store information, how to operate on that information with quantum gates, and how to obtain results with quantum measurements. You will also learn about some of the most important tools currently used to program quantum computers. In particular, we will discuss how to implement and execute quantum circuits with Qiskit and PennyLane.

    This part includes the following chapters:

    Chapter1, Foundations of Quantum Computing

    Chapter2, The Tools of the Trade in Quantum Computing

    Chapter 1Foundations of Quantum Computing

    The beginning is always today. — Mary Shelley

    You may have heard that the mathematics needed to understand quantum computing is arcane, mysterious and difficult…but we utterly disagree! In fact, in this chapter, we will introduce all the concepts that you will need in order to follow the quantum algorithms that we will be studying in the rest of the book. Actually, you may be surprised to see that we will only rely on some linear algebra and a bit of (extremely simple) trigonometry.

    We shall start by giving a quick overview of what quantum computing is, what the current state of the art is, and what the main applications are expected to be. After that, we will introduce the model of quantum circuits. There are several computational models for quantum computing, but this is the most popular one and, moreover, it’s the one that we will be using throughout most of the book. Then, we will describe in detail what qubits are, how we can operate on them by using quantum gates, and how we can retrieve results by performing measurements. We will start with the simplest possible case — just a humble qubit! Then, we will steadily build upon that until we learn how to work with as many qubits as we want.

    This chapter will cover the following topics:

    Quantum computing: the big picture

    The basics of the quantum circuit model

    Working with one qubit and the Bloch sphere

    Working with two qubits and entanglement

    Working with multiple qubits and universality

    After reading this chapter, you will have acquired a solid understanding of the fundamentals of quantum computing and you will be more than ready to learn how practical quantum algorithms are developed.

    1.1 Quantum computing: the big picture

    In October 2019, an announcement made by a team of researchers from Google took the scientific world by storm. For the first time ever, a practical demonstration of quantum computational advantage had been shown. The results, published in the prestigious Nature journal [9], reported that a quantum computer had solved, in just a few minutes, a problem that would have taken the most powerful classical supercomputer in the world thousands of years.

    Although the task solved by the quantum computer has no direct practical applications and it was later claimed that the computing time with classical resources had been overestimated (see [75] and, also, [73]), this feat remains a milestone in the history of computing and has fueled interest in quantum computing all over the world. So, what can these mysterious quantum computers do? How do they work in order to achieve these mind-blowing speed-ups?

    We could define quantum computing as the study of the application of properties of quantum systems (such as superposition, entanglement, and interference) to accelerate some computational tasks. These properties do not manifest in our macroscopic world and, although they are present at the fundamental level in our computing devices, they are not explicitly used in the traditional computing models that we employ to build our microprocessors and to design our algorithms. For this reason, quantum computers behave in a radically different way to classical computers, making it possible to solve some tasks much more efficiently than with traditional computing devices.

    The most famous problem for which quantum algorithms offer a huge advantage over classical methods is finding prime factors of big integers. The best known classical algorithm for this task requires an amount of time that grows almost exponentially with the length of the number (see AppendixC, Computational Complexity, for all the concepts referred to computational complexity, including exponential growth). Thus, factoring numbers that are several thousand bits long becomes infeasible with classical computers, and this inefficiency is the basis for some widely used cryptographic protocols, such as RSA, proposed by Rivest, Shamir, and Adleman [80].

    Nevertheless, more than twenty years ago, the mathematician Peter Shor proved in a celebrated paper [87] that a quantum computer could factor numbers taking an amount of time that no longer grows exponentially with the size of the input, but only polynomially. Other examples in which quantum algorithms outperform classical ones include finding elements satisfying a given condition from an unsorted list (with Grover’s algorithm [48]) or sampling from the solutions of systems of linear equations (using the famous HHL algorithm [49]).

    Wonderful as the properties of these quantum algorithms are, they require quantum computers that are fault tolerant and more powerful than those available today. This is why, in the last few years, many researchers have focused on studying quantum algorithms that try to obtain some advantage with the noisy intermediate-scale quantum computers, also known as NISQdevices, that are at our disposal now. The NISQ name was coined by John Preskill in a greatly enjoyable article [78] and has been widely adopted to describe the evolutionary stage in which quantum hardware currently is.

    Machine learning and optimization are two of the fields that are being actively explored in this NISQ era. In these areas, many interesting algorithms have been proposed in recent years; some examples are the QuantumApproximate Optimization Algorithm (QAOA), the VariationalQuantum Eigensolver (VQE), or different quantum flavors of machine learning models, including Quantum Support Vector Machines (QSVMs) and Quantum Neural Networks (QNNs).

    Since these algorithms are fairly new, we still lack a complete understanding of their full capabilities. However, some partial theoretical results show some evidence that these approaches can offer some advantages over what is possible with classical computers, for instance, by giving us better approximations to the solutions of hard combinatorial optimizationproblems or by showing better performance when learning from particular datasets.

    Exploring the real possibilities of these NISQ computers and the algorithms designed to take advantage of them will be crucial in the short and medium term, and it may very likely pave the way for the first practical applications of quantum computing to real-world problems.

    We believe that you can be part of the exciting task of making quantum computing applications a reality and we would like to help you on that journey. But, for that, we need to start by setting in place the tools that we will be using throughout the book.

    If you are already familiar with the quantum circuit model, you can skip the rest of this chapter. However, we recommend that you at least skim through the following sections so that you can get familiar with the conventions and choices of notation that we will use in this book.

    1.2 The basics of the quantum circuit model

    We have mentioned that quantum computing relies on quantum phenomena such as superposition, entanglement, and interference to perform computations. But what does this really mean? To make this explicit, we need to define a particular computational model that allow us to describe mathematically how to take advantage of all these properties.

    There are many such models, including quantum Turing machines, measurement-based quantum computing (also known as one-wayquantum computing), or adiabatic quantum computing, and all of them are equivalent in power. However, the most popular one — and the one that we will be using for the most part in the book — is the quantum circuitmodel.

    To learn more…

    In addition to the quantum circuit model, sometimes we will also use the adiabatic model. All the necessary concepts will be introduced in Chapter4, Quantum Adiabatic Computing and Quantum Annealing.

    Every computation has three elements: data, operations, and output. In the quantum circuit model, these correspond to some concepts that you may have already heard about: qubits, quantum gates, and measurements. Through the remainder of this chapter, we will briefly review all of them, highlighting some special details that will be of particular importance when talking about quantum machine learning and quantum optimization algorithms; at the same time, we will show the notation that will be used throughout the book. But before committing to that, let us have a quick overview of what a quantum circuit is.

    Let’s have a look at Figure1.1. It shows a simple quantum circuit. The three horizontal lines that you see are sometimes called wires, and they represent the qubits that we are working with. Thus, in this case, we have three qubits. The circuit is meant to be read from left to right, and it represents all the different operations that are performed on the qubits. It is customary to assume that, at the very beginning, all the qubits are in state . You do not need to worry yet about what means, but please notice how we have indicated that this is indeed the initial state of all the wires by writing to the left of each of them.

    Figure 1.1

    :

    An example of a simple quantum circuit.

    In that circuit, we start by applying an operation called a gate on the top qubit; we will explain in the next section what all of these operations do, but note that we represent them with little boxes with the name of the operation inside. After that initial gate, we apply individual gates , , and on the top, middle, and bottom qubits and, then, a two-qubit gate on the top and middle qubits followed by a three-qubit gate, which acts on all the qubits at the same time. Finally, we measure the top and bottom qubits (we will get to measurements in the next section, don’t worry), and we represent this in the circuit using the gauge symbol. Notice that, after these measurements, the wires are represented with double lines, to indicate that we have obtained a result — technically, we say that the state of the qubit has collapsed to a classical value. This means that, from this point on, we do not have quantum data anymore, only classical bits. This collapse may seem a little bit mysterious (it is!), but don’t worry. In the next section, we will explain in detail the process by which quantum information (qubits) is transformed into classical data (bits).

    As you may have noticed, quantum circuits are somewhat similar to digital ones, in which we have wires representing bits and different logical gates such as AND, OR, and NOT acting on them. However, our qubits, quantum gates, and measurements obey the rules of quantum mechanics and show some properties that are not found in classical circuits. The rest of this chapter is devoted to explaining all of this in detail, starting with the simplest of cases, that of a single qubit, but growing all the way up to fully-fledged quantum circuits that can use as many qubits and gates as desired.

    Ready? Let’s start, then!

    1.3 Working with one qubit and the Bloch sphere

    One of the advantages of using a computational model is that you can forget about the particularities of the physical implementation of your computer and focus instead on the properties of the elements on which you store information and the operations you can perform on them. For instance, we could define a qubit as a (physical) quantum system that is capable of being in two different states. In practice, it could be a photon with two possible polarizations, a particle with two possible values for its spin, or a superconducting circuit, whose current can be flowing in one of two directions. When using the quantum circuit model, we can forget about those implementation details and just define a qubit…as a mathematical vector!

    1.3.1 What is a qubit?

    In fact, a qubit (short for quantum bit, sometimes also written as qbit, Qbit or even q-bit) is the minimal information unit in quantum computing. In the same way that a bit (short for binary digit) can be in state or in state , a qubit can be in state or in state . Here, we are using the so-called Dirac notation, where these funny-looking symbols surrounding and are called kets and are used to indicate that we are dealing with vectors instead of regular numbers. In fact, and are not the only possibilities for the state of a qubit and, in general, it could be in a superposition of the form

    where and are complex numbers, called amplitudes, such that . The quantity is called the norm or length of the state and, when it is equal to , we say that the state is normalized.

    To learn more…

    If you need a refresher on complex numbers or vector spaces, please check AppendixA, Complex Numbers, and AppendixB, Basic LinearAlgebra.

    All these possible values for the state of a single qubit are vectors that live in a complex vector space of dimension 2 (in fact, they live in what is called a Hilbert space, but since we will be working only with finite dimensions, there is no real difference). Thus we shall fix the vectors and as elements of a special basis, which we will refer to as the computational basis. We will represent these vectors, constituents of the computational basis, as the column vectors

    and hence

    If we are given a qubit and we want to determine or, rather, estimate its state, all we can do is perform a measurement and get one of two possible results: 0 or 1. We have nonetheless seen how a qubit can be in infinitely many states, so how does the state of a qubit determine the outcome of a measurement? As you likely already know, in quantum physics, these measurements are not deterministic, but probabilistic. In particular, given any qubit , the probability of getting upon a measurement is , while that of getting is . Naturally, these two probabilities must add up to 1, hence the need for the normalizationcondition.

    If upon measuring a qubit we get, let’s say, , we then know that, after the measurement, the state of the qubit is , and we say that the qubit has collapsed into that state. If we obtain , the state collapses to . Since we are obtaining results that correspond to and , we say that we are measuring in the computational basis.

    Exercise 1.1

    What is the probability of measuring 0 if the state of a qubit is ? And the probability of measuring 1? What if the state of the qubit is ? And if it is ?

    So a qubit is, mathematically, just a 2-dimensional vector that satisfies a normalization condition. Who could have known? But the surprises do not end here. In the next subsection, we will see how we can use those funny-looking kets to compute inner products in a very easy way.

    1.3.2 Dirac notation and inner products

    Dirac notation can not only be used for column vectors, but also for row vectors. In that case, we talk of bras, which, together with kets, can be used to form bra-kets. This name is a pun, because, as we are about to show, bra-kets are, in fact, inner products that are written — you guessed it — between brackets. To be more mathematically precise, with each ket we can associate a bra that is its adjoint or conjugatetranspose or Hermitian transpose. In order to obtain this adjoint, we take the ket’s column vector, we transpose it and conjugate each of its coordinates (which are, as we already know, complex numbers). We use to denote the bra associated with and to denote the bra associated with , so we have

    and, in general,

    where, as it is customary, we use the dagger symbol () for the adjoint.

    Important note

    When finding the adjoint, do not forget to conjugate the complex numbers! For instance, it holds that

    One of the reasons why Dirac notation is so popular for working with quantum systems is that, by using it, we can easily compute the inner products of kets and bras. For instance, we can readily show that

    This proves that and are not just elements of any basis but of an orthonormal one, since and are orthogonal and of length 1. Thus, we can compute the inner product of two states and using Dirac notation by noting that

    where and are the complex conjugates of and .

    Exercise 1.2

    What is the inner product of and ? And the inner product of and ?

    To learn more…

    Notice that, if , then , which is the probability of measuring if the state is . This is not accidental. In Chapter7, VQE: Variational QuantumEigensolver, for example, we will use measurements in orthonormal bases other than the computational one, and we will see how, in that case, the probability of measuring the result associated to an element of a given orthonormal basis is exactly .

    We now know what qubits are, how to measure them, and even how to benefit from Dirac notation for some useful computations. The only thing remaining is to study how to operate on qubits. Are you ready? It is time for us to get you introduced to the mighty quantum gates!

    1.3.3 One-qubit quantum gates

    So far, we have focused on how a qubit stores information in its state and on how we can access (part of) that information with measurements. But in order to develop useful algorithms, we also need some way of manipulating the state of qubits to perform computations.

    Since a qubit is, fundamentally, a quantum system, its evolution follows the laws of quantum mechanics. More precisely, if we suppose that our system is isolated from its environment, it obeys the famous Schrödingerequation.

    To learn more…

    The time-independent Schrödinger equation can be written as

    where is the Hamiltonian of the system, is the state vector of the system at time , is the imaginary unit, and is the reduced Planck constant.

    We will talk more about Hamiltonians in Chapter3, QUBO:Quadratic Unconstrained Binary Optimization, Chapter4, QuantumAdiabatic Computing and Quantum Annealing, and Chapter7, VQE:Variational Quantum Eigensolver.

    Don’t panic! To program a quantum computer, you don’t need to know how to solve Schrödinger’s equation. In fact, the only thing that you need to know is that its solutions are always a special type of linear transformations. For the purposes of the quantum circuit model, since we are working in finite-dimensional spaces and we have fixed a basis, the operations can be described by matrices that are applied to the vectors that represent the states of the qubits.

    But not any kind of matrix does the trick. According to quantum mechanics, the only matrices that we can use are the so-called unitary matrices, which are the matrices such that

    where is the identity matrix and is the adjoint of , that is, the matrix obtained by transposing and replacing each element by its complex conjugate. This means that any unitary matrix is invertible and its inverse is given by . In the context of the quantum circuit model, the operations represented by these matrices are called quantum gates.

    To learn more…

    It is relatively easy to check that unitary matrices preserve vector lengths (see, for instance, Section 5.7.5 in Dancing with Qubits, by Robert Sutor [92]). That is, if is a unitary matrix and is a quantum state (and, hence, its norm is , as we already know) then also is a valid quantum state because its norm is still . For this reason, we can safely apply unitary matrices to our quantum states and rest assured that the resulting states will satisfy the normalization condition.

    When we have just one qubit, our unitary matrices need to be of size because the state vector is of dimension 2. Thus, the simplest example of a quantum gate is the identity matrix of dimension 2, which transforms the state of the qubit by... well, by not transforming it at all. A less boring example is the gate, whose matrix is given by

    The gate is also called the NOT gate, because its action on the elements of the computational basis is

    which is exactly what the NOT gate does in classical digital circuits.

    Exercise 1.3

    Check that the gate matrix is, indeed, unitary. What is the inverse of ? What is the action of on a general qubit in a state of the form ?

    A quantum gate with no classical analog is the Hadamard or gate, given by

    This gate is extremely useful in quantum computing, for it can create superposition. To be precise, if we apply the gate on a qubit in state , we obtain

    This state is so important that it has its own name and symbol. It is called the plus state and it is denoted by . In a similar way, we have that

    and, as you probably guessed, this state is called the minus state and it is denoted by .

    Exercise 1.4

    Check that the gate matrix is, indeed, unitary. What is the action of on and ? What is the action of on and ?

    Of course, we can apply several gates to the same qubit one after the other. For instance, consider the following circuit:

    We read gates from left to right, so in the preceding circuit we would first apply an gate, then an gate and, finally, another gate. You can easily check that, if the initial state of the qubit is , it would end up again in state . But were its initial state , the final state would become .

    It turns out that this operation is also very important, and, of course, it has its own name: we call it the gate. From its action on and , we can tell that its matrix will be

    something that we could have also deduced by multiplying the matrices of the gates , , and one after the other.

    Exercise 1.5

    Check that and that