Skip to content

"sampler" is currently a keyword argument for QNSPSA #218

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

Merged
merged 11 commits into from
Mar 31, 2025

Conversation

julenl
Copy link
Contributor

@julenl julenl commented Mar 4, 2025

Summary

The example provided for QNSPSA specifies the sampler option as if it would be positional, but it is actually a keyword argument.
I don't understand the "*" or the possibility of the sampler to be None. But in order to make it work without removing those, the sampler hast to be specified as "sampler"=sampler.

Details and comments

The example provided specifies the sampler option as if it would be positional, but it is actually a keyword argument.
I don't understand the "*" or the possibility of the sampler to be None. But in order to make it work without removing those, the sampler hast to be especified as "sampler"=sampler.
"sampler" is currently a keyword argument
@Cryoris
Copy link
Collaborator

Cryoris commented Mar 4, 2025

Hmm this could've been an unfortunate result of #17, which changed the signature from supporting either (1) a backend and an expectation (from the old opflow) or (2) a sampler, to only using the sampler. Since the sampler is now required it might be better to just make it a required argument, i.e. change the signature to

def get_fidelity(ansatz: QuantumCircuit, sampler: BaseSampler) -> ...

As suggested by @Cryoris, instead of the workaround on the example text, I updated the arguments of the get_fidelity method to include only the required elements and avoid confusion.
@Cryoris
Copy link
Collaborator

Cryoris commented Mar 6, 2025

This change makes sense to me, this probably merits an upgrade release note stating that the sampler argument can now be passed positionally as well instead of only as keyword. The CI is failing quite heavily but they all seem unrelated... there were some Aer issues that have been fixed, let's see if re-triggering the CI helps

@woodsp-ibm
Copy link
Member

woodsp-ibm commented Mar 7, 2025

@Cryoris As far as I can see, from quickly running mypy locally, is that make mypy fails with the newer 2.2.x releases of numpy installed whereas with numpy 2.1.3 or earlier it passes fine. You can sort of see that here as under Python 3.9 it works since its using numpy 2.0.2 which seems to be the last version released for 3.9. If we just wanted CI to pass we could limit numpy version here to < 2.2.0; or even disable running mypy in CI for now leaving CI using the latest numpy and therefore running unit testing with that.

See numpy/numpy#28076 where this aspect of 2.2 and failing for mypy is discussed, which I found once I had seen the above behavior.

- |
The `None` value for the `sampler` on the `QNSPSA.get_fidelity()` function
is no longer valid. A proper sampler must be provided.

Copy link
Member

Choose a reason for hiding this comment

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

My take would be all that is needed here would be an upgrade section - here are no deprecations (ie deprecated decorators added where function is kept for now and removed later - for instance with support of None which was simply removed at this time. There are no issues - the change fixes an issue but issues is for things we know fail in the release, ie current issues that were not addressed yet, nor are there any new major features added for a features sections. See the description of what goes in the sections here
https://docs.openstack.org/reno/latest/user/usage.html#editing-a-release-note

Now we could have a prelude but again it refers to deprecated - this text is combined with other release note preludes to make, in effect an overall intro for the release. I am not sure this is significant enough for that.

My take in upgrade would be to state that a sampler not must be provided as the default value of None has been removed. You can also note that the sampler has been made a positional argument so does not need to be passed using that as a keyword. FYI of course you can still do so if you wish, as in the following test case where calls get_fidelity() which

"""Test QN-SPSA optimizer is serializable."""

has not been changed and still works. I.e all the unit tests here in CI pass

Ran: 588 tests in 71.4273 sec.

  • Passed: 574
  • Skipped: 14
  • Expected Fail: 0
  • Unexpected Success: 0
  • Failed: 0

CI overall is still failing overall though, as noted above, around mypy checking.

@woodsp-ibm woodsp-ibm mentioned this pull request Mar 11, 2025
The list of arguments for `QNSPSA.get_fidelity()` has been updated to
include only the `ansatz` and the `sampler`, and removing previous
wildcard option. From now on, the sampler option must be explicitly
especified, as a positional or keyword argument, and it cannot be `None`.
Copy link
Member

@woodsp-ibm woodsp-ibm Mar 17, 2025

Choose a reason for hiding this comment

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

Suggested change
especified, as a positional or keyword argument, and it cannot be `None`.
specified, as a positional or keyword argument, and it cannot be `None`.

Should fix the Checks spelling failure. In the other file the Checks are failing as the copyright year needs updating but it's too far away from the place that was changed to do a suggestion like I did above

Wrong Copyright Year:'qiskit_algorithms/optimizers/qnspsa.py':  Current:'# (C) Copyright IBM 2021, 2024.' Correct:'# (C) Copyright IBM 2021, 2025.'

I had done another PR which fixed the other CI failures and having merged that and now updated this PR from main most is passing except the above 2 items. The CI failure against Qiskit Main is expected as there are a lot of changes needed to make it Qiskit 2.0 compatible, but those checks are not required to pass unlike the Checks which is required and is failing due to the above couple of items.

Copy link
Member

Choose a reason for hiding this comment

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

As noted above, in the latter part of that comment, the copyright date of that changed file qnspsa.py needs updating otherwise, as you can see from the CI that just ran with the one spelling change applied, its still failing,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh! Sorry about that...

julenl and others added 3 commits March 19, 2025 11:32
…70947f3cd7.yaml


Fixed typo

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Updated the Copyright year
@coveralls
Copy link

coveralls commented Mar 20, 2025

Pull Request Test Coverage Report for Build 14126985232

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 90.454%

Totals Coverage Status
Change from base Build 14080856357: 0.0%
Covered Lines: 6396
Relevant Lines: 7071

💛 - Coveralls

…70947f3cd7.yaml


A more accurate rephrasing of the sentence, avoiding the misleading term "wildcard".

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
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.

Thanks for doing this and making the contribution. LGTM.

@woodsp-ibm woodsp-ibm merged commit ac7f29f into qiskit-community:main Mar 31, 2025
16 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants