dwavesystems / dimod

A shared API for QUBO/Ising samplers.
https://docs.ocean.dwavesys.com/en/stable/docs_dimod/
Apache License 2.0
121 stars 80 forks source link

fix_variables method for DQM object #797

Open hsadeghidw opened 3 years ago

hsadeghidw commented 3 years ago

Given a variable, fix the value of the variable to one of its cases and remove the variable.

dqm = dimod.DiscreteQuadraticModel()
dqm.add_variable(5, 'a')
dqm.add_variable(5, 'b')
dqm.set_quadratic('a', 'b', {(1, 4): 5})

assert(dqm.get_linear('a', 1) == 0)

dqm.fix_variable('b', 4)

assert(dqm.get_linear('a', 1) == 5)
assert('b' not in dqm.variables)

# also be able to fix a bunch of variables at once

dqm = dimod.DiscreteQuadraticModel()
dqm.add_variable(5, 'a')
dqm.add_variable(5, 'b')
dqm.set_quadratic('a', 'b', {(1, 4): 5})

dqm.fix_variables({'a': 1, 'b': 4})
assert(dqm.num_variables() == 0)
arcondello commented 3 years ago

Do we also want to negate cases? Something like dqm.fix_variable_case('a', 4, 0)

hsadeghidw commented 3 years ago

The case value of a variable is a property of the SampleSet. It may be interesting to be able to change variable case in a sampleset and observe the modified energy.

arcondello commented 3 years ago

I am not sure I understand what you mean?

hsadeghidw commented 3 years ago

okay, I guess I didn't understand what you meant by negating cases. Would dqm.fix_variable_case('a', 4, 0) remove case 4? and therefore reduce the dqm.num_cases('a') by one?

If so, this would be useful. We can even do dqm.fix_variable({'a': [1, 3,4]}) to only keep the three cases [1,3,4]. Or equally, dqm.fix_variable_case('a', {0: 0, 2: 0})

However, I think from user's point of view, this can be confusing because we are not tracking cases by the label.

arcondello commented 3 years ago

Yeah, I was thinking about it more and upon reflection I think a separate method like dqm.exclude_case('a', 0) would be a lot clearer. Rather than making is a switching behavior in dqm.fix_variable.