From 513e09b681b7e6585312cf9db29ca54bcebd4d79 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Thu, 19 May 2016 20:28:43 +0100 Subject: [PATCH] Magical networkx integration :sparkling_heart: --- .agignore | 1 + abp/graphstate.py | 9 +++++---- abp/util.py | 2 ++ tests/test_nxgraphstate.py | 14 ++++++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 .agignore create mode 100644 abp/util.py create mode 100644 tests/test_nxgraphstate.py diff --git a/.agignore b/.agignore new file mode 100644 index 0000000..af8404c --- /dev/null +++ b/.agignore @@ -0,0 +1 @@ +static/scripts/three.js diff --git a/abp/graphstate.py b/abp/graphstate.py index 82d37c5..6bf223c 100644 --- a/abp/graphstate.py +++ b/abp/graphstate.py @@ -6,9 +6,12 @@ import itertools as it import clifford import json import qi -import networkx as nx +try: + from networkx import Graph as NXGraph +except ImportError: + NXGraph = object -class GraphState(object): +class GraphState(NXGraph): def __init__(self, nodes=[]): self.adj, self.node = {}, {} @@ -196,5 +199,3 @@ class GraphState(object): """ Check equality between graphs """ return self.adj == other.adj and self.node == other.node - - diff --git a/abp/util.py b/abp/util.py new file mode 100644 index 0000000..b4e15f6 --- /dev/null +++ b/abp/util.py @@ -0,0 +1,2 @@ +def xyz(x, y, z=0): + return {"x": x, "y": y, "z": z} diff --git a/tests/test_nxgraphstate.py b/tests/test_nxgraphstate.py new file mode 100644 index 0000000..3ade04c --- /dev/null +++ b/tests/test_nxgraphstate.py @@ -0,0 +1,14 @@ +from abp.graphstate import GraphState +from abp.util import xyz +import networkx as nx + +def simple_test(): + g = GraphState() + g.add_node(0, xyz(0, 0, 0)) + g.add_node(1, xyz(0, 0, 0)) + g.act_hadamard(0) + g.act_hadamard(1) + g.act_cz(0, 1) + print g.node[0]["position"] + print nx.to_edgelist(g) +