qiskit-community / qiskit-machine-learning

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

Multi-class Classification Problem Using QSVC #792

Closed pragmatic-philosopher09 closed 5 months ago

pragmatic-philosopher09 commented 7 months ago

Tutorial of Sorts for Multiclass Classification Using Qiskit 1.0.2's QSVC

I was going through this tutorial and found it immensely helpful for understanding certain fundamentals on quantum kernels: https://github.com/qiskit-community/qiskit-machine-learning/blob/main/docs/tutorials/03_quantum_kernel.ipynb

Now, there's a use case of mine where I've done feature extraction from a dataset and I've to pass 'train_features' and 'train_labels' to the QSVC for a multi-class classification problem. This is pertinent to a research I'm pursuing to test the performance of some quantum ML methods on different aspects of classical data. Could you provide some tutorial where you could probably showcase one example of a multi-class classification (using any strategies like 'one v/s rest', 'one v/s one' approach) using QSVC. For reference, without divulging too much information of my research, the 'train_features.shape' has 'torch.Size([28248, 7])' and 'train_labels.shape' has a 'torch.Size([28248])'

woodsp-ibm commented 7 months ago

For such multiclass approaches there is the scikit-learn multiclass functionality. The QSVC is a minimal utility class that extends the scikit-learn SVC to simplify the usage. But as that tutorial shows you can directly use SVC - its really just about how the quantum kernel is passed in, and while it may be computed with quantum, once done it's all the same to SVC so you can leverage tooling from scikit-learn.

edoaltamura commented 7 months ago

This could indeed be useful to many within the community, thanks @pragmatic-philosopher09 for noting this usage and @woodsp-ibm for the suggestion. @pragmatic-philosopher09, could you please post a minimal working example using the scikit-learn multiclass in this issue once you've had the chance to delve into it?

edoaltamura commented 5 months ago

Hi @pragmatic-philosopher09 I just got the opportunity to follow up on this topic.

Multi-class classification

Multi-class cases are already supported by QSVC because it directly inherits from the Sklearn SVC, which supports them: see the documentation. in QSVC you can call the 'one-versus-one' decision strategy as

from qiskit_machine_learning.algorithms import QSVC

# Assuming you have already built the kernel, here `adhoc_kernel`
qsvc = QSVC(quantum_kernel=adhoc_kernel, decision_function_shape='ovo')
qsvc.fit(...)

From SKlearn, this decision_function_shape strategy builds n_classes * (n_classes - 1) / 2 classifiers, each training on the data pairwise. In your case, you will have n_classes=7, resulting in 21 classifiers.

Decision strategy, e.g. 'one v/s rest', 'one v/s one'

Similarly, these can be invoked directly in QSVC because of the inheritance from SVC. You should refer to the SKlearn documentation to check which strategies are supported, and what the defaults are. A note about compliance - to ensure compatibility with other classifiers, as of SKlearn version 0.19 it is sometimes advised to convert to

qsvc.decision_function_shape = "ovr"

I hope this information is helpful and don't hesitate to reach out again for questions or input.