Setting up Objective function for CQM

I have previoulsy used BQM model for computation for problems. However I want to stup the same problem as an CQM by expressingthe cnstraints explicitly and not introducing them into the objective as penalties, nd also change the type of variables to 'REAL".

However I am not sure how to construct the objectvie function, as in CQM we don't have methods like : add_quadratic_from , ```add_linear_from``` , etc.  as it dos in case of BQMs. 

Can someone help me on this ?



1 comment
  • There are two methods to add an objective function to a CQM object.

    • Build a BQM(or Quadratic Model if your problem has real or integer variables) and add it to the CQM. Here is a minimal code example:
    from dimod import ConstrainedQuadraticModel, BinaryQuadraticModel

    #Define BQM or QM
    bqm = BinaryQuadraticModel()
    bqm.add_linear_from({'i': 2})
    bqm.add_quadratic_from({('i','j'): -0.5})

    #Set objective
    cqm = ConstrainedQuadraticModel()
    • Set the objective function using symbolic math. This method is specially useful for small models used for experimenting and formulating problems.
    from dimod import Binary, ConstrainedQuadraticModel
    i = Integer('i')
    j = Integer('j')
    cqm = ConstrainedQuadraticModel()
    cqm.set_objective(2*i - 0.5*i*j)

    You might also find the following resources useful to get started with the CQM solver:


    Comment actions Permalink

Please sign in to leave a comment.

Didn't find what you were looking for?

New post