qxmt.models.qsvc module#
- class qxmt.models.qsvc.QSVC(kernel, n_jobs=2, **kwargs)#
Bases:
BaseKernelModel
Quantum Support Vector Classification (QSVC) model. This class wraps the sklearn.svm.SVC class to provide a QSVC model. Then, many methods use the same interface as the sklearn.svm.SVC class. References: https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html
Examples
>>> from qxmt.models.qsvc import QSVC >>> from qxmt.kernels.pennylane import FidelityKernel >>> from qxmt.feature_maps.pennylane.defaults import ZZFeatureMap >>> from qxmt.configs import DeviceConfig >>> from qxmt.devices.builder import DeviceBuilder >>> config = DeviceConfig( ... platform="pennylane", ... name="default.qubit", ... n_qubits=2, ... shots=1000, >>> ) >>> device = DeviceBuilder(config).build() >>> feature_map = ZZFeatureMap(2, 2) >>> kernel = FidelityKernel(device, feature_map) >>> model = QSVC(kernel=kernel) >>> model.fit(X_train, y_train) >>> model.predict(X_test) np.array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1])
- Parameters:
kernel (BaseKernel)
n_jobs (int)
kwargs (Any)
- __init__(kernel, n_jobs=2, **kwargs)#
Initialize the QSVC model.
- Parameters:
kernel (BaseKernel) – kernel instance of BaseKernel class
n_jobs (int) – number of jobs for parallel computation
kwargs (Any)
- Return type:
None
- cross_val_score(X, y, **kwargs)#
Cross validation score of the QSVC model. Default to use the Accuracy score.
- Parameters:
X (np.ndarray) – numpy array of features
y (np.ndarray) – numpy array of target values
kwargs (Any)
- Returns:
numpy array of scores
- Return type:
np.ndarray
- decision_function(X)#
Predict the decision function value with given features.
- Parameters:
X (np.ndarray) – numpy array of features
- Returns:
numpy array of decision function values
- Return type:
np.ndarray
- fit(X, y, save_shots_path=None, **kwargs)#
Fit the model with given features and target values.
- Parameters:
X (np.ndarray) – numpy array of features
y (np.ndarray) – numpy array of target values
save_shots_path (Optional[Path | str], optional) – save path for the shot results. Defaults to None.
kwargs (Any)
- Return type:
None
- get_params()#
Get the parameters of the model.
- Return type:
dict
- hyperparameter_search(X, y, sampler_type, search_space, search_args, objective=None, refit=True)#
Search the best hyperparameters for the QSVC model.
- Parameters:
X (np.ndarray) – dataset for search
y (np.ndarray) – target values for search
sampler_type (str) – sampler type for hyperparameter search (grid, random, tpe)
search_space (dict[str, list[Any]]) – search space for hyperparameter search
search_args (dict[str, Any]) – search arguments for hyperparameter search
objective (Optional[Callable], optional) – objective function for search. Defaults to None.
refit (bool, optional) – refit the model with best hyperparameters. Defaults to True.
- Raises:
ValueError – Not supported search type
- Returns:
best hyperparameters
- Return type:
dict[str, Any]
- load(path)#
Load the trained model from the given path.
- Parameters:
path (str | Path) – path to load the model
- Returns:
loaded QSVC model
- Return type:
- predict(X)#
Predict the target value with given features.
- Parameters:
X (np.ndarray) – numpy array of features
- Returns:
numpy array of predicted values
- Return type:
np.ndarray
- predict_proba(X)#
Predict the probability of target value with given features. This method is only available for SVC with probability=True.
- Parameters:
X (np.ndarray) – numpy array of features
- Returns:
numpy array of predicted probabilities
- Return type:
np.ndarray
- save(path)#
Save the model to the given path.
- Parameters:
path (str | Path) – path to save the model
- Return type:
None
- set_params(params)#
Set the parameters of the model.
- Parameters:
params (dict)
- Return type:
None