-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
simplify the noise model construction for noisy simulation with FakeBackendV2 #8979
base: main
Are you sure you want to change the base?
Conversation
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:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for working on this. I suggest to change the scope of the PR since Aer's pulse simulator does not support BackendV2. And we need to care about the users of qiskit_aer<0.11.0 since BackendV2 support of Aer's NoiseModel is added in 0.11.0 (very recent).
if pulse_job: # pulse job | ||
raise QiskitError("Pulse simulation is currently not supported for V2 fake backends.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to keep these lines. To run pulse job, you need to do the same things as in FakeBackend(BackendV1).run. However, PulseSystemModel has not yet support BackendV2 so you cannot to have it. I suggest to change the scope of this PR and focus on just replacing the _get_noise_model_from_backend_v2
with from_backend
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @itoko thank you, i have done the changes based on your advice, no more error in ci/cd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@HuangJunye can you review the code as well. let me know if further changes ?
@@ -193,8 +193,8 @@ def test_run_2qubit(self, backend): | |||
|
|||
self.assertDictAlmostEqual(result.quasi_dists[0], {0: 1}, 0.1) | |||
self.assertDictAlmostEqual(result.quasi_dists[1], {1: 1}, 0.1) | |||
self.assertDictAlmostEqual(result.quasi_dists[2], {2: 1}, 0.1) | |||
self.assertDictAlmostEqual(result.quasi_dists[3], {3: 1}, 0.1) | |||
self.assertDictAlmostEqual(result.quasi_dists[2], {2: 1}, 0.2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need to change delta values?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@t-imamichi , i have to put 0.2, otherwise, ci/cd has error. Can you advice if this is not needed? @itoko, what's your thoughts on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change implies that this PR amplifies the error of BackendSampler
with a noise model.
Does this PR set a different noise parameter values?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this PR is to removed the function that generate fake noise _get_noise_model_from_backend_v2
and use real noise noise_model = NoiseModel.from_backend(self)
from Aer. That's why the noise has different value. @itoko your comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strange.. I'll take a deeper look at where the increase of error comes from early next week.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've looked into the above unexpected behavior and found two issues:
- A bug in Aer where excessive errors (other than
ReadoutError
) are added tomeasure
instructions only when the noises are generated fromTarget
object (Edit: -> Fix device noise model from BackendV2 not to have excessive quantum errors on measures/resets qiskit-aer#1649) - An issue in
convert_to_target
in Terra, that drops asymmetric readout error properties (prob_meas0_prep1
andprob_meas1_prep0
) defined inprops_dict
since they are not defined inInstructionProperties
. We may need to accept the drops or introduce a newMeasureInstructionProperties
to store them additionally. (Edit: -> Support asymmetric readout errors in FakeBackendV2 #9097)
I'll fix the first bug as soon as possible (hopefully the fix should be included in aer 0.11.2).
As for the second issue, I'll create an issue to discuss how to address it.
Sorry, please wait for them, @cometta
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking into this, Itoko-san.
@cometta: This is why we have tests - when stuff starts failing unexpectedly, we need to figure out why it's failing, not modify the tests to mask it. Tests are more than just things that need to be made to pass, they're also a tool for us to help find errors in our code.
features: | ||
- | | ||
Reverting to the previous behavior. | ||
Add back noisy simulation of Pulse job for FakeBackendV2. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line should be removed now. Also please update the title and top message of this PR as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@itoko i can remove the release note yaml file. Can advice on what is the suitable title for this PR ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't need to remove the release note, rather we need to update the release note to tell aer >= 0.11.2 (previously, no version restriction for Aer) is now required for users who want noisy simulation with FakeBackendV2 (maybe in others
or upgrades
section).
Regarding title, I would say Simplify the noise model construction for noisy simulation with FakeBackendV2
or something but you could think of a better one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i reverted back release note with extra note you mentioned on versioning. I also updated this PR title according to your recommendation
@@ -119,10 +119,15 @@ def _parse_channels(self, channels): | |||
def _setup_sim(self): | |||
if _optionals.HAS_AER: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only aer > 0.11.0 supports BackendV2 so we may need to check the version of Aer here. And we may need to tell users to fall back to BasicAer in that case by changing the following warning message in run
method.
if not _optionals.HAS_AER:
warnings.warn("Aer not found using BasicAer and no noise", RuntimeWarning)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@itoko i implemented the version checking feature that you suggested and give warning if ae < 0.11.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks but I'm not sure the updated code works well with aer < 0.11.0. Have you tested it with aer < 0.11.0? I guess the same version check may be necessary in _setup_sim
.
@HuangJunye With this PR, we are changing to require aer >= 0.11.2 for FackBackendV2 users who want to do noisy simulation. I personally don't think it's too restrictive so I'm fine with it but I'd like to hear what others think about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a new feature and Aer is just an optional accelerator/extender here, so it will be ok if it can't be made to work with Aer < 0.11 - this PR won't go out until at least Terra 0.23 anyway. However, it might not hurt to leave in the old function for a release, and continue to use it if Aer 0.10 (or whatever the minimum supported version is) is present.
With the inline version parsing here, I'm considering adding versioning methods to the lazy import checkers, which should simplify some of this. I've made #9086 to track that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jakelishman can the versioning methods to the lazy import checkers
with helper functions consider as a separate ticket? For this ticket just hardcode checking of versioning? I rebase the code as terra just released 0.23 . Let me know what else can i do so that this ticket can be close asap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, they would be a separate ticket (I made #9086 to track it, and I'll add the feature when I have time). Terra 0.23.0 hasn't been released yet - it's due in January. We released Terra 0.22.3 yesterday, which might have been the confusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i pushed the code to run for Terra 0.23.0 still has error in ci/cd . Can comment @jakelishman
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking at this. I have a couple of concerns about things that are being done in the PR, but in general it'll be good for us to rely on Aer's native support again.
|
||
if _optionals.HAS_AER: | ||
from pkg_resources import parse_version | ||
from qiskit_aer import version as aer_version |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This cannot succeed if Aer
is <0.11, because it didn't publish itself with this name until Aer 0.11.0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jakelishman is the preferable way to get version is using qiskit.__qiskit_version__
to check for aer version?
with warnings.catch_warnings(): | ||
warnings.filterwarnings( | ||
"ignore", category=UserWarning, module="qiskit_aer.noise.device" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we suppressing warnings? The warnings are something we should look to fix, not hide - either Aer is spuriously warning, or on the Terra side we need to first modify our data so that it doesn't cause warnings; it's the caller here that has the most information to decide how to modify the data to avoid the warnings. I suspect this is related to Aer issues about spuriously warning, in which case we should not put in the filter, and let Aer fix them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. This is a consequence of a bug in Aer and I've just fixed it in Qiskit/qiskit-aer#1639 (merged yesterday). These lines should be removed. Sorry @cometta for not clearly telling this suppression is a temporary solution that should be tested in your local environment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i removed above lines
if not _optionals.HAS_AER: | ||
|
||
if _optionals.HAS_AER: | ||
from pkg_resources import parse_version |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Terra doesn't have a direct requirement on pkg_resources
(and this module is effectively deprecated anyway), so we can't use this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jakelishman should i wait for the helper function #9086 to check versioning or use other python function to check for the version for now? pkg_resources
comes together with python installation. there is no need to install additional python library
@@ -193,8 +193,8 @@ def test_run_2qubit(self, backend): | |||
|
|||
self.assertDictAlmostEqual(result.quasi_dists[0], {0: 1}, 0.1) | |||
self.assertDictAlmostEqual(result.quasi_dists[1], {1: 1}, 0.1) | |||
self.assertDictAlmostEqual(result.quasi_dists[2], {2: 1}, 0.1) | |||
self.assertDictAlmostEqual(result.quasi_dists[3], {3: 1}, 0.1) | |||
self.assertDictAlmostEqual(result.quasi_dists[2], {2: 1}, 0.2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking into this, Itoko-san.
@cometta: This is why we have tests - when stuff starts failing unexpectedly, we need to figure out why it's failing, not modify the tests to mask it. Tests are more than just things that need to be made to pass, they're also a tool for us to help find errors in our code.
@combine( | ||
backend=[be for be in FAKE_PROVIDER_FOR_BACKEND_V2.backends() if be.num_qubits > 1], | ||
optimization_level=[0, 1, 2, 3], | ||
) | ||
def test_circuit_on_fake_backend_v2(self, backend, optimization_level): | ||
if not optionals.HAS_AER and backend.num_qubits > 20: | ||
self.skipTest("Unable to run fake_backend %s without qiskit-aer" % backend.backend_name) | ||
job = execute( | ||
self.circuit, | ||
backend, | ||
optimization_level=optimization_level, | ||
seed_simulator=42, | ||
seed_transpiler=42, | ||
) | ||
result = job.result() | ||
counts = result.get_counts() | ||
max_count = max(counts.items(), key=operator.itemgetter(1))[0] | ||
self.assertEqual(max_count, "11") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we removing this test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i reverted it.
@@ -119,10 +119,15 @@ def _parse_channels(self, channels): | |||
def _setup_sim(self): | |||
if _optionals.HAS_AER: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a new feature and Aer is just an optional accelerator/extender here, so it will be ok if it can't be made to work with Aer < 0.11 - this PR won't go out until at least Terra 0.23 anyway. However, it might not hurt to leave in the old function for a release, and continue to use it if Aer 0.10 (or whatever the minimum supported version is) is present.
With the inline version parsing here, I'm considering adding versioning methods to the lazy import checkers, which should simplify some of this. I've made #9086 to track that.
@itoko @jakelishman are you able to advice on fail on unit-tests when running ci/cd. Is it due to aer ? |
@cometta It seems you are getting two sets of test failures: One is for docs and the other is due to Aer. I guess the first one might be due to some invalid strings in release note. As for the second one, it is expected failures, let's wait for Aer's bug-fix. |
Pull Request Test Coverage Report for Build 4080494526
💛 - Coveralls |
@jakelishman terra 0.23 already released, the ci/cd test still has error. can comment how to proceed? |
I tried this PR locally and it fails at
The backend noise model seems different from one of the main branch. Do you have any idea, @itoko? |
Yeah, that looks like a true test failure caused by this change, but it's sufficiently close that it's maybe that the test is too strict. Ideally we should do the maths and work out what the approximate margin of error is for that test with a given noise model, and either increase its number of shots (within reason) or relax the tightness of the test a bit. |
The
That of main branch is
The circuit is as follows. n = 5
qc = QuantumCircuit(n, n - 1)
qc.measure(range(n - 1), range(n - 1)) The difference looks larger than sampling error. I'm not sure what causes it. |
We can't make that call without knowing the size of the measurement error in each branch (since this PR looks at noise models), the number of shots, and any error mitigation that's being done, right? |
The number of shots is 1000 and no mitigation is applied in the test. |
Summary
FakeBackendV2
after Aer adds support for BackendV2 #7831FakeBackendV2.run
method after Aer adds support for BackendV2 #7829Details and comments