Skip to content

Commit ebec38b

Browse files
dstrain115vtomole
andauthored
Add Parameter to cirq_google (#6102)
* Add Parameter to cirq_google - This object will be able to store device parameters and is intended to be attached as a sweep meta-data. - Add study __init__ as a cirq_google module. Co-authored-by: Victory Omole <[email protected]>
1 parent 36d67c1 commit ebec38b

8 files changed

+122
-0
lines changed

cirq-google/cirq_google/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@
131131
SimulatedProcessorWithLocalDeviceRecord,
132132
)
133133

134+
from cirq_google import study
135+
134136
from cirq_google import experimental
135137

136138

cirq-google/cirq_google/json_resolver_cache.py

+1
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,5 @@ def _old_xmon(*args, **kwargs):
8484
'cirq.google.EngineResult': cirq_google.EngineResult,
8585
'cirq.google.GridDevice': cirq_google.GridDevice,
8686
'cirq.google.GoogleCZTargetGateset': cirq_google.GoogleCZTargetGateset,
87+
'cirq.google.DeviceParameter': cirq_google.study.device_parameter.DeviceParameter,
8788
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"cirq_type": "cirq.google.DeviceParameter",
3+
"path": [
4+
"test",
5+
"subdir",
6+
"key"
7+
],
8+
"idx": 4,
9+
"value": 7.5
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cirq_google.study.device_parameter.DeviceParameter(path=['test', 'subdir', 'key'], idx=4, value=7.5)

cirq-google/cirq_google/json_test_data/spec.py

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
'EngineResult',
6464
'GridDevice',
6565
'GoogleCZTargetGateset',
66+
'DeviceParameter',
6667
]
6768
},
6869
resolver_cache=_class_resolver_dictionary(),
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
"""Classes for running sweeps particular to Google hardware."""
16+
17+
from cirq_google.study.device_parameter import DeviceParameter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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"])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
import cirq
15+
import cirq_google
16+
17+
18+
def test_parameters():
19+
param = cirq_google.study.DeviceParameter(path=('test', 'subdir'), idx=2, value='tmp')
20+
cirq.testing.assert_equivalent_repr(param, global_vals={'cirq_google': cirq_google})

0 commit comments

Comments
 (0)