@@ -48,13 +48,13 @@ class GraphState(graphstate.GraphState, networkx.Graph): | |||||
#self.ws.close() | #self.ws.close() | ||||
#self.connect_to_server() | #self.connect_to_server() | ||||
def layout(self, dim=3): | |||||
def layout(self, dim=2): | |||||
""" Automatically lay out the graph """ | """ Automatically lay out the graph """ | ||||
pos = networkx.spring_layout(self, dim, scale=np.sqrt(self.order())) | pos = networkx.spring_layout(self, dim, scale=np.sqrt(self.order())) | ||||
middle = np.average(pos.values(), axis=0) | middle = np.average(pos.values(), axis=0) | ||||
pos = {key: value - middle for key, value in pos.items()} | 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): | def add_vops(self): | ||||
""" Automatically add vops if they're not present """ | """ Automatically add vops if they're not present """ | ||||
@@ -63,7 +63,10 @@ class GraphState(object): | |||||
def remove_vop(self, a, avoid): | def remove_vop(self, a, avoid): | ||||
""" Reduces VOP[a] to the identity """ | """ Reduces VOP[a] to the identity """ | ||||
others = set(self.adj[a]) - {avoid} | 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"]]): | for v in reversed(clifford.decompositions[self.node[a]["vop"]]): | ||||
if v == "x": | if v == "x": | ||||
@@ -256,6 +259,7 @@ class GraphState(object): | |||||
def to_stabilizer(self): | def to_stabilizer(self): | ||||
""" Get the stabilizer of this graph """ | """ Get the stabilizer of this graph """ | ||||
return | |||||
output = {a: {} for a in self.node} | output = {a: {} for a in self.node} | ||||
for a, b in it.product(self.node, self.node): | for a, b in it.product(self.node, self.node): | ||||
if a == b: | if a == b: | ||||
@@ -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() | |||||
@@ -3,7 +3,6 @@ from anders_briegel import graphsim | |||||
from abp import CircuitModel | from abp import CircuitModel | ||||
from abp import clifford | from abp import clifford | ||||
import random | import random | ||||
from copy import deepcopy | |||||
import numpy as np | import numpy as np | ||||
from tqdm import tqdm | from tqdm import tqdm | ||||
from abp.anders_cz import cz_table as abczt | 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) | assert_equal(a, b) | ||||
def test_cz_hadamard(N=20): | |||||
def _test_cz_hadamard(N=10): | |||||
""" Test CZs and Hadamards at random """ | """ Test CZs and Hadamards at random """ | ||||
clifford.use_old_cz() | 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""" | """ Test everything""" | ||||
clifford.use_old_cz() | clifford.use_old_cz() | ||||
@@ -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) | |||||
@@ -82,11 +82,13 @@ def test_all(N=20): | |||||
a.cphase(q, q+1) | a.cphase(q, q+1) | ||||
b.act_cz(q, q+1) | b.act_cz(q, q+1) | ||||
else: | 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 | |||||