-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Insertion noise model #4672
Merged
Merged
Insertion noise model #4672
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
046b884
Remove files for subsequent PRs
95-martin-orion 55069a9
Add coverage for OpId
95-martin-orion 75dc3e8
Clarify if-case
95-martin-orion 4f9fdc0
snake_case_gamma
95-martin-orion 32011ca
Merge branch 'master' into cirq-noisegen-4a
CirqBot 2baaaa7
Add insertion noise model.
95-martin-orion 224ee36
Update to use new match methods
95-martin-orion 3243b3c
Merge branch 'master' into cirq-noisegen-4b
95-martin-orion 4d460c2
Apply review cleanup
95-martin-orion 4a2caeb
Merge branch 'master' into cirq-noisegen-4b
CirqBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Copyright 2021 The Cirq Developers | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import dataclasses | ||
from typing import TYPE_CHECKING, Dict, List, Optional, Sequence | ||
|
||
from cirq import devices, ops | ||
from cirq.devices import noise_utils | ||
|
||
if TYPE_CHECKING: | ||
import cirq | ||
|
||
|
||
@dataclasses.dataclass | ||
class InsertionNoiseModel(devices.NoiseModel): | ||
"""Simple base noise model for inserting operations. | ||
|
||
Operations generated by this model for a given moment are all added into a | ||
single "noise moment", which is added before or after the original moment | ||
based on `prepend`. | ||
|
||
Args: | ||
ops_added: a map of gate types (and optionally, qubits they act on) to | ||
operations that should be added. | ||
prepend: whether to add the new moment before the current one. | ||
require_physical_tag: whether to only apply noise to operations tagged | ||
with PHYSICAL_GATE_TAG. | ||
""" | ||
|
||
ops_added: Dict[noise_utils.OpIdentifier, 'cirq.Operation'] = dataclasses.field( | ||
default_factory=dict | ||
) | ||
prepend: bool = False | ||
require_physical_tag: bool = True | ||
|
||
def noisy_moment( | ||
self, moment: 'cirq.Moment', system_qubits: Sequence['cirq.Qid'] | ||
) -> 'cirq.OP_TREE': | ||
noise_ops: List['cirq.Operation'] = [] | ||
candidate_ops = [ | ||
op | ||
for op in moment | ||
if (not self.require_physical_tag) or noise_utils.PHYSICAL_GATE_TAG in op.tags | ||
] | ||
for op in candidate_ops: | ||
match_id: Optional[noise_utils.OpIdentifier] = None | ||
candidate_ids = [op_id for op_id in self.ops_added if op in op_id] | ||
for op_id in candidate_ids: | ||
if match_id is None or op_id.is_proper_subtype_of(match_id): | ||
match_id = op_id | ||
if match_id is not None: | ||
noise_ops.append(self.ops_added[match_id]) | ||
if not noise_ops: | ||
return [moment] | ||
if self.prepend: | ||
return [ops.Moment(noise_ops), moment] | ||
return [moment, ops.Moment(noise_ops)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# Copyright 2021 The Cirq Developers | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import cirq | ||
from cirq.devices.insertion_noise_model import InsertionNoiseModel | ||
from cirq.devices.noise_utils import ( | ||
PHYSICAL_GATE_TAG, | ||
OpIdentifier, | ||
) | ||
|
||
|
||
def test_insertion_noise(): | ||
q0, q1 = cirq.LineQubit.range(2) | ||
op_id0 = OpIdentifier(cirq.XPowGate, q0) | ||
op_id1 = OpIdentifier(cirq.ZPowGate, q1) | ||
model = InsertionNoiseModel( | ||
{op_id0: cirq.T(q0), op_id1: cirq.H(q1)}, require_physical_tag=False | ||
) | ||
assert not model.prepend | ||
|
||
moment_0 = cirq.Moment(cirq.X(q0), cirq.X(q1)) | ||
assert model.noisy_moment(moment_0, system_qubits=[q0, q1]) == [ | ||
moment_0, | ||
cirq.Moment(cirq.T(q0)), | ||
] | ||
|
||
moment_1 = cirq.Moment(cirq.Z(q0), cirq.Z(q1)) | ||
assert model.noisy_moment(moment_1, system_qubits=[q0, q1]) == [ | ||
moment_1, | ||
cirq.Moment(cirq.H(q1)), | ||
] | ||
|
||
moment_2 = cirq.Moment(cirq.X(q0), cirq.Z(q1)) | ||
assert model.noisy_moment(moment_2, system_qubits=[q0, q1]) == [ | ||
moment_2, | ||
cirq.Moment(cirq.T(q0), cirq.H(q1)), | ||
] | ||
|
||
moment_3 = cirq.Moment(cirq.Z(q0), cirq.X(q1)) | ||
assert model.noisy_moment(moment_3, system_qubits=[q0, q1]) == [moment_3] | ||
|
||
|
||
def test_prepend(): | ||
q0, q1 = cirq.LineQubit.range(2) | ||
op_id0 = OpIdentifier(cirq.XPowGate, q0) | ||
op_id1 = OpIdentifier(cirq.ZPowGate, q1) | ||
model = InsertionNoiseModel( | ||
{op_id0: cirq.T(q0), op_id1: cirq.H(q1)}, prepend=True, require_physical_tag=False | ||
) | ||
|
||
moment_0 = cirq.Moment(cirq.X(q0), cirq.Z(q1)) | ||
assert model.noisy_moment(moment_0, system_qubits=[q0, q1]) == [ | ||
cirq.Moment(cirq.T(q0), cirq.H(q1)), | ||
moment_0, | ||
] | ||
|
||
|
||
def test_require_physical_tag(): | ||
q0, q1 = cirq.LineQubit.range(2) | ||
op_id0 = OpIdentifier(cirq.XPowGate, q0) | ||
op_id1 = OpIdentifier(cirq.ZPowGate, q1) | ||
model = InsertionNoiseModel({op_id0: cirq.T(q0), op_id1: cirq.H(q1)}) | ||
assert model.require_physical_tag | ||
|
||
moment_0 = cirq.Moment(cirq.X(q0).with_tags(PHYSICAL_GATE_TAG), cirq.Z(q1)) | ||
assert model.noisy_moment(moment_0, system_qubits=[q0, q1]) == [ | ||
moment_0, | ||
cirq.Moment(cirq.T(q0)), | ||
] | ||
|
||
|
||
def test_supertype_matching(): | ||
# Demonstrate that the model applies the closest matching type | ||
# if multiple types match a given gate. | ||
q0 = cirq.LineQubit(0) | ||
op_id0 = OpIdentifier(cirq.Gate, q0) | ||
op_id1 = OpIdentifier(cirq.XPowGate, q0) | ||
model = InsertionNoiseModel( | ||
{op_id0: cirq.T(q0), op_id1: cirq.S(q0)}, require_physical_tag=False | ||
) | ||
|
||
moment_0 = cirq.Moment(cirq.Rx(rads=1).on(q0)) | ||
assert model.noisy_moment(moment_0, system_qubits=[q0]) == [ | ||
moment_0, | ||
cirq.Moment(cirq.S(q0)), | ||
] | ||
|
||
moment_1 = cirq.Moment(cirq.Y(q0)) | ||
assert model.noisy_moment(moment_1, system_qubits=[q0]) == [ | ||
moment_1, | ||
cirq.Moment(cirq.T(q0)), | ||
] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nit: Docstring.
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 defines the
noisy_moment
method of the parent class and inherits its docstring. Compare withnoisy_moments
in ConstantQubitNoiseModel.