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

Fix a transpile issue of BackendSampler #9236

Merged
merged 6 commits into from
Dec 6, 2022

Conversation

t-imamichi
Copy link
Member

@t-imamichi t-imamichi commented Dec 2, 2022

Summary

BackendSampler raises an error if two runs are called sequentially as follows. This PR fixes the issue.
This PR is straightforward. So, it might be better to skip circuits that are already transpiled.

from qiskit import Aer, QuantumCircuit
from qiskit.primitives import BackendSampler

sampler = BackendSampler(Aer.get_backend('aer_simulator'))

qc1 = QuantumCircuit(1,1)
qc1.h(0)
qc1.measure(0, 0)
qc2 = QuantumCircuit(1,1)
qc2.x(0)
qc2.measure(0, 0)

print(sampler.run([qc1]).result())  # OK
print(sampler.run([qc2]).result())  # NG

output

SamplerResult(quasi_dists=[{1: 0.5078125, 0: 0.4921875}], metadata=[{'shots': 1024}])
Traceback (most recent call last):
  File "/home/ima/github/qiskit/terra/tmp/backendsampler.py", line 14, in <module>
    print(sampler.run([qc2]).result())
  File "/home/ima/github/qiskit/terra/qiskit/primitives/primitive_job.py", line 50, in result
    return self._future.result()
  File "/usr/lib/python3.10/concurrent/futures/_base.py", line 451, in result
    return self.__get_result()
  File "/usr/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
    raise self._exception
  File "/usr/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/ima/github/qiskit/terra/qiskit/primitives/backend_sampler.py", line 149, in _call
    bound_circuits = [
  File "/home/ima/github/qiskit/terra/qiskit/primitives/backend_sampler.py", line 150, in <listcomp>
    transpiled_circuits[i]
IndexError: list index out of range

Details and comments

@t-imamichi t-imamichi requested review from a team and ikkoham as code owners December 2, 2022 16:54
@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:

@t-imamichi t-imamichi added the mod: primitives Related to the Primitives module label Dec 2, 2022
@coveralls
Copy link

coveralls commented Dec 2, 2022

Pull Request Test Coverage Report for Build 3628568741

  • 4 of 4 (100.0%) changed or added relevant lines in 1 file are covered.
  • 4 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.002%) to 84.588%

Files with Coverage Reduction New Missed Lines %
src/sabre_swap/layer.rs 1 98.43%
qiskit/pulse/library/waveform.py 3 91.49%
Totals Coverage Status
Change from base Build 3619413698: -0.002%
Covered Lines: 63093
Relevant Lines: 74589

💛 - Coveralls

ikkoham
ikkoham previously approved these changes Dec 5, 2022
Copy link
Contributor

@ikkoham ikkoham left a comment

Choose a reason for hiding this comment

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

Thank you. Good catch! LGTM

@ikkoham ikkoham added stable backport potential The bug might be minimal and/or import enough to be port to stable Changelog: Bugfix Include in the "Fixed" section of the changelog labels Dec 5, 2022
@ikkoham ikkoham added this to the 0.22.4 milestone Dec 5, 2022
@ikkoham
Copy link
Contributor

ikkoham commented Dec 5, 2022

@t-imamichi Could you write the release note, please?

@t-imamichi
Copy link
Member Author

This PR is straightforward and it transpiles all circuits even if some of them are already transpiled in the past. Do you think we need to optimize the transpilation?

@t-imamichi
Copy link
Member Author

I made an optimized version t-imamichi/qiskit-terra@fix-backend-sampler...t-imamichi:qiskit-terra:fix-bs2
Which do you prefer, @ikkoham?

@ikkoham
Copy link
Contributor

ikkoham commented Dec 5, 2022

I'm fine with both. I optimized the transpiration in Aer.
https://github.com/Qiskit/qiskit-aer/blob/main/qiskit_aer/primitives/sampler.py#L194-L196

But, skip_transpilation may be exposed to public in the future.
#9074
If we introduce this, we need to fix the transpilation code. (Whether to expose or not should be discussed in the refactor PR.)

@t-imamichi
Copy link
Member Author

If AerSampler does the optimization, it seems good to do it for BackendEstimator too. I will merge it #9236 (comment).

Copy link
Contributor

@ikkoham ikkoham left a comment

Choose a reason for hiding this comment

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

LGTM!

@mergify mergify bot merged commit cc28f82 into Qiskit:main Dec 6, 2022
mergify bot pushed a commit that referenced this pull request Dec 6, 2022
* fix an issue of transpile of BackendSampler

* optimize

* add reno

* updated test

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit cc28f82)
mergify bot added a commit that referenced this pull request Dec 7, 2022
* fix an issue of transpile of BackendSampler

* optimize

* add reno

* updated test

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit cc28f82)

Co-authored-by: Takashi Imamichi <31178928+t-imamichi@users.noreply.github.com>
Cryoris pushed a commit to Cryoris/qiskit-terra that referenced this pull request Jan 12, 2023
* fix an issue of transpile of BackendSampler

* optimize

* add reno

* updated test

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
@t-imamichi t-imamichi deleted the fix-backend-sampler branch January 30, 2023 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog mod: primitives Related to the Primitives module 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.

4 participants