# minimize number of variables used

how do i minimize the number of variables used.

i want to write something like this which necessarily means find the best possible solutions with the least number of variables.

but it does not accept it. it needs a sense. aka: >, < and so on

Thank you!

• Hello,

I am not sure I fully understand your question, but I have some resources that might be useful.

For the code snippet you gave cqm.add_constraint(quicksum(all_vars.values())) do you think that this function would accomplish what you are trying to do?

The objective function represents the energy of the system and will be solved by being minimized.

There are a few resources and code examples that might do what you are thinking of.

The dimod.binary.BinaryQuadraticModel.fix_variables function allows variables to be set to a desired fixed value, though this would require the user to know which variables they want to fix.

The presolve algorithms perform preprocessing locally to reduce redundant variables and constraints, so this might be useful.

The MIQUBO Method of Feature Selection example demonstrates how to find an optimal set to predict the survival of Titanic passengers.

There is also a Feature Selection for CQM which uses the CQM solver to do feature selection on a couple of data sets.

These last two seem to be closer to the desired behaviour, but please let us know if this helps and if you have any more questions.

• Thank you so much for this helpful post.

I am trying to implement the minimum vertex cover problem

So here is my objective:
//minimize the number of vertices/variables
//minimize(Σ xi , i ∈ V) (the sum is over all nodes in the graph)
cqm.set_objective(-1.5*quicksum(all_vars.values()))

And here is my constraint:
//xi + xj ≥ 1 for all (i, j) ∈ E
for i in range(len(pair_list)):

It does not seem to work.

I did not want to write the constraint the way i wrote it above but it is the only way Dwave accepted it.
I wanted to write it like this:
cqm.add_constraint((all_vars[pair_list[i][0]] + all_vars[pair_list[i][1]]) >= 1 for i in range(len(pair_list)))

I am stuck in this part.
• Hello,

Could you please explain what you mean when you say that it doesn't work?
Are you not able to submit the problem due to a code error? Are the results not in line with what you are expecting to see? Have you run the problem multiple times with different output energies?

For your list of pairs, is it a list of sets, or a list of ordered pairs? In order to simplify calculations, using a list of sets makes things easier. For example (0, 1) and (1, 0) should not appear in the list together.

Looking at the two code examples for adding constraints, I noticed that they are quite similar. It appears that you would like to use something like a Python List Comprehension. Is that correct?
Looking at the documentation, adding one comparison constraint at a time is supported.

To help clarify further, we would need a minimal code example with input data included that can be copied into a script and run. Please do not include any of your own personal or problem information. We also would need the output you are seeing with the example, as well as the output you would expect to see if the problem were to run successfully. For simplicity and easy understanding of code, a minimal/small code example is very useful.

I hope this helps guide you in the right direction. Thank you again for reaching out to us.

• Thank you so much David for your help.

It is very much appreciated!

Will let you know if I get stuck once again.