From 4438ca80c37147f7fc1dd8c2d77a2c15d7feee13 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Tue, 24 May 2016 00:29:09 +0100 Subject: [PATCH] Not much --- abp/graphstate.py | 19 +++---- scripts/try_stabilizing.py | 21 +++++++ tests/test_against_anders_and_briegel.py | 72 ++++++++++++++++-------- tests/test_against_circuit_model.py | 22 +++++++- 4 files changed, 99 insertions(+), 35 deletions(-) create mode 100644 scripts/try_stabilizing.py diff --git a/abp/graphstate.py b/abp/graphstate.py index 1abefee..c995ad7 100644 --- a/abp/graphstate.py +++ b/abp/graphstate.py @@ -167,17 +167,14 @@ class GraphState(object): def to_stabilizer(self): """ Get the stabilizer of this graph """ - # TODO: VOPs are not implemented yet - output = "" - for a in self.adj: - for b in self.adj: - if a == b: - output += " X " - elif a in self.adj[b]: - output += " Z " - else: - output += " I " - output += "\n" + output = {a:{} for a in self.node} + for a, b in it.product(self.node, self.node): + if a == b: + output[a][b] = "X" + elif a in self.adj[b]: + output[a][b] = "Z" + else: + output[a][b] = "I" return output def adj_list(self): diff --git a/scripts/try_stabilizing.py b/scripts/try_stabilizing.py new file mode 100644 index 0000000..450d360 --- /dev/null +++ b/scripts/try_stabilizing.py @@ -0,0 +1,21 @@ +import abp +import numpy as np +from anders_briegel import graphsim + +def wah(): + N = 10 + a = graphsim.GraphRegister(N) + + for i in range(1000): + if np.random.random()>0.5: + j = np.random.randint(0, N-1) + a.hadamard(j) + else: + q = np.random.randint(0, N-2) + a.cphase(q, q+1) + + a.print_stabilizer() + + +u = abp.GraphState(xrange(2)) +print u.to_stabilizer() diff --git a/tests/test_against_anders_and_briegel.py b/tests/test_against_anders_and_briegel.py index b008db6..d102c09 100644 --- a/tests/test_against_anders_and_briegel.py +++ b/tests/test_against_anders_and_briegel.py @@ -1,5 +1,6 @@ from abp import GraphState from anders_briegel import graphsim +from abp import CircuitModel from abp import clifford import random import difflib @@ -16,6 +17,13 @@ def compare(a, b): print bb raise +def isequal(a, b): + """ TODO: Sketchy as you like. Remove this abomination """ + aa = a.get_adj_list() + bb = b.adj_list() + return re.sub("\\s", "", aa) == re.sub("\\s", "", bb) + + def test_hadamard(): """ Test hadamards """ a = graphsim.GraphRegister(1) @@ -50,23 +58,45 @@ def test_cz_table(N=10): clifford.use_old_cz() - for j in range(24): - a = graphsim.GraphRegister(2) - b = GraphState() - b.add_node(0) - b.add_node(1) - compare(a, b) + for i in range(24): + for j in range(24): - a.local_op(0, graphsim.LocCliffOp(j)) - b.act_local_rotation(0, j) + a = graphsim.GraphRegister(2) + b = GraphState() + b.add_nodes([0, 1]) - a.local_op(1, graphsim.LocCliffOp(j)) - b.act_local_rotation(1, j) + 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) - compare(a, b) + a.cphase(0, 1) + b.act_cz(0, 1) + + compare(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) + + compare(a, b) def test_with_cphase_gates_hadamard_only(N=10): @@ -87,18 +117,16 @@ def test_with_cphase_gates_hadamard_only(N=10): compare(a, b) -def test_all(N=10): +def test_all(N=3): """ Test all gates at random """ + #TODO: Currently fails. Why??? clifford.use_old_cz() a = graphsim.GraphRegister(N) - b = GraphState() - - for i in range(N): - b.add_node(i) + b = GraphState(range(N)) - for i in range(100): + for i in range(1000): if random.random()>0.5: j = random.randint(0, N-1) a.hadamard(j) @@ -107,9 +135,7 @@ def test_all(N=10): q = random.randint(0, N-2) a.cphase(q, q+1) b.act_cz(q, q+1) - - aa = a.get_adj_list() - bb = b.adj_list() - compare(a, b) + compare(a, b) + #print b diff --git a/tests/test_against_circuit_model.py b/tests/test_against_circuit_model.py index ffe90ad..4f76fa8 100644 --- a/tests/test_against_circuit_model.py +++ b/tests/test_against_circuit_model.py @@ -7,7 +7,7 @@ from tqdm import tqdm REPEATS = 1 -def test_single_qubit(n=1): +def test_single_qubit(): """ A multi qubit test with Hadamards only""" for repeat in tqdm(range(REPEATS), desc="Testing against circuit model"): g = GraphState([0]) @@ -66,3 +66,23 @@ def test_all_multiqubit(n=4): assert g.to_state_vector() == c assert g.to_state_vector() == c + +def test_all(n=4): + """ A multi qubit test with arbitrary local rotations """ + g = GraphState(range(n)) + c = CircuitModel(n) + for step in tqdm(xrange(1000), "Testing a deep circuit against the circuit model"): + if random.random()>0.5: + qubit = np.random.randint(0, n - 1) + rotation = np.random.randint(0, 24 - 1) + 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) + assert g.to_state_vector() == c + print g.to_state_vector() + print c +