Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
A Practical Guide to Quantum Machine Learning and Quantum Optimization

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

Product type Book
Published in Mar 2023
Publisher Packt
ISBN-13 9781804613832
Pages 680 pages
Edition 1st Edition
Languages
Authors (2):
Elías F. Combarro Elías F. Combarro
Profile icon Elías F. Combarro
Samuel González-Castillo Samuel González-Castillo
Profile icon Samuel González-Castillo
View More author details

Table of Contents (27) Chapters

Preface Part I: I, for One, Welcome our New Quantum Overlords
Chapter 1: Foundations of Quantum Computing Chapter 2: The Tools of the Trade in Quantum Computing Part II: When Time is Gold: Tools for Quantum Optimization
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 Adaptive Search Chapter 7: VQE: Variational Quantum Eigensolver Part III: A Match Made in Heaven: Quantum Machine Learning
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 Part IV: Afterword and Appendices
Chapter 13: Afterword: The Future of Quantum Computing
Assessments Bibliography
Index
Other Books You May Enjoy Appendix A: Complex Numbers
Appendix B: Basic Linear Algebra Appendix C: Computational Complexity Appendix D: Installing the Tools Appendix E: Production Notes

Chapter 9
Quantum Support Vector Machines

Artificial Intelligence is the new electricity
— Andrew Ng

In the previous chapter, we learned the basics of machine learning and we got a sneak peek into quantum machine learning. It is now time for us to work with our first family of quantum machine learning models: that of Quantum Support Vector Machines (often abbreviated as QSVMs). These are very popular models, and they are most naturally used in binary classification problems.

In this chapter, we shall learn what (classical) support vector machines are and how they are used, and we will use this knowledge as a foundation to understand quantum support vector machines. In addition, we will explore how to implement and train quantum support vector machines with Qiskit and PennyLane.

The contents of this chapter are the following:

  • Support vector machines

  • Going quantum

  • Quantum support vector machines in PennyLane

  • Quantum support vector machines in Qiskit

9.1 Support vector machines

QSVMs are actually particular cases of Support Vector Machines (abbreviated as SVMs). In this section, we will explore how these SVMs work and how they’re used in machine learning. We will do so by first motivating the SVM formalism with some simple examples, and then building up from there: all the way up into how SVMs can be used to tackle complex classification problems with the kernel trick.

9.1.1 The simplest classifier you could think of

Let us forget about data for a moment and begin by considering a very naive problem. Let’s say that we want to build a very simple classifier on the real line. In order to do this, all we have to do is split the real number line into two disjoint categories in such a way that any number belong to exactly one of these two categories. Thus, if we are given any input (a real number), our classifier will return the category to which it belongs.

What would be the easiest way in which you could do this? Odds...

9.2 Going quantum

As we have already mentioned, quantum support vector machines are particular cases of SVMs. To be more precise, they are particular cases of SVMs that rely on the kernel trick.

We have seen in the previous section how, with the kernel trick, we take our data to a feature space: a higher dimensional space in which, we hope, our data will be separable by a hyperplane with the right choice of feature map. This feature space is usually just the ordinary Euclidean space but, well, with a higher dimension. But we can consider other choices. How about…the space of quantum states?

9.2.1 The general idea behind quantum support vector machines

A QSVM works just like an ordinary SVM that relies on the kernel trick — with the only difference that it uses as feature space a certain space of quantum states.

As we discussed before, whenever we use the kernel trick, all we need from the feature space is a kernel function. That’s the only ingredient involving the...

9.3 Quantum support vector machines in PennyLane

It has been a long journey but, finally, we are ready to see QSVMs in action. In this section, we are going to train and run a bunch of QSVM models using PennyLane. Just to get started, let’s import NumPy and set a seed so that our results are reproducible:

import numpy as np 
 
 
 
seed = 1234 
 
np.random.seed(seed)

9.3.1 Setting the scene for training a QSVM

Now, if we want to train QSVMs, we need some data to work with. In today’s ever-changing job market, you should always keep your options open and, as promising as quantum machine learning may be, you may want to have a backup career plan. Well, we’ve got you covered. Have you ever dreamed of becoming a world-class sommelier? Today is your lucky day! (We are just kidding, of course, but we will use this wine theme to give some flavor to our example!)

We’ve already seen how the scikit-learn package offers lots of tools and resources for machine learning...

9.4 Quantum support vector machines in Qiskit

In the previous section, we mastered the use of QSVMs in PennyLane. You may want to review subsection 9.3.1 and the beginning of subsection 9.3.3. That is where we prepare the dataset that we will be using here too. In addition to running the code in those subsections, you will have to do the following import:

from sklearn.metrics import accuracy_score

Now it’s time for us to switch to Qiskit. In some ways, Qiskit can be easier to use than PennyLane — although this is probably a matter of taste. In addition, Qiskit will enable us to directly train and run our QSVM models using the real quantum computers available at IBM Quantum. Nevertheless, for now, let us begin with QSVMs on our beloved Qiskit Aer simulator.

9.4.1 QSVMs on Qiskit Aer

To get started, let us just import Qiskit:

from qiskit import *

When we defined a QSVM in PennyLane, we had to ”manually” implement a kernel function in order to pass it to...

Summary

In this chapter, we first learned what support vector machines are, and how they can be trained to solve binary classification problems. We began by considering vanilla vector machines, and then we introduced the kernel trick — which opened up a world of possibilities! In particular, we saw how QSVMs are nothing more than a support vector machine with a quantum kernel.

From there on, we learned how quantum kernels actually work and how to implement them. We explored the essential role of feature maps, and discussed a few of the most well-known ones.

Finally, we learned how to implement, train, and use quantum support vector machines with PennyLane and Qiskit. In addition, we were able to very easily run QSVMs on real hardware thanks to Qiskit’s interface to IBM Quantum.

And that pretty much covers how QSVMs can help you can identify wines — or solve any other classification task — like an expert, all while happily ignoring what the ”alkalinity...

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 2023 Publisher: Packt ISBN-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.
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}