diff --git a/dimod/constrained.py b/dimod/constrained.py index abd8a2716..ce04a8843 100644 --- a/dimod/constrained.py +++ b/dimod/constrained.py @@ -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) diff --git a/tests/test_serialization_fileview.py b/tests/test_serialization_fileview.py index 3e4408fbd..1b10de597 100644 --- a/tests/test_serialization_fileview.py +++ b/tests/test_serialization_fileview.py @@ -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')