diff --git a/abp/viz.py b/abp/viz.py index a1ac25e..8d2b335 100644 --- a/abp/viz.py +++ b/abp/viz.py @@ -2,7 +2,7 @@ Allows us to visualize the state in a browser """ -import atexit, json +import atexit, json, time from graphstate import GraphState from websocket import create_connection @@ -21,14 +21,9 @@ class VisibleGraphState(GraphState): self.update() self.ws.close() - def to_json(self): - """ We override to_json() so that we send the whole `ngbh` structure in JS-friendly form """ - ngbh = {a: {b: True for b in self.ngbh[a]} - for a in self.ngbh} - return {"vops": self.vops, "ngbh": ngbh, "meta": self.meta} - - def update(self): + def update(self, delay = 0.5): """ Call this function when you are ready to send data to the browser """ data = json.dumps(self.to_json()) self.ws.send(data) + time.sleep(delay) diff --git a/examples/lattice_demo.py b/examples/lattice_demo.py index 4a15906..a6ab817 100644 --- a/examples/lattice_demo.py +++ b/examples/lattice_demo.py @@ -1,19 +1,25 @@ from abp.viz import VisibleGraphState +from abp.util import xyz import numpy as np import time import itertools -square_unit_cell = (((0, 0), (0, 1)), ((0, 0), (1, 0)), ((1, 0), (1, 1)), ((0, 1), (1, 1))) -funny_unit_cell = (((0, 0), (0, 1)), ((0, 0), (1, 0)), ((1, 0), (1, 1)), ((0, 1), (1, 1)), ((0, 0), (.5, .5))) +square_unit_cell = ( + ((0, 0), (0, 1)), ((0, 0), (1, 0)), ((1, 0), (1, 1)), ((0, 1), (1, 1))) +funny_unit_cell = (((0, 0), (0, 1)), ((0, 0), (1, 0)), + ((1, 0), (1, 1)), ((0, 1), (1, 1)), ((0, 0), (.5, .5))) + def add_offset(vector, offset): """ Offset a vector in n-dimensional space """ - return tuple(v+o for v, o in zip(vector, offset)) + return tuple(v + o for v, o in zip(vector, offset)) + def offset_unit_cell(unit_cell, offset): """ Offset a unit cell """ return {(add_offset(a, offset), add_offset(b, offset)) for a, b in unit_cell} + def lattice(unit_cell, size): """ Generate a lattice from a unit cell """ edges = set() @@ -23,15 +29,14 @@ def lattice(unit_cell, size): nodes = set(itertools.chain(*edges)) return nodes, edges -#s = VisibleGraphState() -nodes, edges = lattice(funny_unit_cell, (10, 10)) +# s = VisibleGraphState() +nodes, edges = lattice(square_unit_cell, (4, 4)) psi = VisibleGraphState() for node in nodes: - pos = {"x": node[0], "y": node[1], "z": 0} - psi.add_node(str(node), meta={"position":pos}) + psi.add_node(str(node), position=xyz(node[0], node[1])) + psi.act_hadamard(str(node)) for edge in edges: - psi.add_edge(str(edge[0]), str(edge[1])) - + psi.act_cz(str(edge[0]), str(edge[1])) diff --git a/examples/local_complementation.py b/examples/local_complementation.py new file mode 100644 index 0000000..9bba344 --- /dev/null +++ b/examples/local_complementation.py @@ -0,0 +1,20 @@ +from abp.viz import VisibleGraphState +from abp.util import xyz +from abp.clifford import * + +psi = VisibleGraphState() +psi.add_node(0, position = xyz(0, 0)) +psi.add_node(1, position = xyz(1, 1)) +psi.add_node(2, position = xyz(3, 2)) +psi.add_node(3, position = xyz(0, 3)) + +for n in psi.node: + psi.act_hadamard(n) + +psi.act_cz(0, 1) +psi.act_cz(0, 3) +psi.act_cz(1, 3) +psi.act_cz(1, 2) +while True: + psi.update() + psi.local_complementation(1) diff --git a/static/index.html b/static/index.html index 20313e4..b2de929 100644 --- a/static/index.html +++ b/static/index.html @@ -34,6 +34,7 @@