This avoids problems with testing against the wrong package version
Joseph Weston authored on 30/11/2019 18:31:571 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,41 +0,0 @@ |
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 |
This makes the function a bit clearer.
Joseph Weston authored on 15/11/2019 17:52:56... | ... |
@@ -30,12 +30,12 @@ def from_classical(bitstring): |
30 | 30 |
""" |
31 | 31 |
|
32 | 32 |
bitstring = "".join(map(str, bitstring)) |
33 |
- n = len(bitstring) |
|
33 |
+ n_qubits = len(bitstring) |
|
34 | 34 |
try: |
35 | 35 |
index = int(bitstring, base=2) |
36 | 36 |
except ValueError: |
37 | 37 |
raise ValueError("Input is not a classical bitstring") from None |
38 | 38 |
|
39 |
- state = np.zeros(2 ** n, dtype=complex) |
|
39 |
+ state = np.zeros(1 << n_qubits, dtype=complex) |
|
40 | 40 |
state[index] = 1 |
41 | 41 |
return state |
also add relevant tests.
Joseph Weston authored on 09/11/2019 00:48:19... | ... |
@@ -8,4 +8,34 @@ The computational basis for :math:`n` qubits is ordered by the number represente |
8 | 8 |
by the associated classical bitstring. |
9 | 9 |
""" |
10 | 10 |
|
11 |
-__all__ = [] |
|
11 |
+import numpy as np |
|
12 |
+ |
|
13 |
+__all__ = ["from_classical"] |
|
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[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 = 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(2 ** n, dtype=complex) |
|
40 |
+ state[index] = 1 |
|
41 |
+ return state |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,11 @@ |
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 |
+__all__ = [] |