|
| 1 | +# Copyright 2022 The Cirq Developers |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Any, Dict, List, Sequence, Type, Union |
| 16 | +import cirq |
| 17 | + |
| 18 | + |
| 19 | +class GoogleCZTargetGateset(cirq.CZTargetGateset): |
| 20 | + """`cirq.CZTargetGateset` implementation tailored to Google devices. |
| 21 | +
|
| 22 | + In addition to features available from `cirq.CZTargetGateset`, `GoogleCZTargetGateset` contains |
| 23 | + a flag, `eject_paulis`, to enable the postprocess transformers `cirq.eject_phased_paulis` and |
| 24 | + `cirq.eject_z`, which will push X, Y, Z, PhasedX, and certain PhasedXZ gates to the end of the |
| 25 | + circuit. |
| 26 | + """ |
| 27 | + |
| 28 | + def __init__( |
| 29 | + self, |
| 30 | + atol: float = 1e-8, |
| 31 | + eject_paulis: bool = False, |
| 32 | + additional_gates: Sequence[Union[Type[cirq.Gate], cirq.Gate, cirq.GateFamily]] = (), |
| 33 | + ): |
| 34 | + """Initializes GoogleCZTargetGateset. |
| 35 | +
|
| 36 | + Args: |
| 37 | + atol: A limit on the amount of absolute error introduced by the transformation. |
| 38 | + eject_paulis: Whether to enable postprocess transformers `cirq.eject_z` and |
| 39 | + `cirq.eject_phased_paulis`. If enabled, these transformers will remove tags (e.g. |
| 40 | + `cirq_google.PhysicalZTag`) from single-qubit Pauli operations. Defaults to False. |
| 41 | + additional_gates: Sequence of additional gates / gate families which should also |
| 42 | + be "accepted" by this gateset. This is empty by default. |
| 43 | + """ |
| 44 | + super().__init__(atol=atol, allow_partial_czs=False, additional_gates=additional_gates) |
| 45 | + self.eject_paulis = eject_paulis |
| 46 | + self._additional_gates_repr_str = ", ".join( |
| 47 | + [cirq.ops.gateset._gate_str(g, repr) for g in additional_gates] |
| 48 | + ) |
| 49 | + |
| 50 | + @property |
| 51 | + def postprocess_transformers(self) -> List[cirq.TRANSFORMER]: |
| 52 | + """List of transformers which should be run after decomposing individual operations. |
| 53 | +
|
| 54 | + If `eject_paulis` is enabled in the constructor, adds `cirq.eject_phased_paulis` and |
| 55 | + `cirq.eject_z` in addition to postprocess_transformers already available in |
| 56 | + `cirq.CompilationTargetGateset`. |
| 57 | + """ |
| 58 | + transformers: List[cirq.TRANSFORMER] = [ |
| 59 | + cirq.create_transformer_with_kwargs( |
| 60 | + cirq.merge_single_qubit_moments_to_phxz, atol=self.atol |
| 61 | + ), |
| 62 | + cirq.create_transformer_with_kwargs(cirq.drop_negligible_operations, atol=self.atol), |
| 63 | + cirq.drop_empty_moments, |
| 64 | + ] |
| 65 | + |
| 66 | + if self.eject_paulis: |
| 67 | + return ( |
| 68 | + transformers[:1] |
| 69 | + + [ |
| 70 | + cirq.create_transformer_with_kwargs(cirq.eject_phased_paulis, atol=self.atol), |
| 71 | + cirq.create_transformer_with_kwargs(cirq.eject_z, atol=self.atol), |
| 72 | + ] |
| 73 | + + transformers[1:] |
| 74 | + ) |
| 75 | + return transformers |
| 76 | + |
| 77 | + def __repr__(self) -> str: |
| 78 | + return ( |
| 79 | + 'cirq_google.GoogleCZTargetGateset(' |
| 80 | + f'atol={self.atol}, ' |
| 81 | + f'eject_paulis={self.eject_paulis}, ' |
| 82 | + f'additional_gates=[{self._additional_gates_repr_str}]' |
| 83 | + ')' |
| 84 | + ) |
| 85 | + |
| 86 | + def _value_equality_values_(self) -> Any: |
| 87 | + return self.atol, self.eject_paulis, frozenset(self.additional_gates) |
| 88 | + |
| 89 | + @classmethod |
| 90 | + def _json_namespace_(cls) -> str: |
| 91 | + return 'cirq.google' |
| 92 | + |
| 93 | + def _json_dict_(self) -> Dict[str, Any]: |
| 94 | + return { |
| 95 | + 'atol': self.atol, |
| 96 | + 'eject_paulis': self.eject_paulis, |
| 97 | + 'additional_gates': list(self.additional_gates), |
| 98 | + } |
| 99 | + |
| 100 | + @classmethod |
| 101 | + def _from_json_dict_(cls, atol, eject_paulis, additional_gates, **kwargs): |
| 102 | + return cls(atol=atol, eject_paulis=eject_paulis, additional_gates=additional_gates) |
0 commit comments