Created
June 21, 2021 18:15
-
-
Save rdisipio/c24bd68d77fd36a2a848fab3f857c68f to your computer and use it in GitHub Desktop.
Define a DQM model for graph partitioning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import networkx as nx | |
| import dimod | |
| from dwave.system import LeapHybridDQMSampler | |
| ... | |
| dqm = dimod.DiscreteQuadraticModel() | |
| num_nodes = G.number_of_nodes() | |
| for i in G.nodes: | |
| dqm.add_variable(num_partitions, label=i) | |
| for i in G.nodes: | |
| linear_term = ... | |
| dqm.set_linear(i, linear_term) | |
| # Quadratic term for node pairs that do *not* share edges | |
| for p0, p1 in nx.non_edges(G): | |
| quadratic_term = ... | |
| dqm.set_quadratic(p0, p1, {(c, c): quadratic_term for c in range(num_partitions)}) | |
| # Quadratic term for node pairs which have edges between them | |
| for p0, p1 in G.edges: | |
| quadratic_term = ... | |
| dqm.set_quadratic(p0, p1, {(c, c): quadratic_term for c in range(num_partitions)}) | |
| # solve! | |
| sampler = LeapHybridDQMSampler() | |
| sampleset = sampler.sample_dqm(dqm, label='Example - Graph Partitioning DQM') | |
| # do something with the lowest-energy solution: | |
| sample = sampleset.first.sample |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment