Skip to content

Commit

Permalink
Handle exception when BQM is passed to DQM sampler
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdullahjavednesar committed Feb 28, 2022
1 parent 4fa4b27 commit dc53ec4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions dwave/system/samplers/leap_hybrid_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,12 @@ def min_time_limit(self, dqm):
first two pairs that represent problems with "density" between 1 to
100).
"""
ec = (dqm.num_variable_interactions() * dqm.num_cases() /
max(dqm.num_variables(), 1))
from dimod.discrete.discrete_quadratic_model import DiscreteQuadraticModel
if isinstance(dqm, DiscreteQuadraticModel):
ec = (dqm.num_variable_interactions() * dqm.num_cases() /
max(dqm.num_variables(), 1))
else:
raise TypeError(f"Expecting DiscreteQuadraticModel object, got {type(dqm)}")
limits = np.array(self.properties['minimum_time_limit'])
t = np.interp(ec, limits[:, 0], limits[:, 1])
return max([5, t])
Expand Down
4 changes: 4 additions & 0 deletions tests/test_leaphybriddqmsolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ def get_solver(self, *args, **kwargs):
sampler = LeapHybridDQMSampler()

dqm = dimod.DQM()
bqm = dimod.BinaryQuadraticModel('SPIN')

with self.assertRaises(ValueError):
sampler.sample_dqm(dqm, time_limit=1)

with self.assertRaises(ValueError):
sampler.sample_dqm(dqm, time_limit=10000000)

with self.assertRaises(TypeError):
sampler.sample_dqm(bqm)

def test_DQM_subclass_without_serialization_can_be_sampled(self):
"""Test that DQM subclasses that do not implement serialization can
still be sampled by LeapHybridDQMSampler.
Expand Down

0 comments on commit dc53ec4

Please sign in to comment.