From c15366469390048c1fe0e56d026f8309ce656165 Mon Sep 17 00:00:00 2001 From: MichaelBroughton Date: Wed, 16 Mar 2022 20:50:07 -0700 Subject: [PATCH] Serialize QubitPermutationGate. (#5092) Step towards fixing #5090 --- cirq-core/cirq/json_resolver_cache.py | 1 + cirq-core/cirq/ops/permutation_gate.py | 4 ++++ .../json_test_data/QubitPermutationGate.json | 12 ++++++++++++ .../json_test_data/QubitPermutationGate.repr | 1 + cirq-core/cirq/protocols/json_test_data/spec.py | 1 - 5 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 cirq-core/cirq/protocols/json_test_data/QubitPermutationGate.json create mode 100644 cirq-core/cirq/protocols/json_test_data/QubitPermutationGate.repr diff --git a/cirq-core/cirq/json_resolver_cache.py b/cirq-core/cirq/json_resolver_cache.py index f15252c3256..ca7d2065e26 100644 --- a/cirq-core/cirq/json_resolver_cache.py +++ b/cirq-core/cirq/json_resolver_cache.py @@ -145,6 +145,7 @@ def _parallel_gate_op(gate, qubits): 'QasmUGate': cirq.circuits.qasm_output.QasmUGate, '_QubitAsQid': raw_types._QubitAsQid, 'QuantumFourierTransformGate': cirq.QuantumFourierTransformGate, + 'QubitPermutationGate': cirq.QubitPermutationGate, 'RandomGateChannel': cirq.RandomGateChannel, 'TensoredConfusionMatrices': cirq.TensoredConfusionMatrices, 'RepetitionsStoppingCriteria': cirq.work.RepetitionsStoppingCriteria, diff --git a/cirq-core/cirq/ops/permutation_gate.py b/cirq-core/cirq/ops/permutation_gate.py index e3fb9bfff02..188e82f4da2 100644 --- a/cirq-core/cirq/ops/permutation_gate.py +++ b/cirq-core/cirq/ops/permutation_gate.py @@ -96,3 +96,7 @@ def __repr__(self) -> str: def _json_dict_(self) -> Dict[str, Any]: return protocols.obj_to_dict_helper(self, attribute_names=['permutation']) + + @classmethod + def _from_json_dict_(cls, permutation: Sequence[int], **kwargs): + return cls(permutation) diff --git a/cirq-core/cirq/protocols/json_test_data/QubitPermutationGate.json b/cirq-core/cirq/protocols/json_test_data/QubitPermutationGate.json new file mode 100644 index 00000000000..1b84ad232c1 --- /dev/null +++ b/cirq-core/cirq/protocols/json_test_data/QubitPermutationGate.json @@ -0,0 +1,12 @@ +{ + "cirq_type": "QubitPermutationGate", + "permutation": [ + 0, + 5, + 2, + 1, + 6, + 3, + 4 + ] +} diff --git a/cirq-core/cirq/protocols/json_test_data/QubitPermutationGate.repr b/cirq-core/cirq/protocols/json_test_data/QubitPermutationGate.repr new file mode 100644 index 00000000000..f31702b5713 --- /dev/null +++ b/cirq-core/cirq/protocols/json_test_data/QubitPermutationGate.repr @@ -0,0 +1 @@ +cirq.QubitPermutationGate([0, 5, 2, 1, 6, 3, 4]) diff --git a/cirq-core/cirq/protocols/json_test_data/spec.py b/cirq-core/cirq/protocols/json_test_data/spec.py index f23a193d5ac..57374985c77 100644 --- a/cirq-core/cirq/protocols/json_test_data/spec.py +++ b/cirq-core/cirq/protocols/json_test_data/spec.py @@ -59,7 +59,6 @@ 'QasmOutput', 'QuantumState', 'QubitOrder', - 'QubitPermutationGate', 'QuilFormatter', 'QuilOutput', 'SimulationTrialResult',