Skip to content

Implement __getstate__ and __setstate__ #1

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

Merged
merged 1 commit into from
Mar 27, 2025
Merged
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
9 changes: 7 additions & 2 deletions qiskit/primitives/primitive_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ 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"""
def __getstate__(self):
_ = self.result()
_ = self.status()
state = self.__dict__.copy()
state["_future"] = None
return state

def __setstate__(self, state):
self.__dict__.update(state)
self._future = None

def result(self) -> ResultT:
Expand Down
51 changes: 51 additions & 0 deletions test/python/primitives/test_primitive_job.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2025.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Tests for PrimitiveJob."""

import pickle
from test import QiskitTestCase

import numpy as np
from ddt import data, ddt

from qiskit import QuantumCircuit
from qiskit.primitives import PrimitiveJob, StatevectorSampler


@ddt
class TestPrimitiveJob(QiskitTestCase):
"""Tests PrimitiveJob."""

@data(1, 2, 3)
def test_serialize(self, size):
"""Test serialize."""
n = 2
qc = QuantumCircuit(n)
qc.h(range(n))
qc.measure_all()
sampler = StatevectorSampler()
job = sampler.run([qc] * size)
obj = pickle.dumps(job)
job2 = pickle.loads(obj)
self.assertIsInstance(job2, PrimitiveJob)
self.assertEqual(job.job_id(), job2.job_id())
self.assertEqual(job.status(), job2.status())
self.assertEqual(job.metadata, job2.metadata)
result = job.result()
result2 = job2.result()
self.assertEqual(result.metadata, result2.metadata)
self.assertEqual(len(result), len(result2))
for i in range(len(result)):
self.assertEqual(result[i].metadata, result2[i].metadata)
self.assertEqual(result[i].data.keys(), result2[i].data.keys())
np.testing.assert_allclose(result[i].join_data().array, result2[i].join_data().array)