Skip to content
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
15 changes: 15 additions & 0 deletions dwave/system/composites/embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
)


def _validate_bqm(bqm):
if not isinstance(bqm, dimod.BinaryQuadraticModel):
raise TypeError(
"Embedding composites only accept binary quadratic models (BQMs). "
"For constrained quadratic models (CQMs), use an appropriate sampler "
"such as LeapHybridCQMSampler."
)


class EmbeddingComposite(dimod.ComposedSampler):
"""Maps problems to a structured sampler.

Expand Down Expand Up @@ -238,6 +247,8 @@ def sample(self, bqm, chain_strength=None,
See the example in the :class:`.EmbeddingComposite` class.

"""
_validate_bqm(bqm)

if return_embedding is None:
return_embedding = self.return_embedding_default

Expand Down Expand Up @@ -517,6 +528,8 @@ def sample(self, bqm, **parameters):
:obj:`~dimod.SampleSet`

"""
_validate_bqm(bqm)

if self.embedding is None:
# get an embedding using the current find_embedding function
embedding_parameters = parameters.pop('embedding_parameters', None)
Expand Down Expand Up @@ -668,6 +681,8 @@ def permissive_child_structure(sampler):
**kwargs)

def sample(self, bqm, **parameters):
_validate_bqm(bqm)

child = self.child

# we want to pass only the parameters relevent to the child sampler
Expand Down
7 changes: 7 additions & 0 deletions tests/test_embedding_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ def test_instantiation_smoketest(self):

dimod.testing.assert_sampler_api(sampler)

def test_sample_rejects_cqm(self):
sampler = EmbeddingComposite(MockDWaveSampler())
cqm = dimod.ConstrainedQuadraticModel()

with self.assertRaisesRegex(TypeError, "binary quadratic model"):
sampler.sample(cqm)

def test_sample_ising(self):
sampler = EmbeddingComposite(MockDWaveSampler())

Expand Down