From 75730f954befe731c7fcd4b6de0043a871b33f19 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Mon, 1 Aug 2016 11:48:41 -0700 Subject: [PATCH] Add examples/visualization/auto_layout.py --- abp/graphstate.py | 3 -- examples/visualization/auto_layout.py | 53 +++++++++++++++++++++++++++ examples/visualization/lattice_3d.py | 4 +- 3 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 examples/visualization/auto_layout.py diff --git a/abp/graphstate.py b/abp/graphstate.py index 533fa56..dff6ae5 100644 --- a/abp/graphstate.py +++ b/abp/graphstate.py @@ -383,9 +383,6 @@ class GraphState(object): |00000>: 0.18+0.00j |00001>: 0.18+0.00j ... - .. todo:: - Doesn't work with non-``int`` node labels - """ if len(self.node) > 15: raise ValueError("Cannot build state vector: too many qubits") diff --git a/examples/visualization/auto_layout.py b/examples/visualization/auto_layout.py new file mode 100644 index 0000000..8367e51 --- /dev/null +++ b/examples/visualization/auto_layout.py @@ -0,0 +1,53 @@ +from abp.fancy import GraphState +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(*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_node(str(node)) + psi.act_hadamard(str(node)) + +for edge in edges: + psi.act_cz(str(edge[0]), str(edge[1])) + diff --git a/examples/visualization/lattice_3d.py b/examples/visualization/lattice_3d.py index 73215a7..e4a229b 100644 --- a/examples/visualization/lattice_3d.py +++ b/examples/visualization/lattice_3d.py @@ -41,7 +41,7 @@ def lattice(unit_cell, size): nodes = set(itertools.chain(*edges)) return nodes, edges -nodes, edges = lattice(threedee_unit_cell, (6, 6, 6)) +nodes, edges = lattice(threedee_unit_cell, (3, 3, 3)) psi = GraphState() for node in nodes: @@ -51,5 +51,3 @@ for node in nodes: for edge in edges: psi.act_cz(str(edge[0]), str(edge[1])) -#print psi.to_state_vector() -