Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PrimitiveJob serializable #12963

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
3 changes: 0 additions & 3 deletions qiskit/primitives/containers/data_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ def __init__(self, *, shape: ShapeInput = (), **data):
def __len__(self):
return len(self._data)

def __setattr__(self, *_):
raise NotImplementedError

t-imamichi marked this conversation as resolved.
Show resolved Hide resolved
def __repr__(self):
vals = [f"{name}={_value_repr(val)}" for name, val in self.items()]
if self.ndim:
Expand Down
33 changes: 23 additions & 10 deletions qiskit/primitives/primitive_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def __init__(self, function, *args, **kwargs):
super().__init__(str(uuid.uuid4()))
self._future = None
self._function = function
self._result = None
self._status = None
self._args = args
self._kwargs = kwargs

Expand All @@ -46,19 +48,30 @@ def _submit(self):
self._future = executor.submit(self._function, *self._args, **self._kwargs)
executor.shutdown(wait=False)

def _prepare_dump(self): # This method allows PrimitiveJob to be serialized
kt474 marked this conversation as resolved.
Show resolved Hide resolved
_ = self.result()
_ = self.status()
self._future = None

def result(self) -> ResultT:
self._check_submitted()
return self._future.result()
if self._result is None:
self._check_submitted()
self._result = self._future.result()
return self._result

def status(self) -> JobStatus:
self._check_submitted()
if self._future.running():
return JobStatus.RUNNING
elif self._future.cancelled():
return JobStatus.CANCELLED
elif self._future.done() and self._future.exception() is None:
return JobStatus.DONE
return JobStatus.ERROR
if self._status is None:
self._check_submitted()
if self._future.running():
# we should not store status running because it is not completed
return JobStatus.RUNNING
elif self._future.cancelled():
self._status = JobStatus.CANCELLED
elif self._future.done() and self._future.exception() is None:
self._status = JobStatus.DONE
else:
self._status = JobStatus.ERROR
return self._status

def _check_submitted(self):
if self._future is None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
features_primitives:
- |
To make :class:`.PrimitiveJob` serializable, `qiskit.primitives.containers.DataBin` has been
kt474 marked this conversation as resolved.
Show resolved Hide resolved
updated to be pickleable. As a result, :class:`.PrimitiveResult` is now also pickleable.
Loading