Browse Source

Example is working now

master
Pete Shadbolt 7 years ago
parent
commit
8bd121aec0
10 changed files with 14 additions and 255 deletions
  1. +0
    -1
      abp/__init__.py
  2. +12
    -0
      abp/graphstate.py
  3. +0
    -52
      examples/visualization/auto_layout.py
  4. +0
    -24
      examples/visualization/grid_2d.py
  5. +0
    -20
      examples/visualization/issues/unpositioned_nodes.py
  6. +0
    -45
      examples/visualization/lattice_2d.py
  7. +0
    -56
      examples/visualization/lattice_3d.py
  8. +0
    -8
      examples/visualization/new.py
  9. +2
    -3
      examples/visualization/raussendorf.py
  10. +0
    -46
      examples/visualization/stress.py

+ 0
- 1
abp/__init__.py View File

@@ -1,7 +1,6 @@
# Alias some stuff to make imports cleaner # Alias some stuff to make imports cleaner
from abp.graphstate import GraphState from abp.graphstate import GraphState
from abp.nxgraphstate import NXGraphState from abp.nxgraphstate import NXGraphState
from abp.vizclient import VizClient
from abp.qi import CircuitModel from abp.qi import CircuitModel


DETERMINISTIC = False DETERMINISTIC = False

+ 12
- 0
abp/graphstate.py View File

@@ -9,6 +9,7 @@ import json, random
from . import qi, clifford, util from . import qi, clifford, util
import abp import abp
from .stabilizer import Stabilizer from .stabilizer import Stabilizer
import requests




class GraphState(object): class GraphState(object):
@@ -26,6 +27,7 @@ class GraphState(object):
""" """


self.adj, self.node = {}, {} self.adj, self.node = {}, {}
self.url = None
try: try:
# Cloning from a networkx graph # Cloning from a networkx graph
self.adj = data.adj.copy() self.adj = data.adj.copy()
@@ -492,3 +494,13 @@ class GraphState(object):
g.adj = self.adj.copy() g.adj = self.adj.copy()
return g return g


def show(self):
""" Shares the state on the server and displays browser """
if self.url == None:
self.url = requests.get("https://abv.peteshadbolt.co.uk/").url

data = json.dumps(self.to_json(stringify=True))
print("Shared state to {}".format(self.url))
return requests.post("{}/graph".format(self.url), data=data)


+ 0
- 52
examples/visualization/auto_layout.py View File

@@ -1,52 +0,0 @@
from abp import GraphState, VizClient
from abp.util import xyz
import numpy as np
import time
import itertools
import networkx as nx

threedee_unit_cell = (
(( 0, 0, 0), (0, 1, 0)),
(( 0, 0, 0), (1, 0, 0)),
(( 1, 0, 0), (1, 1, 0)),
(( 0, 1, 0), (1, 1, 0)),

(( 0, 0, 1), (0, 1, 1)),
(( 0, 0, 1), (1, 0, 1)),
(( 1, 0, 1), (1, 1, 1)),
(( 0, 1, 1), (1, 1, 1)),

(( 0, 0, 0), (0, 0, 1)),
(( 0, 1, 0), (0, 1, 1)),
(( 1, 0, 0), (1, 0, 1)),
(( 1, 1, 0), (1, 1, 1))
)

def add_offset(vector, offset):
""" Offset a vector in n-dimensional space """
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()
for offset in itertools.product(*list(map(range, size))):
edges |= offset_unit_cell(unit_cell, offset)

nodes = set(itertools.chain(*edges))
return nodes, edges

nodes, edges = lattice(threedee_unit_cell, (3, 3, 3))

psi = GraphState(nodes)

for a, b in edges:
psi.act_cz(a, b)

v = VizClient()
v.update(psi)

+ 0
- 24
examples/visualization/grid_2d.py View File

@@ -1,24 +0,0 @@
from abp import GraphState, VizClient
from abp.util import xyz
import itertools

def grid_2d(width, height):
""" Make a 2D grid """
psi = GraphState()
grid = list(itertools.product(list(range(width)), list(range(height))))

for x, y in grid:
psi.add_qubit((x, y), position=xyz(x, y, 0), vop=0)

for x, y in grid:
if x<width-1: psi.act_cz((x, y), (x+1, y))
if y<height-1: psi.act_cz((x, y), (x, y+1))

return psi


if __name__ == '__main__':
psi = grid_2d(5, 5)
v = VizClient()
v.update(psi)


+ 0
- 20
examples/visualization/issues/unpositioned_nodes.py View File

@@ -1,20 +0,0 @@
from abp import GraphState, VizClient
from abp.util import xyz

# Prepare to visualize
v = VizClient()

# Make a graph state with position attributes
g = GraphState()
for i in range(5):
g.add_qubit(i, position=xyz(i, 0, 0), vop="identity")
g.act_czs((0,1),(1,2),(2,3),(3,4))

# Show it
v.update(g, 3)

# Add a qubit with no position
g.add_qubit('start')

# Show it
v.update(g)

+ 0
- 45
examples/visualization/lattice_2d.py View File

@@ -1,45 +0,0 @@
from abp import GraphState, VizClient
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)))


def add_offset(vector, offset):
""" Offset a vector in n-dimensional space """
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()
for offset in itertools.product(*list(map(range, size))):
edges |= offset_unit_cell(unit_cell, offset)

nodes = set(itertools.chain(*edges))
return nodes, edges

# s = VisibleGraphState()
nodes, edges = lattice(square_unit_cell, (10, 10))

psi = GraphState()
v = VizClient()
for node in nodes:
psi.add_qubit(str(node), position=xyz(node[0], node[1]))
psi.act_hadamard(str(node))
v.update(psi, 0.1)

for edge in edges:
psi.act_cz(str(edge[0]), str(edge[1]))
v.update(psi, 0.1)


+ 0
- 56
examples/visualization/lattice_3d.py View File

@@ -1,56 +0,0 @@
from abp import GraphState, VizClient
from abp.util import xyz
import numpy as np
import time
import itertools
import networkx as nx

threedee_unit_cell = (
(( 0, 0, 0), (0, 1, 0)),
(( 0, 0, 0), (1, 0, 0)),
(( 1, 0, 0), (1, 1, 0)),
(( 0, 1, 0), (1, 1, 0)),

(( 0, 0, 1), (0, 1, 1)),
(( 0, 0, 1), (1, 0, 1)),
(( 1, 0, 1), (1, 1, 1)),
(( 0, 1, 1), (1, 1, 1)),

(( 0, 0, 0), (0, 0, 1)),
(( 0, 1, 0), (0, 1, 1)),
(( 1, 0, 0), (1, 0, 1)),
(( 1, 1, 0), (1, 1, 1))
)

def add_offset(vector, offset):
""" Offset a vector in n-dimensional space """
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()
for offset in itertools.product(*list(map(range, size))):
edges |= offset_unit_cell(unit_cell, offset)

nodes = set(itertools.chain(*edges))
return nodes, edges

nodes, edges = lattice(threedee_unit_cell, (3, 3, 3))

psi = GraphState()
for node in nodes:
psi.add_qubit(str(node), position=xyz(*node))
psi.act_hadamard(str(node))

for edge in edges:
psi.act_cz(str(edge[0]), str(edge[1]))

v = VizClient()
v.update(psi)


+ 0
- 8
examples/visualization/new.py View File

@@ -1,8 +0,0 @@
from abp import GraphState, VizClient
from abp.util import xyz

v = VizClient()
g = GraphState(5)
for i in range(5):
g.node[i]["position"] = xyz(i, 0, 0)
v.update(g)

+ 2
- 3
examples/visualization/raussendorf.py View File

@@ -1,4 +1,4 @@
from abp import GraphState, VizClient
from abp import GraphState
from abp.util import xyz from abp.util import xyz
import numpy as np import numpy as np
import time import time
@@ -52,6 +52,5 @@ for node in nodes:
for edge in edges: for edge in edges:
psi.act_cz(edge[0], edge[1]) psi.act_cz(edge[0], edge[1])


v = VizClient()
v.update(psi)
psi.show()



+ 0
- 46
examples/visualization/stress.py View File

@@ -1,46 +0,0 @@
from abp import GraphState, VizClient
from abp.util import xyz
import numpy as np
import time
import itertools

threedee_unit_cell = (
(( 0, 0, 0), (0, 1, 0)),
(( 0, 0, 0), (1, 0, 0)),
(( 1, 0, 0), (1, 1, 0)),
(( 0, 1, 0), (1, 1, 0)))

def add_offset(vector, offset):
""" Offset a vector in n-dimensional space """
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()
for offset in itertools.product(*map(range, size)):
edges |= offset_unit_cell(unit_cell, offset)

nodes = set(itertools.chain(*edges))
return nodes, edges

nodes, edges = lattice(threedee_unit_cell, (4, 4, 4))

v = VizClient()
while True:
psi = GraphState()
for node in nodes:
v.update(psi, 0.1)
psi.add_qubit(str(node), position=xyz(node[0], node[1], node[2]), vop="identity")

for edge in edges:
v.update(psi, 0.1)
psi.act_cz(str(edge[0]), str(edge[1]))
del psi



Loading…
Cancel
Save