From b878528cce0ffc9762e6bd3d39759d2d298fe767 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Mon, 25 Jul 2016 05:41:18 +0100 Subject: [PATCH] Another one down --- tests/old/test_against_anders_and_briegel.py | 157 ------------------- tests/test_against_anders_and_briegel.py | 128 +++------------ 2 files changed, 23 insertions(+), 262 deletions(-) delete mode 100644 tests/old/test_against_anders_and_briegel.py diff --git a/tests/old/test_against_anders_and_briegel.py b/tests/old/test_against_anders_and_briegel.py deleted file mode 100644 index b5cf65b..0000000 --- a/tests/old/test_against_anders_and_briegel.py +++ /dev/null @@ -1,157 +0,0 @@ -from abp import GraphState -from anders_briegel import graphsim -from abp import CircuitModel -from abp import clifford -import random -import numpy as np -from tqdm import tqdm -import itertools as it -from config import * - -def assert_equal(a, b, debug=""): - assert a.to_json() == b.to_json() - -def test_hadamard(): - """ Test hadamards """ - a = graphsim.GraphRegister(1) - b = GraphState() - b.add_node(0) - - assert_equal(a, b) - a.hadamard(0) - b.act_hadamard(0) - assert_equal(a, b) - a.hadamard(0) - b.act_hadamard(0) - assert_equal(a, b) - - -def test_local_rotations(): - """ Test local rotations """ - a = graphsim.GraphRegister(1) - b = GraphState() - b.add_node(0) - assert_equal(a, b) - - for i in range(1000): - j = random.randint(0, 23) - a.local_op(0, graphsim.LocCliffOp(j)) - b.act_local_rotation(0, j) - assert_equal(a, b) - - -def test_times_table(): - """ Test times table """ - for i, j in it.product(range(24), range(24)): - a = graphsim.GraphRegister(1) - b = GraphState([0]) - a.local_op(0, graphsim.LocCliffOp(i)) - a.local_op(0, graphsim.LocCliffOp(j)) - b.act_local_rotation(0, i) - b.act_local_rotation(0, j) - assert_equal(a, b) - - -def test_cz_table(N=10): - """ Test the CZ table """ - - clifford.use_old_cz() - - for i in range(24): - for j in range(24): - - a = graphsim.GraphRegister(2) - b = GraphState() - b.add_nodes([0, 1]) - - a.local_op(0, graphsim.LocCliffOp(i)) - b.act_local_rotation(0, i) - a.local_op(1, graphsim.LocCliffOp(j)) - b.act_local_rotation(1, j) - - a.cphase(0, 1) - b.act_cz(0, 1) - - assert_equal(a, b) - - for i in range(24): - for j in range(24): - - a = graphsim.GraphRegister(2) - b = GraphState() - b.add_nodes([0, 1]) - - a.local_op(0, graphsim.LocCliffOp(10)) - b.act_local_rotation(0, 10) - - a.cphase(0, 1) - b.act_cz(0, 1) - - a.local_op(0, graphsim.LocCliffOp(i)) - b.act_local_rotation(0, i) - a.local_op(1, graphsim.LocCliffOp(j)) - b.act_local_rotation(1, j) - - a.cphase(0, 1) - b.act_cz(0, 1) - - assert_equal(a, b) - - -def test_with_cphase_gates_hadamard_only(N=10): - """ Hadamrds and CPHASEs, deterministic """ - - a = graphsim.GraphRegister(N) - b = GraphState() - - for i in range(N): - a.hadamard(i) - b.add_node(i) - b.act_hadamard(i) - - for i in range(N-1): - a.cphase(i, i+1) - b.act_cz(i, i+1) - - assert_equal(a, b) - -def test_cz_hadamard(N=10): - """ Test CZs and Hadamards at random """ - - clifford.use_old_cz() - - a = graphsim.GraphRegister(N) - b = GraphState(range(N)) - 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) - 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) - assert_equal(a, b) - - - -def test_all(N=9): - """ Test everything""" - - clifford.use_old_cz() - - a = graphsim.GraphRegister(N) - b = GraphState(range(N)) - for i in tqdm(range(REPEATS), desc="Testing all gates against Anders and Briegel"): - if random.random()>0.5: - j = random.randint(0, N-1) - u = random.randint(0, 23) - a.local_op(j, graphsim.LocCliffOp(u)) - b.act_local_rotation(j, u) - else: - q = random.randint(0, N-2) - a.cphase(q, q+1) - b.act_cz(q, q+1) - assert_equal(a, b, str(i)) - - diff --git a/tests/test_against_anders_and_briegel.py b/tests/test_against_anders_and_briegel.py index db76afa..385784b 100644 --- a/tests/test_against_anders_and_briegel.py +++ b/tests/test_against_anders_and_briegel.py @@ -1,138 +1,56 @@ from abp import GraphState, CircuitModel, clifford from anders_briegel import graphsim import numpy as np +from numpy import random from tqdm import tqdm import itertools as it import mock REPEATS = 100 +DEPTH = 100 def test_hadamard(): """ Test hadamards """ - a = graphsim.GraphRegister(1) - b = GraphState() - b.add_node(0) - - assert_equal(a, b) - a.hadamard(0) - b.act_hadamard(0) - assert_equal(a, b) - a.hadamard(0) - b.act_hadamard(0) - assert_equal(a, b) + circuit = [(0, "hadamard")] + mock.test_circuit(circuit, 1) def test_local_rotations(): """ Test local rotations """ - a = graphsim.GraphRegister(1) - b = GraphState() - b.add_node(0) - assert_equal(a, b) - - for i in range(1000): - j = random.randint(0, 23) - a.local_op(0, graphsim.LocCliffOp(j)) - b.act_local_rotation(0, j) - assert_equal(a, b) - + for i in tqdm(range(REPEATS), "Testing local rotations"): + circuit = [(0, random.choice(range(24))) for j in range(DEPTH)] + mock.test_circuit(circuit, 1) def test_times_table(): """ Test times table """ for i, j in it.product(range(24), range(24)): - a = graphsim.GraphRegister(1) - b = GraphState([0]) - a.local_op(0, graphsim.LocCliffOp(i)) - a.local_op(0, graphsim.LocCliffOp(j)) - b.act_local_rotation(0, i) - b.act_local_rotation(0, j) - assert_equal(a, b) + circuit = [(0, i), (0, j)] + mock.test_circuit(circuit, 1) -def test_cz_table(N=10): +def test_cz_table(): """ Test the CZ table """ - for i, j in it.product(range(24), range(24)): circuit = [(0, i), (1, j), ((0, 1), "cz")] mock.test_circuit(circuit, 2) - #for i in range(24): - #for j in range(24): - - #a = graphsim.GraphRegister(2) - #b = GraphState() - #b.add_nodes([0, 1]) - - #a.local_op(0, graphsim.LocCliffOp(10)) - #b.act_local_rotation(0, 10) - - #a.cphase(0, 1) - #b.act_cz(0, 1) - - #a.local_op(0, graphsim.LocCliffOp(i)) - #b.act_local_rotation(0, i) - #a.local_op(1, graphsim.LocCliffOp(j)) - #b.act_local_rotation(1, j) - - #a.cphase(0, 1) - #b.act_cz(0, 1) - - #assert_equal(a, b) - - -def test_with_cphase_gates_hadamard_only(N=10): - """ Hadamrds and CPHASEs, deterministic """ - a = graphsim.GraphRegister(N) - b = GraphState() - - for i in range(N): - a.hadamard(i) - b.add_node(i) - b.act_hadamard(i) - - for i in range(N-1): - a.cphase(i, i+1) - b.act_cz(i, i+1) - - assert_equal(a, b) - -def test_cz_hadamard(N=10): +def test_cz_hadamard(n=10): """ Test CZs and Hadamards at random """ - - clifford.use_old_cz() - - a = graphsim.GraphRegister(N) - b = GraphState(range(N)) 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) - 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) - assert_equal(a, b) + circuit = random.choice(["cz", "hadamard"], DEPTH) + circuit = [(mock.random_pair(n), gate) if gate =="cz" + else (random.choice(range(n)), gate) + for gate in circuit] + mock.test_circuit(circuit, n) - - -def test_all(N=9): +def test_all(n=10): """ Test everything""" - - clifford.use_old_cz() - - a = graphsim.GraphRegister(N) - b = GraphState(range(N)) - for i in tqdm(range(REPEATS), desc="Testing all gates against Anders and Briegel"): - if random.random()>0.5: - j = random.randint(0, N-1) - u = random.randint(0, 23) - a.local_op(j, graphsim.LocCliffOp(u)) - b.act_local_rotation(j, u) - else: - q = random.randint(0, N-2) - a.cphase(q, q+1) - b.act_cz(q, q+1) - assert_equal(a, b, str(i)) + for i in tqdm(range(REPEATS), desc="Testing CZ and Hadamard against A&B"): + circuit = random.choice(["cz"]*10 + range(24), DEPTH) + circuit = [(mock.random_pair(n), gate) if gate =="cz" + else (random.choice(range(n)), gate) + for gate in circuit] + mock.test_circuit(circuit, n)