# Graph similarity¶

Technical details are available in the API documentation: sf.apps.similarity

This page looks at how to use GBS to construct a similarity measure between graphs, known as a graph kernel [51]. Kernels can be applied to graph-based data for machine learning tasks such as classification using a support vector machine.

## Graph data¶

We begin by fixing a dataset of graphs to consider and loading GBS samples from these graphs, which will be needed in the following.

Let’s use the MUTAG dataset of graphs [46][47]. This is a dataset of 188 different graphs that each correspond to the structure of a chemical compound. Our goal is to use GBS samples from these graphs to measure their similarity.

The data module provides pre-calculated GBS samples for selected graphs in the MUTAG dataset. Each set of samples is generated by encoding the graph into a GBS device, and collecting photon click events. We’ll start by loading four sets of samples and visualizing the corresponding graphs.

from strawberryfields.apps import data, plot, similarity

m0 = data.Mutag0()
m1 = data.Mutag1()
m2 = data.Mutag2()
m3 = data.Mutag3()


These datasets contain both the adjacency matrix of the graph and the samples generated through GBS. We can access the adjacency matrix through:

m0_a = m0.adj


Samples from these graphs can be accessed by indexing:

print(m0[0])


Out:

[0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]


We can now plot the four graphs using the 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")