Skip to content

Commit

Permalink
Merge pull request #843 from arcondello/feature/add-CQM-to-file-load
Browse files Browse the repository at this point in the history
Load CQMs saved as files with load(..) function
  • Loading branch information
arcondello authored Jun 11, 2021
2 parents 4fd10b0 + 46009f9 commit a18309d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions dimod/constrained.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,7 @@ def to_file(self, *, spool_size: int = int(1e9)) -> tempfile.SpooledTemporaryFil


CQM = ConstrainedQuadraticModel


# register fileview loader
load.register(CQM_MAGIC_PREFIX, ConstrainedQuadraticModel.from_file)
16 changes: 16 additions & 0 deletions tests/test_serialization_fileview.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,22 @@ def test_bqm(self):
bqm = BinaryQuadraticModel({'a': -1}, {'ab': 1}, 7, 'SPIN')
self.assertEqual(bqm, load(bqm.to_file()))

def test_cqm(self):
cqm = dimod.CQM()

bqm = BinaryQuadraticModel({'a': -1}, {'ab': 1}, 1.5, 'SPIN')
cqm.add_constraint(bqm, '<=')
cqm.add_constraint(bqm, '>=') # add it again

new = load(cqm.to_file())

self.assertEqual(cqm.objective, new.objective)
self.assertEqual(set(cqm.constraints), set(new.constraints))
for label, constraint in cqm.constraints.items():
self.assertEqual(constraint.lhs, new.constraints[label].lhs)
self.assertEqual(constraint.rhs, new.constraints[label].rhs)
self.assertEqual(constraint.sense, new.constraints[label].sense)

def test_dqm(self):
dqm = dimod.DiscreteQuadraticModel()
dqm.add_variable(5, 'a')
Expand Down

0 comments on commit a18309d

Please sign in to comment.