dwavesystems / dwavesystems/dimod

Add absolute value and power operator for the BinaryQuadraticModel

Open
#1,189 5 comments 0 reactions 0 assignees View on GitHub
enhancement question
Dominant language
Python
Stars
143
Forks
91
Avg merge
1h 24m
Merged PRs (30d)
3

Description

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:

```
# Add binary variables
x = [dimod.Binary(i) for i in range(N)]

# Loop over rows of matrix A
for i in range(rows):
# Initialize expra
expra = 0*len(x)
for j in range(N2):
if A[i] != 0:
# Build the linear expression
expra += A[i]*x[j]
# Take the abs value
expraR[i] = abs(expra - number)

# Construct the CQM
cqm = CQM()

## Add the objective (L1-norm)
cqm.set_objective(sum(expraR))
```

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 variable ```x```. There are no issues in generating ```expra```, but there is a problem when defining ```expraR```, 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'```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.