示例#1
0
 def test_digraph_distance_matrix_node_hole(self):
     graph = retworkx.generators.directed_path_graph(4)
     graph.remove_node(0)
     dist = retworkx.digraph_distance_matrix(graph)
     expected = np.array([[0.0, 1.0, 2.0], [0.0, 0.0, 1.0], [0.0, 0.0,
                                                             0.0]])
     self.assertTrue(np.array_equal(dist, expected))
示例#2
0
    def _compute_distance_matrix(self):
        """Compute the full distance matrix on pairs of nodes.

        The distance map self._dist_matrix is computed from the graph using
        all_pairs_shortest_path_length.
        """
        if not self.is_connected():
            raise CouplingError("coupling graph not connected")
        self._dist_matrix = rx.digraph_distance_matrix(self.graph, as_undirected=True)
示例#3
0
 def test_digraph_distance_matrix_as_undirected(self):
     graph = retworkx.PyDiGraph()
     graph.add_nodes_from(list(range(7)))
     graph.add_edges_from_no_data([(0, 1), (0, 6), (1, 2), (2, 3), (3, 4),
                                   (4, 5), (5, 6)])
     dist = retworkx.digraph_distance_matrix(graph, as_undirected=True)
     expected = numpy.array([[0., 1., 2., 3., 3., 2., 1.],
                             [1., 0., 1., 2., 3., 3., 2.],
                             [2., 1., 0., 1., 2., 3., 3.],
                             [3., 2., 1., 0., 1., 2., 3.],
                             [3., 3., 2., 1., 0., 1., 2.],
                             [2., 3., 3., 2., 1., 0., 1.],
                             [1., 2., 3., 3., 2., 1., 0.]])
     self.assertTrue(numpy.array_equal(dist, expected))
示例#4
0
 def test_digraph_distance_matrix_parallel(self):
     graph = retworkx.PyDiGraph()
     graph.add_nodes_from(list(range(7)))
     graph.add_edges_from_no_data([(0, 1), (0, 6), (1, 2), (2, 3), (3, 4),
                                   (4, 5), (5, 6)])
     dist = retworkx.digraph_distance_matrix(graph, parallel_threshold=5)
     expected = numpy.array([[0., 1., 2., 3., 4., 5., 1.],
                             [0., 0., 1., 2., 3., 4., 5.],
                             [0., 0., 0., 1., 2., 3., 4.],
                             [0., 0., 0., 0., 1., 2., 3.],
                             [0., 0., 0., 0., 0., 1., 2.],
                             [0., 0., 0., 0., 0., 0., 1.],
                             [0., 0., 0., 0., 0., 0., 0.]])
     self.assertTrue(numpy.array_equal(dist, expected))
示例#5
0
    def compute_distance_matrix(self):
        """Compute the full distance matrix on pairs of nodes.

        The distance map self._dist_matrix is computed from the graph using
        all_pairs_shortest_path_length. This is normally handled internally
        by the :attr:`~qiskit.transpiler.CouplingMap.distance_matrix`
        attribute or the :meth:`~qiskit.transpiler.CouplingMap.distance` method
        but can be called if you're accessing the distance matrix outside of
        those or want to pre-generate it.
        """
        if self._dist_matrix is None:
            if not self.is_connected():
                raise CouplingError("coupling graph not connected")
            self._dist_matrix = rx.digraph_distance_matrix(self.graph,
                                                           as_undirected=True)
示例#6
0
 def test_digraph_distance_matrix(self):
     graph = retworkx.PyDiGraph()
     graph.add_nodes_from(list(range(7)))
     graph.add_edges_from_no_data([(0, 1), (0, 6), (1, 2), (2, 3), (3, 4),
                                   (4, 5), (5, 6)])
     dist = retworkx.digraph_distance_matrix(graph)
     expected = np.array([
         [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 1.0],
         [0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0],
         [0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0],
         [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0],
         [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 2.0],
         [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
         [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
     ])
     self.assertTrue(np.array_equal(dist, expected))
示例#7
0
 def test_digraph_distance_matrix_parallel_as_undirected(self):
     graph = retworkx.PyDiGraph()
     graph.add_nodes_from(list(range(7)))
     graph.add_edges_from_no_data([(0, 1), (0, 6), (1, 2), (2, 3), (3, 4),
                                   (4, 5), (5, 6)])
     dist = retworkx.digraph_distance_matrix(graph,
                                             parallel_threshold=5,
                                             as_undirected=True)
     expected = np.array([
         [0.0, 1.0, 2.0, 3.0, 3.0, 2.0, 1.0],
         [1.0, 0.0, 1.0, 2.0, 3.0, 3.0, 2.0],
         [2.0, 1.0, 0.0, 1.0, 2.0, 3.0, 3.0],
         [3.0, 2.0, 1.0, 0.0, 1.0, 2.0, 3.0],
         [3.0, 3.0, 2.0, 1.0, 0.0, 1.0, 2.0],
         [2.0, 3.0, 3.0, 2.0, 1.0, 0.0, 1.0],
         [1.0, 2.0, 3.0, 3.0, 2.0, 1.0, 0.0],
     ])
     self.assertTrue(np.array_equal(dist, expected))