From 928c2de24e6637b1b395fbfd31388ba694304647 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Thu, 3 Nov 2022 08:54:11 -0400 Subject: [PATCH 1/2] 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. --- qiskit/primitives/backend_estimator.py | 2 +- ...e-no-max-experiments-e2ca41ec61de353e.yaml | 7 +++++ .../primitives/test_backend_estimator.py | 26 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/fix-backend-primitive-no-max-experiments-e2ca41ec61de353e.yaml diff --git a/qiskit/primitives/backend_estimator.py b/qiskit/primitives/backend_estimator.py index 11fc1fe538e4..977a3b478855 100644 --- a/qiskit/primitives/backend_estimator.py +++ b/qiskit/primitives/backend_estimator.py @@ -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: diff --git a/releasenotes/notes/fix-backend-primitive-no-max-experiments-e2ca41ec61de353e.yaml b/releasenotes/notes/fix-backend-primitive-no-max-experiments-e2ca41ec61de353e.yaml new file mode 100644 index 000000000000..5d48ef70b378 --- /dev/null +++ b/releasenotes/notes/fix-backend-primitive-no-max-experiments-e2ca41ec61de353e.yaml @@ -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`. diff --git a/test/python/primitives/test_backend_estimator.py b/test/python/primitives/test_backend_estimator.py index 8826336670ba..751c9c2f728a 100644 --- a/test/python/primitives/test_backend_estimator.py +++ b/test/python/primitives/test_backend_estimator.py @@ -25,6 +25,7 @@ from qiskit.providers.fake_provider import FakeNairobi, FakeNairobiV2 from qiskit.quantum_info import SparsePauliOp from qiskit.test import QiskitTestCase +from qiskit.utils.optionals import HAS_AER BACKENDS = [FakeNairobi(), FakeNairobiV2()] @@ -297,6 +298,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() From 050d1ba38e2bb2632d4d6fc7afe050a8c0ec125f Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Thu, 3 Nov 2022 09:53:03 -0400 Subject: [PATCH 2/2] Update test/python/primitives/test_backend_estimator.py --- test/python/primitives/test_backend_estimator.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/python/primitives/test_backend_estimator.py b/test/python/primitives/test_backend_estimator.py index 751c9c2f728a..2fcdd6212288 100644 --- a/test/python/primitives/test_backend_estimator.py +++ b/test/python/primitives/test_backend_estimator.py @@ -25,7 +25,6 @@ from qiskit.providers.fake_provider import FakeNairobi, FakeNairobiV2 from qiskit.quantum_info import SparsePauliOp from qiskit.test import QiskitTestCase -from qiskit.utils.optionals import HAS_AER BACKENDS = [FakeNairobi(), FakeNairobiV2()]