/
engine_result.py
113 lines (97 loc) · 3.79 KB
/
engine_result.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Copyright 2022 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
from typing import Optional, Mapping, TYPE_CHECKING, Any, Dict
import numpy as np
from cirq import study
if TYPE_CHECKING:
import cirq
class EngineResult(study.ResultDict):
"""A ResultDict with additional job metadata.
Please see the documentation for `cirq.ResultDict` for more information.
Additional Attributes:
job_id: A string job identifier.
job_finished_time: A timestamp for when the job finished.
"""
def __init__(
self,
*, # Forces keyword args.
job_id: str,
job_finished_time: datetime.datetime,
params: Optional[study.ParamResolver] = None,
measurements: Optional[Mapping[str, np.ndarray]] = None,
records: Optional[Mapping[str, np.ndarray]] = None,
):
"""Initialize the result.
Args:
job_id: A string job identifier.
job_finished_time: A timestamp for when the job finished; will be converted to UTC.
params: A ParamResolver of settings used for this result.
measurements: A dictionary from measurement gate key to measurement
results. See `cirq.ResultDict`.
records: A dictionary from measurement gate key to measurement
results. See `cirq.ResultDict`.
"""
super().__init__(params=params, measurements=measurements, records=records)
self.job_id = job_id
self.job_finished_time = job_finished_time
@classmethod
def from_result(
cls, result: 'cirq.Result', *, job_id: str, job_finished_time: datetime.datetime
):
if isinstance(result, study.ResultDict):
# optimize by using private methods
return cls(
params=result._params,
measurements=result._measurements,
records=result._records,
job_id=job_id,
job_finished_time=job_finished_time,
)
else:
return cls(
params=result.params,
measurements=result.measurements,
records=result.records,
job_id=job_id,
job_finished_time=job_finished_time,
)
def __eq__(self, other):
if not isinstance(other, EngineResult):
return False
return (
super().__eq__(other)
and self.job_id == other.job_id
and self.job_finished_time == other.job_finished_time
)
def __repr__(self) -> str:
return (
f'cirq_google.EngineResult(params={self.params!r}, '
f'records={self._record_dict_repr()}, '
f'job_id={self.job_id!r}, '
f'job_finished_time={self.job_finished_time!r})'
)
@classmethod
def _json_namespace_(cls) -> str:
return 'cirq.google'
def _json_dict_(self) -> Dict[str, Any]:
d = super()._json_dict_()
d['job_id'] = self.job_id
d['job_finished_time'] = self.job_finished_time
return d
@classmethod
def _from_json_dict_(cls, params, records, job_id, job_finished_time, **kwargs):
return cls._from_packed_records(
params=params, records=records, job_id=job_id, job_finished_time=job_finished_time
)