From d2ed60889ecf8d4ed8f0d03d2ea7fba7d1529e0e Mon Sep 17 00:00:00 2001 From: Justin Pan Date: Tue, 10 Dec 2024 23:54:52 -0800 Subject: [PATCH 1/2] Support inverse gate JSON seralization --- cirq-core/cirq/json_resolver_cache.py | 1 + cirq-core/cirq/ops/raw_types.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/cirq-core/cirq/json_resolver_cache.py b/cirq-core/cirq/json_resolver_cache.py index 0f866e39e54..f391431e584 100644 --- a/cirq-core/cirq/json_resolver_cache.py +++ b/cirq-core/cirq/json_resolver_cache.py @@ -281,4 +281,5 @@ def _symmetricalqidpair(qids): 'sympy.EulerGamma': lambda: sympy.EulerGamma, 'complex': complex, 'datetime.datetime': _datetime, + '_InverseCompositeGate': cirq.inverse, } diff --git a/cirq-core/cirq/ops/raw_types.py b/cirq-core/cirq/ops/raw_types.py index 168c3cd1765..51555cbeb8a 100644 --- a/cirq-core/cirq/ops/raw_types.py +++ b/cirq-core/cirq/ops/raw_types.py @@ -985,6 +985,7 @@ class _InverseCompositeGate(Gate): def __init__(self, original: Gate) -> None: self._original = original + self.val = original def _qid_shape_(self): return protocols.qid_shape(self._original) @@ -1046,6 +1047,9 @@ def __repr__(self) -> str: def __str__(self) -> str: return f'{self._original!s}†' + def _json_dict_(self) -> Dict[str, Any]: + return protocols.obj_to_dict_helper(self, attribute_names=["val"]) + def _validate_qid_shape(val: Any, qubits: Sequence['cirq.Qid']) -> None: """Helper function to validate qubits for gates and operations. From fd7c097633b5e88f4b82e1e42cd4489bda7a2d84 Mon Sep 17 00:00:00 2001 From: Justin Pan Date: Sat, 14 Dec 2024 21:42:36 -0800 Subject: [PATCH 2/2] Add test coverage for inverseCompositeGate --- cirq-core/cirq/ops/gate_operation_test.py | 1 - .../json_test_data/_InverseCompositeGate.json | 10 ++++++++++ .../json_test_data/_InverseCompositeGate.repr | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 cirq-core/cirq/protocols/json_test_data/_InverseCompositeGate.json create mode 100644 cirq-core/cirq/protocols/json_test_data/_InverseCompositeGate.repr diff --git a/cirq-core/cirq/ops/gate_operation_test.py b/cirq-core/cirq/ops/gate_operation_test.py index 46cf3cae1a5..d911c25261b 100644 --- a/cirq-core/cirq/ops/gate_operation_test.py +++ b/cirq-core/cirq/ops/gate_operation_test.py @@ -506,7 +506,6 @@ def all_subclasses(cls): cirq.transformers.measurement_transformers._ConfusionChannel, cirq.transformers.measurement_transformers._ModAdd, cirq.transformers.routing.visualize_routed_circuit._SwapPrintGate, - cirq.ops.raw_types._InverseCompositeGate, cirq.circuits.qasm_output.QasmTwoQubitGate, cirq.ops.MSGate, # Interop gates diff --git a/cirq-core/cirq/protocols/json_test_data/_InverseCompositeGate.json b/cirq-core/cirq/protocols/json_test_data/_InverseCompositeGate.json new file mode 100644 index 00000000000..65160b3d388 --- /dev/null +++ b/cirq-core/cirq/protocols/json_test_data/_InverseCompositeGate.json @@ -0,0 +1,10 @@ +{ + "cirq_type": "_InverseCompositeGate", + "val": { + "cirq_type": "QubitPermutationGate", + "permutation": [ + 0, + 1, + 2 + ]} +} \ No newline at end of file diff --git a/cirq-core/cirq/protocols/json_test_data/_InverseCompositeGate.repr b/cirq-core/cirq/protocols/json_test_data/_InverseCompositeGate.repr new file mode 100644 index 00000000000..cc68af6df87 --- /dev/null +++ b/cirq-core/cirq/protocols/json_test_data/_InverseCompositeGate.repr @@ -0,0 +1 @@ +(cirq.QubitPermutationGate(permutation=(0, 1, 2))**-1)