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
17import dataclasses
18from typing import Any, Dict, Optional
19
20import cirq
21from cirq import _compat
22from cirq.protocols import dataclass_json_dict
23from cirq_google.workflow.quantum_executable import (
24    ExecutableSpec,
25)
26
27
28@dataclasses.dataclass
29class 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
49class 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
69class 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')
88