From 638ed831dc7c4a1d0be0fd7af8a29e7b7a28c62a Mon Sep 17 00:00:00 2001 From: Prakhar Bhatnagar Date: Wed, 14 Feb 2024 19:57:38 -0800 Subject: [PATCH 1/5] add `MSGate` to top level --- cirq-core/cirq/__init__.py | 1 + cirq-core/cirq/json_resolver_cache.py | 1 + cirq-core/cirq/ops/parity_gates_test.py | 4 ++-- cirq-core/cirq/protocols/json_test_data/MSGate.json | 4 ++++ cirq-core/cirq/protocols/json_test_data/MSGate.repr | 1 + 5 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 cirq-core/cirq/protocols/json_test_data/MSGate.json create mode 100644 cirq-core/cirq/protocols/json_test_data/MSGate.repr diff --git a/cirq-core/cirq/__init__.py b/cirq-core/cirq/__init__.py index 22dcdc8768f..47718f2820b 100644 --- a/cirq-core/cirq/__init__.py +++ b/cirq-core/cirq/__init__.py @@ -240,6 +240,7 @@ MatrixGate, MixedUnitaryChannel, M, + MSGate, measure, measure_each, measure_paulistring_terms, diff --git a/cirq-core/cirq/json_resolver_cache.py b/cirq-core/cirq/json_resolver_cache.py index f1d178bb530..8b559a9f100 100644 --- a/cirq-core/cirq/json_resolver_cache.py +++ b/cirq-core/cirq/json_resolver_cache.py @@ -157,6 +157,7 @@ def _symmetricalqidpair(qids): 'LineTopology': cirq.LineTopology, 'Linspace': cirq.Linspace, 'ListSweep': cirq.ListSweep, + 'MSGate': cirq.MSGate, 'MatrixGate': cirq.MatrixGate, 'MixedUnitaryChannel': cirq.MixedUnitaryChannel, 'MeasurementKey': cirq.MeasurementKey, diff --git a/cirq-core/cirq/ops/parity_gates_test.py b/cirq-core/cirq/ops/parity_gates_test.py index 384483ff22a..a63bed42322 100644 --- a/cirq-core/cirq/ops/parity_gates_test.py +++ b/cirq-core/cirq/ops/parity_gates_test.py @@ -258,7 +258,7 @@ def test_trace_distance(): def test_ms_arguments(): eq_tester = cirq.testing.EqualsTester() eq_tester.add_equality_group( - cirq.ms(np.pi / 2), cirq.ops.MSGate(rads=np.pi / 2), cirq.XXPowGate(global_shift=-0.5) + cirq.ms(np.pi / 2), cirq.MSGate(rads=np.pi / 2), cirq.XXPowGate(global_shift=-0.5) ) eq_tester.add_equality_group( cirq.ms(np.pi / 4), cirq.XXPowGate(exponent=0.5, global_shift=-0.5) @@ -325,7 +325,7 @@ def test_ms_diagrams(): def test_json_serialization(): def custom_resolver(cirq_type: str): if cirq_type == "MSGate": - return cirq.ops.MSGate + return cirq.MSGate return None assert cirq.read_json( diff --git a/cirq-core/cirq/protocols/json_test_data/MSGate.json b/cirq-core/cirq/protocols/json_test_data/MSGate.json new file mode 100644 index 00000000000..6a8ff8d3e4e --- /dev/null +++ b/cirq-core/cirq/protocols/json_test_data/MSGate.json @@ -0,0 +1,4 @@ +{ + "cirq_type": "MSGate", + "rads": 1.5707963267948966 +} \ No newline at end of file diff --git a/cirq-core/cirq/protocols/json_test_data/MSGate.repr b/cirq-core/cirq/protocols/json_test_data/MSGate.repr new file mode 100644 index 00000000000..863776f2811 --- /dev/null +++ b/cirq-core/cirq/protocols/json_test_data/MSGate.repr @@ -0,0 +1 @@ +cirq.ms(np.pi/2) \ No newline at end of file From ffdb3de306ad2e8e2b87fa6c7b9c51d46e8bdae9 Mon Sep 17 00:00:00 2001 From: Prakhar Bhatnagar Date: Wed, 28 Feb 2024 14:50:53 -0800 Subject: [PATCH 2/5] mark `MSGate` as `not_yet_serializable` --- cirq-core/cirq/json_resolver_cache.py | 1 - cirq-core/cirq/protocols/json_test_data/MSGate.json | 4 ---- cirq-core/cirq/protocols/json_test_data/MSGate.repr | 1 - cirq-core/cirq/protocols/json_test_data/spec.py | 1 + 4 files changed, 1 insertion(+), 6 deletions(-) delete mode 100644 cirq-core/cirq/protocols/json_test_data/MSGate.json delete mode 100644 cirq-core/cirq/protocols/json_test_data/MSGate.repr diff --git a/cirq-core/cirq/json_resolver_cache.py b/cirq-core/cirq/json_resolver_cache.py index 8b559a9f100..f1d178bb530 100644 --- a/cirq-core/cirq/json_resolver_cache.py +++ b/cirq-core/cirq/json_resolver_cache.py @@ -157,7 +157,6 @@ def _symmetricalqidpair(qids): 'LineTopology': cirq.LineTopology, 'Linspace': cirq.Linspace, 'ListSweep': cirq.ListSweep, - 'MSGate': cirq.MSGate, 'MatrixGate': cirq.MatrixGate, 'MixedUnitaryChannel': cirq.MixedUnitaryChannel, 'MeasurementKey': cirq.MeasurementKey, diff --git a/cirq-core/cirq/protocols/json_test_data/MSGate.json b/cirq-core/cirq/protocols/json_test_data/MSGate.json deleted file mode 100644 index 6a8ff8d3e4e..00000000000 --- a/cirq-core/cirq/protocols/json_test_data/MSGate.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "cirq_type": "MSGate", - "rads": 1.5707963267948966 -} \ No newline at end of file diff --git a/cirq-core/cirq/protocols/json_test_data/MSGate.repr b/cirq-core/cirq/protocols/json_test_data/MSGate.repr deleted file mode 100644 index 863776f2811..00000000000 --- a/cirq-core/cirq/protocols/json_test_data/MSGate.repr +++ /dev/null @@ -1 +0,0 @@ -cirq.ms(np.pi/2) \ No newline at end of file diff --git a/cirq-core/cirq/protocols/json_test_data/spec.py b/cirq-core/cirq/protocols/json_test_data/spec.py index 5b32c638086..acfbc83888d 100644 --- a/cirq-core/cirq/protocols/json_test_data/spec.py +++ b/cirq-core/cirq/protocols/json_test_data/spec.py @@ -39,6 +39,7 @@ 'KakDecomposition', 'LinearCombinationOfGates', 'LinearCombinationOfOperations', + 'MSGate', # namespace conflict with `cirq_ionq.ionq_native_gates.MSGate` 'PauliSumCollector', 'PauliSumExponential', 'PeriodicValue', From dde24b6c0fbbf5cbdad7e17f36699cbbe277172f Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Fri, 15 Mar 2024 17:03:29 -0700 Subject: [PATCH 3/5] Revert "mark `MSGate` as `not_yet_serializable`" This reverts commit ffdb3de306ad2e8e2b87fa6c7b9c51d46e8bdae9. --- cirq-core/cirq/json_resolver_cache.py | 1 + cirq-core/cirq/protocols/json_test_data/MSGate.json | 4 ++++ cirq-core/cirq/protocols/json_test_data/MSGate.repr | 1 + cirq-core/cirq/protocols/json_test_data/spec.py | 1 - 4 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 cirq-core/cirq/protocols/json_test_data/MSGate.json create mode 100644 cirq-core/cirq/protocols/json_test_data/MSGate.repr diff --git a/cirq-core/cirq/json_resolver_cache.py b/cirq-core/cirq/json_resolver_cache.py index f1d178bb530..8b559a9f100 100644 --- a/cirq-core/cirq/json_resolver_cache.py +++ b/cirq-core/cirq/json_resolver_cache.py @@ -157,6 +157,7 @@ def _symmetricalqidpair(qids): 'LineTopology': cirq.LineTopology, 'Linspace': cirq.Linspace, 'ListSweep': cirq.ListSweep, + 'MSGate': cirq.MSGate, 'MatrixGate': cirq.MatrixGate, 'MixedUnitaryChannel': cirq.MixedUnitaryChannel, 'MeasurementKey': cirq.MeasurementKey, diff --git a/cirq-core/cirq/protocols/json_test_data/MSGate.json b/cirq-core/cirq/protocols/json_test_data/MSGate.json new file mode 100644 index 00000000000..6a8ff8d3e4e --- /dev/null +++ b/cirq-core/cirq/protocols/json_test_data/MSGate.json @@ -0,0 +1,4 @@ +{ + "cirq_type": "MSGate", + "rads": 1.5707963267948966 +} \ No newline at end of file diff --git a/cirq-core/cirq/protocols/json_test_data/MSGate.repr b/cirq-core/cirq/protocols/json_test_data/MSGate.repr new file mode 100644 index 00000000000..863776f2811 --- /dev/null +++ b/cirq-core/cirq/protocols/json_test_data/MSGate.repr @@ -0,0 +1 @@ +cirq.ms(np.pi/2) \ No newline at end of file diff --git a/cirq-core/cirq/protocols/json_test_data/spec.py b/cirq-core/cirq/protocols/json_test_data/spec.py index acfbc83888d..5b32c638086 100644 --- a/cirq-core/cirq/protocols/json_test_data/spec.py +++ b/cirq-core/cirq/protocols/json_test_data/spec.py @@ -39,7 +39,6 @@ 'KakDecomposition', 'LinearCombinationOfGates', 'LinearCombinationOfOperations', - 'MSGate', # namespace conflict with `cirq_ionq.ionq_native_gates.MSGate` 'PauliSumCollector', 'PauliSumExponential', 'PeriodicValue', From 90043bbf88a489ced1282de64c5fa51e3ec4a631 Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Fri, 15 Mar 2024 17:37:52 -0700 Subject: [PATCH 4/5] Test json serialization of cirq.MSGate without custom resolver Not yet passing. --- cirq-core/cirq/ops/parity_gates_test.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/cirq-core/cirq/ops/parity_gates_test.py b/cirq-core/cirq/ops/parity_gates_test.py index a63bed42322..5194fbe3822 100644 --- a/cirq-core/cirq/ops/parity_gates_test.py +++ b/cirq-core/cirq/ops/parity_gates_test.py @@ -323,15 +323,7 @@ def test_ms_diagrams(): def test_json_serialization(): - def custom_resolver(cirq_type: str): - if cirq_type == "MSGate": - return cirq.MSGate - return None - - assert cirq.read_json( - json_text=cirq.to_json(cirq.ms(np.pi / 2)), resolvers=[custom_resolver] - ) == cirq.ms(np.pi / 2) - assert custom_resolver('X') is None + assert cirq.read_json(json_text=cirq.to_json(cirq.ms(np.pi / 2))) == cirq.ms(np.pi / 2) @pytest.mark.parametrize('gate_cls', (cirq.XXPowGate, cirq.YYPowGate, cirq.ZZPowGate)) From f95947861c9a3bb59ba08ece800cb1ff1d669e50 Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Fri, 15 Mar 2024 17:43:12 -0700 Subject: [PATCH 5/5] Use the "cirq" namespace to JSON-serialize `cirq.MSGate` The namespace-less cirq_type `MSGate` is used by `cirq_ionq.MSGate`. --- cirq-core/cirq/json_resolver_cache.py | 2 +- cirq-core/cirq/ops/parity_gates.py | 5 +++++ cirq-core/cirq/protocols/json_test_data/MSGate.json | 4 ---- cirq-core/cirq/protocols/json_test_data/cirq.MSGate.json | 4 ++++ .../json_test_data/{MSGate.repr => cirq.MSGate.repr} | 0 cirq-core/cirq/protocols/json_test_data/spec.py | 1 + 6 files changed, 11 insertions(+), 5 deletions(-) delete mode 100644 cirq-core/cirq/protocols/json_test_data/MSGate.json create mode 100644 cirq-core/cirq/protocols/json_test_data/cirq.MSGate.json rename cirq-core/cirq/protocols/json_test_data/{MSGate.repr => cirq.MSGate.repr} (100%) diff --git a/cirq-core/cirq/json_resolver_cache.py b/cirq-core/cirq/json_resolver_cache.py index 8b559a9f100..4880046618a 100644 --- a/cirq-core/cirq/json_resolver_cache.py +++ b/cirq-core/cirq/json_resolver_cache.py @@ -157,7 +157,7 @@ def _symmetricalqidpair(qids): 'LineTopology': cirq.LineTopology, 'Linspace': cirq.Linspace, 'ListSweep': cirq.ListSweep, - 'MSGate': cirq.MSGate, + 'cirq.MSGate': cirq.MSGate, 'MatrixGate': cirq.MatrixGate, 'MixedUnitaryChannel': cirq.MixedUnitaryChannel, 'MeasurementKey': cirq.MeasurementKey, diff --git a/cirq-core/cirq/ops/parity_gates.py b/cirq-core/cirq/ops/parity_gates.py index 52f9a79a374..4522a0a04e0 100644 --- a/cirq-core/cirq/ops/parity_gates.py +++ b/cirq-core/cirq/ops/parity_gates.py @@ -399,6 +399,11 @@ def __repr__(self) -> str: return 'cirq.ms(np.pi/2)' return f'cirq.ms({self._exponent!r}*np.pi/2)' + # the default namespace is already occupied by cirq_ionq.MSGate + @classmethod + def _json_namespace_(cls) -> str: + return 'cirq' + def _json_dict_(self) -> Dict[str, Any]: return protocols.obj_to_dict_helper(self, ["rads"]) diff --git a/cirq-core/cirq/protocols/json_test_data/MSGate.json b/cirq-core/cirq/protocols/json_test_data/MSGate.json deleted file mode 100644 index 6a8ff8d3e4e..00000000000 --- a/cirq-core/cirq/protocols/json_test_data/MSGate.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "cirq_type": "MSGate", - "rads": 1.5707963267948966 -} \ No newline at end of file diff --git a/cirq-core/cirq/protocols/json_test_data/cirq.MSGate.json b/cirq-core/cirq/protocols/json_test_data/cirq.MSGate.json new file mode 100644 index 00000000000..bd4c0debee2 --- /dev/null +++ b/cirq-core/cirq/protocols/json_test_data/cirq.MSGate.json @@ -0,0 +1,4 @@ +{ + "cirq_type": "cirq.MSGate", + "rads": 1.5707963267948966 +} diff --git a/cirq-core/cirq/protocols/json_test_data/MSGate.repr b/cirq-core/cirq/protocols/json_test_data/cirq.MSGate.repr similarity index 100% rename from cirq-core/cirq/protocols/json_test_data/MSGate.repr rename to cirq-core/cirq/protocols/json_test_data/cirq.MSGate.repr diff --git a/cirq-core/cirq/protocols/json_test_data/spec.py b/cirq-core/cirq/protocols/json_test_data/spec.py index 5b32c638086..05bebc2c80e 100644 --- a/cirq-core/cirq/protocols/json_test_data/spec.py +++ b/cirq-core/cirq/protocols/json_test_data/spec.py @@ -22,6 +22,7 @@ name="cirq", packages=[cirq, cirq.work], test_data_path=pathlib.Path(__file__).parent, + custom_class_name_to_cirq_type={"MSGate": "cirq.MSGate"}, resolver_cache=_class_resolver_dictionary(), not_yet_serializable=[ 'Alignment',