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 support for BackendV2 in NoiseModel.from_backend #1484

Merged
merged 23 commits into from
Jul 12, 2022

Conversation

itoko
Copy link
Contributor

@itoko itoko commented Mar 16, 2022

Summary

Fixes #1477 by adding support for BackendV2 in NoiseModel.from_backend.

Details and comments

Updates basic_device_readout_errors and basic_device_gate_errors functions in noise.device.models module so that they can take a Target instead of a BackendProperties.

Not supporting standard_gates and warnings options for BackendV2 because those options are (planned to be) deprecated.

Requiring terra#7736 as a prerequisite.

@itoko itoko force-pushed the fix-1477-backendv2 branch from 72b9651 to 6294344 Compare April 21, 2022 01:21
@itoko itoko force-pushed the fix-1477-backendv2 branch from 395c5eb to a198212 Compare April 21, 2022 01:49
@itoko itoko marked this pull request as ready for review April 22, 2022 00:39
@itoko itoko changed the title [WIP] Add support for BackendV2 in NoiseModel.from_backend Add support for BackendV2 in NoiseModel.from_backend Apr 22, 2022
@hhorii hhorii requested a review from mtreinish April 26, 2022 06:43
Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

Overall LGTM, some small inline comments. The only other thing is that we need to bump the minimum qiskit-terra version to 0.20.0 since some of the QubitProperties usage was only added in 0.20.0


logger = logging.getLogger(__name__)


def basic_device_readout_errors(properties):
def basic_device_readout_errors(properties, target=None):
Copy link
Member

Choose a reason for hiding this comment

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

Should we make properties optional here too? Right now you'll have to call this like basic_device_readout_errors(None, target=target) to use this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Done in 9a6ac73

qiskit/providers/aer/noise/device/models.py Outdated Show resolved Hide resolved
@@ -84,18 +102,43 @@ def basic_device_gate_errors(properties,
standard_gates (bool): DEPRECATED, If true return errors as standard
qobj gates. If false return as unitary
qobj instructions (Default: None).
warnings (bool): Display warnings (Default: True).
warnings (bool): PLAN TO BE DEPRECATED, Display warnings (Default: True).
Copy link
Member

Choose a reason for hiding this comment

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

We should raise a PendingDeprecationWarning if this is set. (Maybe change the default to None and condition the warning on it not being None.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in 30c3930

qiskit/providers/aer/noise/device/models.py Outdated Show resolved Hide resolved
qiskit/providers/aer/noise/device/models.py Outdated Show resolved Hide resolved
qiskit/providers/aer/noise/noise_model.py Outdated Show resolved Hide resolved
target = copy.deepcopy(target)
for op_name, qubits, value in gate_lengths:
prop = target[op_name][qubits]
prop.duration = value * _NANOSECOND_UNITS[gate_length_units]
Copy link
Member

Choose a reason for hiding this comment

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

Does _NANOSECOND_UNITS convert to ns or to seconds? The units for InstructionProperties.duration should be in seconds.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! Fixed at bad25ec

prop = target[op_name][qubits]
prop.duration = value * _NANOSECOND_UNITS[gate_length_units]
target.update_instruction_properties(op_name, qubits, prop)
all_qubit_properties = backend.target.qubit_properties
Copy link
Member

Choose a reason for hiding this comment

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

You can use the backend.qubit_properties() method as a backup here if needed since the qubit_properties target attribute only was added in 0.20.0, so it's possible that a BackendV2 instance from 0.19.x is missing the target implementation but has it on the backend. This pretty unlikely though so it's not required.

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've just bumped the minimum terra version to 0.20.0 in a46948b.

Comment on lines 236 to 246
def test_noise_model_from_backend_v2(self):
circ = QuantumCircuit(2)
circ.x(0)
circ.x(1)
circ.measure_all()

backend = mock.FakeBackendV2()
noise_model = NoiseModel.from_backend(backend)
circ = transpile(circ, backend, optimization_level=0)
result = AerSimulator().run(circ, noise_model=noise_model).result()
self.assertTrue(result.success)
Copy link
Member

Choose a reason for hiding this comment

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

Do you want to add some tests that use fake devices from terra like FakeLagosV2 or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion. I added it in 192a7ab and found a bug, so fixed in 5e88501

@itoko
Copy link
Contributor Author

itoko commented Jun 17, 2022

@mtreinish Thank you for your review. I've updated the code accordingly. Could you take a second look?

Copy link
Member

@mtreinish mtreinish 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 making the updates so quickly. Just an inline question and a suggested improvement to the warning message. Otherwise I think this is good to go.

else:
# create from Target
for q in range(target.num_qubits):
prop = target['measure'][(q,)]
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure we want to do this with a get item here without checking that measure is defined in the target. I think in practical terms almost every target will have this defined but it's not guaranteed.

qiskit/providers/aer/noise/device/models.py Outdated Show resolved Hide resolved
qiskit/providers/aer/noise/noise_model.py Outdated Show resolved Hide resolved
@itoko
Copy link
Contributor Author

itoko commented Jun 28, 2022

@mtreinish I've got an error during tox -edocs. It seems to come from warnings, treated as error by "-W" option in the sphinx-build command. How do you think we should fix this error?

@mtreinish
Copy link
Member

It's caused by the recent sphinx release, #1548 should have fixed it

@itoko itoko requested a review from mtreinish July 11, 2022 00:30
Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

LGTM, sorry for the slow review. Thanks for updating things

@mtreinish mtreinish added Changelog: New Feature Include in the Added section of the changelog automerge This PR will automatically merge once its CI has passed labels Jul 12, 2022
@mergify mergify bot merged commit c53acd3 into Qiskit:main Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge This PR will automatically merge once its CI has passed Changelog: New Feature Include in the Added section of the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for BackendV2 in NoiseModel.from_backend method
3 participants