dwavesystems / dwavebinarycsp

Map constraint satisfaction problems with binary variables to binary quadratic models.
https://docs.ocean.dwavesys.com/projects/binarycsp/en/latest
Apache License 2.0
21 stars 28 forks source link

Non-zero minimum energy in BQM returned by stitch function #86

Open conquistador1492 opened 5 years ago

conquistador1492 commented 5 years ago

stitch function returns BQM with non-zero energy of ground state for constraint with 1 or 2 variables. In 3 and more variables cases, everything is fine(below I give examples). I'm not sure it's a bug, but zero energy might be useful if somebody combines many different constraints. Then, they may be assured that the final state has zero energy.

csp0 = ConstraintSatisfactionProblem(BINARY)
csp0.add_constraint(lambda a: a, ['a'])

csp1 = ConstraintSatisfactionProblem(BINARY)
csp1.add_constraint(lambda a, b: a == b, ['a', 'b'])

csp2 = ConstraintSatisfactionProblem(BINARY)
csp2.add_constraint(lambda a, b: a & b, ['a', 'b'])

csp3 = ConstraintSatisfactionProblem(BINARY)
csp3.add_constraint(lambda a, b, c: a == b == c, ['a', 'b', 'c'])

csp4 = ConstraintSatisfactionProblem(BINARY)
csp4.add_constraint(lambda a, b, c: a == b == c, ['a', 'b', 'c'])

csp5 = ConstraintSatisfactionProblem(BINARY)
csp5.add_constraint(lambda a, b, c, d: (a & b) & c | d, ['a', 'b', 'c', 'd'])

for csp in [csp0, csp1, csp2, csp3, csp4, csp5]:
    bqm = stitch(csp)
    response = SimulatedAnnealingSampler().sample(bqm, num_reads=100)
    print(sorted(response.data(), key=lambda x: x.energy)[0].energy)

As a result, we get:

-1.0
-1.0
-2.0
0.0
0.0
-2.0000000011677344e-07

Environment

m3ller commented 5 years ago

stitch(..) calls on dwavesystems/penaltymodel repo to help build the bqm. The penaltymodel is interested in making the gap between excited states and ground states as large as possible, but currently, it does not make the effort to shift the bqm so that the ground is always at 0.

Hence, this issue is not a bug, but it would be a great candidate for code enhancement.