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

Add run method to primitives #8382

Merged
merged 33 commits into from
Aug 9, 2022
Merged

Add run method to primitives #8382

merged 33 commits into from
Aug 9, 2022

Conversation

ikkoham
Copy link
Contributor

@ikkoham ikkoham commented Jul 20, 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 Jul 25, 2022

Pull Request Test Coverage Report for Build 2821812085

  • 193 of 228 (84.65%) changed or added relevant lines in 6 files are covered.
  • 7 unchanged lines in 5 files lost coverage.
  • Overall coverage decreased (-0.02%) to 84.017%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/primitives/sampler.py 41 42 97.62%
qiskit/providers/job.py 5 7 71.43%
qiskit/primitives/base_estimator.py 56 66 84.85%
qiskit/primitives/base_sampler.py 38 48 79.17%
qiskit/primitives/primitive_job.py 24 36 66.67%
Files with Coverage Reduction New Missed Lines %
qiskit/primitives/estimator.py 1 94.12%
qiskit/primitives/sampler.py 1 93.83%
qiskit/providers/job.py 1 57.14%
qiskit/primitives/base_estimator.py 2 83.94%
qiskit/primitives/base_sampler.py 2 81.0%
Totals Coverage Status
Change from base Build 2820428459: -0.02%
Covered Lines: 56214
Relevant Lines: 66908

💛 - Coveralls

@t-imamichi
Copy link
Member

I'm wondering which is appropriate, concurrent.Future or asyncio.Future. I feel we might be able to simplify the code using async features, but I'm not sure yet.

@ikkoham
Copy link
Contributor Author

ikkoham commented Jul 26, 2022

@t-imamichi If my understanding is correct, asyncio is non blocking but single thread. concurrent.futures supports multi thread (or process) prfgramming. In our case, it is nice to run the computation concurrently. So, I choose the multithreading.

@renier
Copy link
Contributor

renier commented Jul 26, 2022

I'm wondering which is appropriate, concurrent.Future or asyncio.Future. I feel we might be able to simplify the code using async features, but I'm not sure yet.

asyncio.Future would be fine as is, except it requires the user to use await which then requires you to use async functions, which then require using asyncio loops. asyncio is more intrusive to the user's programming model.

@jyu00
Copy link
Contributor

jyu00 commented Jul 26, 2022

@rathishcholarajan suggested somewhere that we should use run() instead of submit() to be consistent with backend.run(). I think that's a good idea if you all agree.

@ikkoham ikkoham changed the title [WIP] Add submit method to primitives [WIP] Add run method to primitives Jul 28, 2022
@ikkoham ikkoham marked this pull request as ready for review August 1, 2022 02:59
@ikkoham ikkoham requested a review from a team as a code owner August 1, 2022 02:59
@ikkoham
Copy link
Contributor Author

ikkoham commented Aug 3, 2022

@rathishcholarajan @jyu00 Thank you for your review. I improved codes according to your comments.

@t-imamichi
Copy link
Member

LGTM. I have just a few minor comments.

jyu00
jyu00 previously approved these changes Aug 4, 2022
Copy link
Contributor

@jyu00 jyu00 left a comment

Choose a reason for hiding this comment

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

1 small comment but otherwise LGTM!

Comment on lines +143 to +149
index = self._circuit_ids.get(id(circuit))
if index is not None:
circuit_indices.append(index)
else:
circuit_indices.append(len(self._circuits))
self._circuit_ids[id(circuit)] = len(self._circuits)
self._circuits.append(circuit)
Copy link
Contributor

Choose a reason for hiding this comment

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

Personally I'd use an OrderedDict that has the circuit ids as the keys and circuits as the values. This allowed a single variable instead of having to have both _circuits and _circuit_ids. AnOrderedDict also allows accessing by index (e.g. od.values()[0] to support the deprecated circuit indexes.

The down side is you'd probably have to refactor the _call method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now dict is ordered, so we can simply use dict. But it takes O(n) to find index.
In the future (after deprecation of __call__), we don't need ids, so I want to refactor after that.

Co-authored-by: Takashi Imamichi <31178928+t-imamichi@users.noreply.github.com>
Copy link
Contributor

@jyu00 jyu00 left a comment

Choose a reason for hiding this comment

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

Some comments after comparing to the ibm provider PR.

Comment on lines 190 to 191
def close(self):
"""Close the session and free resources"""
Copy link
Contributor

Choose a reason for hiding this comment

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

I think close() can be deprecated as well, since there is no longer a session associated with a primitive.

Copy link
Contributor Author

@ikkoham ikkoham Aug 5, 2022

Choose a reason for hiding this comment

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

Yes. __exit__ was deprecated instead.

@@ -184,7 +198,7 @@ def circuits(self) -> tuple[QuantumCircuit, ...]:
Returns:
The quantum circuits to be sampled.
"""
return self._circuits
return tuple(self._circuits)
Copy link
Contributor

Choose a reason for hiding this comment

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

Now that circuits can be added to each run(), is this method intended to return the concatenated list of all circuits?

Copy link
Contributor Author

@ikkoham ikkoham Aug 5, 2022

Choose a reason for hiding this comment

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

Yes. I'm not sure that we need this new interface. Should we deprecate this?

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess wanting to see all circuits you submitted is a plausible use case? We already have to keep the circuits around so keeping this function shouldn't be a big deal.

@ikkoham
Copy link
Contributor Author

ikkoham commented Aug 8, 2022

@jyu00 @t-imamichi Can we merge this PR? or do we have other discussion points?

Copy link
Contributor

@jyu00 jyu00 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 requested a review from t-imamichi August 9, 2022 02:03
Copy link
Member

@t-imamichi t-imamichi 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!

@ikkoham
Copy link
Contributor Author

ikkoham commented Aug 9, 2022

@mergify refresh

@mergify
Copy link
Contributor

mergify bot commented Aug 9, 2022

refresh

✅ Pull request refreshed

@mergify mergify bot merged commit db61292 into Qiskit:main Aug 9, 2022
hhorii added a commit to Qiskit/qiskit-aer that referenced this pull request Aug 22, 2022
Recent releases of Terra and Aer are not synchronized and Terra-0.22 will be released before Aer-0.12 will be released.
Primitives in Terra 0.22 have new feature run method. Qiskit/qiskit#8382.
This PR makes Aer 0.11 compatible with both Terra 0.21 and 0.22.

Co-authored-by: Hiroshi Horii <hhorii@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Deprecation Include in "Deprecated" section of changelog Changelog: New Feature Include in the "Added" section of the changelog mod: primitives Related to the Primitives module priority: high
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants