From 7016e75be6a616757aef267c76ea8fb4fe382aab Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Mon, 25 Jul 2016 05:50:25 +0100 Subject: [PATCH] Cleaner measurement testing --- tests/mock.py | 16 ++++----- tests/old/test_measurement_deterministic.py | 38 --------------------- tests/test_against_anders_and_briegel.py | 18 ++++++++-- tests/{old => }/test_measurement.py | 0 4 files changed, 23 insertions(+), 49 deletions(-) delete mode 100644 tests/old/test_measurement_deterministic.py rename tests/{old => }/test_measurement.py (100%) diff --git a/tests/mock.py b/tests/mock.py index 9dd8dc6..e66d124 100644 --- a/tests/mock.py +++ b/tests/mock.py @@ -10,7 +10,6 @@ from numpy import random # We always run with A&B's CZ table when we are testing clifford.use_old_cz() - class AndersWrapper(graphsim.GraphRegister): """ A wrapper for A&B to make the interface identical and enable equality testing """ @@ -28,11 +27,10 @@ class AndersWrapper(graphsim.GraphRegister): super(AndersWrapper, self).cphase(a, b) def measure(self, qubit, basis, force): - basis = clifford.by_name[basis] basis = {1: graphsim.lco_X, 2: graphsim.lco_Y, - 3: graphsim.lco_Z}[clifford.by_name[basis]] - super(AndersWrapper, self).measure(qubit, basis, None, force) + 3: graphsim.lco_Z}[clifford.by_name[str(basis)]] + return super(AndersWrapper, self).measure(qubit, basis, None, force) def __eq__(self, other): return self.to_json() == other.to_json() @@ -55,6 +53,7 @@ class ABPWrapper(GraphState): def __eq__(self, other): return self.to_json() == other.to_json() + class CircuitModelWrapper(qi.CircuitModel): def __init__(self, nodes=[]): @@ -85,7 +84,7 @@ def random_graph_circuit(n=10): def random_stabilizer_circuit(n=10): """ Generate a random stabilizer state, without any VOPs """ return random_graph_circuit(n) + \ - [(i, random.choice(range(24))) for i in range(n)] + [(i, random.choice(range(24))) for i in range(n)] def bell_pair(): @@ -110,16 +109,18 @@ def simple_graph(): g.act_circuit((edge, "cz") for edge in edges) return g + def circuit_to_state(Base, n, circuit): """ Convert a circuit to a state, given a base class """ g = Base(range(n)) g.act_circuit(circuit) return g + def test_circuit(circuit, n): """ Check that two classes exhibit the same behaviour for a given circuit """ - a = circuit_to_state(ABPWrapper, n, circuit) - b = circuit_to_state(AndersWrapper, n, circuit) + a = circuit_to_state(ABPWrapper, n, circuit) + b = circuit_to_state(AndersWrapper, n, circuit) assert a == b @@ -127,4 +128,3 @@ if __name__ == '__main__': for i in range(1000): test_circuit(random_graph_circuit(10), 10) test_circuit(random_stabilizer_circuit(10), 10) - diff --git a/tests/old/test_measurement_deterministic.py b/tests/old/test_measurement_deterministic.py deleted file mode 100644 index cbf41fc..0000000 --- a/tests/old/test_measurement_deterministic.py +++ /dev/null @@ -1,38 +0,0 @@ -from abp import GraphState, clifford -from anders_briegel import graphsim -import numpy as np -from tqdm import tqdm -import dummy -import itertools as it - -import networkx as nx - - -#def all_simple_graphs(filename="tests/graph5.g6"): - #""" Generate all possible simple graphs """ - #with open(filename) as f: - #for line in tqdm(f): - #yield nx.parse_graph6(line.strip()) - -#def rotated(simple_graphs): - #for g in simple_graphs: - #for r in it.product(*[range(24)]*2): - #yield g, r - - -#print len(list(rotated(all_simple_graphs()))) - - -#N = 3 -#m = {1: graphsim.lco_X, 2: graphsim.lco_Y, 3: graphsim.lco_Z} - -#measurements = (3, 2, 1) -#outcomes = (0, 1) -#local_ops = it.combinations_with_replacement(range(24), N) -#edge_patterns = - -#print list(local_ops) - -#print len(list(local_ops)) -#print list(edge_patterns) - diff --git a/tests/test_against_anders_and_briegel.py b/tests/test_against_anders_and_briegel.py index 385784b..8b1d5fc 100644 --- a/tests/test_against_anders_and_briegel.py +++ b/tests/test_against_anders_and_briegel.py @@ -21,6 +21,7 @@ def test_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)): @@ -39,18 +40,29 @@ def test_cz_hadamard(n=10): """ Test CZs and Hadamards at random """ for i in tqdm(range(REPEATS), desc="Testing CZ and Hadamard against A&B"): circuit = random.choice(["cz", "hadamard"], DEPTH) - circuit = [(mock.random_pair(n), gate) if gate =="cz" + 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=10): """ Test everything""" 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" + 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) +def test_single_qubit_measurement(): + """ Check that single qubits work """ + space = it.product(range(24), ("px", "py", "pz"), (0, 1)) + for rotation, measurement, outcome in tqdm(space, "Testing single qubit measurements"): + a = mock.circuit_to_state(mock.ABPWrapper, 1, [(0, rotation)]) + b = mock.circuit_to_state(mock.AndersWrapper, 1, [(0, rotation)]) + result_a = a.measure(0, measurement, outcome) + result_b = b.measure(0, measurement, outcome) + assert result_a == result_b + assert a == b diff --git a/tests/old/test_measurement.py b/tests/test_measurement.py similarity index 100% rename from tests/old/test_measurement.py rename to tests/test_measurement.py