Skip to content

Commit

Permalink
Fix Backend primitive classes for BackendV1 with no max_experiments (#…
Browse files Browse the repository at this point in the history
…9069) (#9072)

* Fix Backend primitive classes for BackendV1 with no max_experiments

The ``max_experiments`` field in the BackendConfiguration for a BackendV1
backend is not a required field. While in practice most real backends
set it, some simulators (including aer) do not. This causes a failure
when using the Backend primitive classes with these backends as we were
previously assuming the ``max_experiments`` attribute was always
present.

* Update test/python/primitives/test_backend_estimator.py

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit b2d3dcf)

Co-authored-by: Matthew Treinish <[email protected]>
  • Loading branch information
mergify[bot] and mtreinish authored Nov 3, 2022
1 parent cc8aadf commit bece288
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion qiskit/primitives/backend_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _run_circuits(
metadata.append(circ.metadata)
circ.metadata = {}
if isinstance(backend, BackendV1):
max_circuits = backend.configuration().max_experiments
max_circuits = getattr(backend.configuration(), "max_experiments", None)
elif isinstance(backend, BackendV2):
max_circuits = backend.max_circuits
if max_circuits:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
Fixed an issue with the backend primitive classes :class:`~.BackendSampler`
and :class:`~.BackendEstimator` which prevented running with a
:class:`~.BackendV1` instance that does not have a ``max_experiments``
field set in its :class:`~.BackendConfiguration`.
25 changes: 25 additions & 0 deletions test/python/primitives/test_backend_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,31 @@ def test_job_size_limit_v1(self):
estimator.run([qc] * k, [op] * k, params_list).result()
self.assertEqual(run_mock.call_count, 10)

def test_no_max_circuits(self):
"""Test BackendEstimator works with BackendV1 and no max_experiments set."""
backend = FakeNairobi()
config = backend.configuration()
del config.max_experiments
backend._configuration = config
backend.set_options(seed_simulator=123)
qc = RealAmplitudes(num_qubits=2, reps=2)
op = SparsePauliOp.from_list([("IZ", 1), ("XI", 2), ("ZY", -1)])
k = 5
params_array = np.random.rand(k, qc.num_parameters)
params_list = params_array.tolist()
params_list_array = list(params_array)
estimator = BackendEstimator(backend=backend)
target = estimator.run([qc] * k, [op] * k, params_list).result()
with self.subTest("ndarrary"):
result = estimator.run([qc] * k, [op] * k, params_array).result()
self.assertEqual(len(result.metadata), k)
np.testing.assert_allclose(result.values, target.values, rtol=0.2, atol=0.2)

with self.subTest("list of ndarray"):
result = estimator.run([qc] * k, [op] * k, params_list_array).result()
self.assertEqual(len(result.metadata), k)
np.testing.assert_allclose(result.values, target.values, rtol=0.2, atol=0.2)


if __name__ == "__main__":
unittest.main()

0 comments on commit bece288

Please sign in to comment.