Skip to content

Commit dadfdcb

Browse files
prakharb10tanujkhattarpavoljuhas
authored
Added MSGate to top level (#6466)
* add `MSGate` to top level * mark `MSGate` as `not_yet_serializable` * Revert "mark `MSGate` as `not_yet_serializable`" This reverts commit ffdb3de. * Test json serialization of cirq.MSGate without custom resolver Not yet passing. * Use the "cirq" namespace to JSON-serialize `cirq.MSGate` The namespace-less cirq_type `MSGate` is used by `cirq_ionq.MSGate`. --------- Co-authored-by: Tanuj Khattar <[email protected]> Co-authored-by: Pavol Juhas <[email protected]>
1 parent 0f4822b commit dadfdcb

File tree

7 files changed

+15
-10
lines changed

7 files changed

+15
-10
lines changed

cirq-core/cirq/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@
240240
MatrixGate,
241241
MixedUnitaryChannel,
242242
M,
243+
MSGate,
243244
measure,
244245
measure_each,
245246
measure_paulistring_terms,

cirq-core/cirq/json_resolver_cache.py

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ def _symmetricalqidpair(qids):
157157
'LineTopology': cirq.LineTopology,
158158
'Linspace': cirq.Linspace,
159159
'ListSweep': cirq.ListSweep,
160+
'cirq.MSGate': cirq.MSGate,
160161
'MatrixGate': cirq.MatrixGate,
161162
'MixedUnitaryChannel': cirq.MixedUnitaryChannel,
162163
'MeasurementKey': cirq.MeasurementKey,

cirq-core/cirq/ops/parity_gates.py

+5
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,11 @@ def __repr__(self) -> str:
399399
return 'cirq.ms(np.pi/2)'
400400
return f'cirq.ms({self._exponent!r}*np.pi/2)'
401401

402+
# the default namespace is already occupied by cirq_ionq.MSGate
403+
@classmethod
404+
def _json_namespace_(cls) -> str:
405+
return 'cirq'
406+
402407
def _json_dict_(self) -> Dict[str, Any]:
403408
return protocols.obj_to_dict_helper(self, ["rads"])
404409

cirq-core/cirq/ops/parity_gates_test.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def test_trace_distance():
258258
def test_ms_arguments():
259259
eq_tester = cirq.testing.EqualsTester()
260260
eq_tester.add_equality_group(
261-
cirq.ms(np.pi / 2), cirq.ops.MSGate(rads=np.pi / 2), cirq.XXPowGate(global_shift=-0.5)
261+
cirq.ms(np.pi / 2), cirq.MSGate(rads=np.pi / 2), cirq.XXPowGate(global_shift=-0.5)
262262
)
263263
eq_tester.add_equality_group(
264264
cirq.ms(np.pi / 4), cirq.XXPowGate(exponent=0.5, global_shift=-0.5)
@@ -323,15 +323,7 @@ def test_ms_diagrams():
323323

324324

325325
def test_json_serialization():
326-
def custom_resolver(cirq_type: str):
327-
if cirq_type == "MSGate":
328-
return cirq.ops.MSGate
329-
return None
330-
331-
assert cirq.read_json(
332-
json_text=cirq.to_json(cirq.ms(np.pi / 2)), resolvers=[custom_resolver]
333-
) == cirq.ms(np.pi / 2)
334-
assert custom_resolver('X') is None
326+
assert cirq.read_json(json_text=cirq.to_json(cirq.ms(np.pi / 2))) == cirq.ms(np.pi / 2)
335327

336328

337329
@pytest.mark.parametrize('gate_cls', (cirq.XXPowGate, cirq.YYPowGate, cirq.ZZPowGate))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"cirq_type": "cirq.MSGate",
3+
"rads": 1.5707963267948966
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cirq.ms(np.pi/2)

cirq-core/cirq/protocols/json_test_data/spec.py

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
name="cirq",
2323
packages=[cirq, cirq.work],
2424
test_data_path=pathlib.Path(__file__).parent,
25+
custom_class_name_to_cirq_type={"MSGate": "cirq.MSGate"},
2526
resolver_cache=_class_resolver_dictionary(),
2627
not_yet_serializable=[
2728
'Alignment',

0 commit comments

Comments
 (0)