예제 #1
0
def show_all_graphs():
    """
    [show_all_graphs ()] displays all the significant graphs generated to
    get [union_graph]
    """
    # [graph]
    graph_fig = plot.graph(graph)
    graph_fig.show()
    # [dense_graph]
    dense_graph_fig = plot.graph(dense_graph)
    dense_graph_fig.show()
    # [unconnected_graph]
    unconnected_graph_fig = plot.graph(union_graph, sub)
    unconnected_graph_fig.show()
    # [union_graph]
    union_graph_fig = plot.graph(union_graph, sub)
    union_graph_fig.show()
#
# The :mod:`~.apps.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_apps/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:`~.apps.subgraph` module: to identify
# collections of dense subgraphs for a range of sizes. The output of this function is a
The first step is to import the Strawberry Fields ``apps`` module and external dependencies:
"""
from strawberryfields.apps 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:`~.apps.data` module and the
# graph can be visualized using the :mod:`~.apps.plot` module:

TA = data.TaceAs()
A = TA.adj
TA_graph = nx.Graph(A)
plot_graph = plot.graph(TA_graph)
plotly.offline.plot(plot_graph, filename="TACE-AS.html")

##############################################################################
# .. raw:: html
#     :file: ../../examples_apps/TACE-AS.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.

##############################################################################
# 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:
예제 #4
0
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_apps/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:`~.apps.sample.sample`
예제 #5
0
def show_union_graph():
    """
    [show_union_graph] displays the final conjoined graph [union_graph]
    """
    union_graph_fig = plot.graph(union_graph, sub)
    union_graph_fig.show()
예제 #6
0
--------------------------------------------------------------------

# import packages
from strawberryfields.apps import data, plot, sample, clique
import numpy as np
import networkx as nx
import plotly

---------------------------------------------------------------------

# plot graph (randomly generated)
PH = data.PHat()  
A = PH.adj
PH_graph = nx.Graph(A)
plot_graph = plot.graph(PH_graph)
plot_graph.show()

# Complete GBS (the mean density of the subgraphs samples by GBS > the uniform mean density! This means less searching for the classical algorithms)
GBS_dens = []
u_dens = []

for s in samples:
    uniform = list(np.random.choice(16, 20))  
    GBS_dens.append(nx.density(PH_graph.subgraph(s)))
    u_dens.append(nx.density(PH_graph.subgraph(uniform)))

print("GBS mean density = {:.4f}".format(np.mean(GBS_dens)))
print("Uniform mean density = {:.4f}".format(np.mean(u_dens)))
# GBS mean density = 0.3714
# Uniform mean density = 0.1673
예제 #7
0
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:`~.apps.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_apps/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")