예제 #1
0
    def test_draw_edges_min_source_target_margins(self):
        """Test that there is a wider gap between the node and the start of an
        incident edge when min_source_margin is specified.

        This test checks that the use of min_{source/target}_margin kwargs
        result in shorter (more padding) between the edges and source and
        target nodes. As a crude visual example, let 's' and 't' represent
        source and target nodes, respectively:
           Default:
           s-----------------------------t
           With margins:
           s   -----------------------   t
        """
        node_shapes = ["o", "s"]
        graph = retworkx.PyGraph()
        graph.extend_from_edge_list([(0, 1)])
        pos = {0: (0, 0), 1: (1, 0)}  # horizontal layout

        for node_shape in node_shapes:
            with self.subTest(shape=node_shape):
                fig, ax = plt.subplots()
                mpl_draw(
                    graph,
                    pos=pos,
                    ax=ax,
                    node_shape=node_shape,
                    min_source_margin=100,
                    min_target_margin=100,
                )
                _save_images(fig, "test_node_shape_%s.png" % node_shape)
예제 #2
0
    def _mpl(graph: PyGraph, self_loop: bool, **kwargs):
        """
        Auxiliary function for drawing the lattice using matplotlib.

        Args:
            graph : graph to be drawn.
            self_loop : Draw self-loops, which are edges connecting a node to itself.
            **kwargs : Kwargs for drawing the lattice.

        Raises:
            MissingOptionalLibraryError: Requires matplotlib.
        """
        if not HAS_MATPLOTLIB:
            raise MissingOptionalLibraryError(
                libname="Matplotlib", name="_mpl", pip_install="pip install matplotlib"
            )
        from matplotlib import pyplot as plt

        if not self_loop:
            self_loops = [(i, i) for i in range(graph.num_nodes()) if graph.has_edge(i, i)]
            graph.remove_edges_from(self_loops)

        mpl_draw(
            graph=graph,
            **kwargs,
        )
        plt.draw()
예제 #3
0
    def _mpl(graph: PyGraph, self_loop: bool, **kwargs):
        """
        Auxiliary function for drawing the lattice using matplotlib.

        Args:
            graph : graph to be drawn.
            self_loop : Draw self-loops, which are edges connecting a node to itself.
            **kwargs : Kwargs for drawing the lattice.

        Raises:
            MissingOptionalLibraryError: Requires matplotlib.
        """
        # pylint: disable=unused-import
        from matplotlib import pyplot as plt

        if not self_loop:
            self_loops = [(i, i) for i in range(graph.num_nodes())
                          if graph.has_edge(i, i)]
            graph.remove_edges_from(self_loops)

        mpl_draw(
            graph=graph,
            **kwargs,
        )
        plt.draw()
예제 #4
0
 def test_selfloop_with_single_edge_in_edge_list(self):
     fig, ax = plt.subplots()
     # Graph with selfloop
     graph = retworkx.generators.path_graph(2)
     graph.add_edge(1, 1, None)
     pos = {n: (n, n) for n in graph.node_indexes()}
     mpl_draw(graph, pos, ax=ax, edge_list=[(1, 1)])
     _save_images(fig, "test_self_loop.png")
예제 #5
0
 def test_labels_and_colors(self):
     graph = retworkx.PyGraph()
     graph.add_nodes_from(list(range(8)))
     edge_list = [
         (0, 1, 5),
         (1, 2, 2),
         (2, 3, 7),
         (3, 0, 6),
         (5, 6, 1),
         (4, 5, 7),
         (6, 7, 3),
         (7, 4, 7),
     ]
     labels = {}
     labels[0] = r"$a$"
     labels[1] = r"$b$"
     labels[2] = r"$c$"
     labels[3] = r"$d$"
     labels[4] = r"$\alpha$"
     labels[5] = r"$\beta$"
     labels[6] = r"$\gamma$"
     labels[7] = r"$\delta$"
     graph.add_edges_from(edge_list)
     pos = retworkx.random_layout(graph)
     mpl_draw(
         graph,
         pos=pos,
         node_list=[0, 1, 2, 3],
         node_color="r",
         edge_list=[(0, 1), (1, 2), (2, 3), (3, 0)],
         node_size=500,
         alpha=0.75,
         width=1.0,
         labels=lambda x: labels[x],
         font_size=16,
     )
     mpl_draw(
         graph,
         pos=pos,
         node_list=[4, 5, 6, 7],
         node_color="b",
         node_size=500,
         alpha=0.5,
         edge_list=[(4, 5), (5, 6), (6, 7), (7, 4)],
         width=8,
         edge_color="r",
         rotate=False,
         edge_labels=lambda edge: labels[edge],
     )
     fig = plt.gcf()
     _save_images(fig, "test_labels_and_colors.png")
예제 #6
0
    def draw_2d_error_graph(self, graph):
        """Draws a 2d Error Graph.

        Args:
            graph (retworkx.PyGraph) : Error Graph to be visualised.

        Returns:
            retworkx: A 2-d graph.

        Raises:
            QiskitError: If matplotlib is not installed.
        """
        plt.figure(figsize=(self.height, self.width))
        if not HAS_MATPLOTLIB:
            raise QiskitError('please install matplotlib')
        pos = {}
        for i in graph.node_indexes():
            if graph[i][0] == 0:
                pos[i] = (graph[i][1], graph[i][2])
            else:
                pos[i] = (graph[i][1], graph[i][2] + 2)
        return mpl_draw(
            graph,
            pos=pos,
            with_labels=True,
            node_color='red',
            labels=lambda node: str(node),  # pylint: disable=W0108
            edge_labels=lambda edge: str(abs(edge)))
예제 #7
0
 def test_edge_colormap(self):
     graph = retworkx.generators.star_graph(24)
     colors = range(len(graph.edge_list()))
     fig = mpl_draw(
         graph,
         edge_color=colors,
         width=4,
         edge_cmap=plt.cm.Blues,
         with_labels=True,
     )
     _save_images(fig, "test_edge_colors.png")
예제 #8
0
    def draw_2d_decoded_graph(self, graph, edgelist, neutral_nodelist):
        """Draws a 2d Decoded Graph.

        Args:
            graph (retworkx.PyGraph) : Decoded Graph to be visualised

            edgelist (list) : List of matched edges.

            neutral_nodelist (list) : List of matched nodes.

        Returns:
            networkx: A 2-d graph.

        Raises:
            QiskitError: If matplotlib is not installed.
        """
        plt.figure(figsize=(self.height, self.width))
        if not HAS_MATPLOTLIB:
            raise QiskitError('please install matplotlib')
        graph_c = graph.copy()
        pos = {}
        for i in graph_c.node_indexes():
            if graph[i][0] == 0:
                pos[i] = (graph[i][1], graph[i][2])
            else:
                pos[i] = (graph[i][1], graph[i][2] + 2)
        edges = graph.edge_list()
        for edge in edges:
            if (graph[edge[0]], graph[edge[1]]) in edgelist or (
                    graph[edge[1]], graph[edge[0]]) in edgelist:
                pass
            else:
                graph_c.remove_edge(edge[0], edge[1])
        nodes = graph.nodes()
        for node in nodes:
            if node not in neutral_nodelist:
                graph_c.remove_node(graph.nodes().index(node))
        return mpl_draw(
            graph_c,
            pos=pos,
            with_labels=True,
            node_color='blue',
            labels=lambda node: str(node),  # pylint: disable=W0108
            edge_labels=lambda edge: str(abs(edge)))
예제 #9
0
 def test_alpha_iter(self):
     graph = retworkx.generators.grid_graph(4, 6)
     # with fewer alpha elements than nodes
     plt.subplot(131)
     mpl_draw(graph, alpha=[0.1, 0.2])
     # with equal alpha elements and nodes
     num_nodes = len(graph)
     alpha = [x / num_nodes for x in range(num_nodes)]
     colors = range(num_nodes)
     plt.subplot(132)
     mpl_draw(graph, node_color=colors, alpha=alpha)
     # with more alpha elements than nodes
     alpha.append(1)
     plt.subplot(133)
     mpl_draw(graph, alpha=alpha)
     fig = plt.gcf()
     _save_images(fig, "test_alpha_iter.png")
예제 #10
0
 def test_axes(self):
     fig, ax = plt.subplots()
     graph = retworkx.directed_gnp_random_graph(50, 0.75)
     mpl_draw(graph, ax=ax)
     _save_images(fig, "test_axes.png")
예제 #11
0
 def test_empty_graph(self):
     graph = retworkx.PyGraph()
     fig = mpl_draw(graph)
     _save_images(fig, "test_empty.png")
예제 #12
0
 def test_arrows(self):
     graph = retworkx.generators.directed_star_graph(24)
     fig = mpl_draw(graph)
     _save_images(fig, "test_arrows.png")
예제 #13
0
 def test_node_list(self):
     graph = retworkx.generators.star_graph(24)
     node_list = list(range(4)) + list(range(4, 10)) + list(range(10, 14))
     fig = mpl_draw(graph, node_list=node_list)
     _save_images(fig, "test_node_list.png")
예제 #14
0
 def test_draw(self):
     graph = retworkx.generators.star_graph(24)
     options = {"node_color": "black", "node_size": 100, "width": 3}
     fig = mpl_draw(graph, **options)
     _save_images(fig, "test.png")