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

Multi-class Classification Problem Using QSVC #792

Closed
pragmatic-philosopher09 opened this issue Mar 30, 2024 · 3 comments
Closed

Multi-class Classification Problem Using QSVC #792

pragmatic-philosopher09 opened this issue Mar 30, 2024 · 3 comments
Assignees
Labels
good first issue Good for newcomers type: documentation 📖 Issues related to documentation type: feature request 💡 New feature or request

Comments

@pragmatic-philosopher09
Copy link

pragmatic-philosopher09 commented Mar 30, 2024

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
Copy link
Member

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
Copy link
Collaborator

edoaltamura commented Apr 3, 2024

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 edoaltamura added good first issue Good for newcomers type: feature request 💡 New feature or request type: documentation 📖 Issues related to documentation labels Apr 3, 2024
@edoaltamura
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers type: documentation 📖 Issues related to documentation type: feature request 💡 New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants