Repository: dwavesystems/dimod

Issue:
deepcopy(bqm) fails for immutable BQMs
```
$ python
Python 3.6.4 (default, May 29 2018, 20:56:16) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dimod, copy
>>> bqm = dimod.BQM.from_ising({'a': 1}, {})
>>> copy.copy(bqm)
BinaryQuadraticModel({'a': 1}, {}, 0.0, Vartype.SPIN)
>>> copy.deepcopy(bqm)
BinaryQuadraticModel({'a': 1}, {}, 0.0, Vartype.SPIN)
>>> 
>>> bqm.is_writeable = False
>>> copy.copy(bqm)
BinaryQuadraticModel({'a': 1}, {}, 0.0, Vartype.SPIN)
>>> copy.deepcopy(bqm)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/tmp.FevbTVueow/lib/python3.6/copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/tmp/tmp.FevbTVueow/lib/python3.6/copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)
  File "/tmp/tmp.FevbTVueow/lib/python3.6/copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "/tmp/tmp.FevbTVueow/lib/python3.6/copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/tmp/tmp.FevbTVueow/lib/python3.6/copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/tmp/tmp.FevbTVueow/lib/python3.6/copy.py", line 306, in _reconstruct
    value = deepcopy(value, memo)
  File "/tmp/tmp.FevbTVueow/lib/python3.6/copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/tmp/tmp.FevbTVueow/lib/python3.6/copy.py", line 307, in _reconstruct
    y[key] = value
  File "/tmp/tmp.FevbTVueow/lib/python3.6/site-packages/dimod/decorators.py", line 367, in _check_writeable
    raise WriteableError
dimod.exceptions.WriteableError
>>> 
>>> dimod.__version__
'0.8.15'
```

Discussion:

Patch:
diff --git a/dimod/binary_quadratic_model.py b/dimod/binary_quadratic_model.py
index 63ba7614..161b664b 100644
--- a/dimod/binary_quadratic_model.py
+++ b/dimod/binary_quadratic_model.py
@@ -1458,7 +1458,13 @@ class BinaryQuadraticModel(abc.Sized, abc.Container, abc.Iterable):
 
         """
         # new objects are constructed for each, so we just need to pass them in
-        return BinaryQuadraticModel(self.linear, self.quadratic, self.offset, self.vartype, **self.info)
+        new = BinaryQuadraticModel(self.linear,
+                                   self.quadratic,
+                                   self.offset,
+                                   self.vartype,
+                                   **self.info)
+        new.is_writeable = self.is_writeable
+        return new
 
     def energy(self, sample):
         """Determine the energy of the specified sample of a binary quadratic model.
diff --git a/dimod/utilities.py b/dimod/utilities.py
index 54255469..909ce7be 100644
--- a/dimod/utilities.py
+++ b/dimod/utilities.py
@@ -16,6 +16,7 @@
 """
 Utility functions useful for samplers.
 """
+import copy
 import itertools
 
 from six import iteritems, itervalues
@@ -428,6 +429,14 @@ class LockableDict(dict):
     def __delitem__(self, key):
         super(LockableDict, self).__delitem__(key)
 
+    def __deepcopy__(self, memo):
+        new = type(self)()
+        memo[id(self)] = new
+        new.update((copy.deepcopy(key, memo), copy.deepcopy(value, memo))
+                   for key, value in self.items())
+        new.is_writeable = self.is_writeable
+        return new
+
     @lockable_method
     def clear(self):
         super(LockableDict, self).clear()


Test patch:
diff --git a/tests/test_binary_quadratic_model.py b/tests/test_binary_quadratic_model.py
index 7e284793..1874db5a 100644
--- a/tests/test_binary_quadratic_model.py
+++ b/tests/test_binary_quadratic_model.py
@@ -14,6 +14,7 @@
 #
 # ================================================================================================
 
+import copy
 import collections
 import fractions
 import itertools
@@ -2406,3 +2407,29 @@ class TestIsWriteable(unittest.TestCase):
 
         with self.assertRaises(WriteableError):
             bqm.adj['a']['b'] += 5
+
+    def test_copy(self):
+        bqm = dimod.BinaryQuadraticModel.from_ising({}, {'ab': -1})
+        bqm.is_writeable = False
+
+        new = bqm.copy()
+        self.assertEqual(new, bqm)
+        self.assertIsNot(new, bqm)
+        self.assertFalse(new.is_writeable)
+
+    def test_deepcopy(self):
+        bqm = dimod.BinaryQuadraticModel.from_ising({}, {'ab': -1})
+        bqm.is_writeable = False
+
+        new = copy.deepcopy(bqm)
+        self.assertEqual(new, bqm)
+        self.assertIsNot(new, bqm)
+        self.assertFalse(new.is_writeable)
+
+    def test_pickle(self):
+        bqm = dimod.BinaryQuadraticModel.from_ising({}, {'ab': -1})
+        bqm.is_writeable = False
+
+        new = pickle.loads(pickle.dumps(bqm))
+        self.assertEqual(new, bqm)
+        self.assertFalse(new.is_writeable)