From 60e38a5b95d4c2ab362ab3b32bd59c53a2cf84ff Mon Sep 17 00:00:00 2001 From: Abel Valente Date: Mon, 31 Mar 2025 08:40:05 -0300 Subject: [PATCH] Fix result.to_dict() following new 2.0 typing (#14124) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 Co-authored-by: Luciano Bello <766693+1ucian0@users.noreply.github.com> Co-authored-by: Elena Peña Tapia (cherry picked from commit 00c47566fb559402afe3c1ce51d3e03e2f619dac) --- qiskit/result/models.py | 3 +-- qiskit/result/result.py | 18 +++++++++-------- test/python/result/test_result.py | 32 +++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/qiskit/result/models.py b/qiskit/result/models.py index 71fda895952d..7ed9e1ae5b70 100644 --- a/qiskit/result/models.py +++ b/qiskit/result/models.py @@ -145,8 +145,7 @@ def __init__( status (str): The status of the experiment seed (int): The seed used for simulation (if run on a simulator) meas_return (str): The type of measurement returned - header (dict): A free form dictionary - header for the experiment + header (dict): A free form dictionary header for the experiment kwargs: Arbitrary extra fields Raises: diff --git a/qiskit/result/result.py b/qiskit/result/result.py index dadd4e5fa5fc..13bbf0ea709f 100644 --- a/qiskit/result/result.py +++ b/qiskit/result/result.py @@ -34,6 +34,9 @@ class Result: each experiment success) results (list[ExperimentResult]): corresponding results for array of experiments of the input + date (str): optional date field + status (str): optional status field + header (dict): an optional free form dictionary header """ _metadata = {} @@ -88,7 +91,7 @@ def to_dict(self): "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(), + "header": self.header, "job_id": self.job_id, "status": self.status, "success": self.success, @@ -128,11 +131,10 @@ def data(self, experiment=None): 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 (str or QuantumCircuit 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. @@ -179,7 +181,7 @@ def get_memory(self, experiment=None): ['00000', '01000', '10100', '10100', '11101', '11100', '00101', ..., '01010'] Args: - experiment (str or QuantumCircuit or Schedule or int or None): the index of the + experiment (str or QuantumCircuit or int or None): the index of the experiment, as specified by ``data()``. Returns: @@ -231,7 +233,7 @@ 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 (str or QuantumCircuit or int or None): the index of the experiment, as specified by ``data([experiment])``. Returns: @@ -283,7 +285,7 @@ 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 (str or QuantumCircuit 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. @@ -305,7 +307,7 @@ 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 (str or QuantumCircuit 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. @@ -326,7 +328,7 @@ 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 + key (str or QuantumCircuit or int or None): the index of the experiment, as specified by ``data()``. Returns: diff --git a/test/python/result/test_result.py b/test/python/result/test_result.py index f36b19f5660f..12be790ea161 100644 --- a/test/python/result/test_result.py +++ b/test/python/result/test_result.py @@ -121,6 +121,38 @@ def test_result_repr(self): ) self.assertEqual(expected, repr(result)) + def test_result_to_dict(self): + """Test that dictionary is constructed correctly for a results object.""" + raw_counts = {"0x0": 4, "0x2": 10} + data = models.ExperimentResultData(counts=raw_counts) + exp_result_header = {"creg_sizes": [["c0", 2], ["c0", 1], ["c1", 1]], "memory_slots": 4} + exp_result = models.ExperimentResult( + shots=14, success=True, meas_level=2, data=data, header=exp_result_header + ) + header = {"header-property": 1} + result = Result(results=[exp_result], header=header, **self.base_result_args) + + expected = { + "backend_name": "test_backend", + "backend_version": "1.0.0", + "header": {"header-property": 1}, + "date": None, + "job_id": "job-123", + "status": None, + "success": True, + "results": [ + { + "shots": 14, + "success": True, + "data": {"counts": {"0x0": 4, "0x2": 10}}, + "meas_level": 2, + "header": {"creg_sizes": [["c0", 2], ["c0", 1], ["c1", 1]], "memory_slots": 4}, + } + ], + } + + self.assertEqual(expected, result.to_dict()) + def test_multiple_circuits_counts(self): """Test that counts are returned either as a list or a single item.