/
result.py
397 lines (341 loc) · 15.5 KB
/
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 2018.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""Model for schema-conformant Results."""
import copy
import warnings
from qiskit.circuit.quantumcircuit import QuantumCircuit
from qiskit.pulse.schedule import Schedule
from qiskit.exceptions import QiskitError
from qiskit.quantum_info.states import statevector
from qiskit.result.models import ExperimentResult
from qiskit.result import postprocess
from qiskit.result.counts import Counts
from qiskit.qobj.utils import MeasLevel
from qiskit.qobj import QobjHeader
class Result:
"""Model for Results.
Attributes:
backend_name (str): backend name.
backend_version (str): backend version, in the form X.Y.Z.
qobj_id (str): user-generated Qobj id.
job_id (str): unique execution id from the backend.
success (bool): True if complete input qobj executed correctly. (Implies
each experiment success)
results (list[ExperimentResult]): corresponding results for array of
experiments of the input qobj
"""
_metadata = {}
def __init__(
self,
backend_name,
backend_version,
qobj_id,
job_id,
success,
results,
date=None,
status=None,
header=None,
**kwargs,
):
self._metadata = {}
self.backend_name = backend_name
self.backend_version = backend_version
self.qobj_id = qobj_id
self.job_id = job_id
self.success = success
self.results = results
self.date = date
self.status = status
self.header = header
self._metadata.update(kwargs)
def __repr__(self):
out = (
"Result(backend_name='%s', backend_version='%s', qobj_id='%s', "
"job_id='%s', success=%s, results=%s"
% (
self.backend_name,
self.backend_version,
self.qobj_id,
self.job_id,
self.success,
self.results,
)
)
out += f", date={self.date}, status={self.status}, header={self.header}"
for key in self._metadata:
if isinstance(self._metadata[key], str):
value_str = "'%s'" % self._metadata[key]
else:
value_str = repr(self._metadata[key])
out += f", {key}={value_str}"
out += ")"
return out
def to_dict(self):
"""Return a dictionary format representation of the Result
Returns:
dict: The dictionary form of the Result
"""
out_dict = {
"backend_name": self.backend_name,
"backend_version": self.backend_version,
"date": self.date,
"header": None if self.header is None else self.header.to_dict(),
"qobj_id": self.qobj_id,
"job_id": self.job_id,
"status": self.status,
"success": self.success,
"results": [x.to_dict() for x in self.results],
}
out_dict.update(self._metadata)
return out_dict
def __getattr__(self, name):
try:
return self._metadata[name]
except KeyError as ex:
raise AttributeError(f"Attribute {name} is not defined") from ex
@classmethod
def from_dict(cls, data):
"""Create a new ExperimentResultData object from a dictionary.
Args:
data (dict): A dictionary representing the Result to create. It
will be in the same format as output by
:meth:`to_dict`.
Returns:
Result: The ``Result`` object from the input dictionary.
"""
in_data = copy.copy(data)
in_data["results"] = [ExperimentResult.from_dict(x) for x in in_data.pop("results")]
if in_data.get("header") is not None:
in_data["header"] = QobjHeader.from_dict(in_data.pop("header"))
return cls(**in_data)
def data(self, experiment=None):
"""Get the raw data for an experiment.
Note this data will be a single classical and quantum register and in a
format required by the results schema. We recommend that most users use
the get_xxx method, and the data will be post-processed for the data type.
Args:
experiment (str or QuantumCircuit or Schedule or int or None): the index of the
experiment. Several types are accepted for convenience::
* str: the name of the experiment.
* QuantumCircuit: the name of the circuit instance will be used.
* Schedule: the name of the schedule instance will be used.
* int: the position of the experiment.
* None: if there is only one experiment, returns it.
Returns:
dict: A dictionary of results data for an experiment. The data
depends on the backend it ran on and the settings of `meas_level`,
`meas_return` and `memory`.
OpenQASM backends return a dictionary of dictionary with the key
'counts' and with the counts, with the second dictionary keys
containing a string in hex format (``0x123``) and values equal to
the number of times this outcome was measured.
Statevector backends return a dictionary with key 'statevector' and
values being a list[list[complex components]] list of 2^num_qubits
complex amplitudes. Where each complex number is represented as a 2
entry list for each component. For example, a list of
[0.5+1j, 0-1j] would be represented as [[0.5, 1], [0, -1]].
Unitary backends return a dictionary with key 'unitary' and values
being a list[list[list[complex components]]] list of
2^num_qubits x 2^num_qubits complex amplitudes in a two entry list for
each component. For example if the amplitude is
[[0.5+0j, 0-1j], ...] the value returned will be
[[[0.5, 0], [0, -1]], ...].
The simulator backends also have an optional key 'snapshots' which
returns a dict of snapshots specified by the simulator backend.
The value is of the form dict[slot: dict[str: array]]
where the keys are the requested snapshot slots, and the values are
a dictionary of the snapshots.
Raises:
QiskitError: if data for the experiment could not be retrieved.
"""
try:
return self._get_experiment(experiment).data.to_dict()
except (KeyError, TypeError) as ex:
raise QiskitError(f'No data for experiment "{repr(experiment)}"') from ex
def get_memory(self, experiment=None):
"""Get the sequence of memory states (readouts) for each shot
The data from the experiment is a list of format
['00000', '01000', '10100', '10100', '11101', '11100', '00101', ..., '01010']
Args:
experiment (str or QuantumCircuit or Schedule or int or None): the index of the
experiment, as specified by ``data()``.
Returns:
List[str] or np.ndarray: Either the list of each outcome, formatted according to
registers in circuit or a complex numpy np.ndarray with shape:
============ ============= =====
`meas_level` `meas_return` shape
============ ============= =====
0 `single` np.ndarray[shots, memory_slots, memory_slot_size]
0 `avg` np.ndarray[memory_slots, memory_slot_size]
1 `single` np.ndarray[shots, memory_slots]
1 `avg` np.ndarray[memory_slots]
2 `memory=True` list
============ ============= =====
Raises:
QiskitError: if there is no memory data for the circuit.
"""
exp_result = self._get_experiment(experiment)
try:
try: # header is not available
header = exp_result.header.to_dict()
except (AttributeError, QiskitError):
header = None
meas_level = exp_result.meas_level
memory = self.data(experiment)["memory"]
if meas_level == MeasLevel.CLASSIFIED:
return postprocess.format_level_2_memory(memory, header)
elif meas_level == MeasLevel.KERNELED:
return postprocess.format_level_1_memory(memory)
elif meas_level == MeasLevel.RAW:
return postprocess.format_level_0_memory(memory)
else:
raise QiskitError(f"Measurement level {meas_level} is not supported")
except KeyError as ex:
raise QiskitError(
'No memory for experiment "{}". '
"Please verify that you either ran a measurement level 2 job "
'with the memory flag set, eg., "memory=True", '
"or a measurement level 0/1 job.".format(repr(experiment))
) from ex
def get_counts(self, experiment=None):
"""Get the histogram data of an experiment.
Args:
experiment (str or QuantumCircuit or Schedule or int or None): the index of the
experiment, as specified by ``data([experiment])``.
Returns:
dict[str, int] or list[dict[str, int]]: a dictionary or a list of
dictionaries. A dictionary has the counts for each qubit with
the keys containing a string in binary format and separated
according to the registers in circuit (e.g. ``0100 1110``).
The string is little-endian (cr[0] on the right hand side).
Raises:
QiskitError: if there are no counts for the experiment.
"""
if experiment is None:
exp_keys = range(len(self.results))
else:
exp_keys = [experiment]
dict_list = []
for key in exp_keys:
exp = self._get_experiment(key)
try:
header = exp.header.to_dict()
except (AttributeError, QiskitError): # header is not available
header = None
if "counts" in self.data(key).keys():
if header:
counts_header = {
k: v
for k, v in header.items()
if k in {"time_taken", "creg_sizes", "memory_slots"}
}
else:
counts_header = {}
dict_list.append(Counts(self.data(key)["counts"], **counts_header))
elif "statevector" in self.data(key).keys():
vec = postprocess.format_statevector(self.data(key)["statevector"])
dict_list.append(statevector.Statevector(vec).probabilities_dict(decimals=15))
else:
raise QiskitError(f'No counts for experiment "{repr(key)}"')
# Return first item of dict_list if size is 1
if len(dict_list) == 1:
return dict_list[0]
else:
return dict_list
def get_statevector(self, experiment=None, decimals=None):
"""Get the final statevector of an experiment.
Args:
experiment (str or QuantumCircuit or Schedule or int or None): the index of the
experiment, as specified by ``data()``.
decimals (int): the number of decimals in the statevector.
If None, does not round.
Returns:
list[complex]: list of 2^num_qubits complex amplitudes.
Raises:
QiskitError: if there is no statevector for the experiment.
"""
try:
return postprocess.format_statevector(
self.data(experiment)["statevector"], decimals=decimals
)
except KeyError as ex:
raise QiskitError(f'No statevector for experiment "{repr(experiment)}"') from ex
def get_unitary(self, experiment=None, decimals=None):
"""Get the final unitary of an experiment.
Args:
experiment (str or QuantumCircuit or Schedule or int or None): the index of the
experiment, as specified by ``data()``.
decimals (int): the number of decimals in the unitary.
If None, does not round.
Returns:
list[list[complex]]: list of 2^num_qubits x 2^num_qubits complex
amplitudes.
Raises:
QiskitError: if there is no unitary for the experiment.
"""
try:
return postprocess.format_unitary(self.data(experiment)["unitary"], decimals=decimals)
except KeyError as ex:
raise QiskitError(f'No unitary for experiment "{repr(experiment)}"') from ex
def _get_experiment(self, key=None):
"""Return a single experiment result from a given key.
Args:
key (str or QuantumCircuit or Schedule or int or None): the index of the
experiment, as specified by ``data()``.
Returns:
ExperimentResult: the results for an experiment.
Raises:
QiskitError: if there is no data for the experiment, or an unhandled
error occurred while fetching the data.
"""
# Automatically return the first result if no key was provided.
if key is None:
if len(self.results) != 1:
raise QiskitError(
"You have to select a circuit or schedule when there is more than one available"
)
key = 0
# Key is a QuantumCircuit/Schedule or str: retrieve result by name.
if isinstance(key, (QuantumCircuit, Schedule)):
key = key.name
# Key is an integer: return result by index.
if isinstance(key, int):
try:
exp = self.results[key]
except IndexError as ex:
raise QiskitError(f'Result for experiment "{key}" could not be found.') from ex
else:
# Look into `result[x].header.name` for the names.
exp = [
result
for result in self.results
if getattr(getattr(result, "header", None), "name", "") == key
]
if len(exp) == 0:
raise QiskitError('Data for experiment "%s" could not be found.' % key)
if len(exp) == 1:
exp = exp[0]
else:
warnings.warn(
'Result object contained multiple results matching name "%s", '
"only first match will be returned. Use an integer index to "
"retrieve results for all entries." % key
)
exp = exp[0]
# Check that the retrieved experiment was successful
if getattr(exp, "success", False):
return exp
# If unsuccessful check experiment and result status and raise exception
result_status = getattr(self, "status", "Result was not successful")
exp_status = getattr(exp, "status", "Experiment was not successful")
raise QiskitError(result_status, ", ", exp_status)