dwavesystems / dwavesystems/dimod
Equality of CQM objects
- Dominant language
- Python
- Stars
- 143
- Forks
- 91
- Avg merge
- 1h 24m
- Merged PRs (30d)
- 3
Description
Intuitively one would expect the CQM.is_equal() and CQM.is_almost_equal() to mean that the models are logically/mathematically equal.
For example, the constraints b1+b2-1<=0 and b1+b2<=1 are equal (although not identical - in form).
But the methods will return false.
`
qm = QM()
qm.add_variable('BINARY', 'b1')
qm.add_variable('BINARY', 'b2')
qm.add_linear('b1', 1)
qm.add_linear('b2', 1)
cqm1 = CQM()
cqm1.add_constraint(qm <= 1, label='constraint')
cqm2 = CQM()
cqm2.add_constraint(qm - 1 <= 0, label='constraint')
b = cqm1.is_almost_equal(cqm2)
print(f'models are equal: {cqm1.is_almost_equal(cqm2)}')
print(f'models are equal: {cqm1.is_equal(cqm2)}')
`
Assuming that I am using the SDK correctly, the results are not what one would expect.
The Python doc stings also support the notion that the methods are testing for logical/mathematical equivalence.
- equal(): Return True if the given model has the same objective and constraints.
- is_almost_equal(): Return True if the given model's objective and constraints are almost equal.
I would suggest to update the code or the docs to make it clear what the methods do.
My preference would be to alter the code to return the intuitively expected result.
In the case when the documentation is updated/changed, it would be important to have a new equality method that checks for math equality/equivalence.
That is the functionality that the user cares about.
As a user I rely on the is_equal() and is_almost_equal() methods when I construct two CQM objects for the same problem using different styles to make sure that the two models are mathematically the same.
Contributor guide
Assessment
This issue has not been assessed yet.