Skip to content

Add absolute value and power operator for the BinaryQuadraticModel #1189

Open
@angystallone

Description

@angystallone

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'

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestquestionQuestion or general discussion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions