Skip to content

Commit

Permalink
Adjusting the parameters for QuantumMDM (class and pipeline) (#230)
Browse files Browse the repository at this point in the history
* Removing un unused parameter "gen_feature_map" for QuantumMDM
* Adding a new parameter "upper_bound" for QuantumMDM
* Fixed a problem in base class
* Fixed a problem in VotingClassifier
* Tested with QuantumMDM and QuantumSVM
  • Loading branch information
toncho11 authored Dec 20, 2023
1 parent 8ef619b commit 0448cbd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
15 changes: 8 additions & 7 deletions pyriemann_qiskit/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ def fit(self, X, y):

n_features = get_feature_dimension(self._training_input)
self._log("Feature dimension = ", n_features)
self._feature_map = self.gen_feature_map(n_features)
if hasattr(self, "gen_feature_map") and self.gen_feature_map is not None:
self._feature_map = self.gen_feature_map(n_features)
if self.quantum:
if not hasattr(self, "_backend"):
devices = get_devices(self._provider, n_features)
Expand Down Expand Up @@ -619,11 +620,10 @@ class QuanticMDM(QuanticClassifierBase):
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] \
(default : Callable[int, ZZFeatureMap])
Function generating a feature map to encode data into a quantum state.
seed: int | None (default: None)
Random seed for the simulation
upper_bound : int (default: 7)
The maximum integer value for matrix normalization.
See Also
--------
Expand Down Expand Up @@ -651,21 +651,22 @@ def __init__(
q_account_token=None,
verbose=True,
shots=1024,
gen_feature_map=gen_zz_feature_map(),
seed=None,
upper_bound=7,
):
QuanticClassifierBase.__init__(
self, quantum, q_account_token, verbose, shots, gen_feature_map, seed
self, quantum, q_account_token, verbose, shots, None, seed
)
self.metric = metric
self.upper_bound = upper_bound

def _init_algo(self, n_features):
self._log("Convex MDM initiating algorithm")
classifier = MDM(metric=self.metric)
if self.quantum:
self._log("Using NaiveQAOAOptimizer")
self._optimizer = NaiveQAOAOptimizer(
quantum_instance=self._quantum_instance, upper_bound=7
quantum_instance=self._quantum_instance, upper_bound=self.upper_bound
)
else:
self._log("Using ClassicalOptimizer (COBYLA)")
Expand Down
31 changes: 15 additions & 16 deletions pyriemann_qiskit/pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pyriemann.preprocessing import Whitening
from pyriemann_qiskit.utils.filtering import NoDimRed
from pyriemann_qiskit.utils.hyper_params_factory import (
gen_zz_feature_map,
# gen_zz_feature_map,
gen_x_feature_map,
gen_two_local,
get_spsa,
Expand Down Expand Up @@ -331,9 +331,8 @@ class QuantumMDMWithRiemannianPipeline(BasePipeline):
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] \
(default : Callable[int, XFeatureMap])
Function generating a feature map to encode data into a quantum state.
upper_bound : int (default: 7)
The maximum integer value for matrix normalization.
Attributes
----------
Expand All @@ -357,14 +356,14 @@ def __init__(
q_account_token=None,
verbose=True,
shots=1024,
gen_feature_map=gen_zz_feature_map(),
upper_bound=7,
):
self.convex_metric = convex_metric
self.quantum = quantum
self.q_account_token = q_account_token
self.verbose = verbose
self.shots = shots
self.gen_feature_map = gen_feature_map
self.upper_bound = upper_bound

BasePipeline.__init__(self, "QuantumMDMWithRiemannianPipeline")

Expand All @@ -390,12 +389,12 @@ def _create_pipe(self):
filtering = NoDimRed()

clf = QuanticMDM(
metric,
self.quantum,
self.q_account_token,
self.verbose,
self.shots,
self.gen_feature_map,
metric=metric,
quantum=self.quantum,
q_account_token=self.q_account_token,
verbose=self.verbose,
shots=self.shots,
upper_bound=self.upper_bound,
)

return make_pipeline(covariances, filtering, clf)
Expand Down Expand Up @@ -451,13 +450,13 @@ def __init__(
q_account_token=None,
verbose=True,
shots=1024,
gen_feature_map=gen_zz_feature_map(),
upper_bound=7,
):
self.quantum = quantum
self.q_account_token = q_account_token
self.verbose = verbose
self.shots = shots
self.gen_feature_map = gen_feature_map
self.upper_bound = upper_bound
BasePipeline.__init__(self, "QuantumMDMVotingClassifier")

def _create_pipe(self):
Expand All @@ -467,15 +466,15 @@ def _create_pipe(self):
self.q_account_token,
self.verbose,
self.shots,
self.gen_feature_map,
self.upper_bound,
)
clf_mean_convex_dist_euclid = QuantumMDMWithRiemannianPipeline(
"mean",
self.quantum,
self.q_account_token,
self.verbose,
self.shots,
self.gen_feature_map,
self.upper_bound,
)

return make_pipeline(
Expand Down

0 comments on commit 0448cbd

Please sign in to comment.