qiskit-community / qiskit-machine-learning

Quantum Machine Learning
https://qiskit-community.github.io/qiskit-machine-learning/
Apache License 2.0
645 stars 317 forks source link

Extend type hints in the classifiers and regressors #159

Open adekusar-drl opened 3 years ago

adekusar-drl commented 3 years ago

What is the expected behavior?

Since we are going to introduce categorical labels (and perhaps categorical features), I think, we can re-consider type hints for the classifiers and regressors and make them more like in Scikit-Learn: array_like. This may allow to pass plain lists or other types. As an example take a look at the interface here: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html

This issue is just for discussion.

edoaltamura commented 6 months ago

@adekusar-drl, could you please share an example or template of how you'd match the SKlearn type hinting for QML?

adekusar-drl commented 6 months ago

It has been a low priority for a long time, things might have changed. While scikit learn does not expose type hints by default, one of the possible options to take a look at https://numpy.org/doc/stable/reference/typing.html#module-numpy.typing. Numpy has ArrayLike type hint and it may work. This issue is more exploratory one rather something that can be easily implemented. Even if numpy's hinting is good enough, I'd first double check if such hinting brings in any value, not problems.

edoaltamura commented 6 months ago

For instance https://github.com/qiskit-community/qiskit-machine-learning/blob/c59063af02afbb56cfdb27408068aa1c0f5bc718/qiskit_machine_learning/algorithms/classifiers/pegasos_qsvc.py#L128-L130 would become

import numpy.typing as npt

def fit( 
     self, X: npt.ArrayLike, y: npt.ArrayLike, sample_weight: npt.ArrayLike | None = None 
 ) -> "PegasosQSVC": 

or, more strictly,

import numpy.typing as npt

def fit( 
     self, X: npt.NDArray[np.float64], y: npt.NDArray[np.float64], sample_weight: npt.NDArray[np.float64] | None = None 
 ) -> "PegasosQSVC": 

While young, the new typing in Numpy is covered by tests and CI, and made it into a major release. So I'd consider the basic features relatively stable.