Browse code

moved to src directory structure

This avoids problems with testing against the wrong package version

Joseph Weston authored on 30/11/2019 18:31:57
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,41 @@
1
+"""Quantum state vectors
2
+
3
+The quantum state of :math:`n` quantum bits is represented as a 1D array of complex
4
+numbers of length :math:`2^n`; the components of the state vector in the
5
+computational basis.
6
+
7
+The computational basis for :math:`n` qubits is ordered by the number represented
8
+by the associated classical bitstring.
9
+"""
10
+
11
+import numpy as np
12
+
13
+__all__ = ["from_classical"]  # type: ignore
14
+
15
+
16
+def from_classical(bitstring):
17
+    """Return a quantum state corresponding to a classical bitstring.
18
+
19
+    Parameters
20
+    ----------
21
+    bitstring : sequence of bits
22
+        Can be a string like "01011", or a sequence of
23
+        integers.
24
+
25
+    Returns
26
+    -------
27
+    state : ndarray[(2**n,), complex]
28
+        The state vector in the computational basis.
29
+        Has :math:`2^n` components.
30
+    """
31
+
32
+    bitstring = "".join(map(str, bitstring))
33
+    n_qubits = len(bitstring)
34
+    try:
35
+        index = int(bitstring, base=2)
36
+    except ValueError:
37
+        raise ValueError("Input is not a classical bitstring") from None
38
+
39
+    state = np.zeros(1 << n_qubits, dtype=complex)
40
+    state[index] = 1
41
+    return state