I would like to set my problem as a constrained quadratic model (CQM), since a CQM allows users to explicitly define objective and constraints (easier for me, as I am not a QC expert!). The idea is to first submit the problem on the D-Wave LeapHybridCQMSampler to see if it works, and later to go fully quantum by sending it to the DWaveSampler (after conversion from CQM to BQM with dimod.cqm_to_bqm).

In order to build the objective function for my problem, this is what I need to do: for each row of a matrix A, a linear mathematical expression (expra in the script) is formed as a function of the vector x (the binary variable):

# 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)

There are no issues in generating expra, but of course there is a problem when defining expraR, since I cannot calculate the absolute value of the difference inside the parenthesis (L1-norm). My question is: how can I implement operations such as the absolute value into the mathematical expression defining my objective function?

[UPDATE] More details to clarify the context of the question

This problem falls into the category of knapsack problems, which can be handled by quantum computers. I’m trying to map the knapsack problem (minimization of an objective function + set of constraints) to a CQM for the D-Wave hybrid sampler. Specifically, I’m in the step of setting up an arithmetic expression for the objective function. There are no specific issues for the definition of expra, but the problem comes when trying to play with it by introducing the absolute value of the difference btw expra and a scalar (to define expraR). The reason is that operations such as the absolute value are apparently not allowed (and the power is not allowed either, so I cannot replace abs(x) with (x ** 2) ** 0.5). In a classical approach, this would be handled for instance by the Model class in docplex (http://ibmdecisionoptimization.github.io/docplex-doc/mp/docplex.mp.model.html), which can be used to embed modeling objects and which returns mathematical expressions for a variety of operations applied to the Model object.

NOTE: x is a list, x[j] and expra are dimod.binary.binary_quadratic_model.BinaryQuadraticModel.

Thank you in advance, Angela


1 Answer 1


It looks like you want to loop the matrix, correct? My question is why you can't calculate the absolute value between two distinct elements in your matrix? Can you please explain you question clear. Thank you!

  • 1
    $\begingroup$ I have added more details into the question $\endgroup$
    – Angela
    May 20 at 9:29
  • $\begingroup$ I understand your problem better now. Is it possible to do calculation in terms of abs() before you traasform the value to binary model? $\endgroup$ May 23 at 19:22
  • 1
    $\begingroup$ Unfortunately not... The problem seems to be the BQM can currently accept only a limited number of operations. I have opened a feature request on dimod GitHub page: github.com/dwavesystems/dimod/issues/1189 $\endgroup$
    – Angela
    May 24 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.