Skip to content

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.

This algorithm takes the following steps:

  • 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 Hilbert space.
  • Calculate the kernel matrix
    • The kernel entries are the fidelities between different feature vectors
    • In the case of QSVM - this is done on a Quantum computer.
  • 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.


[1] Havlíček, V., Córcoles, A.D., Temme, K. et al. Supervised learning with quantum-enhanced feature spaces. Nature 567, 209-212 (2019).

Creating a QSVM object

The Classiq API allows one to create a QSVM object, which handles the 3 steps of the algorithm:

  1. Train
  2. Test
  3. Predict

First, let us create a QSVM instance.

When creating a QSVM instance, one must supply which feature map will be used.

A feature map is a way to encode classical data into quantum. Here, we chose to encode the data onto the surface of the bloch sphere. Behind the scenes, this can be translated (in 2 dimensions) to:  circuit

Where x is the 2D input vector, and the circuit takes a single qubit per data-point.

from classiq import QSVM

qsvm = QSVM("bloch_sphere")

Training QSVM

In order to train, we pass data and labels, which may be either a list or a np.array.

qsvm.train(data, labels)

Testing QSVM

In order to test, we pass data and labels, which may be either a list or a np.array. In return, we get an accuracy (float). between 0 to 1, indicating how good our training was (1 is better)

accuracy: float = qsvm.test(data, labels)

Predicting QSVM

The predict method takes in data, and returns labels.

predicted_labels: np.ndarray = qsvm.predict(predict_input)

Putting all the steps together

By passing all the data in the constructor, we may call a single method (.run) to handle all of the steps for us. For example:

from classiq import QSVM

qsvm = QSVM(
    "bloch_sphere", train_data, train_labels, test_data, test_labels, predict_data

The above code will sequentially run train -> test -> predict.

If we were to pass only train and test data, then only these methods would run, and predict wouldn't.