Browse code

Compute number of qubits by using bit length rather than logarithms

This is more idiomatic.

Joseph Weston authored on 15/11/2019 17:32:29
Showing 1 changed files
... ...
@@ -38,7 +38,7 @@ def _check_valid_gate(gate):
38 38
         and gate.shape[0] == gate.shape[1]
39 39
         # has size 2**n, n > 1
40 40
         and np.log2(gate.shape[0]).is_integer()
41
-        and np.log2(gate.shape[0]) > 0
41
+        and gate.shape[0].bit_length() > 1
42 42
         # is unitary
43 43
         and np.allclose(gate @ gate.conjugate().transpose(), np.identity(gate.shape[0]))
44 44
     ):
... ...
@@ -52,9 +52,7 @@ def n_qubits(gate):
52 52
     an integer power of 2.
53 53
     """
54 54
     _check_valid_gate(gate)
55
-    n = np.log2(gate.shape[0])
56
-    assert n.is_integer()
57
-    return int(n)
55
+    return gate.shape[0].bit_length() - 1
58 56
 
59 57
 
60 58
 def controlled(gate):