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)
cqm.set_objective(sum(expraR))
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