Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.26k stars 2.37k forks source link

Make `DataBin` pickle-able #12787

Open phasefactor opened 4 months ago

phasefactor commented 4 months ago

What should we add?

Currently when using pickle with PrimitiveResult or PubResult the DataBin class will throw a NotImplementedError from __setattr__() when you try to pickle.load().

The change needed is trivial, so I was not sure if this omission was originally made on purpose...

However, I think being able to save results locally instead of relying on referencing them by job id would be a benefit.

There is some value in not requiring network connectivity and making them easily shareable/archive-able.

The necessary change to databin.py: 96 is :

    def __setattr__(self, name, value):
        super().__setattr__(name, value)
1ucian0 commented 3 months ago

It seems intentional. Maybe @ihincks has more context.

samanthavbarron commented 1 month ago

I also came here to suggest this. IIRC the decision has something to with restricting the slots in resulting objects.

I would personally benefit a lot from this because it consequently prevents PrimitiveResult objects from being pickled as well.

import pickle
from qiskit.primitives.containers import DataBin

filename = "tmp.pkl"
with open(filename, "wb") as f:
    pickle.dump(DataBin(x=[1]), f)

with open(filename, "rb") as f:
    pickle.load(f)