Browse code

Add a function for constructing an arbitrary state vector

Joseph Weston authored on 15/11/2019 18:34:36
Showing 1 changed files
... ...
@@ -28,6 +28,15 @@ def unitary(n_qubits):
28 28
     )
29 29
 
30 30
 
31
+def ket(n_qubits):
32
+    size = 1 << n_qubits
33
+    return (
34
+        hnp.arrays(complex, (size,), valid_complex)
35
+        .filter(lambda v: np.linalg.norm(v) > 0)  # vectors must be normalizable
36
+        .map(lambda v: v / np.linalg.norm(v))
37
+    )
38
+
39
+
31 40
 single_qubit_gates = unitary(1)
32 41
 two_qubit_gates = unitary(2)
33 42
 n_qubit_gates = n_qubits.flatmap(unitary)