Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quantum Kernel #2

Merged
merged 39 commits into from
Mar 21, 2021
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
0f73cd9
Added quantum_kernel, qsvc and qsvr classes.
attp Mar 3, 2021
1309347
Add quantum kernel tests and tutorial
attp Mar 4, 2021
ded371b
Merge branch 'master' into kernel
manoelmarques Mar 9, 2021
539f187
Merge branch 'master' into kernel
manoelmarques Mar 11, 2021
93f17a8
Merge branch 'master' of https://github.com/Qiskit/qiskit-machine-lea…
attp Mar 14, 2021
48e14f2
Merge branch 'kernel' of github.com:attp/qiskit-machine-learning into…
attp Mar 14, 2021
b8810a2
Addressing PR comments from @woodsp-ibm
attp Mar 15, 2021
f5fc036
merge master, fix conflicts, lints, spell
manoelmarques Mar 15, 2021
c9aeea6
Move test qkernel to test/kernels folder
manoelmarques Mar 15, 2021
4ddd693
fix copyright, notebook
manoelmarques Mar 15, 2021
f574626
add QuantumKernel to html docs
manoelmarques Mar 15, 2021
3887e42
Disable failed unit test method
manoelmarques Mar 15, 2021
e882b4e
fix html
manoelmarques Mar 15, 2021
89bdf3d
Merge branch 'master' of https://github.com/Qiskit/qiskit-machine-lea…
attp Mar 15, 2021
9f29409
Merge branch 'kernel' of github.com:attp/qiskit-machine-learning into…
attp Mar 15, 2021
be856a1
Remove unnecessary assignment, as per PR discussion.
attp Mar 15, 2021
03bbd7c
Added default feature map.
attp Mar 16, 2021
b791e3d
Fixed broken init files
attp Mar 16, 2021
09fd4a8
fix doc
manoelmarques Mar 16, 2021
80747fd
Add QSVR
manoelmarques Mar 17, 2021
3e8b1d3
Changed default feature map to PauliFeatureMap, with no features, the…
attp Mar 18, 2021
9b8acc6
Changed QSVC to take a quantum kernl and added tests
attp Mar 18, 2021
f053ba6
Added docstring to init
attp Mar 18, 2021
a19c8b1
Made ZZFeatureMap with 2 qubits default and modified evaluate method …
attp Mar 18, 2021
b56db88
Fixed linting
attp Mar 18, 2021
869528e
Edited QSVC and QSVR classes to take QuantumKernel.
attp Mar 18, 2021
9a0bb16
fix docs, style, spell
manoelmarques Mar 18, 2021
73c2d1c
Merge branch 'master' into kernel
manoelmarques Mar 18, 2021
e0561a9
Merge branch 'master' of https://github.com/Qiskit/qiskit-machine-lea…
attp Mar 18, 2021
9153eb5
Fixed quantum kernel setter and docstring
attp Mar 18, 2021
1e3d780
Changed enforce_psd default to true. Added more details to init docst…
attp Mar 18, 2021
424250b
Added tests for sending arguments to SVC constructor.
attp Mar 18, 2021
c82722b
Merge branch 'kernel' of github.com:attp/qiskit-machine-learning into…
attp Mar 18, 2021
9075340
Updated qsvr and made test_qsvr
attp Mar 19, 2021
d5c466d
Renamed quantum kernel tutorial and added example using qsvc
attp Mar 19, 2021
aa8165e
Modifed test_qkernel since default for enforce_psd is now True, not F…
attp Mar 19, 2021
7a7f424
Fix tests
attp Mar 19, 2021
9caa2d9
Merge branch 'master' into kernel
manoelmarques Mar 19, 2021
886cc81
Merge branch 'master' into kernel
manoelmarques Mar 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .pylintdict
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ hashable
hoc
innerproduct
kwargs
langle
ldots
len
maxiter
Expand All @@ -45,7 +46,9 @@ np
numpy
nxd
nxk
nxm
nxn
mxd
opflow
optim
optimizer's
Expand All @@ -55,6 +58,7 @@ parameterexpression
parametrized
params
pca
precomputed
probs
py
pytorch
Expand All @@ -65,28 +69,37 @@ qiskit
qiskit's
qnn
qp
qsvc
qsvr
qubit
qubits
rangle
rbf
readme
Regressor
Regressors
renormalize
scikit
scipy
semidefinite
sigmoid
sklearn
statevector
statevectors
stdout
str
submodules
SVC
svm
SVR
sx
sy
terra
toctree
todo
transpile
univariate
unsymmetric
utils
varform
variational
Expand Down
6 changes: 6 additions & 0 deletions docs/apidocs/qiskit_machine_learning.kernels.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. _qiskit-machine-learning-kernels:

.. automodule:: qiskit_machine_learning.kernels
:no-members:
:no-inherited-members:
:no-special-members:
5 changes: 5 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
'sphinx_autodoc_typehints',
'reno.sphinxext',
'sphinx.ext.doctest',
'sphinx.ext.intersphinx',
]
html_static_path = ['_static']
templates_path = ['_templates']
Expand Down Expand Up @@ -148,3 +149,7 @@
}

autoclass_content = 'both'

intersphinx_mapping = {
'sklearn': ('https://scikit-learn.org/stable', None),
}
1 change: 1 addition & 0 deletions qiskit_machine_learning/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
algorithms
circuit.library
datasets
kernels
neural_networks

"""
Expand Down
16 changes: 15 additions & 1 deletion qiskit_machine_learning/algorithms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,19 @@
:toctree: ../stubs/
:nosignatures:

QSVC
VQC

Regressors
++++++++++
Quantum Support Vector Regressor.

.. autosummary::
:toctree: ../stubs/
:nosignatures:

QSVR

Distribution Learners
+++++++++++++++++++++

Expand All @@ -44,15 +55,18 @@

"""

from .classifiers import VQC
from .classifiers import QSVC, VQC
from .regressors import QSVR
from .distribution_learners import (DiscriminativeNetwork,
GenerativeNetwork,
NumPyDiscriminator,
PyTorchDiscriminator,
QuantumGenerator, QGAN)

__all__ = [
'QSVC',
attp marked this conversation as resolved.
Show resolved Hide resolved
'VQC',
'QSVR',
'DiscriminativeNetwork',
'GenerativeNetwork',
'NumPyDiscriminator',
Expand Down
2 changes: 2 additions & 0 deletions qiskit_machine_learning/algorithms/classifiers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

""" Classifiers Package """

from .qsvc import QSVC
from .vqc import VQC

__all__ = [
'QSVC',
'VQC'
]
63 changes: 63 additions & 0 deletions qiskit_machine_learning/algorithms/classifiers/qsvc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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"""
import logging

from typing import Optional

from sklearn.svm import SVC

from qiskit_machine_learning.kernels.quantum_kernel import QuantumKernel

logger = logging.getLogger(__name__)


class QSVC(SVC):
r"""Quantum Support Vector Classifier.
attp marked this conversation as resolved.
Show resolved Hide resolved

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):
attp marked this conversation as resolved.
Show resolved Hide resolved
"""
Args:
quantum_kernel: QuantumKernel to be used for classification.
"""

self._quantum_kernel = quantum_kernel if quantum_kernel else QuantumKernel()

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
attp marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 19 additions & 0 deletions qiskit_machine_learning/algorithms/regressors/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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.

""" Regressors Package"""

from .qsvr import QSVR

__all__ = [
'QSVR'
]
63 changes: 63 additions & 0 deletions qiskit_machine_learning/algorithms/regressors/qsvr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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 Regressor"""
import logging

from typing import Optional

from sklearn.svm import SVR

from ...kernels.quantum_kernel import QuantumKernel

logger = logging.getLogger(__name__)


class QSVR(SVR):
r"""Quantum Support Vector Regressor.

This class shows how to use a quantum kernel for classification. The class extends
attp marked this conversation as resolved.
Show resolved Hide resolved
`sklearn.svm.SVR <https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVR.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-regression>`_.

**Example**

.. code-block::

qsvr = QSVR(quantum_kernel=qkernel)
qsvr.fit(sample_train,label_train)
qsvr.predict(sample_test)
"""

def __init__(self, *args,
quantum_kernel: Optional[QuantumKernel] = None, **kwargs):
attp marked this conversation as resolved.
Show resolved Hide resolved
"""
Args:
quantum_kernel: QuantumKernel to be used for classification.
"""

self._quantum_kernel = quantum_kernel if quantum_kernel else QuantumKernel()

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
33 changes: 33 additions & 0 deletions qiskit_machine_learning/kernels/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 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 Kernels (:mod:`qiskit_machine_learning.kernels`)

.. currentmodule:: qiskit_machine_learning.kernels

Quantum Kernels
===============

.. autosummary::
:toctree: ../stubs/
:nosignatures:

QuantumKernel

"""

from .quantum_kernel import QuantumKernel

__all__ = [
'QuantumKernel'
]
Loading