From 9623abfa1062f4383b556707645da7407bfec930 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Thu, 21 Jul 2016 18:17:44 +0100 Subject: [PATCH] Think I just did something right --- abp/fancy.py | 6 +- abp/graphstate.py | 6 +- examples/wtf.py | 91 ++++++++++++++++++++++++ tests/test_against_anders_and_briegel.py | 5 +- tests/test_fancy.py | 35 +++++++++ tests/test_measurement.py | 14 ++-- 6 files changed, 144 insertions(+), 13 deletions(-) create mode 100644 examples/wtf.py create mode 100644 tests/test_fancy.py diff --git a/abp/fancy.py b/abp/fancy.py index c5a8c3d..7bb276f 100644 --- a/abp/fancy.py +++ b/abp/fancy.py @@ -48,13 +48,13 @@ class GraphState(graphstate.GraphState, networkx.Graph): #self.ws.close() #self.connect_to_server() - def layout(self, dim=3): + def layout(self, dim=2): """ Automatically lay out the graph """ pos = networkx.spring_layout(self, dim, scale=np.sqrt(self.order())) middle = np.average(pos.values(), axis=0) pos = {key: value - middle for key, value in pos.items()} - for key, (x, y, z) in pos.items(): - self.node[key]["position"] = util.xyz(x, y, z) + for key, (x, y) in pos.items(): + self.node[key]["position"] = util.xyz(x, y, 0) def add_vops(self): """ Automatically add vops if they're not present """ diff --git a/abp/graphstate.py b/abp/graphstate.py index de08ce9..ac1f5da 100644 --- a/abp/graphstate.py +++ b/abp/graphstate.py @@ -63,7 +63,10 @@ class GraphState(object): def remove_vop(self, a, avoid): """ Reduces VOP[a] to the identity """ others = set(self.adj[a]) - {avoid} - swap_qubit = others.pop() if others else avoid + #TODO: this is a hack for determinsim. remove + swap_qubit = min(others) if others else avoid + #swap_qubit = others.pop() if others else avoid # TODO: maybe this is the only problematic part + print "SWAPPING WITH {} (options were {})".format(swap_qubit, tuple(others)) for v in reversed(clifford.decompositions[self.node[a]["vop"]]): if v == "x": @@ -256,6 +259,7 @@ class GraphState(object): def to_stabilizer(self): """ Get the stabilizer of this graph """ + return output = {a: {} for a in self.node} for a, b in it.product(self.node, self.node): if a == b: diff --git a/examples/wtf.py b/examples/wtf.py new file mode 100644 index 0000000..3d4e340 --- /dev/null +++ b/examples/wtf.py @@ -0,0 +1,91 @@ +from abp import GraphState, clifford +from abp.fancy import GraphState as Fancy +from anders_briegel import graphsim +import random +import numpy as np +from tqdm import tqdm +import time +import itertools as it +import sys + +REPEATS = 100000 +N=9 + +def compare(A, B): + keys_same = set(A["node"].keys()) == set(B["node"].keys()) + vops_same = all(A["node"][i]["vop"] == B["node"][i]["vop"] for i in A["node"].keys()) + edges_same = A["adj"] == B["adj"] + if keys_same and vops_same and edges_same: + return True + + sys.exit(0) + print "doing a state vector check" + alice = GraphState(range(N)) + alice.node = A["node"] + alice.adj = A["adj"] + + bob = GraphState(range(N)) + bob.node = B["node"] + bob.adj = B["adj"] + if alice.to_state_vector() == bob.to_state_vector(): + return True + + + return False + + +if __name__ == '__main__': + + clifford.use_old_cz() + + a = graphsim.GraphRegister(N) + b = Fancy(range(N)) + + # Keep comparing until fail + while compare(a.to_json(), b.to_json()): + if random.random()>0.5: + j = np.random.randint(0, N) + u = random.randint(0, 23) + print "> Acting U{} on {}".format(u, j) + a.local_op(j, graphsim.LocCliffOp(u)) + b.act_local_rotation(j, u) + print "Done" + else: + i, j= np.random.randint(0, N, 2) + if i!=j: + print "> Acting CZ on {} & {}".format(i, j) + a.cphase(i, j) + b.act_cz(i, j) + print "Done" + #b.update(delay=0.1) + + + # Show the diff + A = a.to_json()["node"] + B = b.to_json()["node"] + for i in range(N): + if A[i]["vop"] != B[i]["vop"]: + print "{}/ them: {}, me: {}".format(i, A[i]["vop"], B[i]["vop"]) + + # Now construct unitaries + A = a.to_json() + B = b.to_json() + alice = GraphState(range(N)) + alice.node = A["node"] + alice.adj = A["adj"] + + bob = GraphState(range(N)) + bob.node = B["node"] + bob.adj = B["adj"] + print alice.to_state_vector() == bob.to_state_vector() + + + b.layout() + + + + + + + + diff --git a/tests/test_against_anders_and_briegel.py b/tests/test_against_anders_and_briegel.py index 89890dc..e989960 100644 --- a/tests/test_against_anders_and_briegel.py +++ b/tests/test_against_anders_and_briegel.py @@ -3,7 +3,6 @@ from anders_briegel import graphsim from abp import CircuitModel from abp import clifford import random -from copy import deepcopy import numpy as np from tqdm import tqdm from abp.anders_cz import cz_table as abczt @@ -105,7 +104,7 @@ def test_with_cphase_gates_hadamard_only(N=10): assert_equal(a, b) -def test_cz_hadamard(N=20): +def _test_cz_hadamard(N=10): """ Test CZs and Hadamards at random """ clifford.use_old_cz() @@ -126,7 +125,7 @@ def test_cz_hadamard(N=20): -def _test_all(N=9): +def test_all(N=9): """ Test everything""" clifford.use_old_cz() diff --git a/tests/test_fancy.py b/tests/test_fancy.py new file mode 100644 index 0000000..27b6c6d --- /dev/null +++ b/tests/test_fancy.py @@ -0,0 +1,35 @@ +from abp import GraphState, clifford +from abp.fancy import GraphState as Fancy +from anders_briegel import graphsim +import random +import time +import numpy as np +from tqdm import tqdm + +REPEATS = 100000 + +def assert_equal(a, b, debug=""): + assert a.to_json() == b.to_json(), "\n\n" + debug + "\n\n" + str(a.to_json()) + "\n\n" + str(b.to_json()) + + +def test_cz_hadamard(N=9): + """ Test CZs and Hadamards at random """ + + clifford.use_old_cz() + + a = graphsim.GraphRegister(N) + b = Fancy(range(N)) + while a.to_json() == b.to_json(): + if random.random()>0.5: + j = random.randint(0, N-1) + a.hadamard(j) + b.act_hadamard(j) + else: + q = random.randint(0, N-2) + a.cphase(q, q+1) + b.act_cz(q, q+1) + + + + + diff --git a/tests/test_measurement.py b/tests/test_measurement.py index dc411e4..2e44e1e 100644 --- a/tests/test_measurement.py +++ b/tests/test_measurement.py @@ -82,11 +82,13 @@ def test_all(N=20): a.cphase(q, q+1) b.act_cz(q, q+1) else: - pass - #q = random.randint(0, N-2) - #m = random.choice(["px", "py", "pz"]) - #a.measure(q, m) - #b.measure(q, mm) - assert a.to_json() == b.to_json() + q = random.randint(0, N-2) + m = random.choice([1,2,3]) + force = random.choice([0, 1]) + thing=3 + ma = a.measure(q, graphsim.LocCliffOp(m)) + mb = b.measure(q, str(m), force) + print ma, mb + assert ma == mb, i