|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -"""Runtime information dataclasses that accompany execution of executables.""" |
| 15 | +"""Runtime information dataclasses and execution of executables.""" |
16 | 16 |
|
17 | 17 | import dataclasses
|
| 18 | +import os |
| 19 | +import uuid |
18 | 20 | from typing import Any, Dict, Optional, List
|
19 | 21 |
|
20 | 22 | import cirq
|
|
23 | 25 | from cirq_google.workflow._abstract_engine_processor_shim import AbstractEngineProcessorShim
|
24 | 26 | from cirq_google.workflow.quantum_executable import (
|
25 | 27 | ExecutableSpec,
|
| 28 | + QuantumExecutableGroup, |
26 | 29 | )
|
27 | 30 |
|
28 | 31 |
|
@@ -133,3 +136,79 @@ def _json_dict_(self) -> Dict[str, Any]:
|
133 | 136 |
|
134 | 137 | def __repr__(self) -> str:
|
135 | 138 | return _compat.dataclass_repr(self, namespace='cirq_google')
|
| 139 | + |
| 140 | + |
| 141 | +def execute( |
| 142 | + rt_config: QuantumRuntimeConfiguration, |
| 143 | + executable_group: QuantumExecutableGroup, |
| 144 | + base_data_dir: str = ".", |
| 145 | +) -> ExecutableGroupResult: |
| 146 | + """Execute a `cg.QuantumExecutableGroup` according to a `cg.QuantumRuntimeConfiguration`. |
| 147 | +
|
| 148 | + Args: |
| 149 | + rt_config: The `cg.QuantumRuntimeConfiguration` specifying how to execute |
| 150 | + `executable_group`. |
| 151 | + executable_group: The `cg.QuantumExecutableGroup` containing the executables to execute. |
| 152 | + base_data_dir: A filesystem path to write data. We write |
| 153 | + "{base_data_dir}/{run_id}/ExecutableGroupResult.json.gz" |
| 154 | + containing the `cg.ExecutableGroupResult` as well as one file |
| 155 | + "{base_data_dir}/{run_id}/ExecutableResult.{i}.json.gz" per `cg.ExecutableResult` as |
| 156 | + each executable result becomes available. |
| 157 | +
|
| 158 | + Returns: |
| 159 | + The `cg.ExecutableGroupResult` containing all data and metadata for an execution. |
| 160 | +
|
| 161 | + Raises: |
| 162 | + NotImplementedError: If an executable uses the `params` field or anything other than |
| 163 | + a BitstringsMeasurement measurement field. |
| 164 | + ValueError: If `base_data_dir` is not a valid directory. |
| 165 | + """ |
| 166 | + # run_id defaults logic. |
| 167 | + if rt_config.run_id is None: |
| 168 | + run_id = str(uuid.uuid4()) |
| 169 | + else: |
| 170 | + run_id = rt_config.run_id |
| 171 | + |
| 172 | + # base_data_dir handling. |
| 173 | + if not base_data_dir: |
| 174 | + # coverage: ignore |
| 175 | + raise ValueError("Please provide a non-empty `base_data_dir`.") |
| 176 | + |
| 177 | + os.makedirs(f'{base_data_dir}/{run_id}', exist_ok=False) |
| 178 | + |
| 179 | + # Results object that we will fill in in the main loop. |
| 180 | + exegroup_result = ExecutableGroupResult( |
| 181 | + runtime_configuration=rt_config, |
| 182 | + shared_runtime_info=SharedRuntimeInfo(run_id=run_id), |
| 183 | + executable_results=list(), |
| 184 | + ) |
| 185 | + cirq.to_json_gzip(exegroup_result, f'{base_data_dir}/{run_id}/ExecutableGroupResult.json.gz') |
| 186 | + |
| 187 | + # Loop over executables. |
| 188 | + sampler = rt_config.processor.get_sampler() |
| 189 | + n_executables = len(executable_group) |
| 190 | + print() |
| 191 | + for i, exe in enumerate(executable_group): |
| 192 | + runtime_info = RuntimeInfo(execution_index=i) |
| 193 | + |
| 194 | + if exe.params != tuple(): |
| 195 | + raise NotImplementedError("Circuit params are not yet supported.") |
| 196 | + |
| 197 | + circuit = exe.circuit |
| 198 | + |
| 199 | + if not hasattr(exe.measurement, 'n_repetitions'): |
| 200 | + raise NotImplementedError("Only `BitstringsMeasurement` are supported.") |
| 201 | + |
| 202 | + sampler_run_result = sampler.run(circuit, repetitions=exe.measurement.n_repetitions) |
| 203 | + |
| 204 | + exe_result = ExecutableResult( |
| 205 | + spec=exe.spec, |
| 206 | + runtime_info=runtime_info, |
| 207 | + raw_data=sampler_run_result, |
| 208 | + ) |
| 209 | + cirq.to_json_gzip(exe_result, f'{base_data_dir}/{run_id}/ExecutableResult.{i}.json.gz') |
| 210 | + exegroup_result.executable_results.append(exe_result) |
| 211 | + print(f'\r{i+1} / {n_executables}', end='', flush=True) |
| 212 | + print() |
| 213 | + |
| 214 | + return exegroup_result |
0 commit comments