Skip to content

Commit 432d57a

Browse files
authored
Add serialization support for InsertionNoiseModel (#6282)
1 parent cf005c2 commit 432d57a

File tree

5 files changed

+124
-1
lines changed

5 files changed

+124
-1
lines changed

cirq-core/cirq/devices/insertion_noise_model.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
import dataclasses
16-
from typing import TYPE_CHECKING, Dict, List, Optional, Sequence
16+
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence
1717

1818
from cirq import devices
1919
from cirq.devices import noise_utils
@@ -74,3 +74,23 @@ def noisy_moment(
7474
if self.prepend:
7575
return [*noise_steps.moments, moment]
7676
return [moment, *noise_steps.moments]
77+
78+
def __repr__(self) -> str:
79+
return (
80+
f'cirq.devices.InsertionNoiseModel(ops_added={self.ops_added},'
81+
+ f' prepend={self.prepend},'
82+
+ f' require_physical_tag={self.require_physical_tag})'
83+
)
84+
85+
def _json_dict_(self) -> Dict[str, Any]:
86+
return {
87+
'ops_added': list(self.ops_added.items()),
88+
'prepend': self.prepend,
89+
'require_physical_tag': self.require_physical_tag,
90+
}
91+
92+
@classmethod
93+
def _from_json_dict_(cls, ops_added, prepend, require_physical_tag, **kwargs):
94+
return cls(
95+
ops_added=dict(ops_added), prepend=prepend, require_physical_tag=require_physical_tag
96+
)

cirq-core/cirq/devices/insertion_noise_model_test.py

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ def test_insertion_noise():
4747
moment_3 = cirq.Moment(cirq.Z(q0), cirq.X(q1))
4848
assert model.noisy_moment(moment_3, system_qubits=[q0, q1]) == [moment_3]
4949

50+
cirq.testing.assert_equivalent_repr(model)
51+
5052

5153
def test_colliding_noise_qubits():
5254
# Check that noise affecting other qubits doesn't cause issues.
@@ -61,6 +63,8 @@ def test_colliding_noise_qubits():
6163
cirq.Moment(cirq.CNOT(q1, q2)),
6264
]
6365

66+
cirq.testing.assert_equivalent_repr(model)
67+
6468

6569
def test_prepend():
6670
q0, q1 = cirq.LineQubit.range(2)
@@ -106,3 +110,5 @@ def test_supertype_matching():
106110

107111
moment_1 = cirq.Moment(cirq.Y(q0))
108112
assert model.noisy_moment(moment_1, system_qubits=[q0]) == [moment_1, cirq.Moment(cirq.T(q0))]
113+
114+
cirq.testing.assert_equivalent_repr(model)

cirq-core/cirq/json_resolver_cache.py

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def _class_resolver_dictionary() -> Dict[str, ObjectFactory]:
4747
import pandas as pd
4848
import numpy as np
4949
from cirq.devices.noise_model import _NoNoiseModel
50+
from cirq.devices import InsertionNoiseModel
5051
from cirq.experiments import GridInteractionLayer
5152
from cirq.experiments.grid_parallel_two_qubit_xeb import GridParallelXEBMetadata
5253

@@ -147,6 +148,7 @@ def _symmetricalqidpair(qids):
147148
'ISwapPowGate': cirq.ISwapPowGate,
148149
'IdentityGate': cirq.IdentityGate,
149150
'InitObsSetting': cirq.work.InitObsSetting,
151+
'InsertionNoiseModel': InsertionNoiseModel,
150152
'KeyCondition': cirq.KeyCondition,
151153
'KrausChannel': cirq.KrausChannel,
152154
'LinearDict': cirq.LinearDict,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
[
2+
{
3+
"cirq_type": "InsertionNoiseModel",
4+
"ops_added": [
5+
[
6+
{
7+
"cirq_type": "OpIdentifier",
8+
"gate_type": "XPowGate",
9+
"qubits": [
10+
{
11+
"cirq_type": "LineQubit",
12+
"x": 0
13+
}
14+
]
15+
},
16+
{
17+
"cirq_type": "GateOperation",
18+
"gate": {
19+
"cirq_type": "BitFlipChannel",
20+
"p": 0.2
21+
},
22+
"qubits": [
23+
{
24+
"cirq_type": "LineQubit",
25+
"x": 0
26+
}
27+
]
28+
}
29+
]
30+
],
31+
"prepend": false,
32+
"require_physical_tag": false
33+
},
34+
{
35+
"cirq_type": "InsertionNoiseModel",
36+
"ops_added": [
37+
[
38+
{
39+
"cirq_type": "OpIdentifier",
40+
"gate_type": "XPowGate",
41+
"qubits": [
42+
{
43+
"cirq_type": "LineQubit",
44+
"x": 0
45+
}
46+
]
47+
},
48+
{
49+
"cirq_type": "GateOperation",
50+
"gate": {
51+
"cirq_type": "BitFlipChannel",
52+
"p": 0.2
53+
},
54+
"qubits": [
55+
{
56+
"cirq_type": "LineQubit",
57+
"x": 0
58+
}
59+
]
60+
}
61+
],
62+
[
63+
{
64+
"cirq_type": "OpIdentifier",
65+
"gate_type": "HPowGate",
66+
"qubits": [
67+
{
68+
"cirq_type": "LineQubit",
69+
"x": 1
70+
}
71+
]
72+
},
73+
{
74+
"cirq_type": "GateOperation",
75+
"gate": {
76+
"cirq_type": "BitFlipChannel",
77+
"p": 0.1
78+
},
79+
"qubits": [
80+
{
81+
"cirq_type": "LineQubit",
82+
"x": 1
83+
}
84+
]
85+
}
86+
]
87+
],
88+
"prepend": false,
89+
"require_physical_tag": false
90+
}
91+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
cirq.devices.InsertionNoiseModel(ops_added={cirq.devices.noise_utils.OpIdentifier(cirq.ops.common_gates.XPowGate, cirq.LineQubit(0)): cirq.bit_flip(p=0.2).on(cirq.LineQubit(0))}, prepend=False, require_physical_tag=False),
3+
cirq.devices.InsertionNoiseModel(ops_added={cirq.devices.noise_utils.OpIdentifier(cirq.ops.common_gates.XPowGate, cirq.LineQubit(0)): cirq.bit_flip(p=0.2).on(cirq.LineQubit(0)), cirq.devices.noise_utils.OpIdentifier(cirq.ops.common_gates.HPowGate, cirq.LineQubit(1)): cirq.bit_flip(p=0.1).on(cirq.LineQubit(1))}, prepend=False, require_physical_tag=False)
4+
]

0 commit comments

Comments
 (0)