Browse code

Make related strategies closer together in 'test_gate.py'

This is a pure refactoring

Joseph Weston authored on 15/11/2019 18:26:27
Showing 1 changed files
... ...
@@ -7,22 +7,32 @@ import pytest
7 7
 import qsim.gate
8 8
 
9 9
 
10
-def unitary(n):
11
-    valid_complex = st.complex_numbers(allow_infinity=False, allow_nan=False)
12
-    return (
13
-        hnp.arrays(complex, (n, n), valid_complex)
14
-        .map(lambda a: np.linalg.qr(a)[0])
15
-        .filter(lambda u: np.all(np.isfinite(u)))
16
-    )
10
+# -- Strategies for generating values --
17 11
 
18 12
 
19 13
 n_qubits = st.shared(st.integers(min_value=1, max_value=6))
14
+
15
+
16
+valid_complex = st.complex_numbers(allow_infinity=False, allow_nan=False)
20 17
 phases = st.floats(
21 18
     min_value=0, max_value=2 * np.pi, allow_nan=False, allow_infinity=False
22 19
 )
23
-single_qubit_gates = unitary(2)
24
-two_qubit_gates = unitary(4)
25
-n_qubit_gates = n_qubits.map(lambda n: 2 ** n).flatmap(unitary)
20
+
21
+
22
+def unitary(n_qubits):
23
+    size = 1 << n_qubits
24
+    return (
25
+        hnp.arrays(complex, (size, size), valid_complex)
26
+        .map(lambda a: np.linalg.qr(a)[0])
27
+        .filter(lambda u: np.all(np.isfinite(u)))
28
+    )
29
+
30
+
31
+single_qubit_gates = unitary(1)
32
+two_qubit_gates = unitary(2)
33
+n_qubit_gates = n_qubits.flatmap(unitary)
34
+
35
+# -- Tests --
26 36
 
27 37
 
28 38
 @given(n_qubits, n_qubit_gates)