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

Paramètres:
  • 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.

Lève:

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.

Paramètres:

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.

Renvoie:

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

Lève:
Type renvoyé:

ndarray

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

Fit the model according to the given training data.

Paramètres:
  • X (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 (ndarray) – shape (n_samples), train labels . Must not contain more than two unique labels.

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

Renvoie:

self, Fitted estimator.

Lève:
  • 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.

Type renvoyé:

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.

Paramètres:

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

Renvoie:

A loaded model.

Lève:

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

Type renvoyé:

Any

predict(X)[source]#

Perform classification on samples in X.

Paramètres:

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.

Renvoie:

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

Lève:
Type renvoyé:

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.

Paramètres:

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.

Paramètres:
  • 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.

Renvoie:

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

Type renvoyé:

float