Browse Source

Test passing

Pete Shadbolt 1 year ago
parent
commit
2698308d49

+ 1
- 1
abp/clifford.py View File

@@ -40,7 +40,7 @@ The complete set of aliases for single-qubit Cliffords is as follows:
40 40
 
41 41
 """
42 42
 
43
-from .tables import *
43
+from tables import *
44 44
 
45 45
 # Aliases
46 46
 identity = by_name["identity"]

+ 1
- 1
abp/graphstate.py View File

@@ -108,7 +108,7 @@ class GraphState(object):
108 108
             >>> g.act_circuit([("hadamard", 0), ("hadamard", 1), ("cz", (0, 1))])
109 109
 
110 110
         """
111
-        for operation, node in circuit:
111
+        for node, operation in circuit:
112 112
             if operation == "cz":
113 113
                 self.act_cz(*node)
114 114
             else:

+ 54
- 0
tests/ab.py View File

@@ -0,0 +1,54 @@
1
+"""
2
+Mock graphs used for testing
3
+"""
4
+
5
+import numpy as np
6
+import abp
7
+from abp import GraphState, clifford, qi
8
+from numpy import random
9
+import pytest
10
+from anders_briegel import graphsim
11
+
12
+# We always run with A&B's CZ table when we are testing
13
+clifford.use_old_cz()
14
+
15
+
16
+class AndersWrapper(graphsim.GraphRegister):
17
+
18
+    """ A wrapper for A&B to make the interface identical and enable equality testing """
19
+
20
+    def __init__(self, nodes):
21
+        assert list(nodes) == list(range(len(nodes)))
22
+        super(AndersWrapper, self).__init__(len(nodes))
23
+
24
+    def act_local_rotation(self, qubit, operation):
25
+        operation = clifford.by_name[str(operation)]
26
+        op = graphsim.LocCliffOp(operation)
27
+        super(AndersWrapper, self).local_op(qubit, op)
28
+
29
+    def act_cz(self, a, b):
30
+        super(AndersWrapper, self).cphase(a, b)
31
+
32
+    def measure(self, qubit, basis, force):
33
+        basis = {1: graphsim.lco_X,
34
+                 2: graphsim.lco_Y,
35
+                 3: graphsim.lco_Z}[clifford.by_name[str(basis)]]
36
+        return super(AndersWrapper, self).measure(qubit, basis, None, force)
37
+
38
+    def __eq__(self, other):
39
+        return self.to_json() == other.to_json()
40
+
41
+    def act_circuit(self, circuit):
42
+        for operation, node in circuit:
43
+            if operation == "cz":
44
+                self.act_cz(*node)
45
+            else:
46
+                self.act_local_rotation(node, operation)
47
+
48
+
49
+def test_circuit(circuit, n):
50
+    """ Check that two classes exhibit the same behaviour for a given circuit """
51
+    a = circuit_to_state(ABPWrapper, n, circuit)
52
+    b = circuit_to_state(AndersWrapper, n, circuit)
53
+    assert a == b
54
+

+ 4
- 45
tests/mock.py View File

@@ -7,45 +7,10 @@ import abp
7 7
 from abp import GraphState, clifford, qi
8 8
 from numpy import random
9 9
 import pytest
10
-from anders_briegel import graphsim
11 10
 
12 11
 # We always run with A&B's CZ table when we are testing
13 12
 clifford.use_old_cz()
14 13
 
15
-
16
-class AndersWrapper(graphsim.GraphRegister):
17
-
18
-    """ A wrapper for A&B to make the interface identical and enable equality testing """
19
-
20
-    def __init__(self, nodes):
21
-        assert list(nodes) == list(range(len(nodes)))
22
-        super(AndersWrapper, self).__init__(len(nodes))
23
-
24
-    def act_local_rotation(self, qubit, operation):
25
-        operation = clifford.by_name[str(operation)]
26
-        op = graphsim.LocCliffOp(operation)
27
-        super(AndersWrapper, self).local_op(qubit, op)
28
-
29
-    def act_cz(self, a, b):
30
-        super(AndersWrapper, self).cphase(a, b)
31
-
32
-    def measure(self, qubit, basis, force):
33
-        basis = {1: graphsim.lco_X,
34
-                 2: graphsim.lco_Y,
35
-                 3: graphsim.lco_Z}[clifford.by_name[str(basis)]]
36
-        return super(AndersWrapper, self).measure(qubit, basis, None, force)
37
-
38
-    def __eq__(self, other):
39
-        return self.to_json() == other.to_json()
40
-
41
-    def act_circuit(self, circuit):
42
-        for operation, node in circuit:
43
-            if operation == "cz":
44
-                self.act_cz(*node)
45
-            else:
46
-                self.act_local_rotation(node, operation)
47
-
48
-
49 14
 class ABPWrapper(GraphState):
50 15
 
51 16
     """ A wrapper for abp, just to ensure determinism """
@@ -103,8 +68,8 @@ def named_node_graph():
103 68
     """ A graph with named nodes"""
104 69
     edges = (0, 1), (1, 2), (2, 0), (0, 3), (100, 200), (200, "named")
105 70
     g = ABPWrapper([0, 1, 2, 3, 100, 200, "named"])
106
-    g.act_circuit(("hadamard", i) for i in g.node)
107
-    g.act_circuit(("cz", edge) for edge in edges)
71
+    g.act_circuit((i, "hadamard") for i in g.node)
72
+    g.act_circuit((edge, "cz") for edge in edges)
108 73
     return g
109 74
 
110 75
 
@@ -112,8 +77,8 @@ def simple_graph():
112 77
     """ A simple graph to test with"""
113 78
     edges = (0, 1), (1, 2), (2, 0), (0, 3), (100, 200)
114 79
     g = ABPWrapper([0, 1, 2, 3, 100, 200])
115
-    g.act_circuit(("hadamard", i) for i in g.node)
116
-    g.act_circuit(("cz", edge) for edge in edges)
80
+    g.act_circuit((i, "hadamard") for i in g.node)
81
+    g.act_circuit((edge, "cz") for edge in edges)
117 82
     return g
118 83
 
119 84
 
@@ -124,12 +89,6 @@ def circuit_to_state(Base, n, circuit):
124 89
     return g
125 90
 
126 91
 
127
-def test_circuit(circuit, n):
128
-    """ Check that two classes exhibit the same behaviour for a given circuit """
129
-    a = circuit_to_state(ABPWrapper, n, circuit)
130
-    b = circuit_to_state(AndersWrapper, n, circuit)
131
-    assert a == b
132
-
133 92
 
134 93
 if __name__ == '__main__':
135 94
     for i in range(1000):

+ 9
- 7
tests/test_against_anders_and_briegel.py View File

@@ -3,7 +3,9 @@ import numpy as np
3 3
 from numpy import random
4 4
 import itertools as it
5 5
 import pytest
6
-mock = pytest.importorskip("mock")
6
+import mock
7
+ab = pytest.importorskip("ab")
8
+
7 9
 
8 10
 REPEATS = 100
9 11
 DEPTH = 100
@@ -12,28 +14,28 @@ PAULIS = ("px", "py", "pz")
12 14
 def test_hadamard():
13 15
     """ Test hadamards """
14 16
     circuit = [(0, "hadamard")]
15
-    mock.test_circuit(circuit, 1)
17
+    ab.test_circuit(circuit, 1)
16 18
 
17 19
 
18 20
 def test_local_rotations():
19 21
     """ Test local rotations """
20 22
     for i in list(range(REPEATS)):
21 23
         circuit = [(0, random.choice(list(range(24)))) for j in range(DEPTH)]
22
-        mock.test_circuit(circuit, 1)
24
+        ab.test_circuit(circuit, 1)
23 25
 
24 26
 
25 27
 def test_times_table():
26 28
     """ Test times table """
27 29
     for i, j in it.product(list(range(24)), list(range(24))):
28 30
         circuit = [(0, i), (0, j)]
29
-        mock.test_circuit(circuit, 1)
31
+        ab.test_circuit(circuit, 1)
30 32
 
31 33
 
32 34
 def test_cz_table():
33 35
     """ Test the CZ table """
34 36
     for i, j in it.product(list(range(24)), list(range(24))):
35 37
         circuit = [(0, i), (1, j), ((0, 1), "cz")]
36
-        mock.test_circuit(circuit, 2)
38
+        ab.test_circuit(circuit, 2)
37 39
 
38 40
 
39 41
 def test_cz_hadamard(n=10):
@@ -43,7 +45,7 @@ def test_cz_hadamard(n=10):
43 45
         circuit = [(mock.random_pair(n), gate) if gate == "cz"
44 46
                    else (random.choice(list(range(n))), gate)
45 47
                    for gate in circuit]
46
-        mock.test_circuit(circuit, n)
48
+        ab.test_circuit(circuit, n)
47 49
 
48 50
 
49 51
 def test_all(n=10):
@@ -53,7 +55,7 @@ def test_all(n=10):
53 55
         circuit = [(mock.random_pair(n), gate) if gate == "cz"
54 56
                    else (random.choice(list(range(n))), gate)
55 57
                    for gate in circuit]
56
-        mock.test_circuit(circuit, n)
58
+        ab.test_circuit(circuit, n)
57 59
 
58 60
 
59 61
 def test_single_qubit_measurement():

+ 1
- 2
tests/test_graphstate.py View File

@@ -2,8 +2,7 @@ from abp import GraphState, CircuitModel, clifford
2 2
 import random
3 3
 import numpy as np
4 4
 import networkx as nx
5
-import pytest
6
-mock = pytest.importorskip("mock")
5
+import mock
7 6
 
8 7
 REPEATS = 100
9 8
 DEPTH = 100

+ 1
- 2
tests/test_json.py View File

@@ -1,6 +1,5 @@
1 1
 import abp
2
-import pytest
3
-mock = pytest.importorskip("mock")
2
+import mock
4 3
 
5 4
 def test_json():
6 5
     """ Test to_json and from_json """

+ 1
- 2
tests/test_mercedes.py View File

@@ -1,7 +1,6 @@
1 1
 from abp import GraphState
2 2
 from abp.util import xyz
3
-import pytest
4
-mock = pytest.importorskip("mock")
3
+import mock
5 4
 
6 5
 def linear_cluster(n):
7 6
     g = GraphState(list(range(n)), vop="hadamard")

+ 1
- 2
tests/test_nx.py View File

@@ -3,8 +3,7 @@ import networkx as nx
3 3
 from abp import GraphState, NXGraphState
4 4
 from abp import clifford
5 5
 from abp.util import xyz
6
-import pytest
7
-mock = pytest.importorskip("mock")
6
+import mock
8 7
 
9 8
 
10 9
 def test_json_basic():

+ 1
- 2
tests/test_qi.py View File

@@ -1,7 +1,6 @@
1 1
 import numpy as np
2 2
 from abp import qi, GraphState
3
-import pytest
4
-mock = pytest.importorskip("mock")
3
+import mock
5 4
 
6 5
 DEPTH = 1000
7 6
 

+ 3
- 2
tests/test_stabilizer.py View File

@@ -1,6 +1,7 @@
1 1
 from abp import GraphState
2
+import mock
2 3
 import pytest
3
-mock = pytest.importorskip("mock")
4
+ab = pytest.importorskip("ab")
4 5
 
5 6
 REPEATS = 1000
6 7
 
@@ -9,7 +10,7 @@ def test_stabilizers_against_anders_and_briegel(n=10):
9 10
     
10 11
     for i in list(range(REPEATS)):
11 12
         c = mock.random_stabilizer_circuit(n)
12
-        g = mock.AndersWrapper(list(range(n)))
13
+        g = ab.AndersWrapper(list(range(n)))
13 14
         g.act_circuit(c)
14 15
         da = g.get_full_stabilizer().to_dictionary()
15 16