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

simplify the noise model construction for noisy simulation with FakeBackendV2 #8979

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

cometta
Copy link
Contributor

@cometta cometta commented Oct 21, 2022

Summary

Details and comments

@cometta cometta requested review from a team and jyu00 as code owners October 21, 2022 06:37
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Oct 21, 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:

  • @Qiskit/terra-core

Copy link
Contributor

@itoko itoko 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 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).

Comment on lines 344 to 345
if pulse_job: # pulse job
raise QiskitError("Pulse simulation is currently not supported for V2 fake backends.")
Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

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)
Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

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?

Copy link
Contributor Author

@cometta cometta Nov 4, 2022

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

Copy link
Contributor

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.

Copy link
Contributor

@itoko itoko Nov 7, 2022

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:

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

Copy link
Member

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.
Copy link
Contributor

@itoko itoko Nov 4, 2022

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.

Copy link
Contributor Author

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 ?

Copy link
Contributor

@itoko itoko Nov 7, 2022

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.

Copy link
Contributor Author

@cometta cometta Nov 8, 2022

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:
Copy link
Contributor

@itoko itoko Nov 4, 2022

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)

Copy link
Contributor Author

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

Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Member

@jakelishman jakelishman 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 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
Copy link
Member

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.

Copy link
Contributor Author

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?

Comment on lines 126 to 129
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", category=UserWarning, module="qiskit_aer.noise.device"
)
Copy link
Member

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Member

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.

Copy link
Contributor Author

@cometta cometta Nov 8, 2022

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)
Copy link
Member

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.

Comment on lines 56 to 74
@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")

Copy link
Member

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?

Copy link
Contributor Author

@cometta cometta Nov 8, 2022

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:
Copy link
Member

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.

@cometta cometta changed the title -Add back noisy simulation of Pulse job for FakeBackendV2 simplify the noise model construction for noisy simulation with FakeBackendV2 Nov 8, 2022
@cometta
Copy link
Contributor Author

cometta commented Nov 8, 2022

@itoko @jakelishman are you able to advice on fail on unit-tests when running ci/cd. Is it due to aer ?

@itoko
Copy link
Contributor

itoko commented Nov 10, 2022

@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.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 4080494526

  • 6 of 8 (75.0%) changed or added relevant lines in 1 file are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.0006%) to 85.245%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/providers/fake_provider/fake_backend.py 6 8 75.0%
Files with Coverage Reduction New Missed Lines %
qiskit/providers/fake_provider/fake_backend.py 1 90.63%
Totals Coverage Status
Change from base Build 4077166277: 0.0006%
Covered Lines: 67037
Relevant Lines: 78640

💛 - Coveralls

@cometta
Copy link
Contributor Author

cometta commented Feb 6, 2023

@jakelishman terra 0.23 already released, the ci/cd test still has error. can comment how to proceed?

@t-imamichi
Copy link
Member

I tried this PR locally and it fails at test.python.primitives.test_backend_sampler.TestBackendSampler.test_run_empty_parameter_2.

======================================================================
FAIL: test_run_empty_parameter_2 (test.python.primitives.test_backend_sampler.TestBackendSampler)
test.python.primitives.test_backend_sampler.TestBackendSampler.test_run_empty_parameter_2
----------------------------------------------------------------------
testtools.testresult.real._StringException: Traceback (most recent call last):
  File "/Users/ima/envs/dev310/lib/python3.10/site-packages/ddt.py", line 220, in wrapper
    return func(self, *args, **kwargs)
  File "/Users/ima/tasks/3_2023/qiskit/terra/test/python/primitives/test_backend_sampler.py", line 238, in test_run_empty_parameter
    self.assertDictAlmostEqual(quasi_dist, {0: 1.0}, delta=0.1)
  File "/Users/ima/tasks/3_2023/qiskit/terra/qiskit/test/base.py", line 146, in assertDictAlmostEqual
    raise self.failureException(msg)
AssertionError: (0: 0.899 != 1.0) within 0.1 delta

The backend noise model seems different from one of the main branch. Do you have any idea, @itoko?

@jakelishman
Copy link
Member

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.

@t-imamichi
Copy link
Member

The quasi_dist value of this PR (in line 230 of test_backend_sampler.py) is

{3: 0.001, 6: 0.001, 4: 0.033, 1: 0.017, 0: 0.893, 2: 0.025, 12: 0.003, 8: 0.027}.

That of main branch is

{4: 0.01, 1: 0.011, 8: 0.018, 2: 0.013, 0: 0.948}.

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.

@jakelishman
Copy link
Member

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?

@t-imamichi
Copy link
Member

The number of shots is 1000 and no mitigation is applied in the test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community PR PRs from contributors that are not 'members' of the Qiskit repo
Projects
Status: Waiting for maintainer response
6 participants