qxmt.models.qsvr module#
- class qxmt.models.qsvr.QSVR(kernel, n_jobs=2, **kwargs)#
Bases:
BaseKernelModel
Quantum Support Vector Regression (QSVR) model. This class wraps the sklearn.svm.SVR class to provide a QSVR model. Then, many methods use the same interface as the sklearn.svm.SVR class. References: https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVR.html
Examples
>>> from qxmt.models.qsvc import QSVR >>> 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 = QSVR(kernel=kernel) >>> model.fit(X_train, y_train) >>> model.predict(X_test) np.array([-0.5441202 , -0.19483975, -0.19773099, -0.24928479, 0.12222858])
- Parameters:
kernel (BaseKernel)
n_jobs (int)
kwargs (Any)
- __init__(kernel, n_jobs=2, **kwargs)#
Initialize the QSVR 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 QSVR model. Default to use the R^2 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
- 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 QSVR 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
- 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