Quantum Support Vector Machines (QSVM)
Classiq's QSVM package allows the user to classify labeled data by using the relevant feature map.
Introduction and Background
Quantum Support Vector Machines is the Quantum version of SVM - a data classification method which separates the data using a hyperplane.
The algorithm takes the following steps:
- Feature map - Map the data into a different hyperspace (since the data may be non-linearly-separable in the original space)
- In the case of QSVM - mapping the classical data into a Hilbert space, using gates that are parametric in the data.
- Calculate the kernel matrix
- The kernel entries are the fidelities between different feature vectors
- In the case of QSVM - the kernel \(K(\vec{x}_i \vec{x}_j)\) is found by assigning \(\vec{x}_i\) and \(\vec{x}_j\) into the parametric quantum program, and examining the counts after execution.
- Optimize the dual problem (this is always done classically)
$$ L_D(\alpha) = \sum_{i=1}^t \alpha_i - \frac{1}{2} \sum_{i,j=1}^t y_i y_j \alpha_i \alpha_k K(\vec{x}_i \vec{x}_j) $$
- Where \(t\) is the amount of data points
- the \(\vec{x}_i\)s are the data points
- \(y_i\) is the label \(\in \{-1,1\}\) of each data point
- \(K(\vec{x}_i \vec{x}_j)\) is the kernel matrix element between the \(i\) and \(j\) data points
-
and we optimize over the \(\alpha\)s
-
We expect most of the \(\alpha\)s to be \(0\). The \(\vec{x}_i\)s that correspond to non-zero \(\alpha_i\) are called the Support Vectors.
-
Finally, we may predict unlabeled data by calculating the kernel matrix of the new datum with respect to the support vectors.
$$ \text{Predicted Label}(\vec{s}) = \text{sign} \left( \sum_{i=1}^t y_i \alpha_i^* K(\vec{x}_i , \vec{s}) + b \right), $$
Where
- \(\vec{s}\) is the data point to be classified
- \(\alpha_i^*\) are the optimized \(\alpha\)s
- And \(b\) is the bias.
Reference:
[1] Havlíček, V., Córcoles, A.D., Temme, K. et al. Supervised learning with quantum-enhanced feature spaces. Nature 567, 209-212 (2019). https://doi.org/10.1038/s41586-019-0980-2
Input Data
- The data variables are a list of a list of floats. Each vector in the list represents one data point.
- The lables variables are a list of integers. This list should have the same length as the data variable. Each integer represents the label that the corresponding data point belongs to.
For example:
train_data = [[0.1, 0.2], [1, 2], [10, 20]]
train_labels = [1, 2, 3]
test_data = [[0.3, 0.4], [3, 4], [30, 40]]
test_labels = [1, 2, 3]
predict_data = [[0.5, 0.6], [5, 6], [50, 60]]
Creating a QSVM Model
The Classiq API allows one to create a QSVM
model. The model includes:
- The qmain part - defines the parametric quantum program. This is a function of the feature map chosen.
- The cmain part - runs the qmain parametric quantum program with the data assigned in the quantum program parameters.
Creating the model is done in the following way:
from classiq import construct_qsvm_model
qsvm_model = construct_qsvm_model(
train_data=train_data,
train_labels=train_labels,
test_data=test_data,
test_labels=test_labels,
predict_data=predict_data,
feature_map_function_name="bloch_sphere_feature_map",
bloch_feature_dimension=2,
)
Here, we chose to encode the data onto the surface of the bloch sphere, so feature_map_function_name
is set to "bloch_sphere_feature_map"
with bloch_feature_dimension=2
to because the data is 2D.
Get predicted labels results
from classiq import synthesize, execute, QuantumProgram
from classiq import set_quantum_program_execution_preferences
from classiq.execution import ClassiqBackendPreferences
from classiq.execution import ExecutionPreferences
quantum_program = synthesize(qsvm_model)
QuantumProgram.from_qprog(quantum_program).show()
quantum_program = set_quantum_program_execution_preferences(
quantum_program,
ExecutionPreferences(
num_shots=1024,
backend_preferences=ClassiqBackendPreferences(backend_name="simulator"),
),
)
results = execute(quantum_program).result()
print(results)
The above code will sequentially show the parametric quantum program that represents the feature map.
It will run train
-> test
-> predict
, and will print the results: the predicted labels and the test score
The full code
from classiq import construct_qsvm_model
from classiq import synthesize, execute, QuantumProgram
from classiq import set_quantum_program_execution_preferences
from classiq.execution import ClassiqBackendPreferences
from classiq.execution import ExecutionPreferences
train_data = [[0.1, 0.2], [1, 2], [10, 20]]
train_labels = [1, 2, 3]
test_data = [[0.3, 0.4], [3, 4], [30, 40]]
test_labels = [1, 2, 3]
predict_data = [[0.5, 0.6], [5, 6], [50, 60]]
qsvm_model = construct_qsvm_model(
train_data=train_data,
train_labels=train_labels,
test_data=test_data,
test_labels=test_labels,
predict_data=predict_data,
feature_map_function_name="bloch_sphere_feature_map",
bloch_feature_dimension=2,
)
quantum_program = synthesize(qsvm_model)
QuantumProgram.from_qprog(quantum_program).show()
quantum_program = set_quantum_program_execution_preferences(
quantum_program,
ExecutionPreferences(
num_shots=1024,
backend_preferences=ClassiqBackendPreferences(backend_name="simulator"),
),
)
results = execute(quantum_program).result()
print(results)