QSVM
QSVM
QSVM(
feature_map: QCallable[CArray, QNum],
num_qubits: int,
execution_preferences: (
ExecutionPreferences | None
) = None,
)
Quantum support vector machine (QSVM) model. Classifies classical data into two categories. The model is first trained, and fitted. After pre-training, the model predicts the labels of new data points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_map
|
QCallable
|
Feature map to be used for the kernel |
required |
num_qubits
|
int
|
Number of qubits used to encode each data point |
required |
execution_preferences
|
ExecutionPreferences | None
|
Execution preferences for the kernel |
None
|
Methods:
| Name | Description |
|---|---|
get_qprog |
Returns the quantum program. |
get_svm_model |
Returns the classical SVM model. |
predict |
Predicts labels for new data using a precomputed kernel with a trained SVM model. Evaluates kernel |
test |
Predicts the labels of the test dataset and evaluates the resulting test score using the ground-truth labels. |
train |
Trains an SVM model using a custom precomputed kernel from the training data. |
Attributes:
| Name | Type | Description |
|---|---|---|
execution_preferences |
|
|
feature_map |
|
|
kernel_eval |
|
|
model |
|
|
num_qubits |
|
|
train_data |
ndarray | None
|
|
execution_preferences
execution_preferences = execution_preferences
feature_map
feature_map = feature_map
kernel_eval
kernel_eval = QuantumKernelEvaluator(
feature_map=feature_map,
num_qubits=num_qubits,
execution_preferences=execution_preferences,
)
model
model = SVC(kernel='precomputed')
num_qubits
num_qubits = num_qubits
train_data
train_data: ndarray | None = None
get_svm_model
get_svm_model() -> SVC
Returns the classical SVM model.
predict
predict(data: ndarray) -> ndarray
Predicts labels for new data using a precomputed kernel with a trained SVM model. Evaluates kernel matrix elements which are associated with the support vectors (those associated with non-vanishing coefficients in the prediction equation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[ndarray]
|
List of new data points to predict. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Predicted labels (0,1). |
test
test(
data: ndarray, data_labels: ndarray
) -> tuple[float, ndarray]
Predicts the labels of the test dataset and evaluates the resulting test score using the ground-truth labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[ndarray]
|
List of test data points to predict. |
required |
data_labels
|
ndarray
|
Contains the test data labels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[float, ndarray]
|
containing test score (float) and test labels (np.ndarray[int]). |
train
train(train_data: ndarray, train_labels: ndarray) -> None
Trains an SVM model using a custom precomputed kernel from the training data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_data
|
ndarray
|
Contains the data points (np.ndarray) |
required |
train_labels
|
ndarray
|
Contains the labels (0,1). |
required |
data_and_labels_to_data_dict
data_and_labels_to_data_dict(
data: ndarray, labels: ndarray
) -> dict
data_dict_to_data_and_labels
data_dict_to_data_and_labels(
data_dict: dict,
) -> tuple[ndarray, ndarray]
generate_data
generate_data(
dimension: int = 2,
amount: int = 20,
amount_of_sources: int = 2,
range_min: float = 0,
range_max: float = 2 * pi,
std: float = 0.5,
sources: ndarray | None = None,
) -> dict[int, ndarray]
pauli_feature_map
pauli_feature_map(
data: CArray[CReal],
paulis_list: list[list[Pauli]],
affines: list[list[float]],
connectivity: ConnectivityType,
reps: CInt,
qba: QArray,
) -> None