PegasosQSVC#

class PegasosQSVC(quantum_kernel=None, C=1.0, num_steps=1000, precomputed=False, seed=None)[source]#

Bases: ClassifierMixin, SerializableModelMixin

Implements Pegasos Quantum Support Vector Classifier algorithm. The algorithm has been developed in [1] and includes methods fit, predict and decision_function following the signatures of sklearn.svm.SVC. This implementation is adapted to work with quantum kernels.

Example

quantum_kernel = FidelityQuantumKernel()

pegasos_qsvc = PegasosQSVC(quantum_kernel=quantum_kernel)
pegasos_qsvc.fit(sample_train, label_train)
pegasos_qsvc.predict(sample_test)
References
[1]: Shalev-Shwartz et al., Pegasos: Primal Estimated sub-GrAdient SOlver for SVM.

Pegasos for SVM

Parameters:
  • quantum_kernel (BaseKernel | None) – A quantum kernel to be used for classification. Has to be None when a precomputed kernel is used. If None, and precomputed is False, the quantum kernel will default to FidelityQuantumKernel.

  • C (float) – Positive regularization parameter. The strength of the regularization is inversely proportional to C. Smaller C induce smaller weights which generally helps preventing overfitting. However, due to the nature of this algorithm, some of the computation steps become trivial for larger C. Thus, larger C improve the performance of the algorithm drastically. If the data is linearly separable in feature space, C should be chosen to be large. If the separation is not perfect, C should be chosen smaller to prevent overfitting.

  • num_steps (int) – The number of steps in the Pegasos algorithm. There is no early stopping criterion. The algorithm iterates over all steps.

  • precomputed (bool) – A boolean flag indicating whether a precomputed kernel is used. Set it to True in case of precomputed kernel.

  • seed (int | None) – A seed for the random number generator.

Raises:

ValueError

  • if quantum_kernel is passed and precomputed is set to True. To use a precomputed kernel, quantum_kernel has to be of the None type. - if C is not a positive number.

Attributes

FITTED = 0#
UNFITTED = 1#
num_steps#

Returns number of steps in the Pegasos algorithm.

precomputed#

Returns a boolean flag indicating whether a precomputed kernel is used.

quantum_kernel#

Returns quantum kernel

Methods

decision_function(X)[source]#

Evaluate the decision function for the samples in X.

Parameters:

X (ndarray) – Features. For a callable kernel (an instance of BaseKernel) the shape should be (m_samples, n_features), for a precomputed kernel the shape should be (m_samples, n_samples). Where m denotes the set to be predicted and n the size of the training set. In that case, the kernel values in X have to be calculated with respect to the elements of the set to be predicted and the training set.

Returns:

An array of the shape (n_samples), the decision function of the sample.

Raises:
Return type:

ndarray

fit(X, y, sample_weight=None)[source]#

Fit the model according to the given training data.

Parameters:
  • X (np.ndarray) – Train features. For a callable kernel (an instance of BaseKernel) the shape should be (n_samples, n_features), for a precomputed kernel the shape should be (n_samples, n_samples).

  • y (np.ndarray) – shape (n_samples), train labels . Must not contain more than two unique labels.

  • sample_weight (np.ndarray | None) – this parameter is not supported, passing a value raises an error.

Returns:

self, Fitted estimator.

Raises:
  • ValueError

    • X and/or y have the wrong shape. - X and y have incompatible dimensions. - y includes more than two unique labels. - Pre-computed kernel matrix has the wrong shape and/or dimension.

  • NotImplementedError

    • when a sample_weight which is not None is passed.

Return type:

PegasosQSVC

classmethod load(file_name)#

Loads a model from the file. If the loaded model is not an instance of the class whose method was called, then a warning is raised. Nevertheless, the loaded model may be a valid model.

Parameters:

file_name (str) – a file name or path to load a model from.

Returns:

A loaded model.

Raises:

TypeError – if a loaded model is not an instance of the expected class.

Return type:

Any

predict(X)[source]#

Perform classification on samples in X.

Parameters:

X (ndarray) – Features. For a callable kernel (an instance of BaseKernel) the shape should be (m_samples, n_features), for a precomputed kernel the shape should be (m_samples, n_samples). Where m denotes the set to be predicted and n the size of the training set. In that case, the kernel values in X have to be calculated with respect to the elements of the set to be predicted and the training set.

Returns:

An array of the shape (n_samples), the predicted class labels for samples in X.

Raises:
Return type:

ndarray

save(file_name)#

Saves this model to the specified file. Internally, the model is serialized via dill. All parameters are saved, including a primitive instance that is referenced by internal objects. That means if a model is loaded from a file and is used, for instance, for inference, the same primitive will be used even if a cloud primitive was used.

Parameters:

file_name (str) – a file name or path where to save the model.

score(X, y, sample_weight=None)#

Return the mean accuracy on the given test data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Test samples.

  • y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.

  • sample_weight (array-like of shape (n_samples,), default=None) – Sample weights.

Returns:

score – Mean accuracy of self.predict(X) w.r.t. y.

Return type:

float