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

Update gradient logic for Qiskit Rust circuit data implementation #188

Merged
merged 6 commits into from
Aug 9, 2024

Conversation

woodsp-ibm
Copy link
Member

Summary

Fixes #181

This updates internals of the gradients which more directly manipulated circuit data the way it was when it was managed in Python. The Rust implementation is changing things to be more performant but things are also different so this changes the logic to try accommodate as things are currently done, which is what is in Qiskit stable releases at present, and the new Rust implementation which is presently in main branch of Qsikit repo. #181 notes the aspect of CI which is failing is when we test against main which was done to monitor for any problems here due to changes.

This has partially fixed things but there is another aspect, that I failed to notice/note in #181 that also occurs

      File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/qiskit/circuit/parameterexpression.py", line 418, in __float__
    raise TypeError(

    TypeError: ParameterExpression with unbound parameters (dict_keys([Parameter(__gθ0)])) cannot be cast to a float.

which has not been addressed at present.

Details and comments

@woodsp-ibm woodsp-ibm requested review from Cryoris and ElePT as code owners July 12, 2024 21:09
@woodsp-ibm woodsp-ibm added the on hold Can not fix yet label Jul 12, 2024
@woodsp-ibm
Copy link
Member Author

woodsp-ibm commented Jul 12, 2024

While this would be mergeable, as the tests against main, though they fail do not prevent things from being merged. I labelled this PR onHold as it would be better to have the remaining problem, as noted above, sorted so things pass completely against main.

The remaining problem, i.e.this failure

TypeError: ParameterExpression with unbound parameters (dict_keys([Parameter(__gθ0)])) cannot be cast to a float.

ends up being around the Rust implementation and from these notes, that talk about potential issues, https://github.com/Qiskit/qiskit/blob/main/releasenotes/notes/circuit-gates-rust-5c6ab6c58f7fd2c9.yaml#L47-L79 I added a workaround that simply assigns the current circuit .data back to itself and this "fixes" the issue. So I will commit that momentarily.

@coveralls
Copy link

coveralls commented Jul 12, 2024

Pull Request Test Coverage Report for Build 10177248383

Details

  • 20 of 23 (86.96%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 90.444%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit_algorithms/gradients/reverse/derive_circuit.py 16 19 84.21%
Totals Coverage Status
Change from base Build 10020859087: 0.02%
Covered Lines: 6370
Relevant Lines: 7043

💛 - Coveralls

@woodsp-ibm woodsp-ibm added stable backport potential The bug might be minimal and/or import enough to be port to stable and removed on hold Can not fix yet labels Jul 16, 2024
ElePT
ElePT previously approved these changes Jul 22, 2024
Copy link
Collaborator

@ElePT ElePT left a comment

Choose a reason for hiding this comment

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

Thanks a lot @woodsp-ibm for keeping the code updated. I was planning to eventually look into it but you got there faster :)

qiskit_algorithms/gradients/utils.py Outdated Show resolved Hide resolved
@woodsp-ibm
Copy link
Member Author

The last change, to file gradients/utils.py, is not needed with this PR Qiskit/qiskit/pull/12794, so once this merges to Qiskst main I will remove the workaround from that (i,e the data copy)

@Cryoris
Copy link
Collaborator

Cryoris commented Jul 29, 2024

Qiskit/qiskit#12794 (Jake's PR) fixes parts of this PR, as it removes the need for copying circuit.data. However, it creates new issues due to the fact that derive_circuit uses the private _parameter_table attribute. To avoid such breakage in future, woodsp-ibm#2 rewrites derive_circuit to only use public API. With that PR and Qiskit/qiskit#12794 all tests pass as expected.

So we can either

  1. Merge this as is and merge the public API PR as follow-up fix once Jake's PR is released
  2. Wait until Jake's PR is released in 1.2 and merge this PR with the PR that changes to using only public API

With Qiskit 1.2 imminent maybe option 2 is better?

Use public API in `derive_circuit` only
@woodsp-ibm
Copy link
Member Author

@Cryoris Well it seems that things need a little fixup having merged your PR. A couple of copyright dates and mypy typing by the looks.

@ElePT It seems with main that more stuff is broke - maybe with 1.2 as well. This PR Qiskit/qiskit#12824 changed base types so computeuncompute now fails e.g.

test.time_evolvers.test_pvqd.TestPVQD.test_zero_parameters
----------------------------------------------------------

Captured traceback:
~~~~~~~~~~~~~~~~~~~
    Traceback (most recent call last):

      File "/home/runner/work/qiskit-algorithms/qiskit-algorithms/test/time_evolvers/test_pvqd.py", line 216, in test_zero_parameters
    fidelity_primitive = ComputeUncompute(sampler)

      File "/home/runner/work/qiskit-algorithms/qiskit-algorithms/qiskit_algorithms/state_fidelities/compute_uncompute.py", line 83, in __init__
    raise ValueError(

    ValueError: The sampler should be an instance of BaseSampler, but got <class 'qiskit.primitives.sampler.Sampler'>

@Cryoris
Copy link
Collaborator

Cryoris commented Jul 31, 2024

Copyright & mypy done in 0c99ccb

@woodsp-ibm
Copy link
Member Author

Sampler base class change reverted in Qiskit/qiskit#12871

@woodsp-ibm
Copy link
Member Author

With Qiskit/qiskit#12794 having been merged earlier today I re-ran the CI and it all passes now. So it looks like this is good to go.

Copy link
Collaborator

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

LGTM but having touched the code maybe @ElePT should approve too 😄

@woodsp-ibm woodsp-ibm merged commit 23f3b85 into qiskit-community:main Aug 9, 2024
19 checks passed
@woodsp-ibm woodsp-ibm deleted the fixrevgrad branch August 9, 2024 12:14
mergify bot pushed a commit that referenced this pull request Aug 9, 2024
Co-authored-by: Julien Gacon <jules.gacon@googlemail.com>
(cherry picked from commit 23f3b85)
mergify bot added a commit that referenced this pull request Aug 9, 2024
…ckport #188) (#198)

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
tnemoz pushed a commit to LenaPer/qiskit-algorithms that referenced this pull request Aug 12, 2024
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.

Nightly test against Qiskit main failing
4 participants