Skip to content

Conversation

@aivarsoo
Copy link
Contributor

As discussed in the issue #1364, the variable check in ConstrainedQuadraticModel.add_discrete method can be skipped in some cases and the user can decide if this is truly necessary.

This is implemented by adding a do_var_check boolean variable (default: True), which enables the check if it's true, in the methods ConstrainedQuadraticModel.add_discrete_from_model, ConstrainedQuadraticModel.add_discrete_from_comparison, and ConstrainedQuadraticModel.add_discrete_from_iterable.

@codecov
Copy link

codecov bot commented Feb 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (e7bc4d5) 94.86% compared to head (e55df2f) 94.86%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1365   +/-   ##
=======================================
  Coverage   94.86%   94.86%           
=======================================
  Files          96       96           
  Lines       10324    10324           
=======================================
  Hits         9794     9794           
  Misses        530      530           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@arcondello arcondello left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does need a release note. Probably something like

Add <kwarg name> keyword argument to `ConstrainedQuadraticModel.add_discrete()`, see `#1364 <https://github.com/dwavesystems/dimod/issues/1364>`_.

Also, ideally we would add tests to TestAddDiscrete

def test_overlap_check(self):
    cqm.add_discrete("abc")
    i = cqm.add_variable("INTEGER", "i")

    with self.assertRaises(ValueError):
        cqm.add_discrete("bcd", check_overlaps=True)

    cqm.add_discrete("bcd", check_overlap=False)  # allows overlapping

    with self.assertRaises(ValueError):
        cqm.add_discrete(i, check_overlap=False)  # still checks vartype

label: Optional[Hashable] = None,
copy: bool = True) -> Hashable:
copy: bool = True,
do_var_check: bool = True) -> Hashable:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think check_valid or check_overlaps would be a better name, if we want to keep the "direction" of having it default to True.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check_overlaps is a great name! I'll stick with that

the model can cause issues.

do_var_check: If `True` we perform a variable check. In particular, if
the variables already exist, we make sure they're not used.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
the variables already exist, we make sure they're not used.
the variables already exist, we make sure they're not used in another discrete constraint.

here and elsewhere.

Comment on lines 685 to 687
if do_var_check and v in self.variables:
# it already exists, let's make sure it's not already used
if any(v in self.constraints[label].lhs.variables for label in self.discrete):
Copy link
Member

@arcondello arcondello Feb 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if do_var_check and v in self.variables:
# it already exists, let's make sure it's not already used
if any(v in self.constraints[label].lhs.variables for label in self.discrete):
if v in self.variables:
# it already exists, let's make sure it's not already used
if do_var_check and any(v in self.constraints[label].lhs.variables for label in self.discrete):

we still want to check that existing variables are binary, we just want to skip the check for overlapping constraints.

@arcondello
Copy link
Member

FWIW, I think this keyword arg is sufficiently advanced that we don't need to add a docstring example for it.

@arcondello arcondello merged commit 323115f into dwavesystems:main Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider adding a kwarg to skip checking whether a discrete constraint is valid

2 participants