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

Improve doc for classifiers #197

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RUN apt-get -y install build-essential

RUN pip install urllib3==1.26.12
RUN pip install "numpy<1.24"
RUN pip install qiskit-terra==0.25.2.1
RUN pip install qiskit-terra==0.45.0rc1
RUN python setup.py develop
RUN pip install .[docs]
RUN pip install .[tests]
Expand All @@ -24,10 +24,10 @@ RUN mkdir /root/mne_data
RUN mkdir /home/mne_data

## Workaround for firestore
RUN pip install protobuf==4.24.4
RUN pip install protobuf==4.25.0rc2
RUN pip install google_cloud_firestore==2.12.0
### Missing __init__ file in protobuf
RUN touch /usr/local/lib/python3.9/site-packages/protobuf-4.24.4-py3.9.egg/google/__init__.py
RUN touch /usr/local/lib/python3.9/site-packages/protobuf-4.25.0rc2-py3.9-linux-x86_64.egg/google/__init__.py
## google.cloud.location is never used in these files, and is missing in path.
RUN sed -i 's/from google.cloud.location import locations_pb2//g' '/usr/local/lib/python3.9/site-packages/google_cloud_firestore-2.12.0-py3.9.egg/google/cloud/firestore_v1/services/firestore/client.py'
RUN sed -i 's/from google.cloud.location import locations_pb2//g' '/usr/local/lib/python3.9/site-packages/google_cloud_firestore-2.12.0-py3.9.egg/google/cloud/firestore_v1/services/firestore/transports/base.py'
Expand Down
58 changes: 32 additions & 26 deletions pyriemann_qiskit/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,34 @@
in several modes quantum/classical and simulated/real
quantum computer.
"""
import numpy as np
from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.svm import SVC
from qiskit_ibm_provider import IBMProvider, least_busy
from qiskit.utils import QuantumInstance, algorithm_globals
from qiskit.utils.quantum_instance import logger
from qiskit_machine_learning.algorithms import QSVC, VQC, PegasosQSVC
from qiskit_machine_learning.kernels.quantum_kernel import QuantumKernel
from datetime import datetime
import logging
from .utils.hyper_params_factory import gen_zz_feature_map, gen_two_local, get_spsa
from .utils import get_provider, get_devices, get_simulator
import numpy as np

from pyriemann.classification import MDM
from pyriemann_qiskit.datasets import get_feature_dimension
from pyriemann_qiskit.utils import (
ClassicalOptimizer,
NaiveQAOAOptimizer,
set_global_optimizer,
)
from qiskit.utils import QuantumInstance, algorithm_globals
from qiskit.utils.quantum_instance import logger
from qiskit_ibm_provider import IBMProvider, least_busy
from qiskit_machine_learning.algorithms import QSVC, VQC, PegasosQSVC
from qiskit_machine_learning.kernels.quantum_kernel import QuantumKernel
from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.svm import SVC

from .utils.hyper_params_factory import gen_zz_feature_map, gen_two_local, get_spsa
from .utils import get_provider, get_devices, get_simulator

logger.level = logging.INFO


class QuanticClassifierBase(BaseEstimator, ClassifierMixin):

"""Quantum classification.
"""Quantum classifier

This class implements a scikit-learn wrapper around Qiskit library [1]_.
It provides a mean to run classification tasks on a local and
Expand All @@ -49,12 +51,12 @@ class QuanticClassifierBase(BaseEstimator, ClassifierMixin):
(depending on q_account_token value),
- If false, will perform classical computing instead.
q_account_token : string (default:None)
If quantum==True and q_account_token provided,
If `quantum` is True and `q_account_token` provided,
the classification task will be running on a IBM quantum backend.
If `load_account` is provided, the classifier will use the previous
token saved with `IBMProvider.save_account()`.
verbose : bool (default:True)
If true will output all intermediate results and logs.
If true, will output all intermediate results and logs.
shots : int (default:1024)
Number of repetitions of each circuit, for sampling.
gen_feature_map : Callable[int, QuantumCircuit | FeatureMap] \
Expand All @@ -76,6 +78,7 @@ class QuanticClassifierBase(BaseEstimator, ClassifierMixin):
--------
QuanticSVM
QuanticVQC
QuanticMDM

References
----------
Expand Down Expand Up @@ -234,9 +237,10 @@ def _predict(self, X):

class QuanticSVM(QuanticClassifierBase):

"""Quantum-enhanced SVM classification.
"""Quantum-enhanced SVM classifier

This class implements SVC [1]_ on a quantum machine [2]_.
This class implements a support-vector machine (SVM) classifier [1]_,
called SVC, on a quantum machine [2]_.
Note that if `quantum` parameter is set to `False`
then a classical SVC will be perfomed instead.

Expand Down Expand Up @@ -270,12 +274,12 @@ class QuanticSVM(QuanticClassifierBase):
(depending on q_account_token value),
- If false, will perform classical computing instead.
q_account_token : string (default:None)
If quantum==True and q_account_token provided,
If `quantum` is True and `q_account_token` provided,
the classification task will be running on a IBM quantum backend.
If `load_account` is provided, the classifier will use the previous
token saved with `IBMProvider.save_account()`.
verbose : bool (default:True)
If true will output all intermediate results and logs.
If true, will output all intermediate results and logs.
shots : int (default:1024)
Number of repetitions of each circuit, for sampling.
gen_feature_map : Callable[int, QuantumCircuit | FeatureMap] \
Expand Down Expand Up @@ -401,8 +405,9 @@ def predict(self, X):

class QuanticVQC(QuanticClassifierBase):

"""Variational Quantum Classifier
"""Variational quantum classifier

This class implements a variational quantum classifier (VQC).
Note that there is no classical version of this algorithm.
This will always run on a quantum computer (simulated or not).

Expand All @@ -419,12 +424,12 @@ class QuanticVQC(QuanticClassifierBase):
(depending on q_account_token value).
- If false, will perform classical computing instead.
q_account_token : string (default:None)
If quantum==True and q_account_token provided,
If `quantum` is True and `q_account_token` provided,
the classification task will be running on a IBM quantum backend.
If `load_account` is provided, the classifier will use the previous
token saved with `IBMProvider.save_account()`.
verbose : bool (default:True)
If true will output all intermediate results and logs
If true, will output all intermediate results and logs
shots : int (default:1024)
Number of repetitions of each circuit, for sampling
gen_feature_map : Callable[int, QuantumCircuit | FeatureMap] \
Expand Down Expand Up @@ -552,11 +557,11 @@ def parameter_count(self):

class QuanticMDM(QuanticClassifierBase):

"""Quantum-enhanced MDM
"""Quantum-enhanced MDM classifier

This class is a convex implementation of the MDM [1]_,
that can runs with quantum optimization.
Only log-euclidian distance between trial and class prototypes is supported
This class is a convex implementation of the Minimum Distance to Mean (MDM)
[1]_, which can run with quantum optimization.
Only log-Euclidean distance between trial and class prototypes is supported
at the moment, but any type of metric can be used for centroid estimation.

Notes
Expand All @@ -577,16 +582,17 @@ class QuanticMDM(QuanticClassifierBase):
distance in order to keep the good sensitivity for the classification.
quantum : bool (default: True)
Only applies if `metric` contains a convex distance or mean.

- If true will run on local or remote backend
(depending on q_account_token value),
- If false, will perform classical computing instead.
q_account_token : string (default:None)
If quantum==True and q_account_token provided,
If `quantum` is True and `q_account_token` provided,
the classification task will be running on a IBM quantum backend.
If `load_account` is provided, the classifier will use the previous
token saved with `IBMProvider.save_account()`.
verbose : bool (default:True)
If true will output all intermediate results and logs.
If true, will output all intermediate results and logs.
shots : int (default:1024)
Number of repetitions of each circuit, for sampling.
gen_feature_map : Callable[int, QuantumCircuit | FeatureMap] \
Expand Down