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

Shots and feature map exposure #14

Merged
merged 42 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
2ad611f
hyperparams object?
gcattan Nov 19, 2021
37d03b9
Revert "hyperparams object?"
gcattan Nov 19, 2021
26681ef
expose `shots` parameter
gcattan Nov 19, 2021
03ece51
expose `feature map`
gcattan Nov 19, 2021
bfd0355
push factory
gcattan Nov 19, 2021
6501bc9
add pooch in setup requirements
gcattan Nov 19, 2021
e26db6c
Revert "add pooch in setup requirements"
gcattan Nov 19, 2021
68c7550
update doc requirements according to a3e4e650b83a2a9ccfa5eb9a8b4234d9…
gcattan Nov 19, 2021
e6efe84
add documentation for factory
gcattan Nov 21, 2021
2339f3d
flake8
gcattan Nov 21, 2021
d3edc8c
add gen_zz_feature_map to API
gcattan Nov 23, 2021
ebab3d0
move import of gen_zz_feature_map to the end of import section
gcattan Nov 23, 2021
a75ca04
Improve documentation for gen_zz_feature_map
gcattan Nov 23, 2021
a1b1133
Update pyriemann_qiskit/classification.py
gcattan Nov 23, 2021
5c6069f
Merge branch 'gc/hyperparams' of https://github.com/gcattan/pyRiemann…
gcattan Nov 23, 2021
f16bd5d
install git in gh action
gcattan Nov 23, 2021
78b9677
approval missing
gcattan Nov 23, 2021
d5d5564
Revert "approval missing"
gcattan Nov 23, 2021
cd5db9a
Revert "install git in gh action"
gcattan Nov 23, 2021
cd20619
update pyriemann requirements?
gcattan Nov 23, 2021
06d93cf
Revert "update pyriemann requirements?"
gcattan Nov 23, 2021
6c51700
upgrade pip in doc pipeline
gcattan Nov 23, 2021
c771e53
typo in ghpages
gcattan Nov 23, 2021
951aa66
Revert "typo in ghpages"
gcattan Nov 23, 2021
c728329
Merge branch 'main' into gc/hyperparams
gcattan Nov 23, 2021
835624a
improve ghpages pipeline
gcattan Nov 23, 2021
c770884
install git
gcattan Nov 23, 2021
10ea48d
fix requirement in setup.py
gcattan Nov 23, 2021
bf8d690
missing -y option
gcattan Nov 23, 2021
d0bf4eb
Update pyriemann_qiskit/classification.py
gcattan Nov 24, 2021
4a560ab
Update pyriemann_qiskit/utils/hyper_params_factory.py
gcattan Nov 24, 2021
773bfa8
Update pyriemann_qiskit/classification.py
gcattan Nov 24, 2021
12e53f6
Update pyriemann_qiskit/utils/hyper_params_factory.py
gcattan Nov 24, 2021
995e2bb
Update pyriemann_qiskit/utils/hyper_params_factory.py
gcattan Nov 24, 2021
8b7a0ef
Update pyriemann_qiskit/utils/hyper_params_factory.py
gcattan Nov 24, 2021
f70e7be
fix typing of entanglement attribute
gcattan Nov 24, 2021
d5a547a
- add tests
gcattan Nov 24, 2021
65a46e9
various refactoring and linting
gcattan Nov 24, 2021
514b661
typo
gcattan Nov 24, 2021
2c40e49
generalize -> string value -> value
gcattan Nov 24, 2021
53b16ef
rename feature_dim -> n_features
gcattan Nov 25, 2021
e60dab4
use pytest.raises(ValueError)
gcattan Nov 25, 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
2 changes: 1 addition & 1 deletion doc/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ sphinx-gallery
sphinx-bootstrap_theme
numpydoc
cython
mne
mne[data]>=0.24
seaborn
scikit-learn
joblib
Expand Down
21 changes: 15 additions & 6 deletions pyriemann_qiskit/classification.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Module for classification function."""
import numpy as np

from .utils.hyper_params_factory import gen_zz_feature_map
qbarthelemy marked this conversation as resolved.
Show resolved Hide resolved

from sklearn.base import BaseEstimator, ClassifierMixin

from qiskit import BasicAer, IBMQ
from qiskit.circuit.library import ZZFeatureMap, TwoLocal
from qiskit.circuit.library import TwoLocal
from qiskit.aqua import QuantumInstance, aqua_globals
from qiskit.aqua.quantum_instance import logger
from qiskit.aqua.algorithms import QSVM, SklearnSVM, VQC
Expand Down Expand Up @@ -45,7 +47,11 @@ class QuanticClassifierBase(BaseEstimator, ClassifierMixin):
the classification task will be running on a IBM quantum backend
verbose : bool (default:True)
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], Union[QuantumCircuit, FeatureMap]]
gcattan marked this conversation as resolved.
Show resolved Hide resolved
Generate a feature map.
The feature map encodes the data into a quantum state.
gcattan marked this conversation as resolved.
Show resolved Hide resolved
Notes
-----
.. versionadded:: 0.0.1
Expand All @@ -68,11 +74,14 @@ class QuanticClassifierBase(BaseEstimator, ClassifierMixin):

"""

def __init__(self, quantum=True, q_account_token=None, verbose=True):
def __init__(self, quantum=True, q_account_token=None, verbose=True,
shots=1024, gen_feature_map=gen_zz_feature_map()):
self.verbose = verbose
self._log("Initializing Quantum Classifier")
self.q_account_token = q_account_token
self.quantum = quantum
self.shots = shots
self.gen_feature_map = gen_feature_map
# protected field for child classes
self._training_input = {}

Expand Down Expand Up @@ -148,8 +157,7 @@ def fit(self, X, y):

feature_dim = get_feature_dimension(self._training_input)
self._log("Feature dimension = ", feature_dim)
self._feature_map = ZZFeatureMap(feature_dimension=feature_dim, reps=2,
entanglement='linear')
self._feature_map = self.gen_feature_map(feature_dim)
if self.quantum:
if not hasattr(self, "_backend"):
def filters(device):
Expand All @@ -166,7 +174,8 @@ def filters(device):
self._log("Quantum backend = ", self._backend)
seed_sim = aqua_globals.random_seed
seed_trs = aqua_globals.random_seed
self._quantum_instance = QuantumInstance(self._backend, shots=1024,
self._quantum_instance = QuantumInstance(self._backend,
shots=self.shots,
seed_simulator=seed_sim,
seed_transpiler=seed_trs)
self._classifier = self._init_algo(feature_dim)
Expand Down
5 changes: 5 additions & 0 deletions pyriemann_qiskit/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from . import hyper_params_factory

__all__ = [
'hyper_params_factory',
]
23 changes: 23 additions & 0 deletions pyriemann_qiskit/utils/hyper_params_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from qiskit.circuit.library import ZZFeatureMap


def gen_zz_feature_map(reps=2, entanglement='linear'):
qbarthelemy marked this conversation as resolved.
Show resolved Hide resolved
qbarthelemy marked this conversation as resolved.
Show resolved Hide resolved
"""Return a callable that generate a ZZFeatureMap.
A feature map encodes data into a quantum state.
A ZZFeatureMap is a second-order Pauli-Z evolution circuit.

Parameters
----------
reps : int
The number of repeated circuits, has a min. value of 1.
qbarthelemy marked this conversation as resolved.
Show resolved Hide resolved
entanglement : Union[str, List[List[int]], Callable[[int], List[int]]]
qbarthelemy marked this conversation as resolved.
Show resolved Hide resolved
Specifies the entanglement structure.

Returns
-------
ret : ZZFeatureMap
An instance of ZZFeatureMap
"""
gcattan marked this conversation as resolved.
Show resolved Hide resolved
return lambda dim: ZZFeatureMap(feature_dimension=dim,
gcattan marked this conversation as resolved.
Show resolved Hide resolved
reps=reps,
entanglement=entanglement)