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

check bqm feature first pass #832

Merged
Merged
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
14 changes: 14 additions & 0 deletions dimod/core/structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,17 @@ def to_networkx_graph(self):
G.add_nodes_from(self.nodelist)

return G

def valid_bqm_graph(self, bqm: "BinaryQuadraticModel"):
hbarovertwo marked this conversation as resolved.
Show resolved Hide resolved
"""Validate that problem defined by :class:`dimod.BinaryQuadraticModel`
matches the graph provided by the sampler.

Args:
bqm: :class:`dimod.BinaryQuadraticModel` object to validate.

Returns:
Boolean indicating validity of BQM graph compared to sampler graph.

"""
return all(variable in self.adjacency for variable in bqm.variables) and \
all(neighbor in self.adjacency[vertex] for neighbor, vertex in bqm.quadratic)
27 changes: 26 additions & 1 deletion tests/test_structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,29 @@ def edgelist(self):

for u, v in nxG.edges:
self.assertIn(u, G[v])


def test_valid_bqm_graph(self):
class Dummy(dimod.Structured):
@property
def nodelist(self):
return list(range(5))

@property
def edgelist(self):
return [(0,1),(1,2),(2,3)]

valid_structure_bqm = dimod.BQM(
{0:0, 1:1, 2:2, 3:3, 4:4},
{(0,1):1, (1,2):1, (2,3):1},
0.0, dimod.BINARY
)

# Invalid due to extra variable '5' not present in nodelist/edgelist of dummy sampler.
invalid_structure_bqm = dimod.BQM(
{1:1, 2:2, 5:5}, {(1,2):1}, 0.0, dimod.BINARY
)

dummy = Dummy()

self.assertTrue(dummy.valid_bqm_graph(valid_structure_bqm))
self.assertFalse(dummy.valid_bqm_graph(invalid_structure_bqm))