Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle exception when BQM is passed to DQM sampler #444

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dwave/system/samplers/leap_hybrid_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

bqm_to_file = FileView

from dimod.discrete.discrete_quadratic_model import DiscreteQuadraticModel
from dwave.cloud import Client
from dwave.system.utilities import classproperty, FeatureFlags

Expand Down Expand Up @@ -465,6 +466,8 @@ def sample_dqm(self, dqm, time_limit=None, compress=False, compressed=None, **kw
See the example in :class:`LeapHybridDQMSampler`.

"""
if not isinstance(dqm, DiscreteQuadraticModel):
raise TypeError(f"Expecting DiscreteQuadraticModel object, got {type(dqm)}")
if time_limit is None:
time_limit = self.min_time_limit(dqm)
elif time_limit < self.min_time_limit(dqm):
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