Skip to content

Commit 66c694a

Browse files
authored
[cirqflow] Quantum runtime skeleton - part 1 (#4565)
Skeleton of the constituent parts of `ExecutableGroupResult` ![RequestResponse2 (2)](https://user-images.githubusercontent.com/4967059/136300308-21dd2034-01f7-49a3-84a7-0b0d32928918.png) Most of these dataclasses only have barebones attributes which may beg the question: "why do we need a class for this?". Rest assured: I have big plans for adding more attributes to things. This is the first part; splitting up #4556
1 parent 033c604 commit 66c694a

12 files changed

+196
-0
lines changed

cirq-google/cirq_google/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@
124124
QuantumExecutable,
125125
QuantumExecutableGroup,
126126
BitstringsMeasurement,
127+
SharedRuntimeInfo,
128+
RuntimeInfo,
129+
ExecutableResult,
127130
)
128131

129132
from cirq_google import experimental

cirq-google/cirq_google/json_resolver_cache.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,7 @@ def _class_resolver_dictionary() -> Dict[str, ObjectFactory]:
4545
'cirq.google.QuantumExecutable': cirq_google.QuantumExecutable,
4646
'cirq.google.QuantumExecutableGroup': cirq_google.QuantumExecutableGroup,
4747
'cirq.google.KeyValueExecutableSpec': cirq_google.KeyValueExecutableSpec,
48+
'cirq.google.SharedRuntimeInfo': cirq_google.SharedRuntimeInfo,
49+
'cirq.google.RuntimeInfo': cirq_google.RuntimeInfo,
50+
'cirq.google.ExecutableResult': cirq_google.ExecutableResult,
4851
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"cirq_type": "cirq.google.ExecutableResult",
3+
"spec": {
4+
"cirq_type": "cirq.google.KeyValueExecutableSpec",
5+
"executable_family": "cirq_google.algo_benchmarks.example",
6+
"key_value_pairs": [
7+
[
8+
"name",
9+
"test-spec"
10+
]
11+
]
12+
},
13+
"runtime_info": {
14+
"cirq_type": "cirq.google.RuntimeInfo",
15+
"execution_index": 5
16+
},
17+
"raw_data": {
18+
"cirq_type": "Result",
19+
"params": {
20+
"cirq_type": "ParamResolver",
21+
"param_dict": []
22+
},
23+
"measurements": {
24+
"z": {
25+
"packed_digits": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
26+
"binary": true,
27+
"dtype": "float64",
28+
"shape": [
29+
1000,
30+
4
31+
]
32+
}
33+
}
34+
}
35+
}

cirq-google/cirq_google/json_test_data/cirq.google.ExecutableResult.repr

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"cirq_type": "cirq.google.RuntimeInfo",
3+
"execution_index": 5
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cirq_google.RuntimeInfo(execution_index=5)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"cirq_type": "cirq.google.SharedRuntimeInfo",
3+
"run_id": "my run"
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cirq_google.SharedRuntimeInfo(run_id='my run')

cirq-google/cirq_google/json_test_data/spec.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
'QuantumExecutable',
6060
'QuantumExecutableGroup',
6161
'KeyValueExecutableSpec',
62+
'ExecutableResult',
63+
'RuntimeInfo',
64+
'SharedRuntimeInfo',
6265
]
6366
},
6467
resolver_cache=_class_resolver_dictionary(),

cirq-google/cirq_google/workflow/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@
55
QuantumExecutableGroup,
66
BitstringsMeasurement,
77
)
8+
9+
from cirq_google.workflow.quantum_runtime import (
10+
SharedRuntimeInfo,
11+
RuntimeInfo,
12+
ExecutableResult,
13+
)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copyright 2021 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+
"""Runtime information dataclasses that accompany execution of executables."""
16+
17+
import dataclasses
18+
from typing import Any, Dict, Optional
19+
20+
import cirq
21+
from cirq import _compat
22+
from cirq.protocols import dataclass_json_dict
23+
from cirq_google.workflow.quantum_executable import (
24+
ExecutableSpec,
25+
)
26+
27+
28+
@dataclasses.dataclass
29+
class SharedRuntimeInfo:
30+
"""Runtime information common to all `cg.QuantumExecutable`s in an execution of a
31+
`cg.QuantumExecutableGroup`.
32+
33+
There is one `cg.SharedRuntimeInfo` per `cg.ExecutableGroupResult`.
34+
35+
Args:
36+
run_id: A unique `str` identifier for this run.
37+
"""
38+
39+
run_id: str
40+
41+
def _json_dict_(self) -> Dict[str, Any]:
42+
return dataclass_json_dict(self, namespace='cirq.google')
43+
44+
def __repr__(self) -> str:
45+
return _compat.dataclass_repr(self, namespace='cirq_google')
46+
47+
48+
@dataclasses.dataclass
49+
class RuntimeInfo:
50+
"""Runtime information relevant to a particular `cg.QuantumExecutable`.
51+
52+
There is one `cg.RuntimeInfo` per `cg.ExecutableResult`
53+
54+
Args:
55+
execution_index: What order (in its `cg.QuantumExecutableGroup`) this
56+
`cg.QuantumExecutable` was executed.
57+
"""
58+
59+
execution_index: int
60+
61+
def _json_dict_(self) -> Dict[str, Any]:
62+
return dataclass_json_dict(self, namespace='cirq.google')
63+
64+
def __repr__(self) -> str:
65+
return _compat.dataclass_repr(self, namespace='cirq_google')
66+
67+
68+
@dataclasses.dataclass
69+
class ExecutableResult:
70+
"""Results for a `cg.QuantumExecutable`.
71+
72+
Args:
73+
spec: The `cg.ExecutableSpec` typifying the `cg.QuantumExecutable`.
74+
runtime_info: A `cg.RuntimeInfo` dataclass containing information gathered during
75+
execution of the `cg.QuantumExecutable`.
76+
raw_data: The `cirq.Result` containing the data from the run.
77+
"""
78+
79+
spec: Optional[ExecutableSpec]
80+
runtime_info: RuntimeInfo
81+
raw_data: cirq.Result
82+
83+
def _json_dict_(self) -> Dict[str, Any]:
84+
return dataclass_json_dict(self, namespace='cirq.google')
85+
86+
def __repr__(self) -> str:
87+
return _compat.dataclass_repr(self, namespace='cirq_google')
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2021 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+
import cirq
16+
import cirq_google as cg
17+
import numpy as np
18+
from cirq_google.workflow.quantum_executable_test import _get_example_spec
19+
20+
21+
def cg_assert_equivalent_repr(value):
22+
"""cirq.testing.assert_equivalent_repr with cirq_google.workflow imported."""
23+
return cirq.testing.assert_equivalent_repr(
24+
value,
25+
global_vals={
26+
'cirq_google': cg,
27+
},
28+
)
29+
30+
31+
def test_shared_runtime_info():
32+
shared_rtinfo = cg.SharedRuntimeInfo(run_id='my run')
33+
cg_assert_equivalent_repr(shared_rtinfo)
34+
35+
36+
def test_runtime_info():
37+
rtinfo = cg.RuntimeInfo(execution_index=5)
38+
cg_assert_equivalent_repr(rtinfo)
39+
40+
41+
def test_executable_result():
42+
rtinfo = cg.RuntimeInfo(execution_index=5)
43+
er = cg.ExecutableResult(
44+
spec=_get_example_spec(name='test-spec'),
45+
runtime_info=rtinfo,
46+
raw_data=cirq.Result(params=cirq.ParamResolver(), measurements={'z': np.ones((1_000, 4))}),
47+
)
48+
cg_assert_equivalent_repr(er)

0 commit comments

Comments
 (0)