Skip to content

Commit

Permalink
Make BackendSampler set shots and stddev_upper_bound of returned Quas…
Browse files Browse the repository at this point in the history
…iDistribution (#9312)

* make BackendSampler pass shots to QuasiDistribution

* also set stddev_upper_bound

* update release note

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
kevinsung and mergify[bot] authored Jan 18, 2023
1 parent 0257c95 commit 72f0f52
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
9 changes: 6 additions & 3 deletions qiskit/primitives/backend_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from __future__ import annotations

import math
from collections.abc import Sequence
from typing import Any, cast

Expand Down Expand Up @@ -162,14 +163,16 @@ def _postprocessing(self, result: Result, circuits: list[QuantumCircuit]) -> Sam
counts = _prepare_counts(result)
shots = sum(counts[0].values())

probabilies = []
probabilities = []
metadata: list[dict[str, Any]] = [{}] * len(circuits)
for count in counts:
prob_dist = {k: v / shots for k, v in count.int_outcomes().items()}
probabilies.append(QuasiDistribution(prob_dist))
probabilities.append(
QuasiDistribution(prob_dist, shots=shots, stddev_upper_bound=math.sqrt(1 / shots))
)
for metadatum in metadata:
metadatum["shots"] = shots
return SamplerResult(probabilies, metadata)
return SamplerResult(probabilities, metadata)

def _transpile(self):
from qiskit.compiler import transpile
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
features:
- |
:class:`~.BackendSampler` now sets the ``shots``
and ``stddev_upper_bound`` attributes of the returned :class:`~.QuasiDistribution`.
Previously it did not.
6 changes: 4 additions & 2 deletions test/python/primitives/test_backend_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

"""Tests for BackendSampler."""

import math
import unittest
from test import combine

Expand Down Expand Up @@ -96,11 +97,12 @@ def test_sampler_run(self, backend):
"""Test Sampler.run()."""
bell = self._circuit[1]
sampler = BackendSampler(backend=backend)
job = sampler.run(circuits=[bell])
job = sampler.run(circuits=[bell], shots=1000)
self.assertIsInstance(job, JobV1)
result = job.result()
self.assertIsInstance(result, SamplerResult)
# print([q.binary_probabilities() for q in result.quasi_dists])
self.assertEqual(result.quasi_dists[0].shots, 1000)
self.assertEqual(result.quasi_dists[0].stddev_upper_bound, math.sqrt(1 / 1000))
self._compare_probs(result.quasi_dists, self._target[1])

@combine(backend=BACKENDS)
Expand Down

0 comments on commit 72f0f52

Please sign in to comment.