From b7380f802b8800ef8540991b682add1c787489d5 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Fri, 11 Mar 2016 01:17:47 +0000 Subject: [PATCH] Better --- TODO | 4 ---- tables.py | 29 ++++++++++------------------- tests/test_graph.py | 4 ++-- tests/test_viz.py | 13 +++++++------ util.py | 20 -------------------- 5 files changed, 19 insertions(+), 51 deletions(-) delete mode 100644 TODO delete mode 100644 util.py diff --git a/TODO b/TODO deleted file mode 100644 index 18b0f9d..0000000 --- a/TODO +++ /dev/null @@ -1,4 +0,0 @@ -Next things to do: - - Read A&B code which expresses C(1) as products of sqrt(iX), sqrt(iY) - - Read A&B code which exposes LC API - - Implement the above diff --git a/tables.py b/tables.py index 9dc8008..3e4ddcd 100644 --- a/tables.py +++ b/tables.py @@ -7,24 +7,10 @@ unitaries = [p*s for p in permutations for s in signs] import numpy as np from tqdm import tqdm -import qi +import os from functools import reduce import cPickle - -def cache_to_disk(file_name): - """ A decorator to cache the output of a function to disk """ - def wrap(func): - def modified(*args, **kwargs): - try: - output = cPickle.load(open(file_name, "r")) - except (IOError, ValueError): - output = func(*args, **kwargs) - with open(file_name, "w") as f: - cPickle.dump(output, f) - return output - return modified - return wrap - +import qi # TODO: make this more efficient / shorter def find_up_to_phase(u): @@ -47,9 +33,11 @@ def name_of(vop): return "%s" % get_name[vop] if vop in get_name else "VOP%d" % vop -@cache_to_disk("tables.cache") -def construct_tables(): +def construct_tables(filename="tables.cache"): """ Constructs / caches multiplication and conjugation tables """ + if os.path.exists(filename): + return cPickle.load(open(filename, "r")) + by_name = {name: find_up_to_phase(u)[0] for name, u in qi.by_name.items()} get_name = {v:k for k, v in by_name.items()} conjugation_table = [find_up_to_phase(u.H)[0] @@ -57,7 +45,10 @@ def construct_tables(): times_table = [[find_up_to_phase(u * v)[0] for v in unitaries] for u in tqdm(unitaries)] cz_table = None - return by_name, get_name, conjugation_table, times_table, cz_table + output = by_name, get_name, conjugation_table, times_table, cz_table + with open(filename, "w") as f: + cPickle.dump(output, f) + return output decompositions = ("xxxx", "xx", "zzxx", "zz", "zxx", "z", "zzz", "xxz", diff --git a/tests/test_graph.py b/tests/test_graph.py index 576c7a5..a8e25f1 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -58,9 +58,9 @@ def test_edgelist(): def test_million_sites(): - """ Testing making really big graphs """ + """ Testing that making a graph of tent housand qubits takes less than half a second""" g = GraphState() t = time.clock() for i in xrange(100000): g.add_edge(i, i + 1) - print time.clock() - t + assert time.clock() - t < .5 diff --git a/tests/test_viz.py b/tests/test_viz.py index 05350ec..a014713 100644 --- a/tests/test_viz.py +++ b/tests/test_viz.py @@ -1,12 +1,13 @@ from graph import GraphState import viz + def test_viz(): g = GraphState() - g.add_edge(0,1) - g.add_edge(1,2) - g.add_edge(2,0) - g.add_edge(0,3) - g.add_edge(100,200) - #g.remove_vop(0, 1) + g.add_edge(0, 1) + g.add_edge(1, 2) + g.add_edge(2, 0) + g.add_edge(0, 3) + g.add_edge(100, 200) + # g.remove_vop(0, 1) viz.draw(g) diff --git a/util.py b/util.py deleted file mode 100644 index 0e66ebf..0000000 --- a/util.py +++ /dev/null @@ -1,20 +0,0 @@ -""" -Useful but messy crap -""" - - -def cache_to_disk(file_name): - """ A decorator to cache the output of a function to disk """ - def wrap(func): - def modified(*args, **kwargs): - try: - output = cPickle.load(open(file_name, "r")) - except (IOError, ValueError): - output = func(*args, **kwargs) - with open(file_name, "w") as f: - cPickle.dump(output, f) - return output - return modified - return wrap - -