Make `DataBin` pickle-able
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)
It seems intentional. Maybe @ihincks has more context.
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)