From 1f6f621f10c003cf96cb322d453d38748dd66877 Mon Sep 17 00:00:00 2001 From: ikkoham Date: Mon, 21 Feb 2022 16:20:08 +0900 Subject: [PATCH 1/4] fix endian --- qiskit/result/mitigation/utils.py | 2 +- test/python/result/test_mitigators.py | 30 +++++++++++++++++++-------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/qiskit/result/mitigation/utils.py b/qiskit/result/mitigation/utils.py index f256ce480413..085ffebe0872 100644 --- a/qiskit/result/mitigation/utils.py +++ b/qiskit/result/mitigation/utils.py @@ -77,7 +77,7 @@ def str2diag(string): "1": np.array([0, 1], dtype=float), } ret = np.array([1], dtype=float) - for i in string: + for i in reversed(string): if i not in chars: raise QiskitError(f"Invalid diagonal string character {i}") ret = np.kron(chars[i], ret) diff --git a/test/python/result/test_mitigators.py b/test/python/result/test_mitigators.py index d294effc38a2..25e2671c13a6 100644 --- a/test/python/result/test_mitigators.py +++ b/test/python/result/test_mitigators.py @@ -15,23 +15,26 @@ import unittest from collections import Counter + import numpy as np -from qiskit import QiskitError -from qiskit import QuantumCircuit -from qiskit.test import QiskitTestCase + +from qiskit import QiskitError, QuantumCircuit from qiskit.quantum_info import Statevector -from qiskit.result import Counts -from qiskit.result import CorrelatedReadoutMitigator -from qiskit.result import LocalReadoutMitigator -from qiskit.result.utils import marginal_counts +from qiskit.quantum_info.operators.predicates import matrix_equal +from qiskit.result import ( + CorrelatedReadoutMitigator, + Counts, + LocalReadoutMitigator, +) from qiskit.result.mitigation.utils import ( counts_probability_vector, - str2diag, expval_with_stddev, stddev, + str2diag, ) +from qiskit.result.utils import marginal_counts +from qiskit.test import QiskitTestCase from qiskit.test.mock import FakeYorktown -from qiskit.quantum_info.operators.predicates import matrix_equal class TestReadoutMitigation(QiskitTestCase): @@ -431,6 +434,15 @@ def test_error_handling(self): "Assignment matrix columns must be valid probability distributions", ) + def test_expectation_value_endian(self): + """Test that endian for expval is little.""" + mitigators = self.mitigators(self.assignment_matrices()) + counts = Counts({"10": 3, "11": 24, "00": 74, "01": 923}) + for mitigator in mitigators: + expval, _ = mitigator.expectation_value(counts, diagonal="IZ", qubits=[0, 1]) + print(expval) + self.assertAlmostEqual(expval, -1.0, places=0) + class TestLocalReadoutMitigation(QiskitTestCase): """Tests specific to the local readout mitigator""" From 200cd936ac581a8b739165041e01b4a1089bb7c6 Mon Sep 17 00:00:00 2001 From: ikkoham Date: Fri, 25 Feb 2022 23:48:48 +0900 Subject: [PATCH 2/4] add a release note --- .../notes/fix-mitigator-endian-ead88499eb7e12ea.yaml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 releasenotes/notes/fix-mitigator-endian-ead88499eb7e12ea.yaml diff --git a/releasenotes/notes/fix-mitigator-endian-ead88499eb7e12ea.yaml b/releasenotes/notes/fix-mitigator-endian-ead88499eb7e12ea.yaml new file mode 100644 index 000000000000..b83199abdc36 --- /dev/null +++ b/releasenotes/notes/fix-mitigator-endian-ead88499eb7e12ea.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + Fixed the bug in :meth:`~qiskit.result.BaseReadoutMitigator.expectation_value` + where the endian was reversed when a string `diagonal` is passed. + It used to be interpreted as big endian, but now it will be interpreted as little endian, + which is the default for Qiskit. From 9a9248232f426afcb4c3a16c9d2bc3f4133fce76 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Fri, 25 Feb 2022 15:26:03 +0000 Subject: [PATCH 3/4] Reword release note --- .../notes/fix-mitigator-endian-ead88499eb7e12ea.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/releasenotes/notes/fix-mitigator-endian-ead88499eb7e12ea.yaml b/releasenotes/notes/fix-mitigator-endian-ead88499eb7e12ea.yaml index b83199abdc36..d2faa8bd691f 100644 --- a/releasenotes/notes/fix-mitigator-endian-ead88499eb7e12ea.yaml +++ b/releasenotes/notes/fix-mitigator-endian-ead88499eb7e12ea.yaml @@ -1,7 +1,7 @@ --- fixes: - | - Fixed the bug in :meth:`~qiskit.result.BaseReadoutMitigator.expectation_value` - where the endian was reversed when a string `diagonal` is passed. - It used to be interpreted as big endian, but now it will be interpreted as little endian, - which is the default for Qiskit. + Fixed an endianness bug in :meth:`.BaseReadoutMitigator.expectation_value` + when a string ``diagonal`` was passed. It will now correctly be interpreted + as little endian in the same manner as the rest of Qiskit Terra, instead of + big endian. From 6195f884516c361fb27259f8a3f72461bdcda320 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Fri, 25 Feb 2022 15:26:44 +0000 Subject: [PATCH 4/4] Remove debugging print --- test/python/result/test_mitigators.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/python/result/test_mitigators.py b/test/python/result/test_mitigators.py index 25e2671c13a6..d4f6ed04cfc6 100644 --- a/test/python/result/test_mitigators.py +++ b/test/python/result/test_mitigators.py @@ -440,7 +440,6 @@ def test_expectation_value_endian(self): counts = Counts({"10": 3, "11": 24, "00": 74, "01": 923}) for mitigator in mitigators: expval, _ = mitigator.expectation_value(counts, diagonal="IZ", qubits=[0, 1]) - print(expval) self.assertAlmostEqual(expval, -1.0, places=0)