Skip to content

Backend based primitives are not serializable via dill #9033

Closed
@adekusar-drl

Description

@adekusar-drl

Environment

  • Qiskit Terra version: 0.22
  • Python version: 3.7
  • Operating system: Any

What is happening?

Backend based primitives can't be saved via dill. Reference primitives can be loaded/saved. QuantumInstance is also serializable. This issue comes from Qiskit Machine Learning where quantum models can be saved to a file, then loaded to continue training or for inference on a real hardware.

How can we reproduce the issue?

Run a script

import dill
from qiskit import Aer
from qiskit.primitives import BackendSampler

sampler = BackendSampler(Aer.get_backend("aer_simulator"))
with open("sampler.dill", "wb") as f:
    dill.dump(sampler, f)

with open("sampler.dill", "rb") as f:
    sampler = dill.load(f)

What should happen?

An exception is raised:

python __dill_primitives.py

Traceback (most recent call last):
  File "__dill_primitives.py", line 10, in <module>
    sampler = dill.load(f)
  File ".../envs/dev-qml/lib/site-packages/dill/_dill.py", line 313, in load
    return Unpickler(file, ignore=ignore, **kwds).load()
  File ".../envs/dev-qml/lib/site-packages/dill/_dill.py", line 525, in load
    obj = StockUnpickler.load(self)
TypeError: __new__() missing 1 required positional argument: 'backend'

Any suggestions?

No exceptions must be raised.

Metadata

Metadata

Labels

bugSomething isn't workingmod: primitivesRelated to the Primitives module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions