Skip to content

Commit

Permalink
Merge pull request #931 from arcondello/fix/deepcopy-cqm
Browse files Browse the repository at this point in the history
Make `dimod.INTEGER` correctly deepcopy-able
  • Loading branch information
arcondello authored Jul 29, 2021
2 parents 9a8fdd0 + 88e0d82 commit 358f0d0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions dimod/vartypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def __contains__(self, item):
except TypeError:
return False

def __deepcopy__(self, memo):
memo[id(self)] = self
return self


integers = Integers()

Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/fix-INTEGER-deepcopy-a0dbef52bfe3a0a7.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Make ``dimod.INTEGER`` correctly deepcopy-able.
19 changes: 19 additions & 0 deletions tests/test_constrained.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,25 @@ def test_later_defn(self):
cqm.add_variable('i', 'INTEGER')


class TestCopy(unittest.TestCase):
def test_deepcopy(self):
from copy import deepcopy

cqm = CQM()

x = Binary('x')
s = Spin('s')
i = Integer('i')

cqm.set_objective(i + s + x)
constraint = cqm.add_constraint(i + s + x <= 1)

new = deepcopy(cqm)

self.assertTrue(new.objective.is_equal(cqm.objective))
self.assertTrue(new.constraints[constraint].lhs.is_equal(cqm.constraints[constraint].lhs))


class TestSerialization(unittest.TestCase):
def test_functional(self):
cqm = CQM()
Expand Down
26 changes: 26 additions & 0 deletions tests/test_vartypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2021 D-Wave Systems Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import dimod


class TestCopy(unittest.TestCase):
def test_integer(self):
from copy import deepcopy

self.assertIs(dimod.BINARY, deepcopy(dimod.BINARY))
self.assertIs(dimod.INTEGER, deepcopy(dimod.INTEGER))
self.assertIs(dimod.SPIN, deepcopy(dimod.SPIN))

0 comments on commit 358f0d0

Please sign in to comment.