From 784f9d27dcfa6173f570a54487fe016829c44496 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Sat, 23 Jul 2016 16:34:48 +0100 Subject: [PATCH] Add simple tests --- abp/graphstate.py | 12 +++--- tests/demograph.py | 1 + tests/dummy.py | 41 +++++++++++++++++++ tests/test_against_circuit_model.py | 29 ++++++------- tests/test_measurement.py | 38 ----------------- ..._measurement_against_anders_and_briegel.py | 31 ++++++++++++++ 6 files changed, 93 insertions(+), 59 deletions(-) create mode 100644 tests/dummy.py create mode 100644 tests/test_measurement_against_anders_and_briegel.py diff --git a/abp/graphstate.py b/abp/graphstate.py index 22499b0..46075aa 100644 --- a/abp/graphstate.py +++ b/abp/graphstate.py @@ -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)) == "": + return self.to_json() == other.to_json() return self.adj == other.adj and self.node == other.node if __name__ == '__main__': diff --git a/tests/demograph.py b/tests/demograph.py index babbd42..443be90 100644 --- a/tests/demograph.py +++ b/tests/demograph.py @@ -10,3 +10,4 @@ def demograph(): g.add_edge(100, 200) return g + diff --git a/tests/dummy.py b/tests/dummy.py new file mode 100644 index 0000000..878aa07 --- /dev/null +++ b/tests/dummy.py @@ -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 diff --git a/tests/test_against_circuit_model.py b/tests/test_against_circuit_model.py index 130740d..0625622 100644 --- a/tests/test_against_circuit_model.py +++ b/tests/test_against_circuit_model.py @@ -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 diff --git a/tests/test_measurement.py b/tests/test_measurement.py index 741d6d2..72c8a85 100644 --- a/tests/test_measurement.py +++ b/tests/test_measurement.py @@ -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 - diff --git a/tests/test_measurement_against_anders_and_briegel.py b/tests/test_measurement_against_anders_and_briegel.py new file mode 100644 index 0000000..19314e7 --- /dev/null +++ b/tests/test_measurement_against_anders_and_briegel.py @@ -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() + print + #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 +