From b31fb544bf3422e600086442224fb1b3db38f1d5 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Thu, 21 Jul 2016 15:21:39 +0100 Subject: [PATCH] Found a nasty bug --- tests/test_against_anders_and_briegel.py | 26 +++++++------- tests/test_measurement.py | 43 +++++++++++++++++++++--- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/tests/test_against_anders_and_briegel.py b/tests/test_against_anders_and_briegel.py index ecabb3c..89890dc 100644 --- a/tests/test_against_anders_and_briegel.py +++ b/tests/test_against_anders_and_briegel.py @@ -4,12 +4,14 @@ 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 -REPEATS = 100 +REPEATS = 100000 -def assert_equal(a, b): - assert a.to_json() == b.to_json() +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_hadamard(): """ Test hadamards """ @@ -103,14 +105,14 @@ def test_with_cphase_gates_hadamard_only(N=10): assert_equal(a, b) -def test_cz_hadamard(N=3): +def test_cz_hadamard(N=20): """ Test CZs and Hadamards at random """ clifford.use_old_cz() + assert np.allclose(clifford.cz_table, abczt) a = graphsim.GraphRegister(N) b = GraphState(range(N)) - previous_state, previous_cz = None, None for i in tqdm(range(REPEATS), desc="Testing CZ and Hadamard against A&B"): if random.random()>0.5: j = random.randint(0, N-1) @@ -118,14 +120,13 @@ def test_cz_hadamard(N=3): b.act_hadamard(j) else: q = random.randint(0, N-2) - if a!=b: - a.cphase(q, q+1) - b.act_cz(q, q+1) + a.cphase(q, q+1) + b.act_cz(q, q+1) assert_equal(a, b) -def test_all(N=5): +def _test_all(N=9): """ Test everything""" clifford.use_old_cz() @@ -141,9 +142,8 @@ def test_all(N=5): b.act_local_rotation(j, u) else: q = random.randint(0, N-2) - if a!=b: - a.cphase(q, q+1) - b.act_cz(q, q+1) - assert_equal(a, b) + a.cphase(q, q+1) + b.act_cz(q, q+1) + assert_equal(a, b, str(i)) diff --git a/tests/test_measurement.py b/tests/test_measurement.py index 32f9363..dc411e4 100644 --- a/tests/test_measurement.py +++ b/tests/test_measurement.py @@ -1,7 +1,14 @@ import numpy as np from abp import GraphState -from abp import qi +from abp import qi, clifford from anders_briegel import graphsim +from tqdm import tqdm +import random + +REPEATS = 100000 +LOCAL_ROTATION = 0 +CZ = 1 +MEASURE = 2 def test_single_qubit_measurements(): """ Various simple tests of measurements """ @@ -23,7 +30,6 @@ def test_single_qubit_measurements(): g.act_local_rotation(0, "pz") assert g.measure(0, "px") == 1, "Measuring |-> in X gives 1" - def test_random_outcomes(): """ Testing random behaviour """ ones = 0 @@ -47,9 +53,6 @@ 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) @@ -57,3 +60,33 @@ def test_z_measurement_against_ab(): 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: + 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() + +