From 9f472253718fd545cd21cc5d414319e213d984f1 Mon Sep 17 00:00:00 2001 From: ybc1991 Date: Tue, 26 Mar 2024 12:49:00 -0700 Subject: [PATCH 1/3] Add FSimViaModelTag --- cirq-google/cirq_google/__init__.py | 1 + cirq-google/cirq_google/ops/__init__.py | 2 + .../cirq_google/ops/fsim_via_model_tag.py | 44 +++++++++++++++++++ .../ops/fsim_via_model_tag_test.py | 28 ++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 cirq-google/cirq_google/ops/fsim_via_model_tag.py create mode 100644 cirq-google/cirq_google/ops/fsim_via_model_tag_test.py diff --git a/cirq-google/cirq_google/__init__.py b/cirq-google/cirq_google/__init__.py index 0301afbfb20..68cf6fb0a40 100644 --- a/cirq-google/cirq_google/__init__.py +++ b/cirq-google/cirq_google/__init__.py @@ -57,6 +57,7 @@ from cirq_google.ops import ( CalibrationTag, FSimGateFamily, + FSimViaModelTag, InternalGate, PhysicalZTag, SYC, diff --git a/cirq-google/cirq_google/ops/__init__.py b/cirq-google/cirq_google/ops/__init__.py index 4f9f848f209..b52e000c314 100644 --- a/cirq-google/cirq_google/ops/__init__.py +++ b/cirq-google/cirq_google/ops/__init__.py @@ -18,6 +18,8 @@ from cirq_google.ops.fsim_gate_family import FSimGateFamily +from cirq_google.ops.fsim_via_model_tag import FSimViaModelTag + from cirq_google.ops.physical_z_tag import PhysicalZTag from cirq_google.ops.sycamore_gate import SycamoreGate, SYC diff --git a/cirq-google/cirq_google/ops/fsim_via_model_tag.py b/cirq-google/cirq_google/ops/fsim_via_model_tag.py new file mode 100644 index 00000000000..c5ea863dc75 --- /dev/null +++ b/cirq-google/cirq_google/ops/fsim_via_model_tag.py @@ -0,0 +1,44 @@ +# Copyright 2024 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. +"""A class that can be used to denote FSim gate implementation using polynomial model.""" +from typing import Any, Dict + +import cirq + + +class FSimViaModelTag: + """A tag class to denote FSim gate implementation using polynomial model. + + By default, the translation of FSim gate implementation is possible for a certain + angles. For example, when theta=pi/2, phi=0, it translates into the same implementation + as the SWAP gate. If FSimGate is tagged with this class, the translation will become + a coupler gate that with proper coupler strength and coupler length via some polynomial + modelling. Note not all combination of theta and phi in FSim gate are feasible and + you need the calibration for these angle in advance before using them. + """ + + def __str__(self) -> str: + return 'FSimViaModelTag()' + + def __repr__(self) -> str: + return 'cirq_google.FSimViaModelTag()' + + def _json_dict_(self) -> Dict[str, Any]: + return cirq.obj_to_dict_helper(self, []) + + def __eq__(self, other) -> bool: + return isinstance(other, FSimViaModelTag) + + def __hash__(self) -> int: + return 12345 diff --git a/cirq-google/cirq_google/ops/fsim_via_model_tag_test.py b/cirq-google/cirq_google/ops/fsim_via_model_tag_test.py new file mode 100644 index 00000000000..8ac6bdfd361 --- /dev/null +++ b/cirq-google/cirq_google/ops/fsim_via_model_tag_test.py @@ -0,0 +1,28 @@ +# Copyright 2024 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 +import cirq_google + + +def test_equality(): + assert cirq_google.FSimViaModelTag() == cirq_google.FSimViaModelTag() + assert hash(cirq_google.FSimViaModelTag()) == hash(cirq_google.FSimViaModelTag()) + + +def test_syc_str_repr(): + assert str(cirq_google.FSimViaModelTag()) == 'FSimViaModelTag()' + assert repr(cirq_google.FSimViaModelTag()) == 'cirq_google.FSimViaModelTag()' + cirq.testing.assert_equivalent_repr( + cirq_google.FSimViaModelTag(), setup_code=('import cirq\nimport cirq_google\n') + ) From 543d09a3702f0b86cfc91e839ef752276e851d7b Mon Sep 17 00:00:00 2001 From: ybc1991 Date: Tue, 26 Mar 2024 13:13:38 -0700 Subject: [PATCH 2/3] Add json resolver --- cirq-google/cirq_google/json_resolver_cache.py | 1 + cirq-google/cirq_google/json_test_data/FSimViaModelTag.json | 3 +++ cirq-google/cirq_google/json_test_data/FSimViaModelTag.repr | 1 + cirq-google/cirq_google/ops/fsim_via_model_tag.py | 2 +- 4 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 cirq-google/cirq_google/json_test_data/FSimViaModelTag.json create mode 100644 cirq-google/cirq_google/json_test_data/FSimViaModelTag.repr diff --git a/cirq-google/cirq_google/json_resolver_cache.py b/cirq-google/cirq_google/json_resolver_cache.py index 068c43be046..c767a79650d 100644 --- a/cirq-google/cirq_google/json_resolver_cache.py +++ b/cirq-google/cirq_google/json_resolver_cache.py @@ -50,6 +50,7 @@ def _old_xmon(*args, **kwargs): 'GateTabulation': TwoQubitGateTabulation, 'PhysicalZTag': cirq_google.PhysicalZTag, 'FSimGateFamily': cirq_google.FSimGateFamily, + 'FSimViaModelTag': cirq_google.FSimViaModelTag, 'SycamoreTargetGateset': cirq_google.SycamoreTargetGateset, 'cirq.google.BitstringsMeasurement': cirq_google.BitstringsMeasurement, 'cirq.google.QuantumExecutable': cirq_google.QuantumExecutable, diff --git a/cirq-google/cirq_google/json_test_data/FSimViaModelTag.json b/cirq-google/cirq_google/json_test_data/FSimViaModelTag.json new file mode 100644 index 00000000000..34c8def0f2d --- /dev/null +++ b/cirq-google/cirq_google/json_test_data/FSimViaModelTag.json @@ -0,0 +1,3 @@ +{ + "cirq_type": "FSimViaModelTag" +} \ No newline at end of file diff --git a/cirq-google/cirq_google/json_test_data/FSimViaModelTag.repr b/cirq-google/cirq_google/json_test_data/FSimViaModelTag.repr new file mode 100644 index 00000000000..65908348a93 --- /dev/null +++ b/cirq-google/cirq_google/json_test_data/FSimViaModelTag.repr @@ -0,0 +1 @@ +cirq_google.FSimViaModelTag() \ No newline at end of file diff --git a/cirq-google/cirq_google/ops/fsim_via_model_tag.py b/cirq-google/cirq_google/ops/fsim_via_model_tag.py index c5ea863dc75..b9a45eb1eaa 100644 --- a/cirq-google/cirq_google/ops/fsim_via_model_tag.py +++ b/cirq-google/cirq_google/ops/fsim_via_model_tag.py @@ -41,4 +41,4 @@ def __eq__(self, other) -> bool: return isinstance(other, FSimViaModelTag) def __hash__(self) -> int: - return 12345 + return hash("FSimViaModelTag") From d3d659822257ccb9f40e12d7db390e6881389014 Mon Sep 17 00:00:00 2001 From: ybc1991 Date: Thu, 28 Mar 2024 15:19:09 -0700 Subject: [PATCH 3/3] Address the comment --- cirq-google/cirq_google/ops/fsim_via_model_tag.py | 2 +- cirq-google/cirq_google/ops/fsim_via_model_tag_test.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cirq-google/cirq_google/ops/fsim_via_model_tag.py b/cirq-google/cirq_google/ops/fsim_via_model_tag.py index b9a45eb1eaa..b0480f7ec0a 100644 --- a/cirq-google/cirq_google/ops/fsim_via_model_tag.py +++ b/cirq-google/cirq_google/ops/fsim_via_model_tag.py @@ -20,7 +20,7 @@ class FSimViaModelTag: """A tag class to denote FSim gate implementation using polynomial model. - By default, the translation of FSim gate implementation is possible for a certain + Without the tag, the translation of FSim gate implementation is possible only for a certain angles. For example, when theta=pi/2, phi=0, it translates into the same implementation as the SWAP gate. If FSimGate is tagged with this class, the translation will become a coupler gate that with proper coupler strength and coupler length via some polynomial diff --git a/cirq-google/cirq_google/ops/fsim_via_model_tag_test.py b/cirq-google/cirq_google/ops/fsim_via_model_tag_test.py index 8ac6bdfd361..a25076d955e 100644 --- a/cirq-google/cirq_google/ops/fsim_via_model_tag_test.py +++ b/cirq-google/cirq_google/ops/fsim_via_model_tag_test.py @@ -20,7 +20,7 @@ def test_equality(): assert hash(cirq_google.FSimViaModelTag()) == hash(cirq_google.FSimViaModelTag()) -def test_syc_str_repr(): +def test_str_repr(): assert str(cirq_google.FSimViaModelTag()) == 'FSimViaModelTag()' assert repr(cirq_google.FSimViaModelTag()) == 'cirq_google.FSimViaModelTag()' cirq.testing.assert_equivalent_repr(