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 11
The Best of Both Worlds: Hybrid Architectures

Unity makes strength.
— English aphorism

By now, we have a solid understanding of both classical and quantum neural networks. In this chapter, we will leverage this knowledge to explore an interesting kind of model: hybrid architectures of quantum neural networks.

In this chapter, we will discuss what these models are and how they can be useful, and we will also learn how to implement and train them with PennyLane and Qiskit. The whole chapter is going to be very hands-on, and we will also take the time to fill in some gaps regarding the actual practice of training models in real-world scenarios. In addition to this — and just to spice things up a bit — we will go beyond our usual binary classifiers and also consider other kinds of problems.

We’ll cover the following topics in this chapter:

  • The what and why of hybrid architectures

  • Hybrid architectures in PennyLane (with a brief overview of best practices...

11.1 The what and why of hybrid architectures

Up until now, we’ve used the adjective ”hybrid” to describe algorithms that rely on both classical and quantum processing; algorithms such as QAOA or VQE fit in this category, as well as the training of QSVMs and QNNs. When we talk about hybrid architectures or hybrid models, however, we refer to something more specific: we speak about models that combine classical models with other quantum-based models by joining them together and training them as a single unit. Of course, the training of hybrid models will itself be a hybrid algorithm. We know that the terminology might be confusing, but what can we do? Hybrid is too versatile a word to give it up.

In particular, we will combine quantum neural networks with classical neural networks, for they are the two models that fit more naturally together. The way we will go about doing this will be by taking a usual classical neural network and plugging in a quantum neural network...

11.2 Hybrid architectures in PennyLane

In this section, we are going to use PennyLane to implement and train a couple of hybrid QNNs in order to solve some classification problems. Firstly, we will tackle a binary classification problem, just to better understand how hybrid QNNs work in a familiar setting. Then, we will take one step further and do the same for a multi-class classification problem.

Before we get to the problems, though, let us set things up.

11.2.1 Setting things up

As on previous occasions, we shall begin by importing NumPy and TensorFlow and setting a seed for both packages — all to ensure the reproducibility of our results:

import numpy as np 
 
import tensorflow as tf 
 
 
 
seed = 1234 
 
np.random.seed(seed) 
 
tf.random.set_seed(seed)

Now we can also import some useful functions from scikit-learn. We’ve already used them extensively — they need no introduction!

from sklearn.metrics import accuracy_score 
 
from sklearn.model_selection...

11.3 Hybrid architectures in Qiskit

In the previous section, we discussed how hybrid QNNs could be implemented and trained using PennyLane in conjunction with TensorFlow, an ML framework that we already know how to use. We will devote this section to studying how to work with these hybrid architectures in Qiskit, and in this mission we will need to face a new challenge.

For better or for worse, Qiskit doesn’t have a built-in TensorFlow interface at the time of writing. It only has native support for a different ML framework: PyTorch. So, if we want to get those hybrid NNs working on Qiskit, we better learn a thing or two about PyTorch. As daunting as this task may seem, it won’t be such a hassle and it will greatly pay off in the future — and, yes, the future is our next chapter on QGANs.

Important note

We will be using version 1.13 of the PyTorch package. If you are using a different version, things may be slightly different!

What’s so special about...

Summary

This has been a long and intense chapter. We began by learning what hybrid neural networks actually are and in which use cases they can be useful. We then explored how to implement and train these hybrid networks in PennyLane and, along the way, we discussed a few good practices that apply to any machine learning project. In addition, we left our comfort zone and considered a new kind of QML problem: the training of multi-class classifiers.

Once we finished our study of PennyLane, we dived into Qiskit, and a big surprise was waiting for us there. Since Qiskit relied on an interface with the PyTorch ML package for the implementation of hybrid QNNs, we invested a good deal of effort in learning how to use PyTorch. In the process, we saw how PyTorch provided us with a level of flexibility that we simply couldn’t get using TensorFlow and Keras. At the point where we had a solid understanding of the PyTorch package, we got to work with Qiskit and its PyTorch connector and we...

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}