From 302615438b6ea5846f28e7315b556bce12fa97b1 Mon Sep 17 00:00:00 2001 From: Dave Bacon Date: Thu, 19 May 2022 23:12:26 +0000 Subject: [PATCH 1/3] Add json serialization for PauliInteractionGate --- cirq-core/cirq/json_resolver_cache.py | 1 + cirq-core/cirq/ops/pauli_interaction_gate.py | 25 ++++++++----------- .../json_test_data/PauliInteractionGate.json | 15 +++++++++++ .../json_test_data/PauliInteractionGate.repr | 1 + .../cirq/protocols/json_test_data/spec.py | 1 - 5 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 cirq-core/cirq/protocols/json_test_data/PauliInteractionGate.json create mode 100644 cirq-core/cirq/protocols/json_test_data/PauliInteractionGate.repr diff --git a/cirq-core/cirq/json_resolver_cache.py b/cirq-core/cirq/json_resolver_cache.py index da2f3777e6f..c0ca882caff 100644 --- a/cirq-core/cirq/json_resolver_cache.py +++ b/cirq-core/cirq/json_resolver_cache.py @@ -143,6 +143,7 @@ def _symmetricalqidpair(qids): 'ParamResolver': cirq.ParamResolver, 'ParallelGate': cirq.ParallelGate, 'ParallelGateFamily': cirq.ParallelGateFamily, + 'PauliInteractionGate': cirq.PauliInteractionGate, 'PauliMeasurementGate': cirq.PauliMeasurementGate, 'PauliString': cirq.PauliString, 'PauliStringPhasor': cirq.PauliStringPhasor, diff --git a/cirq-core/cirq/ops/pauli_interaction_gate.py b/cirq-core/cirq/ops/pauli_interaction_gate.py index 871ba98f669..42b9803476e 100644 --- a/cirq-core/cirq/ops/pauli_interaction_gate.py +++ b/cirq-core/cirq/ops/pauli_interaction_gate.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import List, Sequence, Tuple, cast, Dict, TYPE_CHECKING +from typing import List, Sequence, Tuple, Dict, TYPE_CHECKING import numpy as np @@ -24,17 +24,11 @@ if TYPE_CHECKING: import cirq -pauli_eigen_map = cast( - Dict[pauli_gates.Pauli, np.ndarray], - { - pauli_gates.X: (np.array([[0.5, 0.5], [0.5, 0.5]]), np.array([[0.5, -0.5], [-0.5, 0.5]])), - pauli_gates.Y: ( - np.array([[0.5, -0.5j], [0.5j, 0.5]]), - np.array([[0.5, 0.5j], [-0.5j, 0.5]]), - ), - pauli_gates.Z: (np.diag([1, 0]), np.diag([0, 1])), - }, -) +PAULI_EIGEN_MAP: Dict[pauli_gates.Pauli, np.ndarray] = { + pauli_gates.X: (np.array([[0.5, 0.5], [0.5, 0.5]]), np.array([[0.5, -0.5], [-0.5, 0.5]])), + pauli_gates.Y: (np.array([[0.5, -0.5j], [0.5j, 0.5]]), np.array([[0.5, 0.5j], [-0.5j, 0.5]])), + pauli_gates.Z: (np.diag([1, 0]), np.diag([0, 1])), +} @value.value_equality @@ -108,8 +102,8 @@ def _eigen_shifts(self) -> List[float]: def _eigen_components(self) -> List[Tuple[float, np.ndarray]]: comp1 = np.kron( - pauli_eigen_map[self.pauli0][not self.invert0], - pauli_eigen_map[self.pauli1][not self.invert1], + PAULI_EIGEN_MAP[self.pauli0][not self.invert0], + PAULI_EIGEN_MAP[self.pauli1][not self.invert1], ) comp0 = np.eye(4) - comp1 return [(0, comp0), (1, comp1)] @@ -153,6 +147,9 @@ def __repr__(self) -> str: return base return f'({base}**{proper_repr(self._exponent)})' + def _json_dict_(self): + return protocols.obj_to_dict_helper(self, ["pauli0", "invert0", "pauli1", "invert1"]) + PauliInteractionGate.CZ = PauliInteractionGate(pauli_gates.Z, False, pauli_gates.Z, False) PauliInteractionGate.CNOT = PauliInteractionGate(pauli_gates.Z, False, pauli_gates.X, False) diff --git a/cirq-core/cirq/protocols/json_test_data/PauliInteractionGate.json b/cirq-core/cirq/protocols/json_test_data/PauliInteractionGate.json new file mode 100644 index 00000000000..fe2403c39b1 --- /dev/null +++ b/cirq-core/cirq/protocols/json_test_data/PauliInteractionGate.json @@ -0,0 +1,15 @@ +{ + "cirq_type": "PauliInteractionGate", + "pauli0": { + "cirq_type": "_PauliZ", + "exponent": 1.0, + "global_shift": 0.0 + }, + "invert0": false, + "pauli1": { + "cirq_type": "_PauliX", + "exponent": 1.0, + "global_shift": 0.0 + }, + "invert1": true +} \ No newline at end of file diff --git a/cirq-core/cirq/protocols/json_test_data/PauliInteractionGate.repr b/cirq-core/cirq/protocols/json_test_data/PauliInteractionGate.repr new file mode 100644 index 00000000000..ffa62360ddf --- /dev/null +++ b/cirq-core/cirq/protocols/json_test_data/PauliInteractionGate.repr @@ -0,0 +1 @@ +cirq.PauliInteractionGate(cirq.Z, False, cirq.X, True) \ 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 1b8adc82779..54493216049 100644 --- a/cirq-core/cirq/protocols/json_test_data/spec.py +++ b/cirq-core/cirq/protocols/json_test_data/spec.py @@ -45,7 +45,6 @@ 'Linspace', 'ListSweep', 'NeutralAtomDevice', - 'PauliInteractionGate', 'PauliSumCollector', 'PauliSumExponential', 'PauliTransform', From 87ad858692fbde1f4fe51a88cf48b7d9c31c1518 Mon Sep 17 00:00:00 2001 From: Dave Bacon Date: Thu, 19 May 2022 23:19:08 +0000 Subject: [PATCH 2/3] types --- cirq-core/cirq/ops/pauli_interaction_gate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cirq-core/cirq/ops/pauli_interaction_gate.py b/cirq-core/cirq/ops/pauli_interaction_gate.py index 42b9803476e..09049390246 100644 --- a/cirq-core/cirq/ops/pauli_interaction_gate.py +++ b/cirq-core/cirq/ops/pauli_interaction_gate.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import List, Sequence, Tuple, Dict, TYPE_CHECKING +from typing import Any, Dict, List, Sequence, TYPE_CHECKING, Tuple import numpy as np @@ -147,7 +147,7 @@ def __repr__(self) -> str: return base return f'({base}**{proper_repr(self._exponent)})' - def _json_dict_(self): + def _json_dict_(self) -> Dict[str, Any]: return protocols.obj_to_dict_helper(self, ["pauli0", "invert0", "pauli1", "invert1"]) From af38bdcc66b817fc7d4e67b79c507a39ae4e81cb Mon Sep 17 00:00:00 2001 From: Dave Bacon Date: Thu, 19 May 2022 23:28:02 +0000 Subject: [PATCH 3/3] remove from list --- cirq-core/cirq/ops/gate_operation_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cirq-core/cirq/ops/gate_operation_test.py b/cirq-core/cirq/ops/gate_operation_test.py index 3352694c045..0750d9d2bd4 100644 --- a/cirq-core/cirq/ops/gate_operation_test.py +++ b/cirq-core/cirq/ops/gate_operation_test.py @@ -505,7 +505,6 @@ def all_subclasses(cls): cirq.interop.quirk.QuirkArithmeticGate, # No reason given for missing json. # TODO(#5353): Serialize these gates. - cirq.PauliInteractionGate, cirq.ArithmeticGate, }