From 5c111c8cf9066cf6c178f833b7d23f2ae6638d4b Mon Sep 17 00:00:00 2001 From: Alejandro Montanez Date: Mon, 4 Apr 2022 06:28:47 -0500 Subject: [PATCH 1/7] date attribute Date attribute is needed for some specific applications such as mitigation in mthree. It is not created by default when running a job in the basicAer backend. --- qiskit/result/result.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/qiskit/result/result.py b/qiskit/result/result.py index c6c3d67aee58..16c2f6788132 100644 --- a/qiskit/result/result.py +++ b/qiskit/result/result.py @@ -62,8 +62,7 @@ def __init__( self.job_id = job_id self.success = success self.results = results - if date is not None: - self.date = date + self.date = date if status is not None: self.status = status if header is not None: From 2576927483855506fb236cd5aade74f8c52d66a0 Mon Sep 17 00:00:00 2001 From: Alejandro Montanez Date: Tue, 26 Apr 2022 08:48:06 -0500 Subject: [PATCH 2/7] Update test_result.py --- test/python/result/test_result.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/python/result/test_result.py b/test/python/result/test_result.py index 3127e77b30ca..72c3356fc797 100644 --- a/test/python/result/test_result.py +++ b/test/python/result/test_result.py @@ -120,7 +120,7 @@ def test_result_repr(self): "results=[ExperimentResult(shots=14, success=True, " "meas_level=2, data=ExperimentResultData(counts={'0x0': 4," " '0x2': 10}), header=QobjExperimentHeader(creg_sizes=" - "[['c0', 2], ['c0', 1], ['c1', 1]], memory_slots=4))])" + "[['c0', 2], ['c0', 1], ['c1', 1]], memory_slots=4))], date=None)" ) self.assertEqual(expected, repr(result)) From 595578d21b2af89e3d5b50873807bc23da6a65f2 Mon Sep 17 00:00:00 2001 From: Alejandro Montanez Date: Wed, 18 May 2022 04:28:09 -0500 Subject: [PATCH 3/7] Adding default attributes header and status I have no clue why in the test_results the attribute status is repeated. --- qiskit/result/result.py | 6 ++---- test/python/result/test_result.py | 3 ++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/qiskit/result/result.py b/qiskit/result/result.py index 16c2f6788132..a1af69185d84 100644 --- a/qiskit/result/result.py +++ b/qiskit/result/result.py @@ -63,10 +63,8 @@ def __init__( self.success = success self.results = results self.date = date - if status is not None: - self.status = status - if header is not None: - self.header = header + self.status = status + self.header = header self._metadata.update(kwargs) def __repr__(self): diff --git a/test/python/result/test_result.py b/test/python/result/test_result.py index 72c3356fc797..9dbe8df2e3b3 100644 --- a/test/python/result/test_result.py +++ b/test/python/result/test_result.py @@ -120,7 +120,8 @@ def test_result_repr(self): "results=[ExperimentResult(shots=14, success=True, " "meas_level=2, data=ExperimentResultData(counts={'0x0': 4," " '0x2': 10}), header=QobjExperimentHeader(creg_sizes=" - "[['c0', 2], ['c0', 1], ['c1', 1]], memory_slots=4))], date=None)" + "[['c0', 2], ['c0', 1], ['c1', 1]], memory_slots=4))], date=None, " + "status=None, status=None)" ) self.assertEqual(expected, repr(result)) From 9782dd5a9dc330e37d73aed0f3783670eb385c7c Mon Sep 17 00:00:00 2001 From: "Kevin J. Sung" Date: Tue, 21 Jun 2022 12:25:26 -0400 Subject: [PATCH 4/7] fix Result repr --- qiskit/result/result.py | 7 +------ test/python/result/test_result.py | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/qiskit/result/result.py b/qiskit/result/result.py index a1af69185d84..b69491a3cbae 100644 --- a/qiskit/result/result.py +++ b/qiskit/result/result.py @@ -80,12 +80,7 @@ def __repr__(self): self.results, ) ) - if hasattr(self, "date"): - out += ", date=%s" % self.date - if hasattr(self, "status"): - out += ", status=%s" % self.status - if hasattr(self, "header"): - out += ", status=%s" % self.header + 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] diff --git a/test/python/result/test_result.py b/test/python/result/test_result.py index b785c6901610..275048f793b7 100644 --- a/test/python/result/test_result.py +++ b/test/python/result/test_result.py @@ -122,7 +122,7 @@ def test_result_repr(self): "meas_level=2, data=ExperimentResultData(counts={'0x0': 4," " '0x2': 10}), header=QobjExperimentHeader(creg_sizes=" "[['c0', 2], ['c0', 1], ['c1', 1]], memory_slots=4))], date=None, " - "status=None, status=None)" + "status=None, header=None)" ) self.assertEqual(expected, repr(result)) From 54e8ebb10c6bf8fb0d57c7259ea8fe791db31c0e Mon Sep 17 00:00:00 2001 From: "Kevin J. Sung" Date: Tue, 21 Jun 2022 12:33:40 -0400 Subject: [PATCH 5/7] add release note --- releasenotes/notes/result-fix-e4eaa021f49b5f99.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 releasenotes/notes/result-fix-e4eaa021f49b5f99.yaml diff --git a/releasenotes/notes/result-fix-e4eaa021f49b5f99.yaml b/releasenotes/notes/result-fix-e4eaa021f49b5f99.yaml new file mode 100644 index 000000000000..2118b8516afc --- /dev/null +++ b/releasenotes/notes/result-fix-e4eaa021f49b5f99.yaml @@ -0,0 +1,9 @@ +--- +upgrade: + - | + :class;`~.Result` was modified so that it always contains ``date``, ``status``, + and ``header`` attributes (set to ``None`` if not specified). +fixes: + - | + Fixed a bug in :meth:`~.Result.__repr__` that caused the attributes to be + specified incorrectly. \ No newline at end of file From ae56a29b32790b632381f085dc158585f6988eb5 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Tue, 21 Jun 2022 17:48:19 +0100 Subject: [PATCH 6/7] Qualify method name in release note --- releasenotes/notes/result-fix-e4eaa021f49b5f99.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/releasenotes/notes/result-fix-e4eaa021f49b5f99.yaml b/releasenotes/notes/result-fix-e4eaa021f49b5f99.yaml index 2118b8516afc..123275116f49 100644 --- a/releasenotes/notes/result-fix-e4eaa021f49b5f99.yaml +++ b/releasenotes/notes/result-fix-e4eaa021f49b5f99.yaml @@ -1,9 +1,9 @@ --- upgrade: - | - :class;`~.Result` was modified so that it always contains ``date``, ``status``, + :class:`.Result` was modified so that it always contains ``date``, ``status``, and ``header`` attributes (set to ``None`` if not specified). fixes: - | - Fixed a bug in :meth:`~.Result.__repr__` that caused the attributes to be - specified incorrectly. \ No newline at end of file + Fixed a bug in :meth:`.Result.__repr__` that caused the attributes to be + specified incorrectly. From a4d6fabf02ea4570c3e1ccd0bff4352e0b3936dc Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Tue, 21 Jun 2022 17:52:03 +0100 Subject: [PATCH 7/7] Remove final conditional attribute lookup on Result --- qiskit/result/result.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/qiskit/result/result.py b/qiskit/result/result.py index b69491a3cbae..b9c820f584b5 100644 --- a/qiskit/result/result.py +++ b/qiskit/result/result.py @@ -99,17 +99,14 @@ def to_dict(self): 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], } - if hasattr(self, "date"): - out_dict["date"] = self.date - if hasattr(self, "status"): - out_dict["status"] = self.status - if hasattr(self, "header"): - out_dict["header"] = self.header.to_dict() out_dict.update(self._metadata) return out_dict @@ -134,7 +131,7 @@ def from_dict(cls, data): in_data = copy.copy(data) in_data["results"] = [ExperimentResult.from_dict(x) for x in in_data.pop("results")] - if "header" in in_data: + if in_data.get("header") is not None: in_data["header"] = QobjHeader.from_dict(in_data.pop("header")) return cls(**in_data)