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

Closed
OkuyanBoga opened this issue Jan 28, 2025 · 1 comment · Fixed by #951
Closed

Saving and Loading Problem of QSVC algorithm #891

OkuyanBoga opened this issue Jan 28, 2025 · 1 comment · Fixed by #951
Labels
type: question 🙋 Question about the code or its uses
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
@edoaltamura
Copy link
Collaborator

It returns an error because you initialise qsvr2 manually instead of letting the @classmethod do it for you. So not all the attributes from dill could overwrite the default ones created ex novo. The fitted check in scipy checks for vars with a starting undescore, which aren't present at initialisation and somehow don't get created on an existing instance.

The example above runs without the NotFittedError if you use the classmethod directly:

qsvc = QSVC(quantum_kernel=adhoc_kernel)
qsvc.fit(train_features, train_labels)
qsvc.save('model')

qsvc2 = QSVC.load('model') # <-- this explicitly uses the @classmethod
print(qsvc2.score(train_features, train_labels))

We could change the naming and add an example to the docs to make this clearer (see upcoming PR).

@edoaltamura edoaltamura added type: question 🙋 Question about the code or its uses and removed type: bug 🐞 Something isn't working labels Jun 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question 🙋 Question about the code or its uses
Projects
None yet
2 participants