Skip to content

Commit

Permalink
Support default_shots in EstimatorV2 local mode (#1773)
Browse files Browse the repository at this point in the history
* Support default_shots

* Add release note

* Add unit test
  • Loading branch information
kt474 authored Jun 27, 2024
1 parent 17749b8 commit b01a1b6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions qiskit_ibm_runtime/fake_provider/local_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from __future__ import annotations

import math
import copy
import logging
import warnings
Expand Down Expand Up @@ -326,6 +327,8 @@ def _run_backend_primitive_v2(
prim_options["default_shots"] = default_shots
primitive_inst = BackendSamplerV2(backend=backend, options=prim_options)
else:
if default_shots := options_copy.pop("default_shots", None):
inputs["precision"] = 1 / math.sqrt(default_shots)
if default_precision := options_copy.pop("default_precision", None):
prim_options["default_precision"] = default_precision
primitive_inst = BackendEstimatorV2(backend=backend, options=prim_options)
Expand Down
2 changes: 2 additions & 0 deletions release-notes/unreleased/1773.feat.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``default_shots`` are now a supported option when using ``EstimatorV2`` in
local testing mode.
11 changes: 10 additions & 1 deletion test/unit/test_local_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,23 @@ def test_v2_sampler_with_accepted_options(self, backend):

@data(FakeManila(), FakeManilaV2(), AerSimulator.from_backend(FakeManila()))
def test_v2_estimator_with_accepted_options(self, backend):
"""Test V1 estimator with accepted options."""
"""Test V2 estimator with accepted options."""
options = {"default_precision": 0.03125, "simulator": {"seed_simulator": 42}}
inst = EstimatorV2(backend=backend, options=options)
job = inst.run(**get_primitive_inputs(inst, backend=backend))
pub_result = job.result()[0]
self.assertDictEqual(pub_result.metadata, {"target_precision": 0.03125})
self.assertEqual(pub_result.data.evs[0], 0.056640625)

@data(FakeManila(), FakeManilaV2(), AerSimulator.from_backend(FakeManila()))
def test_v2_estimator_with_default_shots_option(self, backend):
"""Test V2 estimator with default shots converted to precision."""
options = {"default_shots": 100}
inst = EstimatorV2(backend=backend, options=options)
job = inst.run(**get_primitive_inputs(inst, backend=backend))
pub_result = job.result()[0]
self.assertDictEqual(pub_result.metadata, {"target_precision": 0.1})

@combine(
primitive=[SamplerV2, EstimatorV2], backend=[FakeManila(), FakeManilaV2(), AerSimulator()]
)
Expand Down

0 comments on commit b01a1b6

Please sign in to comment.