def test_dijkstra_path_with_weight_fn_and_target(self): paths = retworkx.digraph_dijkstra_shortest_paths( self.graph, self.a, target=self.e, weight_fn=lambda x: x) expected = { 4: [0, 3, 4], } self.assertEqual(expected, paths)
def test_dijkstra_path_with_no_path(self): g = retworkx.PyDiGraph() a = g.add_node('A') g.add_node('B') path = retworkx.digraph_dijkstra_shortest_paths(g, a) expected = {} self.assertEqual(expected, path)
def test_dijkstra_path_with_target(self): paths = retworkx.digraph_dijkstra_shortest_paths(self.graph, self.a, target=self.e) expected = { 4: [0, 3, 4], } self.assertEqual(expected, paths)
def test_dijkstra_path_undirected(self): paths = retworkx.digraph_dijkstra_shortest_paths(self.graph, self.a, as_undirected=True) expected = { 1: [0, 1], 2: [0, 2], 3: [0, 3], 4: [0, 3, 4], 5: [0, 1, 5], } self.assertEqual(expected, paths)
def test_dijkstra_path_with_weight_fn(self): paths = retworkx.digraph_dijkstra_shortest_paths( self.graph, self.a, weight_fn=lambda x: x) expected = { 1: [0, 1], 2: [0, 1, 2], 3: [0, 3], 4: [0, 3, 4], 5: [0, 1, 5], } self.assertEqual(expected, paths)
def test_dijkstra_path(self): paths = retworkx.digraph_dijkstra_shortest_paths(self.graph, self.a) expected = { # a -> b 1: [0, 1], # a -> c: a, d, c 2: [0, 3, 2], # a -> d 3: [0, 3], # a -> e: a, d, e 4: [0, 3, 4], # a -> f: a, b, f 5: [0, 1, 5], } self.assertEqual(expected, paths)
def shortest_undirected_path(self, physical_qubit1, physical_qubit2): """Returns the shortest undirected path between physical_qubit1 and physical_qubit2. Args: physical_qubit1 (int): A physical qubit physical_qubit2 (int): Another physical qubit Returns: List: The shortest undirected path Raises: CouplingError: When there is no path between physical_qubit1, physical_qubit2. """ paths = rx.digraph_dijkstra_shortest_paths(self.graph, source=physical_qubit1, target=physical_qubit2, as_undirected=True) if not paths: raise CouplingError("Nodes %s and %s are not connected" % (str(physical_qubit1), str(physical_qubit2))) return paths[physical_qubit2]
def has_path(self, a, b): """Checks if a path exists between the two given nodes. Args: a (Operator): initial node b (Operator): final node Returns: bool: returns ``True`` if a path exists """ if a == b: return True return (len( rx.digraph_dijkstra_shortest_paths( self._graph, self._graph.nodes().index(a), self._graph.nodes().index(b), weight_fn=None, default_weight=1.0, as_undirected=False, )) != 0)