Skip to content

Commit 00c4756

Browse files
valente1ucian0ElePT
authored
Fix result.to_dict() following new 2.0 typing (#14124)
* fix result.to_dict() following new 2.0 typing * Update qiskit/result/result.py Co-authored-by: Luciano Bello <766693+1ucian0@users.noreply.github.com> * add result to_dict test * add release note * Update type hints to reflect current use of Result. We no longer have any ExperimentHeader class to call on, so the type can only be dict. * Remove reno, as the bug is technically unreleased --------- Co-authored-by: valente <valente@ar.ibm.com> Co-authored-by: Luciano Bello <766693+1ucian0@users.noreply.github.com> Co-authored-by: Elena Peña Tapia <epenatap@gmail.com>
1 parent 360123e commit 00c4756

3 files changed

Lines changed: 43 additions & 10 deletions

File tree

qiskit/result/models.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ def __init__(
145145
status (str): The status of the experiment
146146
seed (int): The seed used for simulation (if run on a simulator)
147147
meas_return (str): The type of measurement returned
148-
header (dict): A free form dictionary
149-
header for the experiment
148+
header (dict): A free form dictionary header for the experiment
150149
kwargs: Arbitrary extra fields
151150
152151
Raises:

qiskit/result/result.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class Result:
3434
each experiment success)
3535
results (list[ExperimentResult]): corresponding results for array of
3636
experiments of the input
37+
date (str): optional date field
38+
status (str): optional status field
39+
header (dict): an optional free form dictionary header
3740
"""
3841

3942
_metadata = {}
@@ -88,7 +91,7 @@ def to_dict(self):
8891
"backend_name": self.backend_name,
8992
"backend_version": self.backend_version,
9093
"date": self.date,
91-
"header": None if self.header is None else self.header.to_dict(),
94+
"header": self.header,
9295
"job_id": self.job_id,
9396
"status": self.status,
9497
"success": self.success,
@@ -128,11 +131,10 @@ def data(self, experiment=None):
128131
the get_xxx method, and the data will be post-processed for the data type.
129132
130133
Args:
131-
experiment (str or QuantumCircuit or Schedule or int or None): the index of the
134+
experiment (str or QuantumCircuit or int or None): the index of the
132135
experiment. Several types are accepted for convenience::
133136
* str: the name of the experiment.
134137
* QuantumCircuit: the name of the circuit instance will be used.
135-
* Schedule: the name of the schedule instance will be used.
136138
* int: the position of the experiment.
137139
* None: if there is only one experiment, returns it.
138140
@@ -179,7 +181,7 @@ def get_memory(self, experiment=None):
179181
['00000', '01000', '10100', '10100', '11101', '11100', '00101', ..., '01010']
180182
181183
Args:
182-
experiment (str or QuantumCircuit or Schedule or int or None): the index of the
184+
experiment (str or QuantumCircuit or int or None): the index of the
183185
experiment, as specified by ``data()``.
184186
185187
Returns:
@@ -231,7 +233,7 @@ def get_counts(self, experiment=None):
231233
"""Get the histogram data of an experiment.
232234
233235
Args:
234-
experiment (str or QuantumCircuit or Schedule or int or None): the index of the
236+
experiment (str or QuantumCircuit or int or None): the index of the
235237
experiment, as specified by ``data([experiment])``.
236238
237239
Returns:
@@ -283,7 +285,7 @@ def get_statevector(self, experiment=None, decimals=None):
283285
"""Get the final statevector of an experiment.
284286
285287
Args:
286-
experiment (str or QuantumCircuit or Schedule or int or None): the index of the
288+
experiment (str or QuantumCircuit or int or None): the index of the
287289
experiment, as specified by ``data()``.
288290
decimals (int): the number of decimals in the statevector.
289291
If None, does not round.
@@ -305,7 +307,7 @@ def get_unitary(self, experiment=None, decimals=None):
305307
"""Get the final unitary of an experiment.
306308
307309
Args:
308-
experiment (str or QuantumCircuit or Schedule or int or None): the index of the
310+
experiment (str or QuantumCircuit or int or None): the index of the
309311
experiment, as specified by ``data()``.
310312
decimals (int): the number of decimals in the unitary.
311313
If None, does not round.
@@ -326,7 +328,7 @@ def _get_experiment(self, key=None):
326328
"""Return a single experiment result from a given key.
327329
328330
Args:
329-
key (str or QuantumCircuit or Schedule or int or None): the index of the
331+
key (str or QuantumCircuit or int or None): the index of the
330332
experiment, as specified by ``data()``.
331333
332334
Returns:

test/python/result/test_result.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,38 @@ def test_result_repr(self):
121121
)
122122
self.assertEqual(expected, repr(result))
123123

124+
def test_result_to_dict(self):
125+
"""Test that dictionary is constructed correctly for a results object."""
126+
raw_counts = {"0x0": 4, "0x2": 10}
127+
data = models.ExperimentResultData(counts=raw_counts)
128+
exp_result_header = {"creg_sizes": [["c0", 2], ["c0", 1], ["c1", 1]], "memory_slots": 4}
129+
exp_result = models.ExperimentResult(
130+
shots=14, success=True, meas_level=2, data=data, header=exp_result_header
131+
)
132+
header = {"header-property": 1}
133+
result = Result(results=[exp_result], header=header, **self.base_result_args)
134+
135+
expected = {
136+
"backend_name": "test_backend",
137+
"backend_version": "1.0.0",
138+
"header": {"header-property": 1},
139+
"date": None,
140+
"job_id": "job-123",
141+
"status": None,
142+
"success": True,
143+
"results": [
144+
{
145+
"shots": 14,
146+
"success": True,
147+
"data": {"counts": {"0x0": 4, "0x2": 10}},
148+
"meas_level": 2,
149+
"header": {"creg_sizes": [["c0", 2], ["c0", 1], ["c1", 1]], "memory_slots": 4},
150+
}
151+
],
152+
}
153+
154+
self.assertEqual(expected, result.to_dict())
155+
124156
def test_multiple_circuits_counts(self):
125157
"""Test that counts are returned either as a list or a single item.
126158

0 commit comments

Comments
 (0)