From 6da1c23a596cd91b232934037ff7c802ccacc7a1 Mon Sep 17 00:00:00 2001 From: Mason Christensen Date: Wed, 2 Jun 2021 11:02:47 -0700 Subject: [PATCH 1/5] check bqm feature first pass --- dimod/core/structured.py | 23 +++++++++++++++++++++++ tests/test_structured.py | 28 +++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/dimod/core/structured.py b/dimod/core/structured.py index 68691fcb8..b89ae3206 100644 --- a/dimod/core/structured.py +++ b/dimod/core/structured.py @@ -74,6 +74,7 @@ def sample(self, bqm): import abc from collections import namedtuple +from dimod.exceptions import BinaryQuadraticModelStructureError __all__ = ['Structured'] @@ -144,3 +145,25 @@ def to_networkx_graph(self): G.add_nodes_from(self.nodelist) return G + + def check_bqm_structure(self, bqm: "BinaryQuadraticModel"): + """Validate that problem defined by :class:`dimod.BinaryQuadraticModel` + matches the graph provided by the sampler. + + Args: + bqm: :class:`dimod.BinaryQuadraticModel` object to validate. + + Returns: + True if BQM structure matches that of sampler. + + Raises: + :exception:`dimod.exceptions.BinaryQuadraticModelStructureError` + if structure doesn't match. + + """ + if all(variable in self.adjacency for variable in bqm.variables) and \ + all(neighbor in self.adjacency[vertex] for neighbor, vertex in bqm.quadratic): + return True + raise BinaryQuadraticModelStructureError( + "BQM structure does not match graph provided by sampler." + ) diff --git a/tests/test_structured.py b/tests/test_structured.py index 60cce8581..bc981a1f9 100644 --- a/tests/test_structured.py +++ b/tests/test_structured.py @@ -96,4 +96,30 @@ def edgelist(self): for u, v in nxG.edges: self.assertIn(u, G[v]) - \ No newline at end of file + + def test_check_bqm_structure(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.check_bqm_structure(valid_structure_bqm)) + with self.assertRaises(dimod.BinaryQuadraticModelStructureError): + dummy.check_bqm_structure(invalid_structure_bqm) \ No newline at end of file From 95090078cedaf7b2d4fd4222f2d883dd5ecc8718 Mon Sep 17 00:00:00 2001 From: Mason Christensen Date: Wed, 2 Jun 2021 11:15:38 -0700 Subject: [PATCH 2/5] Just return booleans, update name and test --- dimod/core/structured.py | 12 +++--------- tests/test_structured.py | 7 +++---- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/dimod/core/structured.py b/dimod/core/structured.py index b89ae3206..b6ad9473f 100644 --- a/dimod/core/structured.py +++ b/dimod/core/structured.py @@ -146,7 +146,7 @@ def to_networkx_graph(self): return G - def check_bqm_structure(self, bqm: "BinaryQuadraticModel"): + def valid_bqm_structure(self, bqm: "BinaryQuadraticModel"): """Validate that problem defined by :class:`dimod.BinaryQuadraticModel` matches the graph provided by the sampler. @@ -154,16 +154,10 @@ def check_bqm_structure(self, bqm: "BinaryQuadraticModel"): bqm: :class:`dimod.BinaryQuadraticModel` object to validate. Returns: - True if BQM structure matches that of sampler. - - Raises: - :exception:`dimod.exceptions.BinaryQuadraticModelStructureError` - if structure doesn't match. + Boolean indicating validity of BQM structure compared to sampler graph. """ if all(variable in self.adjacency for variable in bqm.variables) and \ all(neighbor in self.adjacency[vertex] for neighbor, vertex in bqm.quadratic): return True - raise BinaryQuadraticModelStructureError( - "BQM structure does not match graph provided by sampler." - ) + return False diff --git a/tests/test_structured.py b/tests/test_structured.py index bc981a1f9..993af755c 100644 --- a/tests/test_structured.py +++ b/tests/test_structured.py @@ -97,7 +97,7 @@ def edgelist(self): for u, v in nxG.edges: self.assertIn(u, G[v]) - def test_check_bqm_structure(self): + def test_valid_bqm_structure(self): class Dummy(dimod.Structured): @property def nodelist(self): @@ -120,6 +120,5 @@ def edgelist(self): dummy = Dummy() - self.assertTrue(dummy.check_bqm_structure(valid_structure_bqm)) - with self.assertRaises(dimod.BinaryQuadraticModelStructureError): - dummy.check_bqm_structure(invalid_structure_bqm) \ No newline at end of file + self.assertTrue(dummy.valid_bqm_structure(valid_structure_bqm)) + self.assertFalse(dummy.valid_bqm_structure(invalid_structure_bqm)) \ No newline at end of file From d1df18fb70690407285e01cb8a8c692f7f40f56a Mon Sep 17 00:00:00 2001 From: Mason Christensen Date: Wed, 2 Jun 2021 11:16:47 -0700 Subject: [PATCH 3/5] trailing newline --- tests/test_structured.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_structured.py b/tests/test_structured.py index 993af755c..bda7902a1 100644 --- a/tests/test_structured.py +++ b/tests/test_structured.py @@ -121,4 +121,5 @@ def edgelist(self): dummy = Dummy() self.assertTrue(dummy.valid_bqm_structure(valid_structure_bqm)) - self.assertFalse(dummy.valid_bqm_structure(invalid_structure_bqm)) \ No newline at end of file + self.assertFalse(dummy.valid_bqm_structure(invalid_structure_bqm)) + \ No newline at end of file From 61957b818fdc8aae1457864f8bba39a699e7fb95 Mon Sep 17 00:00:00 2001 From: Mason Christensen Date: Wed, 2 Jun 2021 11:56:53 -0700 Subject: [PATCH 4/5] Change name for clarity, simplify return --- dimod/core/structured.py | 10 ++++------ tests/test_structured.py | 7 +++---- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/dimod/core/structured.py b/dimod/core/structured.py index b6ad9473f..4ba44160a 100644 --- a/dimod/core/structured.py +++ b/dimod/core/structured.py @@ -146,7 +146,7 @@ def to_networkx_graph(self): return G - def valid_bqm_structure(self, bqm: "BinaryQuadraticModel"): + def valid_bqm_graph(self, bqm: "BinaryQuadraticModel"): """Validate that problem defined by :class:`dimod.BinaryQuadraticModel` matches the graph provided by the sampler. @@ -154,10 +154,8 @@ def valid_bqm_structure(self, bqm: "BinaryQuadraticModel"): bqm: :class:`dimod.BinaryQuadraticModel` object to validate. Returns: - Boolean indicating validity of BQM structure compared to sampler graph. + Boolean indicating validity of BQM graph compared to sampler graph. """ - if all(variable in self.adjacency for variable in bqm.variables) and \ - all(neighbor in self.adjacency[vertex] for neighbor, vertex in bqm.quadratic): - return True - return False + return all(variable in self.adjacency for variable in bqm.variables) and \ + all(neighbor in self.adjacency[vertex] for neighbor, vertex in bqm.quadratic) diff --git a/tests/test_structured.py b/tests/test_structured.py index bda7902a1..365b22e65 100644 --- a/tests/test_structured.py +++ b/tests/test_structured.py @@ -97,7 +97,7 @@ def edgelist(self): for u, v in nxG.edges: self.assertIn(u, G[v]) - def test_valid_bqm_structure(self): + def test_valid_bqm_graph(self): class Dummy(dimod.Structured): @property def nodelist(self): @@ -120,6 +120,5 @@ def edgelist(self): dummy = Dummy() - self.assertTrue(dummy.valid_bqm_structure(valid_structure_bqm)) - self.assertFalse(dummy.valid_bqm_structure(invalid_structure_bqm)) - \ No newline at end of file + self.assertTrue(dummy.valid_bqm_graph(valid_structure_bqm)) + self.assertFalse(dummy.valid_bqm_graph(invalid_structure_bqm)) From 586bb2b306ce3cfdcf89be6846283385fbf817c6 Mon Sep 17 00:00:00 2001 From: Mason Christensen Date: Wed, 2 Jun 2021 13:41:51 -0700 Subject: [PATCH 5/5] unused import --- dimod/core/structured.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dimod/core/structured.py b/dimod/core/structured.py index 4ba44160a..b037dad9d 100644 --- a/dimod/core/structured.py +++ b/dimod/core/structured.py @@ -74,7 +74,6 @@ def sample(self, bqm): import abc from collections import namedtuple -from dimod.exceptions import BinaryQuadraticModelStructureError __all__ = ['Structured']