Skip to content

Saving and Loading Problem of QSVC algorithm #891

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

Open
OkuyanBoga opened this issue Jan 28, 2025 · 0 comments
Open

Saving and Loading Problem of QSVC algorithm #891

OkuyanBoga opened this issue Jan 28, 2025 · 0 comments
Labels
type: bug 🐞 Something isn't working
Milestone

Comments

@OkuyanBoga
Copy link
Collaborator

OkuyanBoga commented Jan 28, 2025

Environment

  • Qiskit Machine Learning version: 0.8.2
  • Python version: 3.11
  • Operating system: WSL2 Ubuntu Linux

What is happening?

I think QSVC does not save the fitted QSVC correctly. Using QSVC.save() and QSVC.load() throws a SVC not fitted error.

sklearn.exceptions.NotFittedError: This QSVC instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.

Also, there is no way to call fitted kernel, and I think it is not saved neither.

How can we reproduce the issue?

from qiskit_machine_learning.datasets import ad_hoc_data

adhoc_dimension = 2
train_features, train_labels, test_features, test_labels, adhoc_total = ad_hoc_data(
    training_size=20,
    test_size=5,
    n=adhoc_dimension,
    gap=0.3,
    plot_data=False,
    one_hot=False,
    include_sample_total=True,
)

from qiskit.circuit.library import ZZFeatureMap
from qiskit.primitives import StatevectorSampler as Sampler
from qiskit_machine_learning.state_fidelities import ComputeUncompute
from qiskit_machine_learning.kernels import FidelityQuantumKernel

adhoc_feature_map = ZZFeatureMap(feature_dimension=adhoc_dimension, reps=2, entanglement="linear")

sampler = Sampler()

fidelity = ComputeUncompute(sampler=sampler)

adhoc_kernel = FidelityQuantumKernel(fidelity=fidelity, feature_map=adhoc_feature_map)

from qiskit_machine_learning.algorithms import QSVC
qsvc = QSVC(quantum_kernel=adhoc_kernel)
qsvc.fit(train_features, train_labels)
qsvc2 = QSVC(quantum_kernel=adhoc_kernel)
qsvc.save('model')

qsvc2.load('model')
print(qsvc2.score(train_features, train_labels))

What should happen?

There is no point in saving and loading model if the fitted quantum kernel is not saved and loaded since it is the main bottleneck of the calculation for simulations.

Any suggestions?

No response

@edoaltamura edoaltamura added the type: bug 🐞 Something isn't working label Feb 3, 2025
@edoaltamura edoaltamura added this to the v.0.8.3 milestone Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug 🐞 Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants