diff --git a/qiskit/primitives/backend_estimator.py b/qiskit/primitives/backend_estimator.py index 977a3b478855..c2e9a424c6f7 100644 --- a/qiskit/primitives/backend_estimator.py +++ b/qiskit/primitives/backend_estimator.py @@ -147,6 +147,9 @@ def __new__( # pylint: disable=signature-differs self = super().__new__(cls) return self + def __getnewargs__(self): + return (self._backend,) + @property def transpile_options(self) -> Options: """Return the transpiler options for transpiling the circuits.""" diff --git a/qiskit/primitives/backend_sampler.py b/qiskit/primitives/backend_sampler.py index a8e62898dbb3..73be4200510e 100644 --- a/qiskit/primitives/backend_sampler.py +++ b/qiskit/primitives/backend_sampler.py @@ -23,10 +23,10 @@ from qiskit.result import QuasiDistribution, Result from qiskit.transpiler.passmanager import PassManager +from .backend_estimator import _prepare_counts, _run_circuits from .base import BaseSampler, SamplerResult from .primitive_job import PrimitiveJob from .utils import _circuit_key -from .backend_estimator import _run_circuits, _prepare_counts class BackendSampler(BaseSampler): @@ -83,6 +83,9 @@ def __new__( # pylint: disable=signature-differs self = super().__new__(cls) return self + def __getnewargs__(self): + return (self._backend,) + @property def preprocessed_circuits(self) -> list[QuantumCircuit]: """ diff --git a/releasenotes/notes/fix-serialization-primitives-c1e44a37cfe7a32a.yaml b/releasenotes/notes/fix-serialization-primitives-c1e44a37cfe7a32a.yaml new file mode 100644 index 000000000000..e3f7d78f5cbc --- /dev/null +++ b/releasenotes/notes/fix-serialization-primitives-c1e44a37cfe7a32a.yaml @@ -0,0 +1,8 @@ +--- +fixes: + - | + Fixed an issue with the primitive classes, :class:`~.BackendSampler` and :class:`~.BackendEstimator`, + where instances were not able to be serialized with ``pickle``. In general these classes are not guaranteed + to be serializable as :class:`~.BackendV2` and :class:`~.BackendV1` instances are not required to be + serializable (and often are not), but the class definitions of :class:`~.BackendSampler` and + :class:`~.BackendEstimator` no longer prevent the use of ``pickle``. diff --git a/test/python/primitives/test_estimator.py b/test/python/primitives/test_estimator.py index 0c56a6c1f7ac..34549ba3854a 100644 --- a/test/python/primitives/test_estimator.py +++ b/test/python/primitives/test_estimator.py @@ -13,9 +13,9 @@ """Tests for Estimator.""" import unittest -from ddt import ddt, data, unpack import numpy as np +from ddt import data, ddt, unpack from qiskit.circuit import Parameter, QuantumCircuit from qiskit.circuit.library import RealAmplitudes