From f03f3d91b2f982543ae69c4d48ef53867afa65fe Mon Sep 17 00:00:00 2001 From: Julien Gacon Date: Tue, 7 Mar 2023 13:15:22 +0100 Subject: [PATCH] Fix bitstring padding in the `BackendSampler` (#9744) * fix padding * add reno (cherry picked from commit 255337662847f9f22982172c186ff98426a6b626) --- qiskit/primitives/backend_sampler.py | 2 +- ...ckendsampler-padding-ed959e6dc3deb3f3.yaml | 7 +++++++ .../python/primitives/test_backend_sampler.py | 21 +++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/fix-backendsampler-padding-ed959e6dc3deb3f3.yaml diff --git a/qiskit/primitives/backend_sampler.py b/qiskit/primitives/backend_sampler.py index 6059ded4f8c6..c2b2a1cd0d75 100644 --- a/qiskit/primitives/backend_sampler.py +++ b/qiskit/primitives/backend_sampler.py @@ -165,7 +165,7 @@ def _postprocessing(self, result: Result, circuits: list[QuantumCircuit]) -> Sam probabilities = [] metadata: list[dict[str, Any]] = [{} for _ in range(len(circuits))] for count in counts: - prob_dist = {k: v / shots for k, v in count.int_outcomes().items()} + prob_dist = {k: v / shots for k, v in count.items()} probabilities.append( QuasiDistribution(prob_dist, shots=shots, stddev_upper_bound=math.sqrt(1 / shots)) ) diff --git a/releasenotes/notes/fix-backendsampler-padding-ed959e6dc3deb3f3.yaml b/releasenotes/notes/fix-backendsampler-padding-ed959e6dc3deb3f3.yaml new file mode 100644 index 000000000000..5d15745e930e --- /dev/null +++ b/releasenotes/notes/fix-backendsampler-padding-ed959e6dc3deb3f3.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + Fixed a bug in the :class:`.BackendSampler` where the binary probability bitstrings + were truncated to the minimal number of bits required to represent the largest outcome + as integer. That means that if e.g. ``{"0001": 1.0}`` was measured, the result was truncated + to ``{"1": 1.0}``. diff --git a/test/python/primitives/test_backend_sampler.py b/test/python/primitives/test_backend_sampler.py index 6f5f52b6babb..8ec32e579c35 100644 --- a/test/python/primitives/test_backend_sampler.py +++ b/test/python/primitives/test_backend_sampler.py @@ -26,6 +26,7 @@ from qiskit.primitives import BackendSampler, SamplerResult from qiskit.providers import JobStatus, JobV1 from qiskit.providers.fake_provider import FakeNairobi, FakeNairobiV2 +from qiskit.providers.basicaer import QasmSimulatorPy from qiskit.test import QiskitTestCase from qiskit.transpiler import PassManager from qiskit.utils import optionals @@ -374,6 +375,26 @@ def test_sequential_run(self): self.assertDictAlmostEqual(result3.quasi_dists[0], {0: 1}, 0.1) self.assertDictAlmostEqual(result3.quasi_dists[1], {1: 1}, 0.1) + def test_outcome_bitstring_size(self): + """Test that the result bitstrings are properly padded. + + E.g. measuring '0001' should not get truncated to '1'. + """ + qc = QuantumCircuit(4) + qc.x(0) + qc.measure_all() + + # We need a noise-free backend here (shot noise is fine) to ensure that + # the only bit string measured is "0001". With device noise, it could happen that + # strings with a leading 1 are measured and then the truncation cannot be tested. + sampler = BackendSampler(backend=QasmSimulatorPy()) + + result = sampler.run(qc).result() + probs = result.quasi_dists[0].binary_probabilities() + + self.assertIn("0001", probs.keys()) + self.assertEqual(len(probs), 1) + def test_bound_pass_manager(self): """Test bound pass manager."""