Pickle issues with kwant.operator.Density
This code fails on different lines in Python 3.5 and Python 3.6
import kwant
import numpy as np
from cloudpickle import cloudpickle
syst = kwant.Builder()
lat = kwant.lattice.chain(norbs=1)
syst[lat(0)] = 0
op = kwant.operator.Density(syst.finalized())
op_dumped = cloudpickle.dumps(op)
op_loaded = cloudpickle.loads(op_dumped)
op_loaded.tocoo()
In Python 3.5 this code fails on the last line
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-15-a351eaa606b5> in <module>()
6 op_dumped = cloudpickle.dumps(op)
7 op_loaded = cloudpickle.loads(op_dumped)
----> 8 op_loaded.tocoo()
kwant/operator.pyx in kwant.operator.Density.tocoo (kwant/operator.c:16416)()
AttributeError: Memoryview is not initialized
In Python 3.6 it fails earlier
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-25-a351eaa606b5> in <module>()
4 syst[lat(0)] = 0
5 op = kwant.operator.Density(syst.finalized())
----> 6 op_dumped = cloudpickle.dumps(op)
7 op_loaded = cloudpickle.loads(op_dumped)
8 op_loaded.tocoo()
/home/basnijholt/anaconda3/lib/python3.6/site-packages/cloudpickle/cloudpickle.py in dumps(obj, protocol)
704
705 cp = CloudPickler(file,protocol)
--> 706 cp.dump(obj)
707
708 return file.getvalue()
/home/basnijholt/anaconda3/lib/python3.6/site-packages/cloudpickle/cloudpickle.py in dump(self, obj)
144 self.inject_addons()
145 try:
--> 146 return Pickler.dump(self, obj)
147 except RuntimeError as e:
148 if 'recursion' in e.args[0]:
/home/basnijholt/anaconda3/lib/python3.6/pickle.py in dump(self, obj)
407 if self.proto >= 4:
408 self.framer.start_framing()
--> 409 self.save(obj)
410 self.write(STOP)
411 self.framer.end_framing()
/home/basnijholt/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
494 reduce = getattr(obj, "__reduce_ex__", None)
495 if reduce is not None:
--> 496 rv = reduce(self.proto)
497 else:
498 reduce = getattr(obj, "__reduce__", None)
TypeError: can't pickle kwant.operator.Density objects