# Help with objective function definition for CQM problem

Hi,

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 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 variablesx = [dimod.Binary(i) for i in range(N)]# Loop over rows of matrix Afor 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 CQMcqm = 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?

Angela

• Hello Angela,

If you are just trying to get your objective to work, my first thought would be to either square your A[i] * x[j] terms or, add a conditional statement to use += for a positive term or -= for a negative term. Possibly changing your A[i] != 0 condition to if ( A[i] < 0 )  ... else if A[i] > 0) ?

For a proper answer to your specific question I will let someone else pitch in as to the proper way to implement abs() in the objective.

Cheers,

Mario

• Thank you Mario!

I'm afraid I cannot square the difference, as I need to implement the L1-norm in the script. Also, the operands '<', '>' are not supported either.

I am curious to know how to circumvent these problems when defining the objective...

• Hello Angela,

Can you clarify why changing :
abs(expra - number)
To:
(extra - number) ** 2
does not work for the case you posted?

Mario.

• Hi Mario,

that's because I need to use the L1-norm, I cannot use the L2-norm (it's problem-related, not algorithm-related)

• Hi Angela,

Are you still having issues calculating the L1 norm? If so, can you please help us understand preventing the absolute value of the L1 norm being calculated?

With kind regards,

Tanjid

• Hi Tanjid,

thank you for inquiring it!

My workaround for this issue would be to build the objective function with CPLEX, which handles optimization problems quite easily: at the end, the objective function which comes out of the for loops is relatively simple, with only 90 binary variables.

Once I have it, the idea is to convert it to a CQM, add some constraints, and then convert it to a BQM to go fully quantum. Does this make sense?

I am not expert of Quantum Computing and I could not come out with a more elegant solution unfortunately. It would actually be great if someone from D-Wave community agreed to collaborate to our project. It would be the first Quantum Computing application in Seismology!

Thanks a lot ;)

Angela