Quantum Machine Learning 102 — QSVM Using Qiskit
As part of the peer learning series, Quantum Computing India had a session on Quantum Machine Learning 102 — QSVM hosted by me. Here’s a quick log of the session. In the end, you will find full video link.
Table Of Content
- What is Support Vector Machine, why do we need Quantum Support Vector Machine?
- Quantum Classification
- Building the Circuit to solve the classification problem
- Hands On !!!
What is Support Vector Machine
- In simple, A support vector Machine is an algorithm that fall under classification in supervised learning.
- In Supervised learning we have a labelled dataset, i.e. there are data entries which belong to some class labelled as a category in the dataset.
- The algorithm tries to learn and predict which category the data point belongs to.
Here in the GIF we can see that it is a binary classification, and the dimension so the dataset is also less, hence it could be easily classified in 3D plane. But Once we go to multiclassification dataset and higher dimensionality our classical SVM will behave like this👇.
Thus here, Quantum Machine Learning comes in to solve this problem but leveraging its property of Entanglement and Superposition and enabling parallel computing.
Quantum Classification
There are 3 steps in brief required to perform a Quantum Classification
- Convert classical data to quantum data
- We need to process the data
- We need to Apply measurements to read the output
Quantum Feature Map
Quantum Feature Maps V(Φ(𝑥⃗)) convert Classical data to Quantum data. Here Φ(…) is a classical function applied on a classical data. V(Φ(𝑥⃗)) is the parameterized circuit which converts the classical data to Quantum Data
The reason of choosing a quantum feature map is to get the quantum advantage.
4 main factors to choose a feature map:
- The feature map circuit depth
- The data map function for encoding the classical data
- The quantum gate set
- The order of expansion
Types of Feature Maps:
ZFeatureMap
The first order Pauli Z-evolution circuit.
A first order diagonal expansion is implemented using the ZFeature Map where |S| = 1. The resulting circuit contains no interactions between features of the encoded data, and therefore no entanglement
Arguments
- feature_dimensions: dimensionality of the classical data (equal to the number of required qubits)
- reps: number of times the feature map circuit is repeated
- data_map_function: function encoding the classical data.
ZZFeatureMap
- Second-order Pauli-Z evolution circuit.
- The ZZFeatureMap feature map allows |S| ≤ 2, so interactions in the data will be encoded in the feature map according to the connectivity graph and the classical data map.
- Here ϕ is a classical non linear function
Arguments
- feature_dimensions: dimensionality of the classical data (equal to the number of required qubits)
- reps: number of times the feature map circuit is repeated
- data_map_function: function encoding the classical data.
- entanglement: generates connectivity
'full'
or'linear'
or defining your own entanglement structure
PauliFeatureMap
- More general form of the feature map
- It allows the user to create feature maps using different gates
- The default value is
['Z', 'ZZ']
, which is equivalent toZZFeatureMap
.
Arguments
- feature_dimensions: dimensionality of the classical data (equal to the number of required qubits)
- reps: number of times the feature map circuit is repeated
- data_map_function: function encoding the classical data.
- entanglement: generates connectivity
'full'
or'linear'
or defining your own entanglement structure - paulis: The list of strings to be paulis
For Multiclass problems
There are various multiclass extensions supported for QSVM i.e.
All Pairs
This extension creates k × (k-1)/2 binary filters
If there are 4 classes i.e. A,B,C and D. Then there will be 6 filters, those are AB,AC,AD, BC, BD and CD. Hence based on these 6 filter values, final class will be predicted.
One Against Rest
In this extension, we create n filters for n classes i.e. one filter for each class. Each filter has to classify if the data point falls in a particular class or not.
Error Correcting Code (ECC)
Here, in this extension we create n bits, for m classes, These bits can be called as filters i.e. while training these bit values will change, but once the training is done
These bits acts a value to the class. for example class 2 here in the example is 100100. So at inferencing, when the test dataset is project, it will generate these bits and then Euclidean distance is calculated with each class. The shortest distance is preferred as the class of the test data point.
The Classical Optimizer
Once we get our predictions, A classical optimization routine changes the values of our circuit and repeats the whole process again.
The task of this loop is to lower the cost function value resulting in higher accuracy.
There are three types of Optimizers:
- COBYLA (Constrained Optimization By Linear Approximation optimizer.)
- SPSA (Simultaneous Perturbation Stochastic Approximation optimizer.)
- SLSQP (Sequential Least Squares Programming optimize)
We will cover each of them when we look in VQC in 103.
QSVM uses SPSA at the backend as it gave the best results, where as VQC gives you a free hand to choose between difference optimizers
Currently the loss function is fixed and cannot be altered.
Lets Dive in to the Code
You can find the whole code here
ZFeature Map:
ZZFeature Map:
PauliFeature Map([Z,X,ZY]):
Multiclass Dataset:
References
Quantum Machine Learning Team:
Join us in the #peerlearning series
Book your slots here —