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 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions qiskit/primitives/containers/data_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ def __init__(self, *, shape: ShapeInput = (), **data):
def __len__(self):
return len(self._data)

def __setattr__(self, *_):
raise NotImplementedError

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ok to remove this? This __setattr__ is preventing the serialization of the job result

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you answer the question, @ihincks?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#12787 asked the same question.

def __repr__(self):
vals = [f"{name}={_value_repr(val)}" for name, val in self.items()]
if self.ndim:
Expand Down
35 changes: 24 additions & 11 deletions qiskit/primitives/primitive_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ class PrimitiveJob(BasePrimitiveJob[ResultT, JobStatus]):
Primitive job class for the reference implementations of Primitives.
"""

def __init__(self, function, *args, **kwargs):
def __init__(self, function=None, *args, **kwargs): # pylint: disable=keyword-arg-before-vararg
"""
Args:
function: A callable function to execute the job.
"""
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):
_ = 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
Loading