|
| 1 | +# Copyright 2023 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, Optional, Sequence |
| 16 | +import dataclasses |
| 17 | +from typing_extensions import Protocol |
| 18 | +import cirq |
| 19 | + |
| 20 | + |
| 21 | +class SupportsDeviceParameter(Protocol): |
| 22 | + """Protocol for using device parameter keys. |
| 23 | +
|
| 24 | + Args: |
| 25 | + path: path of the key to modify, with each sub-folder as a string |
| 26 | + entry in a list. |
| 27 | + idx: If this key is an array, which index to modify. |
| 28 | + value: value of the parameter to be set, if any. |
| 29 | + """ |
| 30 | + |
| 31 | + path: Sequence[str] |
| 32 | + idx: Optional[int] = None |
| 33 | + value: Optional[Any] = None |
| 34 | + |
| 35 | + |
| 36 | +@dataclasses.dataclass |
| 37 | +class DeviceParameter(SupportsDeviceParameter): |
| 38 | + """Class for specifying device parameters. |
| 39 | +
|
| 40 | + For instance, varying the length of pulses, timing, etc. |
| 41 | + This class is intended to be attached to a cirq.Points |
| 42 | + or cirq.Linspace sweep object as a metadata attribute. |
| 43 | +
|
| 44 | + Args: |
| 45 | + path: path of the key to modify, with each sub-folder as a string |
| 46 | + entry in a list. |
| 47 | + idx: If this key is an array, which index to modify. |
| 48 | + value: value of the parameter to be set, if any. |
| 49 | + """ |
| 50 | + |
| 51 | + path: Sequence[str] |
| 52 | + idx: Optional[int] = None |
| 53 | + value: Optional[Any] = None |
| 54 | + |
| 55 | + def __repr__(self) -> str: |
| 56 | + return ( |
| 57 | + 'cirq_google.study.DeviceParameter(' |
| 58 | + f'path={self.path!r}, idx={self.idx}, value={self.value!r})' |
| 59 | + ) |
| 60 | + |
| 61 | + @classmethod |
| 62 | + def _json_namespace_(cls) -> str: |
| 63 | + return 'cirq.google' |
| 64 | + |
| 65 | + @classmethod |
| 66 | + def _from_json_dict_(cls, path, idx, value, **kwargs): |
| 67 | + return DeviceParameter(path=path, idx=idx, value=value) |
| 68 | + |
| 69 | + def _json_dict_(self) -> Dict[str, Any]: |
| 70 | + return cirq.obj_to_dict_helper(self, ["path", "idx", "value"]) |
0 commit comments