m2_a = m2.adj m3_a = m3.adj ############################################################################## # Samples from these graphs can be accessed by indexing: print(m0[0]) ############################################################################## # We can now plot the four graphs using the :mod:`~.gbs.plot` module. To use this module, # we need to convert the adjacency matrices into NetworkX Graphs: import networkx as nx import plotly plot_mutag_0 = plot.graph(nx.Graph(m0_a)) plot_mutag_1 = plot.graph(nx.Graph(m1_a)) plot_mutag_2 = plot.graph(nx.Graph(m2_a)) plot_mutag_3 = plot.graph(nx.Graph(m3_a)) plotly.offline.plot(plot_mutag_0, filename="MUTAG_0.html") ############################################################################## # .. raw:: html # :file: ../../examples_gbs/MUTAG_0.html # .. note:: # The command ``plotly.offline.plot()`` is used to display plots in the documentation. In # practice, you can simply use ``plot_mutag_0.show()`` to view your graph. plotly.offline.plot(plot_mutag_1, filename="MUTAG_1.html")
The first step is to import the Strawberry Fields GBS module and external dependencies: """ from strawberryfields.gbs import data, plot, sample, clique import numpy as np import networkx as nx import plotly ############################################################################## # The adjacency matrix of the TACE-AS graph can be loaded from the :mod:`~.gbs.data` module and the # graph can be visualized using the :mod:`~.gbs.plot` module: TA = data.TaceAs() A = TA.adj TA_graph = nx.Graph(A) graph_fig = plot.graph(TA_graph) plotly.offline.plot(graph_fig, filename="TACE-AS.html") ############################################################################## # .. raw:: html # :file: ../../examples_gbs/TACE-AS.html ############################################################################## # Can you spot any cliques in the graph? It's not so easy using only your eyes! The TACE-AS graph # is sufficiently small that all cliques can be found by performing an exhaustive search over # all subgraphs. For example, below we highlight a small *maximal* clique, i.e., a clique # not contained inside another clique: maximal_clique = [4, 11, 12, 18] maximal_fig = plot.graph(TA_graph, maximal_clique) plotly.offline.plot(maximal_fig, filename="maximal_clique.html")
import plotly adj = np.array( [ [0, 1, 0, 0, 1, 1], [1, 0, 1, 0, 1, 1], [0, 1, 0, 1, 1, 0], [0, 0, 1, 0, 1, 0], [1, 1, 1, 1, 0, 1], [1, 1, 0, 0, 1, 0], ] ) graph = nx.Graph(adj) plotly.offline.plot(plot.graph(graph), filename="random_graph.html") ############################################################################## # .. raw:: html # :file: ../../examples_gbs/random_graph.html # # This is a 6-node graph with the nodes ``[0, 1, 4, 5]`` fully connected to each other. We expect # to be able to sample dense subgraphs with high probability. # # Samples can be generated from this graph through GBS using the :func:`~.gbs.sample.sample` # function: n_mean = 4 samples = 20 s = sample.sample(adj, n_mean, samples)
from strawberryfields.gbs import plot import networkx as nx import plotly adj = np.array([ [0, 1, 0, 0, 1, 1], [1, 0, 1, 0, 1, 1], [0, 1, 0, 1, 1, 0], [0, 0, 1, 0, 1, 0], [1, 1, 1, 1, 0, 1], [1, 1, 0, 0, 1, 0], ]) graph = nx.Graph(adj) plot_graph = plot.graph(graph) plotly.offline.plot(plot_graph, filename="random_graph.html") ############################################################################## # .. raw:: html # :file: ../../examples_gbs/random_graph.html # # .. note:: # The command ``plotly.offline.plot()`` is used to display plots in the documentation. In # practice, you can simply use ``plot_graph.show()`` to view your graph. # # This is a 6-node graph with the nodes ``[0, 1, 4, 5]`` fully connected to each other. We expect # to be able to sample dense subgraphs with high probability. # # Samples can be generated from this graph through GBS using the :func:`~.gbs.sample.sample`
# # The :mod:`~.gbs.data` module has pre-generated GBS samples from this graph. Let's load them, # postselect on samples with a large number of clicks, and convert them to subgraphs: planted = data.Planted() postselected = sample.postselect(planted, 16, 30) pl_graph = nx.to_networkx_graph(planted.adj) samples = sample.to_subgraphs(postselected, pl_graph) print(len(samples)) ############################################################################## # Not bad! We have more than 2000 samples to play with 😎. The planted subgraph is actually easy to # identify; it even appears clearly from the force-directed Kamada-Kawai algorithm that is used to # plot graphs in Strawberry Fields: sub = list(range(20, 30)) plot_graph = plot.graph(pl_graph, sub) plotly.offline.plot(plot_graph, filename="planted.html") ############################################################################## # .. raw:: html # :file: ../../examples_gbs/planted.html # # .. note:: # The command ``plotly.offline.plot()`` is used to display plots in the documentation. In # practice, you can simply use ``plot_graph.show()`` to view the graph. ############################################################################## # A more interesting challenge is to find dense subgraphs of different sizes; it is often # useful to identify many high-density subgraphs, not just the densest ones. This is the purpose of # the :func:`~.subgraph.search` function in the :mod:`~.gbs.subgraph` module: to identify # collections of dense subgraphs for a range of sizes. The output of this function is a