Quantum Support Vector Classifier
- class QSVClassifier(encoding_map=None, quantum_instance=None, gamma='scale')[source]
Bases:
qlearnkit.algorithms.kernel_method_mixin.KernelMethodMixin
,sklearn.base.ClassifierMixin
,qlearnkit.algorithms.quantum_estimator.QuantumEstimator
The Quantum Support Vector Machine algorithm for classification. Maps datapoints to quantum states using a FeatureMap or similar QuantumCircuit.
Example
Classify data using the Iris dataset.
import numpy as np from qlearnkit.algorithms import QSVClassifier from qiskit import BasicAer from qiskit.utils import QuantumInstance, algorithm_globals from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from qiskit.circuit.library import ZZFeatureMap seed = 42 algorithm_globals.random_seed = seed quantum_instance = QuantumInstance(BasicAer.get_backend('statevector_simulator'), shots=1024, optimization_level=1, seed_simulator=seed, seed_transpiler=seed) # Use iris data set for training and test data X, y = load_iris(return_X_y=True) num_features = 2 X = np.asarray([x[0:num_features] for x, y_ in zip(X, y) if y_ != 2]) y = np.asarray([y_ for x, y_ in zip(X, y) if y_ != 2]) encoding_map = ZZFeatureMap(2) qsvc = QSVClassifier( encoding_map=encoding_map, quantum_instance=quantum_instance ) # use iris dataset X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=seed) qsvc.fit(X_train, y_train) print(f"Testing accuracy: " f"{qsvc.score(X_test, y_test):0.2f}")
Testing accuracy: 0.95
Creates a Quantum Support Vector Classifier
- Parameters
encoding_map – map to classical data to quantum states. Default:
ZZFeatureMap
quantum_instance – the quantum instance to set. Can be a
QuantumInstance
or aBackend
gamma – regularization parameter (float or string, default 1.0) Admitted string values: { “scale”, “auto” }
- fit(X, y)[source]
Fits the model using X as training dataset and y as training labels. The actual computation is done at the “predict” stage to allow running the qiskit backend only once
- Parameters
X – training dataset
y – training labels
- predict(X_test)[source]
Solves a Least Squares problem to predict value of input data. Internally distinguishes between binary and multiclass case. For the binary case solves an optimization problem to find a dividing hyperplane. For the multiclass case uses a one-to-rest approach and thus needs to run the algorithm n_classes different times.
- Parameters
X_test – the test data
- Return type
ndarray
- Returns
numpy ndarray of predicted labels