QSVC (Quantum Support Vector Classifier) can sometimes produce new classification decision boundaries that are different from those of classical SVC. An example of this was reported in a previous article. This time, we visualized (part of) the QSVC process that leads to this point. Visualization is not simply beautiful or easy to understand. While it can help us understand the mechanism in concrete terms, it can also be a trigger for new questions to arise, encouraging progress. Javier's book “QML Unlocked” was very useful, and is mentioned at the end of this article.
🔴What to visualize
A small dataset for visualization, 16 samples of Gaussian Parity, was prepared. It consists of two features and a label. The parts to be visualized in the overall flow of QSVC are shown below.
- Normalization (scaling) of input data: No need for visualization.
- Reduction of input data dimension: Not necessary in this case, since it is two-dimensional (2 features).
- Display of input data: Draw data samples in a 2D scatter plot. → Figure 1
- Convert input data to quantum state: Display quantum state on Bloch sphere. → Figure 2
- Create quantum kernel matrix: Represented as a heat map. → Figure 3
- Perform SVC learning: Display the resulting classification decision boundary in 2D. → Figure 4
Here, we use ZZFeatureMap, which is equipped in Qiskit, to convert each sample of the input data in Figure 1 into a quantum state (feature vector). The result is plotted on two Bloch spheres, as shown in Figure 2. As shown in the figure, ZZFeatureMap is a system (considered to be four-dimensional) that includes two quantum bits of entanglement and phase shift, so quantum states are displayed in both q0 and q1.
🔴Visualization of Quantum Kernel Matrix
Next, we create a quantum kernel matrix using the results of the feature map. Each element is the fidelity calculated by the inner product of two combinations of quantum states. Figure 3 shows this matrix as a heat map. The brighter it is, the higher the similarity.
Classical SVC also implicitly calculates the corresponding similarity using a kernel function during training. On the other hand, quantum QSVC calculates all similarities in advance to create a kernel matrix, which is then passed to classical SVC training. SVC training has the option kernel='precomputed'. QSVC repeatedly runs the quantum circuit for all combinations of inputs to create a quantum kernel matrix.
Now, this quantum kernel matrix does not contain any original label information. Therefore, classification cannot be performed using this matrix alone. Learning (SVC learning) using this matrix and label information is required. However, even with just Figure 3, it is possible to predict whether two samples are likely to fall into the same class or into different classes.
For example, the similarity between the vectors of sample numbers 0 and 2 is quite low, as shown in [A] in Figure 3. As shown in Figure 2, the two are almost orthogonal. And they have different labels. Therefore, it is highly likely that they will be placed in different classes.
Furthermore, the similarity between the vectors of sample numbers 4 and 8 is quite high, as shown in [B] in Figure 3. As shown in Figure 2, the two are also heading in a similar direction. And they have the same label. Therefore, it can be predicted that they will be highly likely to be placed in the same class.
🔴Visualization of classification decision boundary
Next, using the quantum kernel matrix in Figure 3 and the original label information, classification training is performed. This training is the same as classical SVC training. The resulting decision boundary is shown in Figure 4. The relationship between data points 0 and 2 [A] and the relationship between 4 and 8 [B] are as expected above.
🔴The decisive difference between quantum QSVC and classical SVC
The decisive difference between QSVC and classical SVC is not whether the kernel matrix is calculated in advance, but in which space the vector inner product (similarity) is calculated. In classical SVC, the input data is also classified after mapping it to a high-dimensional space using a kernel function. However, the properties and structure of that space are different from QSVC.
In QSVC, it is important to know what kind of quantum feature map is used to convert the input into a quantum state. Whether or not QSVC's superiority is demonstrated depends on whether the feature map can realize a mapping that is difficult to realize classically.
🔴[Reference book] Introduction to Javier's "QML Unlocked"
There are few books in Japanese to learn quantum machine learning. The ones already published have a strong theoretical aspect and are not very suitable for beginners. In the meantime, the English book by Javier Mancilla Montero, shown in Figure 5, has very easy-to-understand explanations. Although the above example is my own original work, I obtained very useful information from this book, so I would like to briefly introduce the contents of this book.
It is a compact book (228 pages in total) with 10 chapters. Chapters 1-3 are an overview of quantum computing and machine learning. Chapters 4-7 cover QSVC, and chapters 8-10 cover more advanced topics such as VQC (Variational Quantum Classifier).
Overall, the book focuses on the explanation of QSVC. Although there are few mathematical expressions, the description is precise and quite deep. The explanation of creating a quantum kernel using several types of feature maps is excellent. Going further, it also attempts to combine them for SVC learning (multi-kernel learning). It explains this concretely using quantum platforms such as Qiskit and PennyLane. All the codes provided were able to run perfectly in my local environment.
Those who reach Chapter 4 for the first time may be a little confused. This is because the chapter explains code that demonstrates (1) MinMaxScaling, (2) dimensionality reduction using principal component analysis, (3) quantum state generation using ZZFeatureMap, (4) quantum kernel matrix generation, and (5) SVC learning all at once for a large dataset consisting of 1,000 samples with 20 features. However, there is no need to worry, because the following chapters explain dimensionality reduction in detail in Chapter 5, FeatureMap in Chapter 6, and SVC learning using quantum kernels in Chapter 7. In other words, the overall picture is shown first, followed by specific discussions.
For large-scale input data, creating feature maps and quantum kernel matrices, and training using them takes a lot of time in simulation. Although this book is not a programming book, as a countermeasure, it also provides multi-core parallel processing code using the PennyLane library and Python joblib. In fact, on my M1-mac-mini (8 cores), I was able to achieve a speedup of more than four times.
----------------------------------------------------------
🟢Special thanks to Javier Mancilla Montero, Ph.D. for posting this article of mine on Linkedin, see below.
----------------------------------------------------------
Hello, thanks for your blog post. I am also working my way through Javier's book on QML. Chapter 4 is a bit dense, indeed.
返信削除I have a question about the visualization of the feature map in the form of the two Bloch-spheres, one for each qubit. May I ask how you have done this in Qiskit? I am using Qiskit 1.4.2, and I have Javier's QSVC example working. Like you, I wanted to display the Bloch-spheres with all the entries from the training set for the 2 features. But I can't get a state vector or a density matrix out of the trained QSVC-model. If you could share some code snippets, that would be great. Thanks for your work explaining this!
Thank you for your interest in this article. In Qiskit, one Bloch sphere is designed to display one quantum state, so you will need to devise a little trick. If you contact me personally by email, I will provide you with my sample code.
削除