English
Languages
English
Bengali
French
Hindi
Italian
Japanese
Korean
Malayalam
Russian
Spanish
Tamil
Turkish
Vietnamese
Shortcuts

Source code for qiskit_machine_learning.algorithms.classifiers.qsvc

# This code is part of Qiskit.
#
# (C) Copyright IBM 2021.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Quantum Support Vector Classifier"""

from typing import Optional

from sklearn.svm import SVC

from qiskit.utils.algorithm_globals import algorithm_globals

from qiskit_machine_learning.kernels.quantum_kernel import QuantumKernel


[docs]class QSVC(SVC): r"""Quantum Support Vector Classifier. This class shows how to use a quantum kernel for classification. The class extends `sklearn.svm.SVC <https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html>`_, and thus inherits its methods like ``fit`` and ``predict`` used in the example below. Read more in the `sklearn user guide <https://scikit-learn.org/stable/modules/svm.html#svm-classification>`_. **Example** .. code-block:: qsvc = QSVC(quantum_kernel=qkernel) qsvc.fit(sample_train,label_train) qsvc.predict(sample_test) """ def __init__(self, *args, quantum_kernel: Optional[QuantumKernel] = None, **kwargs): """ Args: quantum_kernel: QuantumKernel to be used for classification. *args: Variable length argument list to pass to SVC constructor. **kwargs: Arbitrary keyword arguments to pass to SVC constructor. """ self._quantum_kernel = quantum_kernel if quantum_kernel else QuantumKernel() if "random_state" not in kwargs: kwargs["random_state"] = algorithm_globals.random_seed super().__init__( kernel=self._quantum_kernel.evaluate, *args, **kwargs, ) @property def quantum_kernel(self) -> QuantumKernel: """Returns quantum kernel""" return self._quantum_kernel @quantum_kernel.setter def quantum_kernel(self, quantum_kernel: QuantumKernel): """Sets quantum kernel""" self._quantum_kernel = quantum_kernel self.kernel = self._quantum_kernel.evaluate

© Copyright 2018, 2021, Qiskit Machine Learning Development Team.

Built with Sphinx using a theme provided by Read the Docs.