dwavesystems / dimod

A shared API for QUBO/Ising samplers.
https://docs.ocean.dwavesys.com/en/stable/docs_dimod/
Apache License 2.0
122 stars 81 forks source link

Implement arithmetic operators for BinaryPolynomial #1215

Open gilirosenberg12 opened 2 years ago

gilirosenberg12 commented 2 years ago

Application It's frustrating that the higher order polynomial class (BinaryPolynomial), does not support basic arithmetic operators. It's also inconsistent with BinaryQuadraticModel, which does support such operators. This limits the usability of this class, since one cannot manipulate its objects in a convenient way.

Proposed Solution dimod should support, at the minimum, the operators that BinaryQuadraticModel supports (see here). In particular: addition, subtraction, multiplication (at least by a scalar!), and division (by a scalar).

(Take a look at qubovert, where this is all implemented...)

Additional Context Here's a minimal reproducer. First, this works fine for BinaryQuadraticModel:

bqm = dimod.BinaryQuadraticModel({0: -1, 1: 1}, {(0, 1): 2}, 0.0, dimod.BINARY)
bqm-bqm

and fails for a BinaryPolynomial:

poly = BinaryPolynomial({(1,): -1, (1,2): .5, (1,2,3): .5}, dimod.SPIN)
poly-poly

By giving the following error:

TypeError: unsupported operand type(s) for -: 'BinaryPolynomial' and 'BinaryPolynomial'
arcondello commented 2 years ago

Duplicate of https://github.com/dwavesystems/dimod/issues/988

gilirosenberg12 commented 2 years ago

@arcondello Thanks for pointing that out, I didn't see the other ticket previously. Perhaps it's still useful to see multiple requests for the same feature? Feel free to close it if that makes sense. I'll just add that at the moment the usability of BinaryPolynomial is severely impacted by the lack of this feature, from my point of view.

arcondello commented 2 years ago

Updating my comment from https://github.com/dwavesystems/dimod/issues/988 and incorporating the feedback in https://github.com/dwavesystems/dimod/issues/1217.

Adding the ability to symbolically manipulate higher order binary models is not especially difficult from an implementation perspective. However, I would like that to come with a more expansive overhaul of higher order models in dimod. One that likely incorporates https://github.com/dwavesystems/dimod/issues/1216, https://github.com/dwavesystems/dimod/issues/1042, https://github.com/dwavesystems/dimod/issues/1218, and https://github.com/dwavesystems/dimod/issues/1219.

In addition to the linked issues above, there is an issue of performance. Right now, aside from HigherOrderComposite, the Ocean ecosystem does not support solving higher order models natively. The performance of models translated into quadratic models via make_quadratic() is mixed for naive models/transformations (e.g. https://github.com/dwavesystems/dimod/issues/754, https://github.com/dwavesystems/dimod/issues/550). I am a bit hesitant to prioritize this work when we don't have native higher order quantum/hybrid/classical solvers.

That said, I do think it's worth tracking.

arcondello commented 2 years ago

For this specific issue, @gilirosenberg12, it would be helpful to understand what sort of models you're creating that need this feature. What sort of density of terms? How many variables/terms? What is the maximum degree? That sort of thing.

That would help motivate and aid in the design for this and the other feature requests.

gilirosenberg12 commented 2 years ago

The density is low, the max degree is 6 or more (but not much more), and there could be hundreds or thousands of variables.

I am a bit hesitant to prioritize this work when we don't have native higher order quantum/hybrid/classical solvers.

Although it would be really nice to have native higher order solvers, that's not strictly necessary - as you know, higher order problem can be reduced to quadratic, and solved with the existing solvers.