From f25d6cb9dfe659476b756487aca0b1e83491d85d Mon Sep 17 00:00:00 2001 From: Cheng Xing Date: Thu, 16 Jun 2022 00:13:01 +0000 Subject: [PATCH 1/2] GateFamily: do not serialize gates_to_accept and gates_to_ignore when empty --- cirq-core/cirq/ops/gateset.py | 9 +++-- .../protocols/json_test_data/GateFamily.json | 12 ++---- .../protocols/json_test_data/Gateset.json | 16 ++------ .../json_test_data/GridDeviceMetadata.json | 40 +++++-------------- .../cirq.google.GridDevice.json | 40 +++++-------------- 5 files changed, 33 insertions(+), 84 deletions(-) diff --git a/cirq-core/cirq/ops/gateset.py b/cirq-core/cirq/ops/gateset.py index d3e442490c7..4760727fd1a 100644 --- a/cirq-core/cirq/ops/gateset.py +++ b/cirq-core/cirq/ops/gateset.py @@ -269,14 +269,17 @@ def _value_equality_values_(self) -> Any: ) def _json_dict_(self) -> Dict[str, Any]: - return { + d: Dict[str, Any] = { 'gate': self._gate_json(), 'name': self.name, 'description': self.description, 'ignore_global_phase': self._ignore_global_phase, - 'tags_to_accept': list(self._tags_to_accept), - 'tags_to_ignore': list(self._tags_to_ignore), } + if self._tags_to_accept: + d['tags_to_accept'] = list(self._tags_to_accept) + if self._tags_to_ignore: + d['tags_to_ignore'] = list(self._tags_to_ignore) + return d @classmethod def _from_json_dict_( diff --git a/cirq-core/cirq/protocols/json_test_data/GateFamily.json b/cirq-core/cirq/protocols/json_test_data/GateFamily.json index 01b96fe241a..9062f9637f7 100644 --- a/cirq-core/cirq/protocols/json_test_data/GateFamily.json +++ b/cirq-core/cirq/protocols/json_test_data/GateFamily.json @@ -4,9 +4,7 @@ "gate": "XPowGate", "name": "Type GateFamily: cirq.ops.common_gates.XPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.XPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "GateFamily", @@ -17,9 +15,7 @@ }, "name": "XFamily", "description": "Just the X gate.", - "ignore_global_phase": false, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": false }, { "cirq_type": "GateFamily", @@ -29,8 +25,7 @@ "ignore_global_phase": true, "tags_to_accept": [ "physical_z" - ], - "tags_to_ignore": [] + ] }, { "cirq_type": "GateFamily", @@ -38,7 +33,6 @@ "name": "Type GateFamily: cirq.ops.common_gates.ZPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.ZPowGate)`\nIgnored tags: ['physical_z']", "ignore_global_phase": true, - "tags_to_accept": [], "tags_to_ignore": [ "physical_z" ] diff --git a/cirq-core/cirq/protocols/json_test_data/Gateset.json b/cirq-core/cirq/protocols/json_test_data/Gateset.json index 51ffe208475..b6c4dbfd6c5 100644 --- a/cirq-core/cirq/protocols/json_test_data/Gateset.json +++ b/cirq-core/cirq/protocols/json_test_data/Gateset.json @@ -7,9 +7,7 @@ "gate": "YPowGate", "name": "Type GateFamily: cirq.ops.common_gates.YPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.YPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "AnyUnitaryGateFamily", @@ -24,9 +22,7 @@ }, "name": "Instance GateFamily: X", "description": "Accepts `cirq.Gate` instances `g` s.t. `g == X`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true } ], "name": null, @@ -44,18 +40,14 @@ }, "name": "Instance GateFamily: X", "description": "Accepts `cirq.Gate` instances `g` s.t. `g == X`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "GateFamily", "gate": "YPowGate", "name": "Type GateFamily: cirq.ops.common_gates.YPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.YPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "AnyUnitaryGateFamily", diff --git a/cirq-core/cirq/protocols/json_test_data/GridDeviceMetadata.json b/cirq-core/cirq/protocols/json_test_data/GridDeviceMetadata.json index 3c717223b64..0843b9744d1 100644 --- a/cirq-core/cirq/protocols/json_test_data/GridDeviceMetadata.json +++ b/cirq-core/cirq/protocols/json_test_data/GridDeviceMetadata.json @@ -94,27 +94,21 @@ "gate": "XPowGate", "name": "Type GateFamily: cirq.ops.common_gates.XPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.XPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "GateFamily", "gate": "YPowGate", "name": "Type GateFamily: cirq.ops.common_gates.YPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.YPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "GateFamily", "gate": "ZPowGate", "name": "Type GateFamily: cirq.ops.common_gates.ZPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.ZPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "GateFamily", @@ -125,9 +119,7 @@ }, "name": "Instance GateFamily: CZ", "description": "Accepts `cirq.Gate` instances `g` s.t. `g == CZ`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "GateFamily", @@ -138,9 +130,7 @@ }, "name": "Instance GateFamily: ISWAP**0.5", "description": "Accepts `cirq.Gate` instances `g` s.t. `g == ISWAP**0.5`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true } ], "name": null, @@ -157,9 +147,7 @@ }, "name": "Instance GateFamily: ISWAP**0.5", "description": "Accepts `cirq.Gate` instances `g` s.t. `g == ISWAP**0.5`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "Duration", @@ -176,9 +164,7 @@ }, "name": "Instance GateFamily: CZ", "description": "Accepts `cirq.Gate` instances `g` s.t. `g == CZ`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "Duration", @@ -191,9 +177,7 @@ "gate": "XPowGate", "name": "Type GateFamily: cirq.ops.common_gates.XPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.XPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "Duration", @@ -206,9 +190,7 @@ "gate": "YPowGate", "name": "Type GateFamily: cirq.ops.common_gates.YPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.YPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "Duration", @@ -221,9 +203,7 @@ "gate": "ZPowGate", "name": "Type GateFamily: cirq.ops.common_gates.ZPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.ZPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "Duration", diff --git a/cirq-google/cirq_google/json_test_data/cirq.google.GridDevice.json b/cirq-google/cirq_google/json_test_data/cirq.google.GridDevice.json index 65747eab1e6..b4497a67584 100644 --- a/cirq-google/cirq_google/json_test_data/cirq.google.GridDevice.json +++ b/cirq-google/cirq_google/json_test_data/cirq.google.GridDevice.json @@ -96,27 +96,21 @@ "gate": "XPowGate", "name": "Type GateFamily: cirq.ops.common_gates.XPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.XPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "GateFamily", "gate": "YPowGate", "name": "Type GateFamily: cirq.ops.common_gates.YPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.YPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "GateFamily", "gate": "ZPowGate", "name": "Type GateFamily: cirq.ops.common_gates.ZPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.ZPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "GateFamily", @@ -127,9 +121,7 @@ }, "name": "Instance GateFamily: CZ", "description": "Accepts `cirq.Gate` instances `g` s.t. `g == CZ`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "GateFamily", @@ -140,9 +132,7 @@ }, "name": "Instance GateFamily: ISWAP**0.5", "description": "Accepts `cirq.Gate` instances `g` s.t. `g == ISWAP**0.5`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true } ], "name": null, @@ -159,9 +149,7 @@ }, "name": "Instance GateFamily: ISWAP**0.5", "description": "Accepts `cirq.Gate` instances `g` s.t. `g == ISWAP**0.5`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "Duration", @@ -178,9 +166,7 @@ }, "name": "Instance GateFamily: CZ", "description": "Accepts `cirq.Gate` instances `g` s.t. `g == CZ`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "Duration", @@ -193,9 +179,7 @@ "gate": "XPowGate", "name": "Type GateFamily: cirq.ops.common_gates.XPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.XPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "Duration", @@ -208,9 +192,7 @@ "gate": "YPowGate", "name": "Type GateFamily: cirq.ops.common_gates.YPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.YPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "Duration", @@ -223,9 +205,7 @@ "gate": "ZPowGate", "name": "Type GateFamily: cirq.ops.common_gates.ZPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.ZPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "Duration", From e131cc5901987e0a755820c265e7d2e48bbcffad Mon Sep 17 00:00:00 2001 From: Cheng Xing Date: Fri, 24 Jun 2022 21:16:16 +0000 Subject: [PATCH 2/2] Fix CZTargetGateset and SqrtIswapTargetGateset json serialization --- .../protocols/json_test_data/CZTargetGateset.json | 12 +++--------- .../json_test_data/SqrtIswapTargetGateset.json | 12 +++--------- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/cirq-core/cirq/protocols/json_test_data/CZTargetGateset.json b/cirq-core/cirq/protocols/json_test_data/CZTargetGateset.json index 23f3615b41d..25da3ca3890 100644 --- a/cirq-core/cirq/protocols/json_test_data/CZTargetGateset.json +++ b/cirq-core/cirq/protocols/json_test_data/CZTargetGateset.json @@ -23,27 +23,21 @@ }, "name": "Instance GateFamily: ISWAP**0.5", "description": "Accepts `cirq.Gate` instances `g` s.t. `g == ISWAP**0.5`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "GateFamily", "gate": "XPowGate", "name": "Type GateFamily: cirq.ops.common_gates.XPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.XPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "GateFamily", "gate": "ZPowGate", "name": "Type GateFamily: cirq.ops.common_gates.ZPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.ZPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true } ] } diff --git a/cirq-core/cirq/protocols/json_test_data/SqrtIswapTargetGateset.json b/cirq-core/cirq/protocols/json_test_data/SqrtIswapTargetGateset.json index 31b16b6b82c..f1ef24fc252 100644 --- a/cirq-core/cirq/protocols/json_test_data/SqrtIswapTargetGateset.json +++ b/cirq-core/cirq/protocols/json_test_data/SqrtIswapTargetGateset.json @@ -32,27 +32,21 @@ }, "name": "Instance GateFamily: CZ", "description": "Accepts `cirq.Gate` instances `g` s.t. `g == CZ`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "GateFamily", "gate": "XPowGate", "name": "Type GateFamily: cirq.ops.common_gates.XPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.XPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true }, { "cirq_type": "GateFamily", "gate": "ZPowGate", "name": "Type GateFamily: cirq.ops.common_gates.ZPowGate", "description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.ZPowGate)`", - "ignore_global_phase": true, - "tags_to_accept": [], - "tags_to_ignore": [] + "ignore_global_phase": true } ] }