emu-mps 1.2.4__py3-none-any.whl → 1.2.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
emu_mps/__init__.py CHANGED
@@ -35,4 +35,4 @@ __all__ = [
35
35
  "SecondMomentOfEnergy",
36
36
  ]
37
37
 
38
- __version__ = "1.2.4"
38
+ __version__ = "1.2.5"
emu_mps/hamiltonian.py CHANGED
@@ -360,7 +360,6 @@ def make_H(
360
360
 
361
361
  nqubits = interaction_matrix.size(dim=1)
362
362
  middle = nqubits // 2
363
-
364
363
  interactions_to_keep = _get_interactions_to_keep(interaction_matrix)
365
364
 
366
365
  cores = [_first_factor(interactions_to_keep[0].item() != 0.0)]
@@ -13,6 +13,7 @@ from pulser import Sequence
13
13
 
14
14
  from emu_base import Results, State, PulserData
15
15
  from emu_base.math.brents_root_finding import BrentsRootFinder
16
+ from emu_mps.constants import DEVICE_COUNT
16
17
  from emu_mps.hamiltonian import make_H, update_H
17
18
  from emu_mps.mpo import MPO
18
19
  from emu_mps.mps import MPS
@@ -83,6 +84,12 @@ class MPSBackendImpl:
83
84
  )
84
85
  self.last_save_time = time.time()
85
86
 
87
+ if self.config.num_gpus_to_use > DEVICE_COUNT:
88
+ self.config.logger.warning(
89
+ f"Requested to use {self.config.num_gpus_to_use} GPU(s) "
90
+ f"but only {DEVICE_COUNT if DEVICE_COUNT > 0 else 'cpu'} available"
91
+ )
92
+
86
93
  @staticmethod
87
94
  def _get_autosave_filepath(autosave_prefix: str) -> pathlib.Path:
88
95
  return pathlib.Path(os.getcwd()) / (autosave_prefix + str(uuid.uuid1()) + ".dat")
@@ -0,0 +1,9 @@
1
+ from .optimiser import minimize_bandwidth
2
+ from .permutations import permute_list, permute_matrix, invert_permutation
3
+
4
+ __all__ = [
5
+ "minimize_bandwidth",
6
+ "permute_list",
7
+ "permute_matrix",
8
+ "invert_permutation",
9
+ ]
@@ -0,0 +1,234 @@
1
+ from scipy.sparse import csr_matrix
2
+ from scipy.sparse.csgraph import reverse_cuthill_mckee
3
+ import numpy as np
4
+ from emu_mps.optimatrix.permutations import permute_matrix, permute_list
5
+ import itertools
6
+
7
+
8
+ def is_symmetric(mat: np.ndarray) -> bool:
9
+ if mat.shape[0] != mat.shape[1]:
10
+ return False
11
+ if not np.allclose(mat, mat.T, atol=1e-8):
12
+ return False
13
+
14
+ return True
15
+
16
+
17
+ def matrix_bandwidth(mat: np.ndarray) -> float:
18
+ """matrix_bandwidth(matrix: np.ndarray) -> float
19
+
20
+ Computes bandwidth as max weighted distance between columns of
21
+ a square matrix as `max (abs(matrix[i, j] * (j - i))`.
22
+
23
+ abs(j-i)
24
+ |<--------->|
25
+ (i,i) (i,j)
26
+ | |
27
+ | * . . . . . |
28
+ | . * . . a . |
29
+ | . . * . . . |
30
+ | . . . * . . |
31
+ | . . . . * . |
32
+ | . . . . . * |
33
+
34
+ Distance from the main diagonal `[i,i]` and element `m[i,j]` along row is
35
+ `abs(j-i)` and therefore the weighted distance is `abs(matrix[i, j] * (j - i))`
36
+
37
+ Parameters
38
+ -------
39
+ matrix :
40
+ square matrix nxn
41
+
42
+ Returns
43
+ -------
44
+ bandwidth of the input matrix
45
+
46
+ Example:
47
+ -------
48
+ >>> matrix = np.array([
49
+ ... [ 1, -17, 2.4],
50
+ ... [ 9, 1, -10],
51
+ ... [-15, 20, 1],])
52
+ >>> matrix_bandwidth(matrix) # 30.0 because abs(-15 * (2-0) == 30)
53
+ 30.0
54
+ """
55
+
56
+ bandwidth = max(abs(el * (index[0] - index[1])) for index, el in np.ndenumerate(mat))
57
+ return float(bandwidth)
58
+
59
+
60
+ def minimize_bandwidth_above_threshold(mat: np.ndarray, threshold: float) -> np.ndarray:
61
+ """
62
+ minimize_bandwidth_above_threshold(matrix, trunc) -> permutation_lists
63
+
64
+ Finds a permutation list that minimizes a bandwidth of a symmetric matrix `A = A.T`
65
+ using the reverse Cuthill-Mckee algorithm from `scipy.sparse.csgraph.reverse_cuthill_mckee`.
66
+ Matrix elements below a threshold `m[i,j] < threshold` are considered as 0.
67
+
68
+ Parameters
69
+ -------
70
+ matrix :
71
+ symmetric square matrix
72
+ threshold :
73
+ matrix elements `m[i,j] < threshold` are considered as 0
74
+
75
+ Returns
76
+ -------
77
+ permutation list that minimizes matrix bandwidth for a given threshold
78
+
79
+ Example:
80
+ -------
81
+ >>> matrix = np.array([
82
+ ... [1, 2, 3],
83
+ ... [2, 5, 6],
84
+ ... [3, 6, 9]])
85
+ >>> threshold = 3
86
+ >>> minimize_bandwidth_above_threshold(matrix, threshold)
87
+ array([1, 2, 0], dtype=int32)
88
+ """
89
+
90
+ matrix_truncated = mat.copy()
91
+ matrix_truncated[mat < threshold] = 0
92
+ rcm_permutation = reverse_cuthill_mckee(
93
+ csr_matrix(matrix_truncated), symmetric_mode=True
94
+ )
95
+ return np.array(rcm_permutation)
96
+
97
+
98
+ def minimize_bandwidth_global(mat: np.ndarray) -> list[int]:
99
+ """
100
+ minimize_bandwidth_global(matrix) -> list
101
+
102
+ Does one optimisation step towards finding
103
+ a permutation of a matrix that minimizes matrix bandwidth.
104
+
105
+ Parameters
106
+ -------
107
+ matrix :
108
+ symmetric square matrix
109
+
110
+ Returns
111
+ -------
112
+ permutation order that minimizes matrix bandwidth
113
+
114
+ Example:
115
+ -------
116
+ >>> matrix = np.array([
117
+ ... [1, 2, 3],
118
+ ... [2, 5, 6],
119
+ ... [3, 6, 9]])
120
+ >>> minimize_bandwidth_global(matrix)
121
+ [2, 1, 0]
122
+ """
123
+ mat_amplitude = np.max(np.abs(mat))
124
+ # Search from 1.0 to 0.1 doesn't change result
125
+ permutations = (
126
+ minimize_bandwidth_above_threshold(mat, trunc * mat_amplitude)
127
+ for trunc in np.arange(start=0.1, stop=1.0, step=0.01)
128
+ )
129
+
130
+ opt_permutation = min(
131
+ permutations, key=lambda perm: matrix_bandwidth(permute_matrix(mat, list(perm)))
132
+ )
133
+ return list(opt_permutation) # opt_permutation is np.ndarray
134
+
135
+
136
+ def minimize_bandwidth_impl(
137
+ matrix: np.ndarray, initial_perm: list[int]
138
+ ) -> tuple[list[int], float]:
139
+ """
140
+ minimize_bandwidth_impl(matrix, initial_perm) -> list
141
+
142
+ Applies initial_perm to a matrix and
143
+ finds the permutation list for a symmetric matrix that iteratively minimizes matrix bandwidth.
144
+
145
+ Parameters
146
+ -------
147
+ matrix :
148
+ symmetric square matrix
149
+ initial_perm: list of integers
150
+
151
+
152
+ Returns
153
+ -------
154
+ permutation order that minimizes matrix bandwidth
155
+
156
+ Example:
157
+ -------
158
+ Periodic 1D chain
159
+ >>> matrix = np.array([
160
+ ... [0, 1, 0, 0, 1],
161
+ ... [1, 0, 1, 0, 0],
162
+ ... [0, 1, 0, 1, 0],
163
+ ... [0, 0, 1, 0, 1],
164
+ ... [1, 0, 0, 1, 0]])
165
+ >>> id_perm = list(range(matrix.shape[0]))
166
+ >>> minimize_bandwidth_impl(matrix, id_perm) # [3, 2, 4, 1, 0] does zig-zag
167
+ ([3, 2, 4, 1, 0], 2.0)
168
+
169
+ Simple 1D chain. Cannot be optimised further
170
+ >>> matrix = np.array([
171
+ ... [0, 1, 0, 0, 0],
172
+ ... [1, 0, 1, 0, 0],
173
+ ... [0, 1, 0, 1, 0],
174
+ ... [0, 0, 1, 0, 1],
175
+ ... [0, 0, 0, 1, 0]])
176
+ >>> id_perm = list(range(matrix.shape[0]))
177
+ >>> minimize_bandwidth_impl(matrix, id_perm)
178
+ ([0, 1, 2, 3, 4], 1.0)
179
+ """
180
+ if initial_perm != list(range(matrix.shape[0])):
181
+ matrix = permute_matrix(matrix, initial_perm)
182
+ bandwidth = matrix_bandwidth(matrix)
183
+ acc_permutation = initial_perm
184
+
185
+ for counter in range(101):
186
+ if counter == 100:
187
+ raise (
188
+ NotImplementedError(
189
+ "The algorithm takes too many steps, " "probably not converging."
190
+ )
191
+ )
192
+
193
+ optimal_perm = minimize_bandwidth_global(matrix)
194
+ test_mat = permute_matrix(matrix, optimal_perm)
195
+ new_bandwidth = matrix_bandwidth(test_mat)
196
+
197
+ if bandwidth <= new_bandwidth:
198
+ break
199
+
200
+ matrix = test_mat
201
+ acc_permutation = permute_list(acc_permutation, optimal_perm)
202
+ bandwidth = new_bandwidth
203
+
204
+ return acc_permutation, bandwidth
205
+
206
+
207
+ def minimize_bandwidth(input_matrix: np.ndarray, samples: int = 100) -> list[int]:
208
+ assert is_symmetric(input_matrix), "Input matrix is not symmetric"
209
+ input_mat = abs(input_matrix)
210
+ # We are interested in strength of the interaction, not sign
211
+
212
+ L = input_mat.shape[0]
213
+ rnd_permutations = itertools.chain(
214
+ [list(range(L))], # First element is always the identity list
215
+ (np.random.permutation(L).tolist() for _ in range(samples)),
216
+ )
217
+
218
+ opt_permutations_and_opt_bandwidth = (
219
+ minimize_bandwidth_impl(input_mat, rnd_perm) for rnd_perm in rnd_permutations
220
+ )
221
+
222
+ best_perm, best_bandwidth = min(
223
+ opt_permutations_and_opt_bandwidth,
224
+ key=lambda perm_and_bandwidth: perm_and_bandwidth[1],
225
+ )
226
+
227
+ assert best_bandwidth <= matrix_bandwidth(input_matrix), "Matrix is not optimised"
228
+ return best_perm
229
+
230
+
231
+ if __name__ == "__main__":
232
+ import doctest
233
+
234
+ doctest.testmod()
@@ -0,0 +1,94 @@
1
+ import numpy as np
2
+
3
+
4
+ def permute_list(input_list: list, permutation: list[int]) -> list:
5
+ """
6
+ Permutes the input list according to the given permutation.
7
+
8
+ Parameters
9
+ -------
10
+ input_list :
11
+ A list to permute.
12
+ permutation :
13
+ A list of indices representing the new order.
14
+
15
+ Returns
16
+ -------
17
+ The permuted list.
18
+
19
+ Example
20
+ -------
21
+ >>> permute_list(['a', 'b', 'c'], [2, 0, 1])
22
+ ['c', 'a', 'b']
23
+ """
24
+
25
+ permuted_list = [None] * len(input_list)
26
+ for i, p in enumerate(permutation):
27
+ permuted_list[i] = input_list[p]
28
+ return permuted_list
29
+
30
+
31
+ def invert_permutation(permutation: list[int]) -> list[int]:
32
+ """
33
+ invert_permutation(permutation) -> inv_permutation
34
+
35
+ Inverts the input permutation list.
36
+
37
+ Parameters
38
+ -------
39
+ permutation :
40
+ A list of indices representing the order
41
+
42
+ Returns
43
+ -------
44
+ permutation list inverse to the input list
45
+
46
+ Example:
47
+ -------
48
+ >>> invert_permutation([2, 0, 1])
49
+ [1, 2, 0]
50
+ """
51
+
52
+ inv_perm = np.empty_like(permutation)
53
+ inv_perm[permutation] = np.arange(len(permutation))
54
+ return list(inv_perm)
55
+
56
+
57
+ def permute_matrix(mat: np.ndarray, permutation: list[int]) -> np.ndarray:
58
+ """
59
+ permute_matrix(matrix, permutation_list) -> permuted_matrix
60
+
61
+ Simultaneously permutes columns and rows according to a permutation list.
62
+
63
+ Parameters
64
+ -------
65
+ matrix :
66
+ square matrix nxn
67
+ permutation :
68
+ permutation list
69
+
70
+ Returns
71
+ -------
72
+ matrix with permuted columns and rows
73
+
74
+ Example:
75
+ -------
76
+ >>> matrix = np.array([
77
+ ... [1, 2, 3],
78
+ ... [4, 5, 6],
79
+ ... [7, 8, 9]])
80
+ >>> permutation = [1, 0, 2]
81
+ >>> permute_matrix(matrix, permutation)
82
+ array([[5, 4, 6],
83
+ [2, 1, 3],
84
+ [8, 7, 9]])
85
+ """
86
+
87
+ perm = np.array(permutation)
88
+ return mat[perm, :][:, perm]
89
+
90
+
91
+ if __name__ == "__main__":
92
+ import doctest
93
+
94
+ doctest.testmod()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: emu-mps
3
- Version: 1.2.4
3
+ Version: 1.2.5
4
4
  Summary: Pasqal MPS based pulse emulator built on PyTorch
5
5
  Author-email: Anton Quelle <anton.quelle@pasqal.com>, Mauro Mendizabal <mauro.mendizabal-pico@pasqal.com>, Stefano Grava <stefano.grava@pasqal.com>, Pablo Le Henaff <pablo.le-henaff@pasqal.com>
6
6
  License: PASQAL OPEN-SOURCE SOFTWARE LICENSE AGREEMENT (MIT-derived)
@@ -22,7 +22,7 @@ Classifier: Programming Language :: Python :: 3.10
22
22
  Classifier: Programming Language :: Python :: Implementation :: CPython
23
23
  Classifier: Programming Language :: Python :: Implementation :: PyPy
24
24
  Requires-Python: >=3.10
25
- Requires-Dist: emu-base==1.2.4
25
+ Requires-Dist: emu-base==1.2.5
26
26
  Description-Content-Type: text/markdown
27
27
 
28
28
  <div align="center">
@@ -33,6 +33,8 @@ Description-Content-Type: text/markdown
33
33
 
34
34
  **Emu-mps** is a backend for the [Pulser low-level Quantum Programming toolkit](https://pulser.readthedocs.io) that lets you run Quantum Algorithms on a simulated device, using GPU acceleration if available. More in depth, emu-mps is designed to **emu**late the dynamics of programmable arrays of neutral atoms, with matrix product states (**mps**). While benchmarking is incomplete as of this writing, early results suggest that this design makes emu-mps faster and more memory-efficient than previous generations of quantum emulators at running simulations with large numbers of qubits.
35
35
 
36
+ As of this writing, Emu-MPS is provided for Linux and macOS but will not work under Windows.
37
+
36
38
  ## Installation
37
39
 
38
40
  **Warning:** installing emu-mps will update pulser-core
@@ -0,0 +1,18 @@
1
+ emu_mps/__init__.py,sha256=EdVngqaa6GtyXmEP2aK3BfTHjYLBJSN3wR0CvltS9NQ,646
2
+ emu_mps/algebra.py,sha256=ngPtTH-j2ZCBWoaJZXlkUyIlug7dY7Q92gzfnRlpPMA,5485
3
+ emu_mps/constants.py,sha256=41LYkKLUCz-oxPbd-j7nUDZuhIbUrnez6prT0uR0jcE,56
4
+ emu_mps/hamiltonian.py,sha256=LcBs6CKBb643a1e9AAVtQoUfa4L_0dIhLOKecx5OOWs,15864
5
+ emu_mps/mpo.py,sha256=7y6q0UIfyX9igQknqtgt6nymuVcgjHlH3-Qv7N7uOZE,8769
6
+ emu_mps/mps.py,sha256=OjG_caqPOioCdOt-bFUkOf2xuNGnKzj0LaMc3EJCHi4,17855
7
+ emu_mps/mps_backend.py,sha256=6fVaq-D4xyicYRjGjhqMEieC7---90LpfpbV7ZD7zkQ,2192
8
+ emu_mps/mps_backend_impl.py,sha256=HKDqUakqSs1THeeEZP5MFZaUyALnaIhrlsZTQ0Qp4qU,20867
9
+ emu_mps/mps_config.py,sha256=MxahrPDaOpfdB6SLG1610iDUOuLR04IaCjKQRk99ICY,3346
10
+ emu_mps/noise.py,sha256=h4X2EFjoC_Ok0gZ8I9wN77RANXaVehTBbjkcbY_GAmY,784
11
+ emu_mps/tdvp.py,sha256=TH4CcBNczRURXYGPXndWKDs0jWXz_x9ozM961uGiSOw,5711
12
+ emu_mps/utils.py,sha256=n9BcpuIz4Kl6EYlATaK8TKsyF-T7FTwbBo6KSAQYzl8,8066
13
+ emu_mps/optimatrix/__init__.py,sha256=lHWYNeThHp57ZrwTwXd0p8bNvcCv0w_AZ31iCWflBUo,226
14
+ emu_mps/optimatrix/optimiser.py,sha256=cVMdm2r_4OpbthcQuFMrJ9rNR9WEJRga9c_lHrJFkhw,6687
15
+ emu_mps/optimatrix/permutations.py,sha256=JRXGont8B4QgbkV9CzrA0w7uzLgBrmZ1J9aa0G52hPo,1979
16
+ emu_mps-1.2.5.dist-info/METADATA,sha256=bOtqQKZZ6ZkK9C6aV4puIipHUm5v1G9-tQrUftkIw0M,5559
17
+ emu_mps-1.2.5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
18
+ emu_mps-1.2.5.dist-info/RECORD,,
@@ -1,15 +0,0 @@
1
- emu_mps/__init__.py,sha256=ou1n7ieIHlszqg9hBVYhvA8IBOpeduAWno3GC7X2dWE,646
2
- emu_mps/algebra.py,sha256=ngPtTH-j2ZCBWoaJZXlkUyIlug7dY7Q92gzfnRlpPMA,5485
3
- emu_mps/constants.py,sha256=41LYkKLUCz-oxPbd-j7nUDZuhIbUrnez6prT0uR0jcE,56
4
- emu_mps/hamiltonian.py,sha256=LALCdAQyYQgqFLVvtB9cHoUilT_7eU-6bAHRhu39lqM,15865
5
- emu_mps/mpo.py,sha256=7y6q0UIfyX9igQknqtgt6nymuVcgjHlH3-Qv7N7uOZE,8769
6
- emu_mps/mps.py,sha256=OjG_caqPOioCdOt-bFUkOf2xuNGnKzj0LaMc3EJCHi4,17855
7
- emu_mps/mps_backend.py,sha256=6fVaq-D4xyicYRjGjhqMEieC7---90LpfpbV7ZD7zkQ,2192
8
- emu_mps/mps_backend_impl.py,sha256=XCrmIjV-ZzI0nTOoaIaM6RGtfo4WHoJakVtoqoTZ7zI,20556
9
- emu_mps/mps_config.py,sha256=MxahrPDaOpfdB6SLG1610iDUOuLR04IaCjKQRk99ICY,3346
10
- emu_mps/noise.py,sha256=h4X2EFjoC_Ok0gZ8I9wN77RANXaVehTBbjkcbY_GAmY,784
11
- emu_mps/tdvp.py,sha256=TH4CcBNczRURXYGPXndWKDs0jWXz_x9ozM961uGiSOw,5711
12
- emu_mps/utils.py,sha256=n9BcpuIz4Kl6EYlATaK8TKsyF-T7FTwbBo6KSAQYzl8,8066
13
- emu_mps-1.2.4.dist-info/METADATA,sha256=nAxhFZeRlvVOqTdfJ-szluFRpa0q8EwbmogczPni3kM,5465
14
- emu_mps-1.2.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
15
- emu_mps-1.2.4.dist-info/RECORD,,