Example #1
0
    def test_graph_mapped(self, graph):
        """Test if function returns correctly processed subgraphs given input samples of the list
        ``quantum_samples``. Note that graph nodes are numbered in this test as [0, 1, 4, 9,
        ...] (i.e., squares of the usual list) as a simple mapping to explore that the optimised
        subgraph returned is still a valid subgraph."""
        graph = nx.relabel_nodes(graph, lambda x: x ** 2)
        graph_nodes = list(graph.nodes)
        subgraphs_mapped = [
            sorted([graph_nodes[i] for i in subgraph]) for subgraph in self.subgraphs
        ]

        assert sample.to_subgraphs(self.quantum_samples, graph) == subgraphs_mapped
Example #2
0
 def test_graph(self, graph):
     """Test if function returns correctly processed subgraphs given input samples of the list
     ``quantum_samples``."""
     assert sample.to_subgraphs(
         graph, samples=self.quantum_samples) == self.subgraphs
maximal_clique = [4, 11, 12, 18]
maximal_fig = plot.graph(TA_graph, maximal_clique)
plotly.offline.plot(maximal_fig, filename="maximal_clique.html")

##############################################################################
# .. raw:: html
#     :file: ../../examples_gbs/maximal_clique.html

##############################################################################
# We'll now use the :mod:`~.gbs.clique` module to find larger cliques in the graph. We can make
# use of the pre-generated samples from the TACE-AS graph in the :mod:`~.gbs.data` module and post-select samples with a specific number of clicks. For
# this tutorial, we'll look at samples with eight clicks, of which there are a total of 1,984:

postselected = sample.postselect(TA, 8, 8)
samples = sample.to_subgraphs(postselected, TA_graph)
print(len(samples))

##############################################################################
# GBS produces samples that correspond to subgraphs of high density. For fun, let's confirm this
# by comparing the average subgraph density in the GBS samples to uniformly generated samples:

GBS_dens = []
u_dens = []

for s in samples:
    uniform = list(np.random.choice(24, 8, replace=False))  # generates uniform sample
    GBS_dens.append(nx.density(TA_graph.subgraph(s)))
    u_dens.append(nx.density(TA_graph.subgraph(uniform)))

print("GBS mean density = {:.4f}".format(np.mean(GBS_dens)))
min_clicks = 3
max_clicks = 4

s = sample.postselect(s, min_clicks, max_clicks)

print(len(s))
s.append([0, 1, 0, 1, 1, 0])

##############################################################################
# As expected, we have fewer samples than before. The number of samples that survive this
# postselection is determined by the mean photon number in GBS. We have also added in our example
# sample ``[0, 1, 0, 1, 1, 0]`` to ensure that there is at least one for the following.
#
# Let's convert our postselected samples to subgraphs:

subgraphs = sample.to_subgraphs(s, graph)

print(subgraphs)

##############################################################################
# We can take a look at one of the sampled subgraphs:

plotly.offline.plot(plot.plot_graph(graph, subgraphs[0]),
                    filename="subgraph.html")

##############################################################################
# .. raw:: html
#     :file: ../../examples_gbs/subgraph.html
#
# These sampled subgraphs act as the starting point for some of the applications made available
# in Strawberry Fields, including the maximum clique and dense subgraph identification problems.
Example #5
0
# In this tutorial, we'll study a 30-node graph with a planted 10-node graph, as considered in
# :cite:`arrazola2018using`. The graph is generated by joining two Erdős–Rényi random graphs. The
# first graph of 20 nodes is created with edge probability of 0.5. The second planted
# graph is generated with edge probability of 0.875. The planted nodes are the last ten nodes in the
# graph. The two graphs are joined by selecting 8 nodes at random from both graphs and adding an
# edge between them. This graph has the sneaky property that even though the planted subgraph is the
# densest of its size, its nodes have a lower average degree than the nodes in the rest of the
# graph.
#
# 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::