Skip to content

Commit

Permalink
Fix serialization of primitives (#9076) (#9152)
Browse files Browse the repository at this point in the history
* fix serialization of primitives

* Update releasenotes/notes/fix-serialization-primitives-c1e44a37cfe7a32a.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* rm tests

* Update releasenotes/notes/fix-serialization-primitives-c1e44a37cfe7a32a.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
(cherry picked from commit a4a6be0)

Co-authored-by: Ikko Hamamura <ikkoham@users.noreply.github.com>
  • Loading branch information
mergify[bot] and ikkoham authored Nov 17, 2022
1 parent 620927e commit 775ac1e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions qiskit/primitives/backend_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
5 changes: 4 additions & 1 deletion qiskit/primitives/backend_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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]:
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -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``.
2 changes: 1 addition & 1 deletion test/python/primitives/test_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 775ac1e

Please sign in to comment.