@@ -142,9 +142,9 @@ class GraphState(object): | |||
return result | |||
def toggle_edges(a, b): | |||
def toggle_edges(self, a, b): | |||
""" Toggle edges between vertex sets a and b """ | |||
done = {} | |||
done = set() | |||
for i, j in it.product(a, b): | |||
if i == j and not (i, j) in done: | |||
done.add((i, j), (j, i)) | |||
@@ -163,12 +163,12 @@ class GraphState(object): | |||
# Do a z on all ngb(vb) \ ngb(v) \ {v}, and some other stuff | |||
self.act_local_rotation(node, "pz") | |||
self.act_local_rotation(friend, "msqy") | |||
for n in set(self.adj[friend]) - set(self.adj(node)) - {node}: | |||
for n in set(self.adj[friend]) - set(self.adj[node]) - {node}: | |||
self.act_local_rotation(n, "pz") | |||
else: | |||
# Do a z on all ngb(v) \ ngb(vb) \ {vb}, and sqy on the friend | |||
self.act_local_rotation(friend, "sqy") | |||
for n in set(self.adj[node]) - set(self.adj(friend)) - {friend}: | |||
for n in set(self.adj[node]) - set(self.adj[friend]) - {friend}: | |||
self.act_local_rotation(n, "pz") | |||
# TODO: Yuk. Just awful! | |||
@@ -202,7 +202,7 @@ class GraphState(object): | |||
def measure_z(self, node, result): | |||
""" Measure the graph in the Z-basis """ | |||
# Disconnect | |||
for neighbour in self.adj[node]: | |||
for neighbour in tuple(self.adj[node]): | |||
self.del_edge(node, neighbour) | |||
if result: | |||
self.act_local_rotation(neighbour, "pz") | |||
@@ -288,6 +288,8 @@ class GraphState(object): | |||
def __eq__(self, other): | |||
""" Check equality between graphs """ | |||
if str(type(other)) == "<class 'anders_briegel.graphsim.GraphRegister'>": | |||
return self.to_json() == other.to_json() | |||
return self.adj == other.adj and self.node == other.node | |||
if __name__ == '__main__': | |||
@@ -10,3 +10,4 @@ def demograph(): | |||
g.add_edge(100, 200) | |||
return g | |||
@@ -0,0 +1,41 @@ | |||
from abp import GraphState, clifford | |||
from anders_briegel import graphsim | |||
def random_state(N=10, messy=True): | |||
""" A state to test on """ | |||
a = GraphState(range(N)) | |||
b = graphsim.GraphRegister(N) | |||
clifford.use_old_cz() | |||
for i in range(N): | |||
a.act_hadamard(i) | |||
b.hadamard(i) | |||
for i in range(10): | |||
j, k= np.random.choice(range(N), 2, replace=False) | |||
a.act_cz(j, k) | |||
b.cphase(j, k) | |||
if not messy: return a, b | |||
for i in range(10): | |||
j = np.random.choice(range(N)) | |||
k = np.random.choice(range(24)) | |||
a.act_local_rotation(j, k) | |||
b.local_op(j, graphsim.LocCliffOp(k)) | |||
for i in range(10): | |||
j, k= np.random.choice(range(N), 2, replace=False) | |||
a.act_cz(j, k) | |||
b.cphase(j, k) | |||
return a, b | |||
def bell(): | |||
a = GraphState(range(2)) | |||
b = graphsim.GraphRegister(2) | |||
a.act_hadamard(0); a.act_hadamard(1); | |||
b.hadamard(0); b.hadamard(1); | |||
a.act_cz(0,1) | |||
b.cphase(0,1) | |||
return a, b |
@@ -35,10 +35,9 @@ def test_hadamard_only_multiqubit(n=6): | |||
assert g.to_state_vector() == c | |||
for i in range(100): | |||
a, b = np.random.randint(0, n - 1, 2) | |||
if a != b: | |||
g.act_cz(a, b) | |||
c.act_cz(a, b) | |||
a, b = np.random.choice(range(n), 2, False) | |||
g.act_cz(a, b) | |||
c.act_cz(a, b) | |||
assert g.to_state_vector() == c | |||
@@ -56,15 +55,14 @@ def test_all_multiqubit(n=4): | |||
assert g.to_state_vector() == c | |||
for repeat in tqdm(range(REPEATS), desc="Testing against circuit model"): | |||
a, b = np.random.randint(0, n - 1, 2) | |||
if a != b: | |||
g.act_cz(a, b) | |||
c.act_cz(a, b) | |||
assert np.allclose(np.sum(np.abs(c.state) ** 2), 1) | |||
assert np.allclose( | |||
np.sum(np.abs(g.to_state_vector().state) ** 2), 1) | |||
a, b = np.random.choice(range(n), 2, False) | |||
g.act_cz(a, b) | |||
c.act_cz(a, b) | |||
assert np.allclose(np.sum(np.abs(c.state) ** 2), 1) | |||
assert np.allclose( | |||
np.sum(np.abs(g.to_state_vector().state) ** 2), 1) | |||
assert g.to_state_vector() == c | |||
assert g.to_state_vector() == c | |||
assert g.to_state_vector() == c | |||
@@ -80,9 +78,8 @@ def test_all(n=8): | |||
g.act_local_rotation(qubit, rotation) | |||
c.act_local_rotation(qubit, clifford.unitaries[rotation]) | |||
else: | |||
a, b = np.random.randint(0, n - 1, 2) | |||
if a != b: | |||
g.act_cz(a, b) | |||
c.act_cz(a, b) | |||
a, b = np.random.choice(range(n), 2, False) | |||
g.act_cz(a, b) | |||
c.act_cz(a, b) | |||
assert g.to_state_vector() == c | |||
@@ -53,42 +53,4 @@ def test_another_projection(): | |||
g.measure(0, "pz", 1) | |||
assert np.allclose(g.to_state_vector().state, qi.one) | |||
def test_z_measurement_against_ab(): | |||
for i in range(10): | |||
a = graphsim.GraphRegister(1) | |||
b = GraphState() | |||
b.add_node(0) | |||
#print a.measure(0, graphsim.lco_Z) | |||
#print b.measure(0, "pz") | |||
def test_all(N=20): | |||
""" Test everything""" | |||
#clifford.use_old_cz() | |||
#a = graphsim.GraphRegister(N) | |||
#b = GraphState(range(N)) | |||
#previous_state, previous_cz = None, None | |||
#for i in tqdm(range(REPEATS), desc="Testing all gates against Anders and Briegel"): | |||
#which = random.choice([LOCAL_ROTATION, CZ, MEASURE]) | |||
#if which == LOCAL_ROTATION: | |||
#j = random.randint(0, N-1) | |||
#u = random.randint(0, 23) | |||
#a.local_op(j, graphsim.LocCliffOp(u)) | |||
#b.act_local_rotation(j, u) | |||
#elif which == CZ: | |||
#q = random.randint(0, N-2) | |||
#if a!=b: | |||
#a.cphase(q, q+1) | |||
#b.act_cz(q, q+1) | |||
#else: | |||
#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 | |||
@@ -0,0 +1,31 @@ | |||
from abp import GraphState, clifford | |||
from anders_briegel import graphsim | |||
import numpy as np | |||
from tqdm import tqdm | |||
import dummy | |||
N = 2 | |||
REPEATS = 10 | |||
PZ = graphsim.lco_Z | |||
def _test_multiqubit_measurement_pz(): | |||
""" Test a multiqubit measurement """ | |||
for i in tqdm(range(REPEATS)): | |||
a, b = dummy.random_state(messy=False) | |||
j = np.random.choice(range(N)) | |||
k = "pz" | |||
a.measure(j, k, 0) | |||
print a.to_json() | |||
print b.to_json() | |||
#assert a.to_json() == b.to_json(), a | |||
def test_multiqubit_pz(): | |||
for i in range(10): | |||
a, b = dummy.bell() | |||
assert a == b | |||
print a.measure(0, "pz", 1) | |||
print b.measure(0, PZ, None, 1) | |||
assert a == b | |||