Skip to content

Commit

Permalink
Add units to device parameter (#6140)
Browse files Browse the repository at this point in the history
* Add units to device parameter

- Add the ability to add unit type for a device parameter.
- This will be important for server-side deserialization, where the server will need to know what units the float values are in.
- This will allow us to do sweeps of parameters that have units, such as nanoseconds, or GigaHertz.
  • Loading branch information
dstrain115 committed Jun 13, 2023
1 parent c5dbb11 commit d236276
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
Expand Up @@ -6,5 +6,6 @@
"key"
],
"idx": 4,
"value": 7.5
"value": 7.5,
"units": "GHz"
}
@@ -1 +1 @@
cirq_google.study.device_parameter.DeviceParameter(path=['test', 'subdir', 'key'], idx=4, value=7.5)
cirq_google.study.device_parameter.DeviceParameter(path=['test', 'subdir', 'key'], idx=4, value=7.5, units='GHz')
11 changes: 8 additions & 3 deletions cirq-google/cirq_google/study/device_parameter.py
Expand Up @@ -46,16 +46,19 @@ class DeviceParameter(SupportsDeviceParameter):
entry in a list.
idx: If this key is an array, which index to modify.
value: value of the parameter to be set, if any.
units: string value of the unit type of the value, if any.
For instance, "GHz", "MHz", "ns", etc.
"""

path: Sequence[str]
idx: Optional[int] = None
value: Optional[Any] = None
units: Optional[str] = None

def __repr__(self) -> str:
return (
'cirq_google.study.DeviceParameter('
f'path={self.path!r}, idx={self.idx}, value={self.value!r})'
f'path={self.path!r}, idx={self.idx}, value={self.value!r}, units={self.units!r})'
)

@classmethod
Expand All @@ -64,7 +67,9 @@ def _json_namespace_(cls) -> str:

@classmethod
def _from_json_dict_(cls, path, idx, value, **kwargs):
return DeviceParameter(path=path, idx=idx, value=value)
return DeviceParameter(
path=path, idx=idx, value=value, units=kwargs['units'] if 'units' in kwargs else None
)

def _json_dict_(self) -> Dict[str, Any]:
return cirq.obj_to_dict_helper(self, ["path", "idx", "value"])
return cirq.obj_to_dict_helper(self, ["path", "idx", "value", "units"])
4 changes: 3 additions & 1 deletion cirq-google/cirq_google/study/device_parameter_test.py
Expand Up @@ -16,5 +16,7 @@


def test_parameters():
param = cirq_google.study.DeviceParameter(path=('test', 'subdir'), idx=2, value='tmp')
param = cirq_google.study.DeviceParameter(
path=('test', 'subdir'), idx=2, value='tmp', units='GHz'
)
cirq.testing.assert_equivalent_repr(param, global_vals={'cirq_google': cirq_google})

0 comments on commit d236276

Please sign in to comment.