# Graph similarity

**URL:** <https://discuss.pennylane.ai/t/graph-similarity/1121>\
**Category:** PennyLane Strawberry Fields\
**Created:** [June 18, 2021, 4:26pm UTC](https://discuss.pennylane.ai/t/graph-similarity/1121 "2021-06-18T16:26:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [June 18, 2021, 4:26pm UTC](https://discuss.pennylane.ai/t/graph-similarity/1121/1 "2021-06-18T16:26:16Z")

</div>

Hi.

In the tutorial Graph Similarity it is stated “Each set of samples is generated by encoding the graph into a GBS device, and collecting photon click events.” How do I do that for default sets of graphs?

Thank you,  
Best

---

<div class="post-metadata">

**Author:** ![Quinten-Blake](https://avatars.discourse-cdn.com/v4/letter/q/ac91a4/32.png) [@Quinten-Blake](https://discuss.pennylane.ai/u/Quinten-Blake)\
**Post date:** [June 18, 2021, 9:36pm UTC](https://discuss.pennylane.ai/t/graph-similarity/1121/2 "2021-06-18T21:36:46Z")

</div>

Hi @_risto,

Thanks for the question.

Could you explain a bit more what you mean by “default sets of graphs”?

There are some pre-generated examples applicable to similarity for the Mutag dataset included within Strawberry Fields. Is this what you mean by “default” (i.e., the built-in ones), or do you have something else in mind? 🤔

Depending on what you’re after, it may be as simple as just using [feature\_vector\_orbits](https://strawberryfields.readthedocs.io/en/stable/code/api/strawberryfields.apps.similarity.feature_vector_orbits.html), which works for an arbitrary input graph, or to first generate samples using [sample](https://strawberryfields.readthedocs.io/en/stable/code/api/strawberryfields.apps.sample.sample.html) and then convert those samples into a feature vector with [feature\_vector\_orbits\_sampling](https://strawberryfields.readthedocs.io/en/stable/code/api/strawberryfields.apps.similarity.feature_vector_orbits_sampling.html).

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [June 19, 2021, 2:15am UTC](https://discuss.pennylane.ai/t/graph-similarity/1121/3 "2021-06-19T02:15:09Z")

</div>

Hi @Quinten-Blake

Thank you for your answer. Will first try what you suggested and will get back to you 🙂

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [June 20, 2021, 3:25am UTC](https://discuss.pennylane.ai/t/graph-similarity/1121/4 "2021-06-20T03:25:18Z")

</div>

I want to use quantum computer you provide on cloud to calculate similarities between graphs of my own choice (not the MUTAG, but any graphs I choose).  
1.) What is the difference between the two methods you mentioned?  
2.) Which one should I use, if I want to run the algorithm on actual quantum device you provide - and can I use this method on my own graphs?  
3.) I use [https://strawberryfields.ai/photonics/demos/tutorial\_X8\_demos.html](https://strawberryfields.ai/photonics/demos/tutorial_X8_demos.html) only after I have obtained feature vectors in [https://strawberryfields.ai/photonics/apps/run\_tutorial\_similarity.html](https://strawberryfields.ai/photonics/apps/run_tutorial_similarity.html) or how do I combine those two tutorials?

---

<div class="post-metadata">

**Author:** ![Tom\_Bromley](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/tom_bromley/32/129_2.png) [@Tom\_Bromley](https://discuss.pennylane.ai/u/Tom_Bromley)\
**Post date:** [June 21, 2021, 6:50pm UTC](https://discuss.pennylane.ai/t/graph-similarity/1121/5 "2021-06-21T18:50:49Z")

</div>

Hey @_risto!

> 1.) What is the difference between the two methods you mentioned?

The `feature_vector_orbits` calculates the orbit probabilities using a brute-force classical simulation of GBS, while the `feature_vector_orbits_sampling` infers the probabilities from input samples (which can be generated on simulator or hardware). Check out the [demo](https://strawberryfields.ai/photonics/apps/run_tutorial_similarity.html#calculating-a-feature-vector) for more of an understanding.

> 2.) Which one should I use, if I want to run the algorithm on actual quantum device you provide - and can I use this method on my own graphs?
> 
> 3.) I use [Quantum algorithms on the Xanadu quantum cloud — Strawberry Fields](https://strawberryfields.ai/photonics/demos/tutorial_X8_demos.html) only after I have obtained feature vectors in [Graph similarity — Strawberry Fields](https://strawberryfields.ai/photonics/apps/run_tutorial_similarity.html) or how do I combine those two tutorials?

`feature_vector_orbits_sampling` should be the approach you choose for hardware compatibility. In this approach you should first generate your samples using GBS, and then pass them to `feature_vector_orbits_sampling`. Hence, you should follow the code in the [X8 tutorial](https://strawberryfields.ai/photonics/demos/tutorial_X8_demos.html#gaussian-boson-sampling) to generate samples, and then pass the result through `feature_vector_orbits_sampling`.

However, while the above approach can work in principle for arbitrary graphs of your choice, the main restriction is due to the current hardware. As you can see [here](https://strawberryfields.ai/photonics/demos/tutorial_X8.html#embedding-bipartite-graphs), the range of graphs supported on the present X8 chips is quite limited, i.e., bipartite graphs with a special type of spectrum. If your own graphs satisfy this criteria, then you should be able to use the hardware. Otherwise, it may be best to use the simulator for now.

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [June 22, 2021, 12:15am UTC](https://discuss.pennylane.ai/t/graph-similarity/1121/6 "2021-06-22T00:15:02Z")

</div>

Hi @Tom_Bromley

What do you mean by spectrum? The values of B which it can take?  
Just to double check if I understand right, we can achieve graph classification via 2 different methods of how we acquire feature vectors.

---

<div class="post-metadata">

**Author:** ![Tom\_Bromley](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/tom_bromley/32/129_2.png) [@Tom\_Bromley](https://discuss.pennylane.ai/u/Tom_Bromley)\
**Post date:** [June 22, 2021, 8:37pm UTC](https://discuss.pennylane.ai/t/graph-similarity/1121/7 "2021-06-22T20:37:27Z")

</div>

Hi @_risto,

> What do you mean by spectrum?

The spectrum of a graph is given by the eigenvalues of its adjacency matrix. For a graph to be compatible with the X8 device, we require:

- The graph must be [bipartite](https://en.wikipedia.org/wiki/Bipartite_graph).
- The eigenvalues of its [adjacency matrix](https://en.wikipedia.org/wiki/Adjacency_matrix) must be in the set \{0, d\} for some real value d.

You can check out two example graphs [here](https://strawberryfields.ai/photonics/demos/tutorial_X8_demos.html#graph-similarity).

> Just to double check if I understand right, we can achieve graph classification via 2 different methods of how we acquire feature vectors.

Right, you can calculate the same feature vector using two different methods. One method (`feature_vector_orbits_sampling`) is for if you have pre-generated samples (e.g. after running on hardware), while the other method (`feature_vector_orbits`) is for if you want to brute-force calculate using a simulator.

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [June 23, 2021, 3:47am UTC](https://discuss.pennylane.ai/t/graph-similarity/1121/8 "2021-06-23T03:47:41Z")

</div>

Hi @Tom_Bromley

Thank you for clarification. I tried to run parts of the [https://strawberryfields.readthedocs.io/en/stable/\_modules/strawberryfields/apps/similarity.html#feature\_vector\_orbits](https://strawberryfields.readthedocs.io/en/stable/_modules/strawberryfields/apps/similarity.html#feature_vector_orbits) but get SyntaxError: unexpected EOF while parsing. Is this the right code to look at?

---

<div class="post-metadata">

**Author:** ![nathan](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nathan/32/85_2.png) [@nathan](https://discuss.pennylane.ai/u/nathan)\
**Post date:** [June 23, 2021, 8:04pm UTC](https://discuss.pennylane.ai/t/graph-similarity/1121/9 "2021-06-23T20:04:45Z")

</div>

Hi @_risto,

If you’re getting `SyntaxError: unexpected EOF while parsing`, it usually means the Python snippet is incomplete. A typical cause of this could be a copy/paste error where some text was missed (e.g., a `return` statement on a function).

Would you be able to provide us with a self-contained minimal code example that you’re trying to run that provides that error? Thanks 🙂

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [July 1, 2021, 3:33pm UTC](https://discuss.pennylane.ai/t/graph-similarity/1121/10 "2021-07-01T15:33:52Z")

</div>

> [@Tom\_Bromley](#):
>
> The spectrum of a graph is given by the eigenvalues of its adjacency matrix. For a graph to be compatible with the X8 device, we require:
> 
> - The graph must be [bipartite](https://en.wikipedia.org/wiki/Bipartite_graph).
> - The eigenvalues of its [adjacency matrix](https://en.wikipedia.org/wiki/Adjacency_matrix) must be in the set {0,d}{0, d} for some real value dd .
> 
> You can check out two example graphs [here](https://strawberryfields.ai/photonics/demos/tutorial_X8_demos.html#graph-similarity).

Does that include only complete bipartite graphs (as mentioned in [Executing programs on X8 devices — Strawberry Fields](https://strawberryfields.ai/photonics/demos/tutorial_X8.html#embedding-bipartite-graphs))?  
Is there any way where I can generate those type of graphs from other type of data (either graphs or images)?

And if using brute force to calculate feacture vectors via `feature_vector_orbits` in this case any type of graphs can be used?

---

<div class="post-metadata">

**Author:** ![Tom\_Bromley](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/tom_bromley/32/129_2.png) [@Tom\_Bromley](https://discuss.pennylane.ai/u/Tom_Bromley)\
**Post date:** [July 2, 2021, 4:37pm UTC](https://discuss.pennylane.ai/t/graph-similarity/1121/11 "2021-07-02T16:37:23Z")

</div>

Hi @_risto!

> Does that include only complete bipartite graphs (as mentioned in [Executing programs on X8 devices — Strawberry Fields](https://strawberryfields.ai/photonics/demos/tutorial_X8.html#embedding-bipartite-graphs))?

Complete bipartite graphs satisfy these criteria but are not the only instances. You can see in Fig. 8 of [this](https://arxiv.org/pdf/2103.02109.pdf) paper that a graph with just pairwise connections also works. Nevertheless, the constraints are quite strong and there isn’t a lot of flexibility in terms of graph connectivity. You do have some flexibility on the edge weights, as can be seen by the adjacency matrices in page 21 of the paper. These can be generated using Eqs. (15) and (16) by picking a unitary U and squeezing r that is compatible with the device.

> Is there any way where I can generate those type of graphs from other type of data (either graphs or images)?

Given the discussion above, it’ll be a challenge to encode more general graphs into the present device in a way that will produce informative output data.

> And if using brute force to calculate feacture vectors via `feature_vector_orbits` in this case any type of graphs can be used?

Yes, in this case any type of graph can be used.
