inechita commited on
Commit
dd1edb0
·
verified ·
1 Parent(s): f6d2706

Upload 2 files

Browse files
Files changed (2) hide show
  1. generate-qpm.py +85 -0
  2. qpm-marimo.py +303 -0
generate-qpm.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import matplotlib.pyplot as plt
3
+
4
+ def generate_random_complex_gaussian_matrix(n):
5
+ matrix = np.empty((n, n, n), dtype=np.complex128)
6
+ for i in range(n):
7
+ for j in range(n):
8
+ real_part = np.random.normal(size=n)
9
+ imag_part = np.random.normal(size=n)
10
+ matrix[i, j] = (real_part + 1j * imag_part) / np.sqrt(2)
11
+ return matrix
12
+
13
+ def orthonormalize_vectors_svd(vectors):
14
+ u, _, v = np.linalg.svd(vectors)
15
+ return u @ v
16
+
17
+ def error_QPM(u):
18
+ err = -1;
19
+
20
+ for i in range(u.shape[0]):
21
+ slice = u[i, :, :]
22
+ err = max(err, np.linalg.norm(slice @ slice.conj().T - np.eye(slice.shape[0])))
23
+ slice = u[:, i, :]
24
+ err = max(err, np.linalg.norm(slice @ slice.conj().T - np.eye(slice.shape[0])))
25
+
26
+ return err
27
+
28
+ def random_quantum_permutation_matrix(n, error_tolerance=1e-6, max_iter=1000):
29
+ u = generate_random_complex_gaussian_matrix(n)
30
+
31
+ iter = 0
32
+ error = error_QPM(u)
33
+ errors = [error] # Initialize a list to store errors
34
+
35
+ while error > error_tolerance and iter < max_iter:
36
+
37
+ # orthonormalize rows
38
+ for i in range(n):
39
+ u[i, :, :] = orthonormalize_vectors_svd(u[i, :, :])
40
+
41
+ # orthonormalize columns
42
+ for j in range(n):
43
+ u[:, j, :] = orthonormalize_vectors_svd(u[:, j, :])
44
+
45
+ error = error_QPM(u)
46
+ errors.append(error) # Append the current error to the list
47
+ iter += 1
48
+
49
+ return u, errors # Return both the matrix and the list of errors
50
+
51
+
52
+ # Example usage
53
+ u, errors = random_quantum_permutation_matrix(6)
54
+
55
+ # Create a figure with two subplots side by side
56
+ fig, axs = plt.subplots(1, 2, figsize=(12, 5))
57
+
58
+ # Plot the errors on a log scale in the first subplot
59
+ axs[0].semilogy(errors)
60
+ axs[0].set_xlabel('Iteration')
61
+ axs[0].set_ylabel('Error (log scale)')
62
+ axs[0].set_title('Error Convergence')
63
+ axs[0].grid(True)
64
+
65
+ # Calculate the absolute values squared of the scalar products of the vectors in the matrix u
66
+ scalar_products = []
67
+ for i in range(u.shape[0]):
68
+ for j in range(1, u.shape[0]):
69
+ for k in range(1, u.shape[0]):
70
+ for l in range(1, u.shape[0]):
71
+ scalar_product = np.abs(np.dot(u[i,j,:], u[k,l,:].conj()))**2
72
+ scalar_products.append(scalar_product)
73
+
74
+ # Plot the histogram in the second subplot
75
+ axs[1].hist(scalar_products, bins=30, edgecolor='black')
76
+ axs[1].set_xlabel('Absolute Values Squared of Scalar Products')
77
+ axs[1].set_ylabel('Frequency')
78
+ axs[1].set_title('Histogram of Absolute Values Squared of Scalar Products')
79
+ axs[1].grid(True)
80
+
81
+ # Adjust layout to prevent overlap
82
+ plt.tight_layout()
83
+ plt.show()
84
+
85
+
qpm-marimo.py ADDED
@@ -0,0 +1,303 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import marimo
2
+
3
+ __generated_with = "0.11.5"
4
+ app = marimo.App(width="medium")
5
+
6
+ @app.cell(hide_code=True)
7
+ def _(mo):
8
+ mo.md(
9
+ r"""
10
+ # Generate random flat quantum permutation matrices
11
+ #
12
+ """
13
+ )
14
+ return
15
+
16
+
17
+ @app.cell(hide_code=True)
18
+ def _(mo):
19
+ mo.md(
20
+ r"""
21
+ Let $A$ be a unital $C^*$-algebra. An $n \times n$ matrix $u = (u_{ij})_{1\le i,j\le n} \in \mathcal M_n(A)$
22
+ is called a **quantum permtuation matrix** if it satisfies the conditions below:
23
+
24
+ 1. *Projection Entries:* Each entry is a projection:
25
+ $$u_{ij}^2 = u_{ij} \quad \text{and} \quad u_{ij}^* = u_{ij} \quad \text{for all } i,j.$$
26
+
27
+ 2. *Row and Column Sums:* The entries in each row and each column sum to the unit of $A$:
28
+ $$\sum_{j=1}^n u_{ij} = 1_A \quad \text{for all } i=1,\dots,n,$$
29
+ $$\sum_{i=1}^n u_{ij} = 1_A \quad \text{for all } j=1,\dots,n.$$
30
+ """
31
+ )
32
+ return
33
+
34
+
35
+ @app.cell(hide_code=True)
36
+ def _(mo):
37
+ mo.md(
38
+ """
39
+ We are interested here in the special case where:
40
+
41
+ 1. the algebra $A$ is finite dimensional: $A = \mathcal M_d(\mathbb C)$
42
+ 2. the projections $u_{ij}$ have *unit rank* (we call $u$ *flat*); in particular $d=n$.
43
+ """
44
+ )
45
+ return
46
+
47
+
48
+ @app.cell
49
+ def _(mo):
50
+ mo.md(
51
+ """
52
+ ## An alogrithm for generating random flat quantum permutation matrices
53
+ ##
54
+ """
55
+ )
56
+ return
57
+
58
+
59
+ @app.cell
60
+ def _(mo):
61
+ mo.md(
62
+ r"""
63
+ We propose the following very simple algorithm for generating quantum permutation matrices, based on normalizing the rows and the columns of a matrix of unit rank projections. The alternating normalization of rows and columns is inspired by the **Sinkhorn algorithm** for producing random bistochastic matrices.
64
+
65
+ 1. Start from a random matrix: $u_{ij}$ is the orthogonal projection on a random gaussian vector
66
+ 2. While the error is larger than some predefined constant and the maximal number of steps has not been attained:
67
+ 3. Normalize each row of $u$
68
+ 4. Normalize each column of $u$
69
+ 5. End-while
70
+ 6. Output the matrix $u$
71
+ """
72
+ )
73
+ return
74
+
75
+
76
+ @app.cell
77
+ def _(mo):
78
+ mo.md(
79
+ r"""
80
+ The row (respectively the column) normalization procedures are performed as follows. Collect all the vectors appearing on the $i$-th row of $u$ in a matrix $R_i$. Replace $R_i$ by $\tilde R_i$, the *closest unitary matrix* to $R_i$. This matrix can be obtained from the *singular value decomposition* of $R_i$:
81
+ $$\text{ if } R_i = V_i \Delta_i W_i^*, \, \text{ then }\, \tilde R_i = V_i W_i^*.$$
82
+ """
83
+ )
84
+ return
85
+
86
+
87
+ @app.cell
88
+ def _(mo):
89
+ mo.md(
90
+ """
91
+ ## Implementation
92
+ ##
93
+ """
94
+ )
95
+ return
96
+
97
+
98
+ @app.cell
99
+ def _(mo):
100
+ eps_slider = mo.ui.slider(0, 10, value=6)
101
+ max_iter_slider = mo.ui.slider(1, 10000, value=2000)
102
+ n_slider = mo.ui.slider(2, 20, step=1, value=4)
103
+ return eps_slider, max_iter_slider, n_slider
104
+
105
+
106
+ @app.cell
107
+ def _(eps_slider, mo):
108
+ mo.md(f"-lg(Error tolerance): {eps_slider} \t error_tolerance = 1e-{eps_slider.value}")
109
+ return
110
+
111
+
112
+ @app.cell
113
+ def _(max_iter_slider, mo):
114
+ mo.md(f"Max iterations: {max_iter_slider} \t max_iter = {max_iter_slider.value}")
115
+ return
116
+
117
+
118
+ @app.cell
119
+ def _(mo, n_slider):
120
+ mo.md(f"Matrix dimension: {n_slider} \t n = {n_slider.value}")
121
+ return
122
+
123
+
124
+ @app.cell
125
+ def _(mo):
126
+ button = mo.ui.run_button(label="Randomize!")
127
+ button
128
+ return (button,)
129
+
130
+
131
+ @app.cell
132
+ def _(errors, plt, scalar_products):
133
+ # Create a figure with two subplots side by side
134
+ fig, axs = plt.subplots(1, 2, figsize=(12, 5))
135
+
136
+ # Plot the errors on a log scale in the first subplot
137
+ axs[0].semilogy(errors)
138
+ axs[0].set_xlabel('Iteration')
139
+ axs[0].set_ylabel('Error (log scale)')
140
+ axs[0].set_title('Error Convergence')
141
+ axs[0].grid(True)
142
+
143
+ # Plot the histogram in the second subplot
144
+ axs[1].hist(scalar_products, bins=30, edgecolor='black')
145
+ axs[1].set_xlabel('Absolute Values Squared of Scalar Products')
146
+ axs[1].set_ylabel('Frequency')
147
+ axs[1].set_title('Histogram of Absolute Values Squared of Scalar Products')
148
+ axs[1].grid(True)
149
+
150
+ # Adjust layout to prevent overlap
151
+ plt.tight_layout()
152
+ plt.gca()
153
+ return axs, fig
154
+
155
+
156
+ @app.cell
157
+ def _(mo):
158
+ mo.md(r"""Note that for $n=2,3$ the histogram of scalar product shows only 0's and 1's: the vectors are either colinear or orthogonal. In other words, the elements $u_{ij}$ **commute**. For $n \geq 4$ this is no longer the case: the scalar product between vectors can take arbitrary values.""")
159
+ return
160
+
161
+
162
+ @app.cell
163
+ def _(mo):
164
+ mo.md(
165
+ r"""
166
+ ## Open questions
167
+ ##
168
+
169
+ 1. Prove the convergence of the algorithm for generic initializations.
170
+ 2. Find the speed of convergence for arbitrary $n$
171
+ 3. What is the distribution of the scalar products for (large / given) $n$? How about the maximal norm of a commutator $[u_{ij}, u_{kl}]$?
172
+ """
173
+ )
174
+ return
175
+
176
+
177
+ @app.cell
178
+ def _(mo):
179
+ mo.md(
180
+ r"""
181
+ ## References
182
+ ##
183
+
184
+ 1. S. Wang, “Quantum symmetry groups of finite spaces,” _Communications in mathematical physics_, vol. 195, no. 1, pp. 195–211, 1998.
185
+ 2. T. Banica, J. Bichon, and B. Collins, “Quantum permutation groups: a survey,” _Banach Center Publications_, vol. 78, no. 1, pp. 13–34, 2007.
186
+ 3. T. Banica, I. Nechita, "Flat matrix models for quantum permutation groups," _Adv. Appl. Math._ 83, 24-46 (2017).
187
+ """
188
+ )
189
+ return
190
+
191
+
192
+ @app.cell
193
+ def _(np):
194
+ def generate_random_complex_gaussian_matrix(n):
195
+ matrix = np.empty((n, n, n), dtype=np.complex128)
196
+ for i in range(n):
197
+ for j in range(n):
198
+ real_part = np.random.normal(size=n)
199
+ imag_part = np.random.normal(size=n)
200
+ matrix[i, j] = (real_part + 1j * imag_part) / np.sqrt(2)
201
+ return matrix
202
+
203
+ def orthonormalize_vectors_svd(vectors):
204
+ u, _, v = np.linalg.svd(vectors)
205
+ return u @ v
206
+
207
+ def error_QPM(u):
208
+ err = -1;
209
+
210
+ for i in range(u.shape[0]):
211
+ slice = u[i, :, :]
212
+ err = max(err, np.linalg.norm(slice @ slice.conj().T - np.eye(slice.shape[0])))
213
+ slice = u[:, i, :]
214
+ err = max(err, np.linalg.norm(slice @ slice.conj().T - np.eye(slice.shape[0])))
215
+
216
+ return err
217
+
218
+ def scalar_products_QPM(u):
219
+ # Calculate the absolute values squared of the scalar products of the vectors in the matrix u
220
+ scalar_products = []
221
+ for i in range(u.shape[0]):
222
+ for j in range(1, u.shape[0]):
223
+ for k in range(1, u.shape[0]):
224
+ for l in range(1, u.shape[0]):
225
+ scalar_product = np.abs(np.dot(u[i,j,:], u[k,l,:].conj()))**2
226
+ scalar_products.append(scalar_product)
227
+
228
+ return scalar_products
229
+ return (
230
+ error_QPM,
231
+ generate_random_complex_gaussian_matrix,
232
+ orthonormalize_vectors_svd,
233
+ scalar_products_QPM,
234
+ )
235
+
236
+
237
+ @app.cell
238
+ def _():
239
+ import marimo as mo
240
+ import numpy as np
241
+ import matplotlib.pyplot as plt
242
+ return mo, np, plt
243
+
244
+
245
+ @app.cell
246
+ def _(
247
+ button,
248
+ eps_slider,
249
+ error_QPM,
250
+ generate_random_complex_gaussian_matrix,
251
+ max_iter_slider,
252
+ n_slider,
253
+ orthonormalize_vectors_svd,
254
+ scalar_products_QPM,
255
+ ):
256
+ button
257
+
258
+ u = generate_random_complex_gaussian_matrix(n_slider.value)
259
+
260
+ error_tolerance = 10**(-eps_slider.value)
261
+ max_iter = max_iter_slider.value
262
+
263
+ iter = 0
264
+ error = error_QPM(u)
265
+ errors = [error] # Initialize a list to store errors
266
+ scalar_products = scalar_products_QPM(u) # Initialize the scalar product list
267
+
268
+ while error > error_tolerance and iter < max_iter:
269
+
270
+ # orthonormalize rows
271
+ for i in range(n_slider.value):
272
+ u[i, :, :] = orthonormalize_vectors_svd(u[i, :, :])
273
+
274
+ # orthonormalize columns
275
+ for j in range(n_slider.value):
276
+ u[:, j, :] = orthonormalize_vectors_svd(u[:, j, :])
277
+
278
+ error = error_QPM(u)
279
+ errors.append(error) # Append the current error to the list
280
+
281
+ if iter % 10 == 0:
282
+ scalar_products = scalar_products_QPM(u) # Update scalar prodcuts
283
+ iter += 1
284
+ return (
285
+ error,
286
+ error_tolerance,
287
+ errors,
288
+ i,
289
+ iter,
290
+ j,
291
+ max_iter,
292
+ scalar_products,
293
+ u,
294
+ )
295
+
296
+
297
+ @app.cell
298
+ def _():
299
+ return
300
+
301
+
302
+ if __name__ == "__main__":
303
+ app.run()