diff --git a/abp/clifford.py b/abp/clifford.py index 64a29c9..38d0c02 100644 --- a/abp/clifford.py +++ b/abp/clifford.py @@ -40,7 +40,7 @@ The complete set of aliases for single-qubit Cliffords is as follows: """ -from .tables import * +from tables import * # Aliases identity = by_name["identity"] diff --git a/abp/graphstate.py b/abp/graphstate.py index 2ef246e..c5500a8 100755 --- a/abp/graphstate.py +++ b/abp/graphstate.py @@ -108,7 +108,7 @@ class GraphState(object): >>> g.act_circuit([("hadamard", 0), ("hadamard", 1), ("cz", (0, 1))]) """ - for operation, node in circuit: + for node, operation in circuit: if operation == "cz": self.act_cz(*node) else: diff --git a/tests/ab.py b/tests/ab.py new file mode 100644 index 0000000..db44235 --- /dev/null +++ b/tests/ab.py @@ -0,0 +1,54 @@ +""" +Mock graphs used for testing +""" + +import numpy as np +import abp +from abp import GraphState, clifford, qi +from numpy import random +import pytest +from anders_briegel import graphsim + +# 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 """ + + def __init__(self, nodes): + assert list(nodes) == list(range(len(nodes))) + super(AndersWrapper, self).__init__(len(nodes)) + + def act_local_rotation(self, qubit, operation): + operation = clifford.by_name[str(operation)] + op = graphsim.LocCliffOp(operation) + super(AndersWrapper, self).local_op(qubit, op) + + def act_cz(self, a, b): + super(AndersWrapper, self).cphase(a, b) + + def measure(self, qubit, basis, force): + basis = {1: graphsim.lco_X, + 2: graphsim.lco_Y, + 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() + + def act_circuit(self, circuit): + for operation, node in circuit: + if operation == "cz": + self.act_cz(*node) + else: + self.act_local_rotation(node, operation) + + +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) + assert a == b + diff --git a/tests/mock.py b/tests/mock.py index 8190edc..87a3146 100644 --- a/tests/mock.py +++ b/tests/mock.py @@ -7,45 +7,10 @@ import abp from abp import GraphState, clifford, qi from numpy import random import pytest -from anders_briegel import graphsim # 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 """ - - def __init__(self, nodes): - assert list(nodes) == list(range(len(nodes))) - super(AndersWrapper, self).__init__(len(nodes)) - - def act_local_rotation(self, qubit, operation): - operation = clifford.by_name[str(operation)] - op = graphsim.LocCliffOp(operation) - super(AndersWrapper, self).local_op(qubit, op) - - def act_cz(self, a, b): - super(AndersWrapper, self).cphase(a, b) - - def measure(self, qubit, basis, force): - basis = {1: graphsim.lco_X, - 2: graphsim.lco_Y, - 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() - - def act_circuit(self, circuit): - for operation, node in circuit: - if operation == "cz": - self.act_cz(*node) - else: - self.act_local_rotation(node, operation) - - class ABPWrapper(GraphState): """ A wrapper for abp, just to ensure determinism """ @@ -103,8 +68,8 @@ def named_node_graph(): """ A graph with named nodes""" edges = (0, 1), (1, 2), (2, 0), (0, 3), (100, 200), (200, "named") g = ABPWrapper([0, 1, 2, 3, 100, 200, "named"]) - g.act_circuit(("hadamard", i) for i in g.node) - g.act_circuit(("cz", edge) for edge in edges) + g.act_circuit((i, "hadamard") for i in g.node) + g.act_circuit((edge, "cz") for edge in edges) return g @@ -112,8 +77,8 @@ def simple_graph(): """ A simple graph to test with""" edges = (0, 1), (1, 2), (2, 0), (0, 3), (100, 200) g = ABPWrapper([0, 1, 2, 3, 100, 200]) - g.act_circuit(("hadamard", i) for i in g.node) - g.act_circuit(("cz", edge) for edge in edges) + g.act_circuit((i, "hadamard") for i in g.node) + g.act_circuit((edge, "cz") for edge in edges) return g @@ -124,12 +89,6 @@ def circuit_to_state(Base, n, 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) - assert a == b - if __name__ == '__main__': for i in range(1000): diff --git a/tests/test_against_anders_and_briegel.py b/tests/test_against_anders_and_briegel.py index 7ec8ba3..0511446 100644 --- a/tests/test_against_anders_and_briegel.py +++ b/tests/test_against_anders_and_briegel.py @@ -3,7 +3,9 @@ import numpy as np from numpy import random import itertools as it import pytest -mock = pytest.importorskip("mock") +import mock +ab = pytest.importorskip("ab") + REPEATS = 100 DEPTH = 100 @@ -12,28 +14,28 @@ PAULIS = ("px", "py", "pz") def test_hadamard(): """ Test hadamards """ circuit = [(0, "hadamard")] - mock.test_circuit(circuit, 1) + ab.test_circuit(circuit, 1) def test_local_rotations(): """ Test local rotations """ for i in list(range(REPEATS)): circuit = [(0, random.choice(list(range(24)))) for j in range(DEPTH)] - mock.test_circuit(circuit, 1) + ab.test_circuit(circuit, 1) def test_times_table(): """ Test times table """ for i, j in it.product(list(range(24)), list(range(24))): circuit = [(0, i), (0, j)] - mock.test_circuit(circuit, 1) + ab.test_circuit(circuit, 1) def test_cz_table(): """ Test the CZ table """ for i, j in it.product(list(range(24)), list(range(24))): circuit = [(0, i), (1, j), ((0, 1), "cz")] - mock.test_circuit(circuit, 2) + ab.test_circuit(circuit, 2) def test_cz_hadamard(n=10): @@ -43,7 +45,7 @@ def test_cz_hadamard(n=10): circuit = [(mock.random_pair(n), gate) if gate == "cz" else (random.choice(list(range(n))), gate) for gate in circuit] - mock.test_circuit(circuit, n) + ab.test_circuit(circuit, n) def test_all(n=10): @@ -53,7 +55,7 @@ def test_all(n=10): circuit = [(mock.random_pair(n), gate) if gate == "cz" else (random.choice(list(range(n))), gate) for gate in circuit] - mock.test_circuit(circuit, n) + ab.test_circuit(circuit, n) def test_single_qubit_measurement(): diff --git a/tests/test_graphstate.py b/tests/test_graphstate.py index d64d632..12f4658 100644 --- a/tests/test_graphstate.py +++ b/tests/test_graphstate.py @@ -2,8 +2,7 @@ from abp import GraphState, CircuitModel, clifford import random import numpy as np import networkx as nx -import pytest -mock = pytest.importorskip("mock") +import mock REPEATS = 100 DEPTH = 100 diff --git a/tests/test_json.py b/tests/test_json.py index fd75911..94cbf1d 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -1,6 +1,5 @@ import abp -import pytest -mock = pytest.importorskip("mock") +import mock def test_json(): """ Test to_json and from_json """ diff --git a/tests/test_mercedes.py b/tests/test_mercedes.py index d4b6d6c..98e9b27 100644 --- a/tests/test_mercedes.py +++ b/tests/test_mercedes.py @@ -1,7 +1,6 @@ from abp import GraphState from abp.util import xyz -import pytest -mock = pytest.importorskip("mock") +import mock def linear_cluster(n): g = GraphState(list(range(n)), vop="hadamard") diff --git a/tests/test_nx.py b/tests/test_nx.py index 9f35131..c28a45a 100644 --- a/tests/test_nx.py +++ b/tests/test_nx.py @@ -3,8 +3,7 @@ import networkx as nx from abp import GraphState, NXGraphState from abp import clifford from abp.util import xyz -import pytest -mock = pytest.importorskip("mock") +import mock def test_json_basic(): diff --git a/tests/test_qi.py b/tests/test_qi.py index eccf5e6..b86593d 100644 --- a/tests/test_qi.py +++ b/tests/test_qi.py @@ -1,7 +1,6 @@ import numpy as np from abp import qi, GraphState -import pytest -mock = pytest.importorskip("mock") +import mock DEPTH = 1000 diff --git a/tests/test_stabilizer.py b/tests/test_stabilizer.py index a71de4b..8340c3e 100644 --- a/tests/test_stabilizer.py +++ b/tests/test_stabilizer.py @@ -1,6 +1,7 @@ from abp import GraphState +import mock import pytest -mock = pytest.importorskip("mock") +ab = pytest.importorskip("ab") REPEATS = 1000 @@ -9,7 +10,7 @@ def test_stabilizers_against_anders_and_briegel(n=10): for i in list(range(REPEATS)): c = mock.random_stabilizer_circuit(n) - g = mock.AndersWrapper(list(range(n))) + g = ab.AndersWrapper(list(range(n))) g.act_circuit(c) da = g.get_full_stabilizer().to_dictionary()