qiskit icon indicating copy to clipboard operation
qiskit copied to clipboard

Make `DataBin` pickle-able

Open phasefactor opened this issue 1 year ago • 1 comments

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)

phasefactor avatar Jul 18 '24 16:07 phasefactor

It seems intentional. Maybe @ihincks has more context.

1ucian0 avatar Aug 07 '24 10:08 1ucian0

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)

samanthavbarron avatar Sep 24 '24 17:09 samanthavbarron