New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add absolute value and power operator for the BinaryQuadraticModel #1189
Comments
Hi @angystallone, I don't have the full context of your code, but it looks like you're trying to minimize the distance between a bqm and single number. In that case, generally the best way to do it would be something like For the feature request, it is often best to think of bqms as polynomials rather than as matrices. If you do that, you'll see why we have not implemented these methods for binary quadratic models in general. Absolute Value Taking a simple linear bqm, def __abs__(self) -> BinaryQuadraticModel:
for v, bias in self.iter_linear():
self.set_linear(v, abs(bias)
for u, v, bias in self.iter_quadratic():
self.set_quadratic(u, v, abs(bias))
self.offset = abs(self.offset) Let's call the output of this new function
Square Root Similarly, we could implement a square root function. Let's try to take the square root of The single variable case def __abs__(self) -> BinaryQuadraticModel:
if self.num_variables > 1:
raise TypeError("cannot take the absolute value of bqms with more than one variable")
for v, bias in self.iter_linear():
self.set_linear(v, abs(bias)
self.offset = abs(self.offset) but that seems like a very specific edge case, and to the point that it would almost be misleading |
Thanks for the quick and clear answer @arcondello. I see your point about matrices and polynomials. Indeed, The absolute value of More on the problem context: it falls into the category of knapsack problems and in the classical approach the linear expression has been coded by the “model” python module in |
Hi @arcondello, I have an update from my side. Even switching to L2-norm (so substituting |
Hi @angystallone , would you be able to share your code? Or, even better, a minimal reproducible example? |
@arcondello Here are the minimal examples for reproducing both the abs value (for L1-norm) and the square (for L2-norm) issues:
In this second case, the problem could be due to the size of
Thanks a lot! |
Currently, only multiplication, addition, subtraction and division of
BinaryQuadraticModel
by a number are allowed. However, to build a more complex objective function, additional operations are needed. Among them, the absolute value is essential for the definition of the objective as the L1-norm, as in the following example:In the code above, for each row of a matrix A, a linear mathematical expression (
expra
in the script) is formed as a function of the binary variablex
. There are no issues in generatingexpra
, but there is a problem when definingexpraR
, since I cannot calculate the absolute value of the difference inside the parenthesis (L1-norm).Using this alternative solution does not work:
expraR[i] = ((expra - number) ** 2) ** 0.5
as the power operator is not supported either, returning the following error:
TypeError: unsupported operand type(s) for ** or pow(): 'BinaryQuadraticModel' and 'float'
The text was updated successfully, but these errors were encountered: