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

Bug/607 fidelity statevector kernel cannot be pickled #778

Conversation

oscar-wallis
Copy link
Collaborator

@oscar-wallis oscar-wallis commented Feb 26, 2024

Summary

Fixes #607 where FidelityStatevectorKernel was throwing an error when pickled due to a known pickling error with functools.lru_cache. This has been fixed by adding a self.cache_size attribute and custom getstate and setstate functions used when pickling and unpickling.

Details and comments

When pickled the lru_cache is set to None in the object dict. When unpicked the cache is re-initiliased with the new cache_size variable and getstate and setstate. Unit tests added to check object pickling and unpickling as well as manually using getstate and setstate to initialise a FidelityStatevectorKernel. Passed all other unit tests.

oscar-wallis and others added 5 commits February 20, 2024 14:21
Added a new param to store cache size and a custom __getstate__  and __setstate__ to handle removing and re-initliasing the lru cache during pickle/unpickling respectively.
Copy link
Member

@woodsp-ibm woodsp-ibm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally if we have a bug that does not show up in the test cases it suggests we need one to test that aspects for better coverage and to make sure it things stay working. I think a test case that pickles and unpickles is needed.

@oscar-wallis
Copy link
Collaborator Author

Normally if we have a bug that does not show up in the test cases it suggests we need one to test that aspects for better coverage and to make sure it things stay working. I think a test case that pickles and unpickles is needed.

Does that mean a test is needed where we pickle and unpickle all qml objects?

@woodsp-ibm
Copy link
Member

Does that mean a test is needed where we pickle and unpickle all qml objects?

Seems like a lot of objects! This is much more an edge case I guess due to lru_cache so I would leave it at just a test for this object - makes sure it works in CI over the platforms and continues to do so.

@oscar-wallis oscar-wallis marked this pull request as draft February 26, 2024 17:24
@coveralls
Copy link

coveralls commented Feb 26, 2024

Pull Request Test Coverage Report for Build 8098671846

Details

  • 9 of 9 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.03%) to 92.636%

Totals Coverage Status
Change from base Build 8098398840: 0.03%
Covered Lines: 1887
Relevant Lines: 2037

💛 - Coveralls

@oscar-wallis
Copy link
Collaborator Author

Added unit testing and edited release notes.

@oscar-wallis oscar-wallis marked this pull request as ready for review February 27, 2024 15:49
Copy link
Contributor

@declanmillar declanmillar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much for addressing this issue! I just have a few minor suggestions.

.gitignore Outdated Show resolved Hide resolved
test/kernels/test_fidelity_statevector_kernel.py Outdated Show resolved Hide resolved
test/kernels/test_fidelity_statevector_kernel.py Outdated Show resolved Hide resolved
@oscar-wallis
Copy link
Collaborator Author

Thanks very much for addressing this issue! I just have a few minor suggestions.

I appreciate the suggestions! I'm surprised some of them weren't picked up by CI. All the changes were approved.

@declanmillar
Copy link
Contributor

You'll need to add from typing import Any to fidelity_statevector_kernel.py, but, unfortunately, I'm not allowed to make that suggestion as it's in an unedited code block.

@oscar-wallis
Copy link
Collaborator Author

oscar-wallis commented Feb 29, 2024

from typing import Any

Can do, why does it need to be added? - for the dict, got it

Copy link
Collaborator

@adekusar-drl adekusar-drl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thank you.

@adekusar-drl adekusar-drl added the stable backport potential The bug might be minimal and/or import enough to be port to stable label Feb 29, 2024
@oscar-wallis oscar-wallis merged commit c59063a into qiskit-community:main Feb 29, 2024
16 checks passed
mergify bot pushed a commit that referenced this pull request Feb 29, 2024
* Made fidelity_statevector_kernel picklable

Added a new param to store cache size and a custom __getstate__  and __setstate__ to handle removing and re-initliasing the lru cache during pickle/unpickling respectively.

* updated notes

* name changes

* spell corrections

* Updated description

* Added unittest for pickling

* Spelling changes

* Making error messages clearer

* Spelling -_-

* Update releasenotes/notes/fix-fid_statevector_kernel-pickling-b7fa2b13a15ec9c6.yaml

Co-authored-by: Declan Millar <[email protected]>

* Update .gitignore

Co-authored-by: Declan Millar <[email protected]>

* Update test/kernels/test_fidelity_statevector_kernel.py

Co-authored-by: Declan Millar <[email protected]>

* Update qiskit_machine_learning/kernels/fidelity_statevector_kernel.py

Co-authored-by: Declan Millar <[email protected]>

* Update test/kernels/test_fidelity_statevector_kernel.py

Co-authored-by: Declan Millar <[email protected]>

* Update qiskit_machine_learning/kernels/fidelity_statevector_kernel.py

Co-authored-by: Declan Millar <[email protected]>

* Added Any class

---------

Co-authored-by: M. Emre Sahin <[email protected]>
Co-authored-by: Declan Millar <[email protected]>
Co-authored-by: Anton Dekusar <[email protected]>
(cherry picked from commit c59063a)
edoaltamura pushed a commit that referenced this pull request Mar 1, 2024
* Made fidelity_statevector_kernel picklable

Added a new param to store cache size and a custom __getstate__  and __setstate__ to handle removing and re-initliasing the lru cache during pickle/unpickling respectively.

* updated notes

* name changes

* spell corrections

* Updated description

* Added unittest for pickling

* Spelling changes

* Making error messages clearer

* Spelling -_-

* Update releasenotes/notes/fix-fid_statevector_kernel-pickling-b7fa2b13a15ec9c6.yaml

Co-authored-by: Declan Millar <[email protected]>

* Update .gitignore

Co-authored-by: Declan Millar <[email protected]>

* Update test/kernels/test_fidelity_statevector_kernel.py

Co-authored-by: Declan Millar <[email protected]>

* Update qiskit_machine_learning/kernels/fidelity_statevector_kernel.py

Co-authored-by: Declan Millar <[email protected]>

* Update test/kernels/test_fidelity_statevector_kernel.py

Co-authored-by: Declan Millar <[email protected]>

* Update qiskit_machine_learning/kernels/fidelity_statevector_kernel.py

Co-authored-by: Declan Millar <[email protected]>

* Added Any class

---------

Co-authored-by: M. Emre Sahin <[email protected]>
Co-authored-by: Declan Millar <[email protected]>
Co-authored-by: Anton Dekusar <[email protected]>
(cherry picked from commit c59063a)

Co-authored-by: oscar-wallis <[email protected]>
@oscar-wallis oscar-wallis deleted the bug/607---FidelityStatevectorKernel-cannot-be-pickled branch March 11, 2024 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FidelityStatevectorKernel cannot be pickled
7 participants