def test_dnx(self): import dimod import dwave_networkx as dnx G = dnx.chimera_graph(1) cut = dnx.maximum_cut(G, dimod.ExactSolver())
def test_edge_cases(self): # get the empty graph G = nx.Graph() S = dnx.maximum_cut(G, ExactSolver()) self.assertTrue(len(S) == 0) S = dnx.weighted_maximum_cut(G, ExactSolver()) self.assertTrue(len(S) == 0)
def test_typical_cases(self): G = nx.complete_graph(10) S = dnx.maximum_cut(G, ExactSolver()) self.assertTrue(len(S) == 5) # half of the nodes with self.assertRaises(dnx.DWaveNetworkXException): S = dnx.weighted_maximum_cut(G, ExactSolver()) nx.set_edge_attributes(G, 1, 'weight') S = dnx.weighted_maximum_cut(G, ExactSolver()) self.assertTrue(len(S) == 5) # half of the nodes G = nx.Graph() G.add_edges_from([(0, 1), (0, 2), (1, 2), (1, 3), (3, 4), (2, 4)]) S = dnx.maximum_cut(G, ExactSolver()) self.assertTrue(len(S) in (2, 3))
def test_max_cut(self): G = dnx.chimera_graph(2, 2, 4) sampler = micro.EmbeddingComposite(micro.DWaveSampler()) cut = dnx.maximum_cut(G, sampler) for u, v in G.edges: self.assertTrue(u in cut or v in cut) self.assertFalse(u in cut and v in cut)
# BusBar500kV, 31-Mar-2019 # D-Wave Challenge 10 import networkx as nx import dwave_networkx as dnx from dwave.system.composites import EmbeddingComposite from dwave.system.samplers import DWaveSampler problem10 = [(nx.florentine_families_graph(), 'families in Renaissance-era Florence'), (nx.karate_club_graph(), 'Karate club'), (nx.davis_southern_women_graph(), 'Davis Southern women graph')] for G, text in problem10: shots = 1000 number_of_rounds = 10 min_max_cut_set = [] for n in range(number_of_rounds): sampler = EmbeddingComposite(DWaveSampler()) max_cut = dnx.maximum_cut(G, sampler, num_reads=shots) min_max_cut_set.append(min(len(max_cut), len(set(G) - set(max_cut)))) print('Max Cut for %s graph is %s.' % (text, min(min_max_cut_set)))
import dwave_networkx as dnx import networkx as nx import dimod # Use basic simulated annealer sampler = dimod.SimulatedAnnealingSampler() # Set up a Networkx Graph G = nx.Graph() G.add_edges_from([(0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (1, 4), (2, 4), (3, 4), (3, 5), (4, 5), (5, 2)]) # Get the max cut candidate = dnx.maximum_cut(G, sampler) if len(candidate) == 3: print(candidate, " is the right length") else: print(candidate, " is not the right length")
def maximum_cut(graph, sampler, params=None): res = dnx.maximum_cut(graph, sampler) return list(res), 'node'
import networkx as nx import dwave_networkx as dnx import dimod import matplotlib.pyplot as plt from dwave.system.samplers import DWaveSampler from dwave.system.composites import EmbeddingComposite sampler = EmbeddingComposite(DWaveSampler(profile='cdl')) G = nx.gnm_random_graph(12, 25) cut = dnx.maximum_cut(G, sampler, num_reads=50) print('Size of the nodes, for maximum edges cut', len(cut)) print('The nodes, for maximum edges cut', cut) subG = G.subgraph(cut) notG = list(set(G.nodes()) - set(cut)) other_subG = G.subgraph(notG) pos = nx.circular_layout(G) plt.figure() nx.draw(G, pos=pos) nx.draw(subG, pos=pos) nx.draw(other_subG, pos=pos, node_color='b') plt.show()