Skip to content

Commit

Permalink
Validate precision in estimator.run() (#1861)
Browse files Browse the repository at this point in the history
* add precision validation

* update message
  • Loading branch information
ptristan3 authored Aug 13, 2024
1 parent c9a0474 commit 7f09810
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions qiskit_ibm_runtime/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ def run(
Returns:
Submitted job.
Raises:
ValueError: if precision value is not strictly greater than 0.
"""
if precision is not None:
if precision <= 0:
raise ValueError("The precision value must be strictly greater than 0.")
coerced_pubs = [EstimatorPub.coerce(pub, precision) for pub in pubs]
validate_estimator_pubs(coerced_pubs)
return self._run(coerced_pubs) # type: ignore[arg-type]
Expand Down
11 changes: 11 additions & 0 deletions test/unit/test_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ def test_unsupported_values_for_estimator_options(self):
inst.options.update(**bad_opt)
self.assertIn(list(bad_opt.keys())[0], str(exc.exception))

def test_invalid_estimator_precision_option(self):
"""Test exception when precision is invalid."""

backend = get_mocked_backend()
backend.configuration().simulator = True

estimator = EstimatorV2(backend=backend)
with self.assertRaises(ValueError) as exc:
estimator.run(**get_primitive_inputs(estimator), precision=0)
self.assertIn("The precision value must be strictly greater than 0", str(exc.exception))

def test_pec_simulator(self):
"""Test error is raised when using pec on simulator without coupling map."""
backend = get_mocked_backend()
Expand Down

0 comments on commit 7f09810

Please sign in to comment.