def test_nice_coordinates(self): G = dnx.pegasus_graph(4, nice_coordinates=True) H = dnx.chimera_graph(3, coordinates=True) for p, q in H.edges(): for t in range(3): pg = (t, ) + p qg = (t, ) + q self.assertTrue(G.has_edge(pg, qg)) coords = dnx.pegasus_coordinates(4) n2p = coords.nice_to_pegasus p2n = coords.pegasus_to_nice n2l = coords.nice_to_linear l2n = coords.linear_to_nice for p in G.nodes(): self.assertEqual(p2n(n2p(p)), p) self.assertEqual(l2n(n2l(p)), p) self.assertTrue(H.has_node(p[1:])) G = dnx.pegasus_graph(4) for p in G.nodes(): self.assertEqual(n2l(l2n(p)), p) G = dnx.pegasus_graph(4, coordinates=True) for p in G.nodes(): self.assertEqual(n2p(p2n(p)), p)
def test_bad_args(self): with self.assertRaises(dnx.DWaveNetworkXException): G = dnx.pegasus_graph(2, offset_lists=[], offsets_index=0) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") G = dnx.pegasus_graph(2, offset_lists=[[0, 1]*6, [0, 1]*6]) self.assertLessEqual(len(w), 13) self.assertGreaterEqual(len(w), 12)
def test_connected_component(self): test_offsets = [[0] * 12] * 2, [[2] * 12, [6] * 12], [[6] * 12, [2, 2, 6, 6, 10, 10] * 2], [[2, 2, 6, 6, 10, 10] * 2] * 2 for offsets in test_offsets: G = dnx.pegasus_graph(4, fabric_only=True, offset_lists=offsets) H = dnx.pegasus_graph(4, fabric_only=False, offset_lists=offsets) nodes = sorted(G) comp = sorted(max((G.subgraph(c).copy() for c in nx.connected_components(G)), key=len)) self.assertEqual(comp, nodes)
def test_dnx_graph(self): # Graph Architecture G = dnx.chimera_graph(4) self.mock_dnx_graph_method(G) G = dnx.pegasus_graph(4) self.mock_dnx_graph_method(G) # Labels G = dnx.pegasus_graph(4, coordinates=True, nice_coordinates=False) self.mock_dnx_graph_nice_coordinates(G)
def test_pegasus(self): T_linear = dnx.pegasus_graph(2) linear_tiling = DWaveNetworkXTiling(T_linear) T_coord = dnx.pegasus_graph(2, coordinates=True) coord_tiling = DWaveNetworkXTiling(T_coord) T_nice = dnx.pegasus_graph(2, nice_coordinates=True) nice_tiling = DWaveNetworkXTiling(T_nice) self.assertEqual(linear_tiling.get_tile(10), coord_tiling.get_tile((0, 0, 10, 0)), nice_tiling.get_tile((1, 0, 0, 0, 2)))
def test_variable_order(self): n = 4 p = dnx.pegasus_graph(n, fabric_only=False) o = dnx.pegasus_elimination_order(n) tw = dnx.elimination_order_width(p, o) self.assertEqual(tw, 12*n-4) p = dnx.pegasus_graph(n, fabric_only=False, coordinates=True) o = dnx.pegasus_elimination_order(n, coordinates=True) tw = dnx.elimination_order_width(p, o) self.assertEqual(tw, 12*n-4)
def test_connected_component(self): from dwave_networkx.generators.pegasus import pegasus_coordinates test_offsets = [[0] * 12] * 2, [[2] * 12, [6] * 12 ], [[6] * 12, [2, 2, 6, 6, 10, 10] * 2 ], [[2, 2, 6, 6, 10, 10] * 2] * 2 for offsets in test_offsets: G = dnx.pegasus_graph(4, fabric_only=True, offset_lists=offsets) H = dnx.pegasus_graph(4, fabric_only=False, offset_lists=offsets) nodes = sorted(G) comp = sorted(max(nx.connected_components(H), key=len)) self.assertEqual(comp, nodes)
def test_sublattice_mappings(self): def check_subgraph_mapping(f, g, h): for v in g: if not h.has_node(f(v)): raise RuntimeError( f"node {v} mapped to {f(v)} is not in {h.graph['name']} ({h.graph['labels']})" ) for u, v in g.edges: if not h.has_edge(f(u), f(v)): raise RuntimeError( f"edge {(u, v)} mapped to {(f(u), f(v))} not present in {h.graph['name']} ({h.graph['labels']})" ) coords5 = dnx.pegasus_coordinates(5) coords3 = dnx.pegasus_coordinates(3) p3l = dnx.pegasus_graph(3) p3c = dnx.pegasus_graph(3, coordinates=True) p3n = coords3.graph_to_nice(p3c) p5l = dnx.pegasus_graph(5) p5c = dnx.pegasus_graph(5, coordinates=True) p5n = coords5.graph_to_nice(p5c) for target in p5l, p5c, p5n: for source in p3l, p3c, p3n: covered = set() for f in dnx.pegasus_sublattice_mappings(source, target): check_subgraph_mapping(f, source, target) covered.update(map(f, source)) self.assertEqual(covered, set(target)) c2l = dnx.chimera_graph(2) c2c = dnx.chimera_graph(2, coordinates=True) c23l = dnx.chimera_graph(2, 3) c32c = dnx.chimera_graph(3, 2, coordinates=True) p5n = dnx.pegasus_graph(5, nice_coordinates=True) p5l = coords5.graph_to_linear(p5n) p5c = coords5.graph_to_pegasus(p5n) for target in p5l, p5c, p5n: for source in c2l, c2c, c23l, c32c, target: covered = set() for f in dnx.pegasus_sublattice_mappings(source, target): check_subgraph_mapping(f, source, target) covered.update(map(f, source)) self.assertEqual(covered, set(target))
def __init__(self, broken_nodes=None, qpu_topology=None, qpu_scale=4, **config): super().__init__(broken_nodes, **config) #An Advantage generation processor, only artificially smaller, #replaces C4 in default MockDWaveSampler if qpu_topology != 'chimera': self.properties['topology'] = { 'type': 'pegasus', 'shape': [qpu_scale] } qpu_graph = dnx.pegasus_graph(qpu_scale, fabric_only=True) else: self.properties['topology'] = { 'type': 'chimera', 'shape': [qpu_scale, qpu_scale, 4] } qpu_graph = dnx.chimera_graph(qpu_scale) #Adjust edge_list, if broken_nodes is None: self.nodelist = sorted(qpu_graph.nodes) self.edgelist = sorted( tuple(sorted(edge)) for edge in qpu_graph.edges) else: self.nodelist = sorted(v for v in qpu_graph.nodes if v not in broken_nodes) self.edgelist = sorted( tuple(sorted((u, v))) for u, v in qpu_graph.edges if u not in broken_nodes and v not in broken_nodes) self.properties['num_qubits'] = len(qpu_graph) self.properties['qubits'] = self.nodelist self.properties['couplers'] = self.edgelist
def test_draw_pegasus_embedding(self): P = dnx.pegasus_graph(2) G = nx.grid_graph([3, 3, 2]) emb = { (0, 0, 0): [35], (0, 0, 1): [12], (0, 0, 2): [31], (0, 1, 0): [16], (0, 1, 1): [36], (0, 1, 2): [11], (0, 2, 0): [39], (0, 2, 1): [6], (0, 2, 2): [41], (1, 0, 0): [34], (1, 0, 1): [13], (1, 0, 2): [30], (1, 1, 0): [17], (1, 1, 1): [37], (1, 1, 2): [10], (1, 2, 0): [38], (1, 2, 1): [7], (1, 2, 2): [40] } dnx.draw_pegasus_embedding(P, emb) dnx.draw_pegasus_embedding(P, emb, embedded_graph=G) dnx.draw_pegasus_embedding(P, emb, interaction_edges=P.edges())
def to_networkx_graph(self): """Converts DWaveSampler's structure to a Chimera or Pegasus NetworkX graph. Returns: :class:`networkx.Graph`: Either an (m, n, t) Chimera lattice or a Pegasus lattice of size m. Examples: This example converts a selected D-Wave system solver to a graph and verifies it has over 2000 nodes. >>> from dwave.system import DWaveSampler ... >>> sampler = DWaveSampler() >>> g = sampler.to_networkx_graph() # doctest: +SKIP >>> len(g.nodes) > 2000 # doctest: +SKIP True """ topology_type = self.properties['topology']['type'] shape = self.properties['topology']['shape'] if topology_type == 'chimera': G = dnx.chimera_graph(*shape, node_list=self.nodelist, edge_list=self.edgelist) elif topology_type == 'pegasus': G = dnx.pegasus_graph(shape[0], node_list=self.nodelist, edge_list=self.edgelist) return G
def __init__(self, *args, **kwargs): super(TestBusclique, self).__init__(*args, **kwargs) self.c16 = dnx.chimera_graph(16) self.p16 = dnx.pegasus_graph(16) self.c16c = dnx.chimera_graph(16, coordinates=True, data=False) self.c428 = dnx.chimera_graph(4, n=2, t=8) self.c248 = dnx.chimera_graph(2, n=4, t=8) self.c42A = dnx.chimera_graph(4, n=2, t=9) c4c_0 = subgraph_node_yield(dnx.chimera_graph(4, coordinates=True), .95) p4c_0 = subgraph_node_yield(dnx.pegasus_graph(4, coordinates=True), .95) c4c = [ c4c_0, subgraph_edge_yield(c4c_0, .95), subgraph_edge_yield_few_bad(c4c_0, .95, 6) ] p4c = [ p4c_0, subgraph_edge_yield(p4c_0, .95), subgraph_edge_yield_few_bad(p4c_0, .95, 6) ] p4coords = dnx.pegasus_coordinates(4) c4coords = dnx.chimera_coordinates(4, 4, 4) c4 = [ relabel(c, c4coords.iter_chimera_to_linear, c4coords.iter_chimera_to_linear_pairs, 4) for c in c4c ] p4 = [ relabel(p, p4coords.iter_pegasus_to_linear, p4coords.iter_pegasus_to_linear_pairs, 4) for p in p4c ] p4n = [ relabel(p, p4coords.iter_pegasus_to_nice, p4coords.iter_pegasus_to_nice_pairs, 4, nice_coordinates=True) for p in p4c ] self.c4, self.c4_nd, self.c4_d = list(zip(c4, c4c)) self.p4, self.p4_nd, self.p4_d = list(zip(p4, p4c, p4n))
def test_empty_list(self): # Set up fragmentation function pg = dnx.pegasus_graph(3) fragment_tuple = get_tuple_fragmentation_fn(pg) # Fragment pegasus coordinates fragments = fragment_tuple([]) self.assertEqual([], fragments)
def test_draw_overlapped_chimera_embedding(self): C = dnx.pegasus_graph(2) emb = {0: [12, 35], 1: [12, 31], 2: [32], 3: [14]} dnx.draw_pegasus_embedding(C, emb, overlapped_embedding=True) dnx.draw_pegasus_embedding(C, emb, overlapped_embedding=True, show_labels=True)
def test_draw_pegasus_biases(self): G = dnx.pegasus_graph(2) h = {v: v % 12 for v in G} J = {(u, v) if u % 2 else (v, u): (u + v) % 24 for u, v in G.edges()} for v in G: J[v, v] = .1 dnx.draw_pegasus(G, linear_biases=h, quadratic_biases=J)
def test_coordinate_basics(self): G = dnx.pegasus_graph(4, fabric_only=False) H = dnx.pegasus_graph(4, coordinates=True, fabric_only=False) coords = pegasus_coordinates(4) Gnodes = G.nodes Hnodes = H.nodes for v in Gnodes: q = Gnodes[v]['pegasus_index'] self.assertIn(q, Hnodes) self.assertEqual(Hnodes[q]['linear_index'], v) self.assertEqual(v, coords.pegasus_to_linear(q)) self.assertEqual(q, coords.linear_to_pegasus(v)) for q in Hnodes: v = Hnodes[q]['linear_index'] self.assertIn(v, Gnodes) self.assertEqual(Gnodes[v]['pegasus_index'], q) self.assertEqual(v, coords.pegasus_to_linear(q)) self.assertEqual(q, coords.linear_to_pegasus(v))
def load_pegasus(tsize): g, chs = pegasus_tiles(tsize, tsize) original_graph = pegasus_graph(tsize, coordinates=True) logging.info("Hardware stats (compressed): nodes: %d(%d) edges: %d(%d)", original_graph.number_of_nodes(), g.number_of_nodes(), original_graph.number_of_edges(), g.number_of_edges()) return chs, g, original_graph
def test_p2(self): G = dnx.pegasus_graph(2, fabric_only=False) # should have 48 nodes self.assertEqual(len(G), 48) # nodes 0,...,47 should be in the graph for n in range(48): self.assertIn(n, G)
def __init__(self, **config): super().__init__() self.properties.update(topology=dict(shape=[6], type='pegasus')) G = dnx.pegasus_graph(6) self.nodelist = list(G.nodes) self.edgelist = list(G.edges)
def test_coordinate_basics(self): from dwave_networkx.generators.pegasus import pegasus_coordinates G = dnx.pegasus_graph(4, fabric_only=False) H = dnx.pegasus_graph(4, coordinates=True, fabric_only=False) coords = pegasus_coordinates(4) Gnodes = G.nodes Hnodes = H.nodes for v in Gnodes: q = Gnodes[v]['pegasus_index'] self.assertIn(q, Hnodes) self.assertEqual(Hnodes[q]['linear_index'], v) self.assertEqual(v, coords.int(q)) self.assertEqual(q, coords.tuple(v)) for q in Hnodes: v = Hnodes[q]['linear_index'] self.assertIn(v, Gnodes) self.assertEqual(Gnodes[v]['pegasus_index'], q) self.assertEqual(v, coords.int(q)) self.assertEqual(q, coords.tuple(v))
def test_empty_list(self): # Set up defragmentation function pg = dnx.pegasus_graph(2) defragment_tuple = get_tuple_defragmentation_fn(pg) # De-fragment chimera coordinates chimera_coords = [] pegasus_coords = defragment_tuple(chimera_coords) self.assertEqual([], pegasus_coords)
def test_single_fragment(self): # Set up defragmentation function pg = dnx.pegasus_graph(4) defragment_tuple = get_tuple_defragmentation_fn(pg) # De-fragment chimera coordinates chimera_coords = [(3, 7, 0, 0)] pegasus_coords = defragment_tuple(chimera_coords) expected_pegasus_coords = [(0, 1, 2, 0)] self.assertEqual(expected_pegasus_coords, pegasus_coords)
def test_multiple_fragments_from_same_qubit(self): # Set up defragmentation function pg = dnx.pegasus_graph(3) defragment_tuple = get_tuple_defragmentation_fn(pg) # De-fragment chimera coordinates chimera_coords = [(9, 8, 1, 1), (9, 11, 1, 1)] pegasus_coords = defragment_tuple(chimera_coords) expected_pegasus_coords = [(1, 1, 7, 1)] self.assertEqual(expected_pegasus_coords, pegasus_coords)
def test_single_vertical_coordinate(self): # Set up fragmentation function pg = dnx.pegasus_graph(6) fragment_tuple = get_tuple_fragmentation_fn(pg) pegasus_coord = (0, 1, 3, 1) fragments = fragment_tuple([pegasus_coord]) expected_fragments = {(7, 7, 0, 1), (8, 7, 0, 1), (9, 7, 0, 1), (10, 7, 0, 1), (11, 7, 0, 1), (12, 7, 0, 1)} self.assertEqual(expected_fragments, set(fragments))
def test_mixed_fragments(self): # Set up defragmenation function pg = dnx.pegasus_graph(8) defragment_tuple = get_tuple_defragmentation_fn(pg) # De-fragment chimera coordinates chimera_coords = [(17, 14, 0, 0), (22, 14, 0, 0), (24, 32, 1, 0), (1, 31, 0, 0)] pegasus_coords = defragment_tuple(chimera_coords) expected_pegasus_coords = {(0, 2, 4, 2), (1, 4, 0, 4), (0, 5, 2, 0)} self.assertEqual(expected_pegasus_coords, set(pegasus_coords))
def test_consistent_linear_nice_pegasus(self): P4 = dnx.pegasus_graph(4, nice_coordinates=True) coords = pegasus_coordinates(4) # `.*_to_*` methods for n, data in P4.nodes(data=True): p = data['pegasus_index'] i = data['linear_index'] self.assertEqual(coords.nice_to_pegasus(n), p) self.assertEqual(coords.pegasus_to_nice(p), n) self.assertEqual(coords.nice_to_linear(n), i) self.assertEqual(coords.linear_to_nice(i), n) self.assertEqual(coords.linear_to_pegasus(i), p) self.assertEqual(coords.pegasus_to_linear(p), i) # `.iter_*_to_*` methods nlist, plist, ilist = zip(*((n, d['pegasus_index'], d['linear_index']) for n, d in P4.nodes(data=True))) self.assertEqual(tuple(coords.iter_nice_to_pegasus(nlist)), plist) self.assertEqual(tuple(coords.iter_pegasus_to_nice(plist)), nlist) self.assertEqual(tuple(coords.iter_nice_to_linear(nlist)), ilist) self.assertEqual(tuple(coords.iter_linear_to_nice(ilist)), nlist) self.assertEqual(tuple(coords.iter_pegasus_to_linear(plist)), ilist) self.assertEqual(tuple(coords.iter_linear_to_pegasus(ilist)), plist) # `.iter_*_to_*_pairs` methods nedgelist = [] pedgelist = [] iedgelist = [] for u, v in P4.edges: nedgelist.append((u, v)) pedgelist.append((P4.nodes[u]['pegasus_index'], P4.nodes[v]['pegasus_index'])) iedgelist.append((P4.nodes[u]['linear_index'], P4.nodes[v]['linear_index'])) self.assertEqual(list(coords.iter_nice_to_pegasus_pairs(nedgelist)), pedgelist) self.assertEqual(list(coords.iter_pegasus_to_nice_pairs(pedgelist)), nedgelist) self.assertEqual(list(coords.iter_nice_to_linear_pairs(nedgelist)), iedgelist) self.assertEqual(list(coords.iter_linear_to_nice_pairs(iedgelist)), nedgelist) self.assertEqual(list(coords.iter_pegasus_to_linear_pairs(pedgelist)), iedgelist) self.assertEqual(list(coords.iter_linear_to_pegasus_pairs(iedgelist)), pedgelist)
def test_single_horizontal_coordinate(self): # Set up fragmentation function pg = dnx.pegasus_graph(2) fragment_tuple = get_tuple_fragmentation_fn(pg) # Fragment pegasus coordinates pegasus_coord = (1, 0, 0, 0) fragments = fragment_tuple([pegasus_coord]) expected_fragments = {(0, 3, 1, 0), (0, 4, 1, 0), (0, 5, 1, 0), (0, 6, 1, 0), (0, 7, 1, 0), (0, 8, 1, 0)} self.assertEqual(expected_fragments, set(fragments))
def test_nice_coordinates(self): G = dnx.pegasus_graph(4, nice_coordinates=True) H = dnx.chimera_graph(3, coordinates=True) for p, q in H.edges(): for t in range(3): pg = (t, ) + p qg = (t, ) + q self.assertTrue(G.has_edge(pg, qg)) n2p = get_nice_to_pegasus_fn() p2n = get_pegasus_to_nice_fn() for p in G.nodes(): self.assertEqual(p2n(*n2p(*p)), p) self.assertTrue(H.has_node(p[1:]))
def embed_qubo_pegasus(Q_matrix, plotIt = False): connectivity_structure = dnx.pegasus_graph(15) # try to minimize G = nx.from_numpy_matrix(Q_matrix) max_chain_length = 0 while(max_chain_length == 0): embedded_graph = minorminer.find_embedding(G.edges(), connectivity_structure) for _, chain in embedded_graph.items(): if len(chain) > max_chain_length: max_chain_length = len(chain) print("max_chain_length", max_chain_length) # try to minimize if plotIt: dnx.draw_chimera_embedding(connectivity_structure, embedded_graph) plt.show()
def test_nice_coordinates(self): p = dnx.pegasus_graph(3, nice_coordinates=True) c = dnx.chimera_graph(24, coordinates=True) num_edges = 0 for u, v in fragmented_edges(p): self.assertTrue(c.has_edge(u, v)) num_edges += 1 #This is a weird edgecount: each node produces 5 extra edges for the internal connections #between fragments corresponding to a pegasus qubit. But then we need to delete the odd #couplers, which aren't included in the chimera graph -- odd couplers make a perfect #matching, so thats 1/2 an edge per node. self.assertEqual(p.number_of_edges() + 9 * p.number_of_nodes() // 2, num_edges)