squlearn.kernel.ml.QGPC

class squlearn.kernel.ml.QGPC(quantum_kernel: KernelMatrixBase, **kwargs)

Quantum Gaussian process classification (QGPC), that extends the scikit-learn sklearn.gaussian_process.GaussianProcessClassifier. GaussianProcessClassifier class to use a quantum kernel.

This class shows how to use a quantum kernel for QGPC. The class inherits its methods like fit and predict from scikit-learn, see the example below. Read more in the scikit-learn user guide. Additional arguments can be set via **kwargs.

Parameters:
  • quantum_kernel (Union[KernelMatrixBase, str]) – The quantum kernel matrix to be used for the GP (either a fidelity quantum kernel (FQK) or projected quantum kernel (PQK) must be provided)

  • **kwargs – Keyword arguments for the quantum kernel matrix, possible arguments can be obtained by calling get_params(). Can be used to set for example the number of qubits (num_qubits=), or (if supported) the number of layers (num_layers=) of the underlying encoding circuit.

See also

squlearn.kernel.ml.QSVC

Quantum Support Vector classification.

Example

from sklearn.datasets import load_iris
from squlearn import Executor
from squlearn.encoding_circuit import HubregtsenEncodingCircuit
from squlearn.kernel.matrix import FidelityKernel
from squlearn.kernel.ml import QGPC
X, y = load_iris(return_X_y=True)

enc_circ = HubregtsenEncodingCircuit(num_qubits=X.shape[1], num_features=X.shape[1], num_layers=2)
q_kernel = FidelityKernel(encoding_circuit=enc_circ, executor=Executor())
q_kernel.assign_parameters(np.random.rand(enc_circ.num_parameters))
qgpc_ansatz = QGPC(quantum_kernel=q_kernel)
qgpc_ansatz.fit(X, y)
qgpc_ansatz.score(X, y)
    0.98...
qgpc_ansatz.predict_proba(X[:2,:])
    array([[0.85643716, 0.07037611, 0.07318673],
    [0.80314475, 0.09988938, 0.09696586]])

Methods:

fit(X, y)

Fit Gaussian process classification model.

Parameters:
  • X – array-like of shape (n_samples, n_features) or list of object Feature vectors or other representations of training data.

  • y – array-like of shape (n_samples,) Target values, must be binary.

Returns:

Returns an instance of self.

get_metadata_routing()

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:

routing – A MetadataRequest encapsulating routing information.

Return type:

MetadataRequest

get_params(deep: bool = True) dict

Returns hyper-parameters and their values of the QGPC class.

Parameters:

deep (bool) – If True, also the parameters for contained objects are returned (default=True).

Returns:

Dictionary with hyper-parameters and values.

log_marginal_likelihood(theta=None, eval_gradient=False, clone_kernel=True)

Return log-marginal likelihood of theta for training data.

In the case of multi-class classification, the mean log-marginal likelihood of the one-versus-rest classifiers are returned.

Parameters:
  • theta (array-like of shape (n_kernel_params,), default=None) – Kernel hyperparameters for which the log-marginal likelihood is evaluated. In the case of multi-class classification, theta may be the hyperparameters of the compound kernel or of an individual kernel. In the latter case, all individual kernel get assigned the same theta values. If None, the precomputed log_marginal_likelihood of self.kernel_.theta is returned.

  • eval_gradient (bool, default=False) – If True, the gradient of the log-marginal likelihood with respect to the kernel hyperparameters at position theta is returned additionally. Note that gradient computation is not supported for non-binary classification. If True, theta must not be None.

  • clone_kernel (bool, default=True) – If True, the kernel attribute is copied. If False, the kernel attribute is modified, but may result in a performance improvement.

Returns:

  • log_likelihood (float) – Log-marginal likelihood of theta for training data.

  • log_likelihood_gradient (ndarray of shape (n_kernel_params,), optional) – Gradient of the log-marginal likelihood with respect to the kernel hyperparameters at position theta. Only returned when eval_gradient is True.

predict(X)

Perform classification on an array of test vectors X.

Parameters:

X (array-like of shape (n_samples, n_features) or list of object) – Query points where the GP is evaluated for classification.

Returns:

C – Predicted target values for X, values are from classes_.

Return type:

ndarray of shape (n_samples,)

predict_proba(X)

Return probability estimates for the test vector X.

Parameters:

X (array-like of shape (n_samples, n_features) or list of object) – Query points where the GP is evaluated for classification.

Returns:

C – Returns the probability of the samples for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute classes_.

Return type:

array-like of shape (n_samples, n_classes)

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

set_params(**params) None

Sets value of the QGPC hyper-parameters.

Parameters:

params – Hyper-parameters and their values, e.g. num_qubits=2.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') QGPC

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object