Skip to content

Conversation

ikkoham
Copy link
Contributor

@ikkoham ikkoham commented May 16, 2022

Summary

This PR enhances Primitives (Sampler and Estimator) and provide a more flexible interface.
This comes from the discussion #8035.

The major difference is that the previous PR provided a function to extend the interface, but this one does so by default.

Details and comments

As discussed offline, this PR is API breaking but only for internals.
If needed, I will add a DeprecationWarning, please point it out in the review comments.

TODO:

  • add the release note.

@ikkoham ikkoham added Changelog: New Feature Include in the "Added" section of the changelog mod: primitives Related to the Primitives module labels May 16, 2022
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the the following people are requested to review this:

@coveralls
Copy link

coveralls commented May 16, 2022

Pull Request Test Coverage Report for Build 2425813167

  • 100 of 110 (90.91%) changed or added relevant lines in 5 files are covered.
  • 9 unchanged lines in 5 files lost coverage.
  • Overall coverage increased (+0.004%) to 84.395%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/primitives/base_sampler.py 32 36 88.89%
qiskit/primitives/base_estimator.py 50 56 89.29%
Files with Coverage Reduction New Missed Lines %
qiskit/primitives/base_estimator.py 1 88.42%
qiskit/primitives/base_sampler.py 1 88.06%
qiskit/primitives/sampler.py 1 92.11%
qiskit/primitives/estimator.py 2 92.31%
qiskit/primitives/utils.py 4 77.78%
Totals Coverage Status
Change from base Build 2409841148: 0.004%
Covered Lines: 54617
Relevant Lines: 64716

💛 - Coveralls

@HuangJunye
Copy link
Collaborator

I think we should have someone (e.g. @rathishcholarajan ) from the runtime provider team to review to get their perspectives.

@ikkoham
Copy link
Contributor Author

ikkoham commented May 17, 2022

I would like to summarize the difference between meta-programming and explicitly passing a name/id in this implementation.

Meta-programming
[+] No need to be aware of passing names and IDs on the subclass.
[-] Difficult to understand and maintain

Passing names/ids
[+] Simple
[-] Duplicated codes
[-] Need to understand that the subclass implementer needs to pass names and ids explicitly

@ikkoham
Copy link
Contributor Author

ikkoham commented May 18, 2022

I found that by using __new__ (instead of __init_subclass__) we can stop meta-programming and write code more concisely. I think this made it both easier to implement and maintain.

@ikkoham ikkoham marked this pull request as ready for review May 18, 2022 06:18
@ikkoham ikkoham requested review from a team and t-imamichi as code owners May 18, 2022 06:18
ikkoham and others added 2 commits May 19, 2022 23:12
Co-authored-by: Rathish Cholarajan <rathishc24@gmail.com>
@ikkoham
Copy link
Contributor Author

ikkoham commented May 19, 2022

@rathishcholarajan Thank you for your review comments. Just to be clear, this PR changes the abstract method from __call__ to _call due to type difference. Basically, it should work by simply changing the method name as in the reference implementation. Although this is an API breaking point, is it possible to follow this change?

@rathishcholarajan
Copy link
Member

rathishcholarajan commented May 19, 2022

@rathishcholarajan Thank you for your review comments. Just to be clear, this PR changes the abstract method from __call__ to _call due to type difference. Basically, it should work by simply changing the method name as in the reference implementation. Although this is an API breaking point, is it possible to follow this change?

Thanks @ikkoham. I can understand it and have made the changes in this PR Qiskit/qiskit-ibm-runtime#331. But have you considered not forcing this validation/conversion in base class __call__ but leaving it upto subclasses to call super().__call__(). That way if someone is just looking at the qiskit-ibm-runtime implementation they know what to pass to __call__.

@ikkoham
Copy link
Contributor Author

ikkoham commented May 19, 2022

@rathishcholarajan Thank you. Your PR looks good.

Yes, the reason why we can't use super().__call__() here is, first, that the argument types of __call__ and _call are different, and also that the return type of __call__ is also different because the return value of super(). __call__ is unknown at the time of BaseClass.
The key point of this new design is that it explicitly simplifies implementation in subclasses by restricting the type of _call.

Copy link
Member

@rathishcholarajan rathishcholarajan left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks a lot @ikkoham! (I pulled in this code to provider and all tests are passing Qiskit/qiskit-ibm-runtime#331)

(Approving and hoping it won't auto merge without the label and since I am not a code owner here, thereby others get enough time to review)

blakejohnson
blakejohnson previously approved these changes May 27, 2022
Copy link
Contributor

@blakejohnson blakejohnson left a comment

Choose a reason for hiding this comment

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

LGTM.

@ikkoham ikkoham added this to the 0.21 milestone May 28, 2022
Cryoris
Cryoris previously approved these changes May 30, 2022
Copy link
Contributor

@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 I'll keep the automerge label off in case someone else (like @levbishop or @ajavadia?) want to have another look 🙂

Copy link
Collaborator

@HuangJunye HuangJunye left a comment

Choose a reason for hiding this comment

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

I can't comment on the technicality of the implementation but I left some comments about the documentation. One major point I want to ask if whether we should also rename parameter_values to parameters now that we are renaming circuit_indices and observable_indices to circuits and observables. I am not sure why the current implementation has parameter_values as argument in __call__ method but parameters in the __init__ method. I think it would be good to unify among the methods.

Co-authored-by: Junye Huang <h.jun.ye@gmail.com>
Co-authored-by: Rathish Cholarajan <rathishc24@gmail.com>
@ikkoham ikkoham dismissed stale reviews from Cryoris, blakejohnson, and rathishcholarajan via bb0918d June 1, 2022 14:36
Copy link
Collaborator

@HuangJunye HuangJunye left a comment

Choose a reason for hiding this comment

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

Looks great! Thanks for adopting the suggestions.

@Cryoris Cryoris merged commit df54ae2 into Qiskit:main Jun 2, 2022
@ikkoham ikkoham deleted the primitives/enhance-interface branch June 6, 2022 00:31
blakejohnson pushed a commit to blakejohnson/qiskit-ibm-runtime that referenced this pull request May 26, 2023
… observables (Qiskit#165)

* Adapt to base class changes Qiskit/qiskit#8065

* Adapt sampler to base class changes in terra

* Fix black

* Fix lint

* Fix parameter names

* Use Ikko's branch from terra temporarily for testing base class changes

* Fix some failing tests

* Fix couple more tests

* Fix all test failures

* Install rust

* Install rust compiler

* Fix disabling prompt when installing rust

* Run cargo

* Fix parameter name

* Fix black

* Update requirements.txt

* Apply suggestions from code review

Co-authored-by: IKKO HAMAMURA <Ikko.Hamamura1@ibm.com>

* Match _call signature with base classes and remove unnecessary casts

* Try with terra pre pre-release

* Double equals needed

* Fix lint

* qiskit-terra 0.21.0 released, so use that

no longer need to install rust since we are not installing terra from source anymore

qiskit-aer is really a dev dependency for this repo and use the main branch code for now since PR Qiskit/qiskit-aer#1521 is not released yet

* Stop inheriting QiskitTestCase so tests do not fail on deprecation warnings

Co-authored-by: IKKO HAMAMURA <Ikko.Hamamura1@ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog mod: primitives Related to the Primitives module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants