Update app.py
Browse files
app.py
CHANGED
@@ -1,470 +1,309 @@
|
|
1 |
import marimo
|
2 |
|
3 |
-
__generated_with = "0.
|
4 |
-
app = marimo.App()
|
5 |
|
6 |
|
7 |
-
@app.cell
|
8 |
-
def
|
9 |
-
import marimo as mo
|
10 |
-
|
11 |
-
mo.md("# Welcome to marimo! 🌊🍃")
|
12 |
-
return (mo,)
|
13 |
-
|
14 |
-
|
15 |
-
@app.cell
|
16 |
-
def __(mo):
|
17 |
-
slider = mo.ui.slider(1, 22)
|
18 |
-
return (slider,)
|
19 |
-
|
20 |
-
|
21 |
-
@app.cell
|
22 |
-
def __(mo, slider):
|
23 |
mo.md(
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
This means that unlike traditional notebooks, marimo notebooks **run
|
28 |
-
automatically** when you modify them or
|
29 |
-
interact with UI elements, like this slider: {slider}.
|
30 |
-
|
31 |
-
{"##" + "🍃" * slider.value}
|
32 |
"""
|
33 |
)
|
34 |
return
|
35 |
|
36 |
|
37 |
@app.cell(hide_code=True)
|
38 |
-
def
|
39 |
-
mo.
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
marimo lets you disable automatic execution: just go into the
|
44 |
-
notebook settings and set
|
45 |
-
|
46 |
-
"Runtime > On Cell Change" to "lazy".
|
47 |
-
|
48 |
-
When the runtime is lazy, after running a cell, marimo marks its
|
49 |
-
descendants as stale instead of automatically running them. The
|
50 |
-
lazy runtime puts you in control over when cells are run, while
|
51 |
-
still giving guarantees about the notebook state.
|
52 |
-
"""
|
53 |
-
)
|
54 |
-
}
|
55 |
-
)
|
56 |
-
return
|
57 |
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
"""
|
63 |
-
Tip: This is a tutorial notebook. You can create your own notebooks
|
64 |
-
by entering `marimo edit` at the command line.
|
65 |
"""
|
66 |
-
)
|
67 |
return
|
68 |
|
69 |
|
70 |
@app.cell(hide_code=True)
|
71 |
-
def
|
72 |
mo.md(
|
73 |
"""
|
74 |
-
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
marimo reads your cells and models the dependencies among them: whenever
|
80 |
-
a cell that defines a global variable is run, marimo
|
81 |
-
**automatically runs** all cells that reference that variable.
|
82 |
-
|
83 |
-
Reactivity keeps your program state and outputs in sync with your code,
|
84 |
-
making for a dynamic programming environment that prevents bugs before they
|
85 |
-
happen.
|
86 |
"""
|
87 |
)
|
88 |
return
|
89 |
|
90 |
|
91 |
-
@app.cell(hide_code=True)
|
92 |
-
def __(changed, mo):
|
93 |
-
(
|
94 |
-
mo.md(
|
95 |
-
f"""
|
96 |
-
**✨ Nice!** The value of `changed` is now {changed}.
|
97 |
-
|
98 |
-
When you updated the value of the variable `changed`, marimo
|
99 |
-
**reacted** by running this cell automatically, because this cell
|
100 |
-
references the global variable `changed`.
|
101 |
-
|
102 |
-
Reactivity ensures that your notebook state is always
|
103 |
-
consistent, which is crucial for doing good science; it's also what
|
104 |
-
enables marimo notebooks to double as tools and apps.
|
105 |
-
"""
|
106 |
-
)
|
107 |
-
if changed
|
108 |
-
else mo.md(
|
109 |
-
"""
|
110 |
-
**🌊 See it in action.** In the next cell, change the value of the
|
111 |
-
variable `changed` to `True`, then click the run button.
|
112 |
-
"""
|
113 |
-
)
|
114 |
-
)
|
115 |
-
return
|
116 |
-
|
117 |
-
|
118 |
@app.cell
|
119 |
-
def
|
120 |
-
changed = False
|
121 |
-
return (changed,)
|
122 |
-
|
123 |
-
|
124 |
-
@app.cell(hide_code=True)
|
125 |
-
def __(mo):
|
126 |
-
mo.accordion(
|
127 |
-
{
|
128 |
-
"Tip: execution order": (
|
129 |
-
"""
|
130 |
-
The order of cells on the page has no bearing on
|
131 |
-
the order in which cells are executed: marimo knows that a cell
|
132 |
-
reading a variable must run after the cell that defines it. This
|
133 |
-
frees you to organize your code in the way that makes the most
|
134 |
-
sense for you.
|
135 |
-
"""
|
136 |
-
)
|
137 |
-
}
|
138 |
-
)
|
139 |
-
return
|
140 |
-
|
141 |
-
|
142 |
-
@app.cell(hide_code=True)
|
143 |
-
def __(mo):
|
144 |
mo.md(
|
145 |
"""
|
146 |
-
|
147 |
-
|
148 |
-
variable.
|
149 |
"""
|
150 |
)
|
151 |
return
|
152 |
|
153 |
|
154 |
-
@app.cell
|
155 |
-
def
|
156 |
-
mo.
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
|
|
|
|
165 |
)
|
166 |
return
|
167 |
|
168 |
|
169 |
-
@app.cell
|
170 |
-
def
|
171 |
-
mo.
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
they can be defined by multiple cells.
|
177 |
-
"""
|
178 |
-
)
|
179 |
-
}
|
180 |
)
|
181 |
return
|
182 |
|
183 |
|
184 |
-
@app.cell
|
185 |
-
def
|
186 |
mo.md(
|
187 |
"""
|
188 |
-
##
|
189 |
-
|
190 |
-
Cells can output interactive UI elements. Interacting with a UI
|
191 |
-
element **automatically triggers notebook execution**: when
|
192 |
-
you interact with a UI element, its value is sent back to Python, and
|
193 |
-
every cell that references that element is re-run.
|
194 |
-
|
195 |
-
marimo provides a library of UI elements to choose from under
|
196 |
-
`marimo.ui`.
|
197 |
"""
|
198 |
)
|
199 |
return
|
200 |
|
201 |
|
202 |
@app.cell
|
203 |
-
def
|
204 |
-
mo.
|
205 |
-
|
|
|
|
|
206 |
|
207 |
|
208 |
@app.cell
|
209 |
-
def
|
210 |
-
|
211 |
-
return
|
212 |
|
213 |
|
214 |
@app.cell
|
215 |
-
def
|
216 |
-
|
217 |
-
return
|
218 |
|
219 |
|
220 |
@app.cell
|
221 |
-
def
|
222 |
-
|
223 |
return
|
224 |
|
225 |
|
226 |
@app.cell
|
227 |
-
def
|
228 |
-
mo.
|
229 |
-
|
|
|
230 |
|
231 |
|
232 |
-
@app.cell
|
233 |
-
def
|
234 |
-
mo.md(
|
235 |
-
"""
|
236 |
-
## 3. marimo is just Python
|
237 |
|
238 |
-
marimo cells parse Python (and only Python), and marimo notebooks are
|
239 |
-
stored as pure Python files — outputs are _not_ included. There's no
|
240 |
-
magical syntax.
|
241 |
|
242 |
-
|
|
|
243 |
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
"""
|
251 |
-
)
|
252 |
-
return
|
253 |
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
## 4. Running notebooks as apps
|
260 |
|
261 |
-
marimo notebooks can double as apps. Click the app window icon in the
|
262 |
-
bottom-right to see this notebook in "app view."
|
263 |
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
"""
|
268 |
-
)
|
269 |
return
|
270 |
|
271 |
|
272 |
-
@app.cell
|
273 |
-
def
|
274 |
mo.md(
|
275 |
-
"""
|
276 |
-
##
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
in a terminal to start the marimo notebook server. From here
|
285 |
-
you can create a new notebook or edit existing ones.
|
286 |
-
|
287 |
-
|
288 |
-
**Running as apps.** Use
|
289 |
-
|
290 |
-
```
|
291 |
-
marimo run notebook.py
|
292 |
-
```
|
293 |
-
|
294 |
-
to start a webserver that serves your notebook as an app in read-only mode,
|
295 |
-
with code cells hidden.
|
296 |
-
|
297 |
-
**Convert a Jupyter notebook.** Convert a Jupyter notebook to a marimo
|
298 |
-
notebook using `marimo convert`:
|
299 |
-
|
300 |
-
```
|
301 |
-
marimo convert your_notebook.ipynb > your_app.py
|
302 |
-
```
|
303 |
-
|
304 |
-
**Tutorials.** marimo comes packaged with tutorials:
|
305 |
-
|
306 |
-
- `dataflow`: more on marimo's automatic execution
|
307 |
-
- `ui`: how to use UI elements
|
308 |
-
- `markdown`: how to write markdown, with interpolated values and
|
309 |
-
LaTeX
|
310 |
-
- `plots`: how plotting works in marimo
|
311 |
-
- `sql`: how to use SQL
|
312 |
-
- `layout`: layout elements in marimo
|
313 |
-
- `fileformat`: how marimo's file format works
|
314 |
-
- `markdown-format`: for using `.md` files in marimo
|
315 |
-
- `for-jupyter-users`: if you are coming from Jupyter
|
316 |
-
|
317 |
-
Start a tutorial with `marimo tutorial`; for example,
|
318 |
-
|
319 |
-
```
|
320 |
-
marimo tutorial dataflow
|
321 |
-
```
|
322 |
-
|
323 |
-
In addition to tutorials, we have examples in our
|
324 |
-
[our GitHub repo](https://www.github.com/marimo-team/marimo/tree/main/examples).
|
325 |
"""
|
326 |
)
|
327 |
return
|
328 |
|
329 |
|
330 |
-
@app.cell
|
331 |
-
def
|
332 |
mo.md(
|
333 |
-
"""
|
334 |
-
##
|
|
|
335 |
|
336 |
-
|
|
|
|
|
337 |
"""
|
338 |
)
|
339 |
return
|
340 |
|
341 |
|
342 |
@app.cell
|
343 |
-
def
|
344 |
-
|
345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
346 |
|
347 |
|
348 |
-
@app.cell
|
349 |
-
def
|
350 |
-
|
351 |
-
|
|
|
|
|
352 |
|
353 |
|
354 |
-
@app.cell
|
355 |
-
def
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
363 |
)
|
364 |
-
return
|
365 |
|
366 |
|
367 |
-
@app.cell
|
368 |
-
def
|
369 |
-
|
370 |
-
"Saving": (
|
371 |
-
"""
|
372 |
-
**Saving**
|
373 |
-
|
374 |
-
- _Name_ your app using the box at the top of the screen, or
|
375 |
-
with `Ctrl/Cmd+s`. You can also create a named app at the
|
376 |
-
command line, e.g., `marimo edit app_name.py`.
|
377 |
-
|
378 |
-
- _Save_ by clicking the save icon on the bottom right, or by
|
379 |
-
inputting `Ctrl/Cmd+s`. By default marimo is configured
|
380 |
-
to autosave.
|
381 |
-
"""
|
382 |
-
),
|
383 |
-
"Running": (
|
384 |
-
"""
|
385 |
-
1. _Run a cell_ by clicking the play ( ▷ ) button on the top
|
386 |
-
right of a cell, or by inputting `Ctrl/Cmd+Enter`.
|
387 |
-
|
388 |
-
2. _Run a stale cell_ by clicking the yellow run button on the
|
389 |
-
right of the cell, or by inputting `Ctrl/Cmd+Enter`. A cell is
|
390 |
-
stale when its code has been modified but not run.
|
391 |
-
|
392 |
-
3. _Run all stale cells_ by clicking the play ( ▷ ) button on
|
393 |
-
the bottom right of the screen, or input `Ctrl/Cmd+Shift+r`.
|
394 |
-
"""
|
395 |
-
),
|
396 |
-
"Console Output": (
|
397 |
-
"""
|
398 |
-
Console output (e.g., `print()` statements) is shown below a
|
399 |
-
cell.
|
400 |
-
"""
|
401 |
-
),
|
402 |
-
"Creating, Moving, and Deleting Cells": (
|
403 |
-
"""
|
404 |
-
1. _Create_ a new cell above or below a given one by clicking
|
405 |
-
the plus button to the left of the cell, which appears on
|
406 |
-
mouse hover.
|
407 |
-
|
408 |
-
2. _Move_ a cell up or down by dragging on the handle to the
|
409 |
-
right of the cell, which appears on mouse hover.
|
410 |
-
|
411 |
-
3. _Delete_ a cell by clicking the trash bin icon. Bring it
|
412 |
-
back by clicking the undo button on the bottom right of the
|
413 |
-
screen, or with `Ctrl/Cmd+Shift+z`.
|
414 |
-
"""
|
415 |
-
),
|
416 |
-
"Disabling Automatic Execution": (
|
417 |
-
"""
|
418 |
-
Via the notebook settings (gear icon) or footer panel, you
|
419 |
-
can disable automatic execution. This is helpful when
|
420 |
-
working with expensive notebooks or notebooks that have
|
421 |
-
side-effects like database transactions.
|
422 |
-
"""
|
423 |
-
),
|
424 |
-
"Disabling Cells": (
|
425 |
-
"""
|
426 |
-
You can disable a cell via the cell context menu.
|
427 |
-
marimo will never run a disabled cell or any cells that depend on it.
|
428 |
-
This can help prevent accidental execution of expensive computations
|
429 |
-
when editing a notebook.
|
430 |
-
"""
|
431 |
-
),
|
432 |
-
"Code Folding": (
|
433 |
-
"""
|
434 |
-
You can collapse or fold the code in a cell by clicking the arrow
|
435 |
-
icons in the line number column to the left, or by using keyboard
|
436 |
-
shortcuts.
|
437 |
-
|
438 |
-
Use the command palette (`Ctrl/Cmd+k`) or a keyboard shortcut to
|
439 |
-
quickly fold or unfold all cells.
|
440 |
-
"""
|
441 |
-
),
|
442 |
-
"Code Formatting": (
|
443 |
-
"""
|
444 |
-
If you have [ruff](https://github.com/astral-sh/ruff) installed,
|
445 |
-
you can format a cell with the keyboard shortcut `Ctrl/Cmd+b`.
|
446 |
-
"""
|
447 |
-
),
|
448 |
-
"Command Palette": (
|
449 |
-
"""
|
450 |
-
Use `Ctrl/Cmd+k` to open the command palette.
|
451 |
-
"""
|
452 |
-
),
|
453 |
-
"Keyboard Shortcuts": (
|
454 |
-
"""
|
455 |
-
Open the notebook menu (top-right) or input `Ctrl/Cmd+Shift+h` to
|
456 |
-
view a list of all keyboard shortcuts.
|
457 |
-
"""
|
458 |
-
),
|
459 |
-
"Configuration": (
|
460 |
-
"""
|
461 |
-
Configure the editor by clicking the gears icon near the top-right
|
462 |
-
of the screen.
|
463 |
-
"""
|
464 |
-
),
|
465 |
-
}
|
466 |
-
return (tips,)
|
467 |
|
468 |
|
469 |
if __name__ == "__main__":
|
470 |
-
app.run()
|
|
|
1 |
import marimo
|
2 |
|
3 |
+
__generated_with = "0.11.5"
|
4 |
+
app = marimo.App(width="medium")
|
5 |
|
6 |
|
7 |
+
@app.cell(hide_code=True)
|
8 |
+
def _(mo):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
mo.md(
|
10 |
+
r"""
|
11 |
+
# Generate random flat quantum permutation matrices
|
12 |
+
#
|
|
|
|
|
|
|
|
|
|
|
13 |
"""
|
14 |
)
|
15 |
return
|
16 |
|
17 |
|
18 |
@app.cell(hide_code=True)
|
19 |
+
def _(mo):
|
20 |
+
mo.md(
|
21 |
+
r"""
|
22 |
+
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)$
|
23 |
+
is called a **quantum permtuation matrix** if it satisfies the conditions below:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
1. *Projection Entries:* Each entry is a projection:
|
26 |
+
$$u_{ij}^2 = u_{ij} \quad \text{and} \quad u_{ij}^* = u_{ij} \quad \text{for all } i,j.$$
|
27 |
|
28 |
+
2. *Row and Column Sums:* The entries in each row and each column sum to the unit of $A$:
|
29 |
+
$$\sum_{j=1}^n u_{ij} = 1_A \quad \text{for all } i=1,\dots,n,$$
|
30 |
+
$$\sum_{i=1}^n u_{ij} = 1_A \quad \text{for all } j=1,\dots,n.$$
|
|
|
|
|
|
|
31 |
"""
|
32 |
+
)
|
33 |
return
|
34 |
|
35 |
|
36 |
@app.cell(hide_code=True)
|
37 |
+
def _(mo):
|
38 |
mo.md(
|
39 |
"""
|
40 |
+
We are interested here in the special case where:
|
41 |
|
42 |
+
1. the algebra $A$ is finite dimensional: $A = \mathcal M_d(\mathbb C)$
|
43 |
+
2. the projections $u_{ij}$ have *unit rank* (we call $u$ *flat*); in particular $d=n$.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
"""
|
45 |
)
|
46 |
return
|
47 |
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
@app.cell
|
50 |
+
def _(mo):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
mo.md(
|
52 |
"""
|
53 |
+
## An alogrithm for generating random flat quantum permutation matrices
|
54 |
+
##
|
|
|
55 |
"""
|
56 |
)
|
57 |
return
|
58 |
|
59 |
|
60 |
+
@app.cell
|
61 |
+
def _(mo):
|
62 |
+
mo.md(
|
63 |
+
r"""
|
64 |
+
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.
|
65 |
+
|
66 |
+
1. Start from a random matrix: $u_{ij}$ is the orthogonal projection on a random gaussian vector
|
67 |
+
2. While the error is larger than some predefined constant and the maximal number of steps has not been attained:
|
68 |
+
3. Normalize each row of $u$
|
69 |
+
4. Normalize each column of $u$
|
70 |
+
5. End-while
|
71 |
+
6. Output the matrix $u$
|
72 |
+
"""
|
73 |
)
|
74 |
return
|
75 |
|
76 |
|
77 |
+
@app.cell
|
78 |
+
def _(mo):
|
79 |
+
mo.md(
|
80 |
+
r"""
|
81 |
+
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$:
|
82 |
+
$$\text{ if } R_i = V_i \Delta_i W_i^*, \, \text{ then }\, \tilde R_i = V_i W_i^*.$$
|
83 |
+
"""
|
|
|
|
|
|
|
|
|
84 |
)
|
85 |
return
|
86 |
|
87 |
|
88 |
+
@app.cell
|
89 |
+
def _(mo):
|
90 |
mo.md(
|
91 |
"""
|
92 |
+
## Implementation
|
93 |
+
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
"""
|
95 |
)
|
96 |
return
|
97 |
|
98 |
|
99 |
@app.cell
|
100 |
+
def _(mo):
|
101 |
+
eps_slider = mo.ui.slider(0, 10, value=6)
|
102 |
+
max_iter_slider = mo.ui.slider(1, 10000, value=2000)
|
103 |
+
n_slider = mo.ui.slider(2, 20, step=1, value=4)
|
104 |
+
return eps_slider, max_iter_slider, n_slider
|
105 |
|
106 |
|
107 |
@app.cell
|
108 |
+
def _(eps_slider, mo):
|
109 |
+
mo.md(f"-lg(Error tolerance): {eps_slider} \t error_tolerance = 1e-{eps_slider.value}")
|
110 |
+
return
|
111 |
|
112 |
|
113 |
@app.cell
|
114 |
+
def _(max_iter_slider, mo):
|
115 |
+
mo.md(f"Max iterations: {max_iter_slider} \t max_iter = {max_iter_slider.value}")
|
116 |
+
return
|
117 |
|
118 |
|
119 |
@app.cell
|
120 |
+
def _(mo, n_slider):
|
121 |
+
mo.md(f"Matrix dimension: {n_slider} \t n = {n_slider.value}")
|
122 |
return
|
123 |
|
124 |
|
125 |
@app.cell
|
126 |
+
def _(mo):
|
127 |
+
button = mo.ui.run_button(label="Randomize!")
|
128 |
+
button
|
129 |
+
return (button,)
|
130 |
|
131 |
|
132 |
+
@app.cell
|
133 |
+
def _(errors, plt, scalar_products):
|
|
|
|
|
|
|
134 |
|
|
|
|
|
|
|
135 |
|
136 |
+
# Create a figure with two subplots side by side
|
137 |
+
fig, axs = plt.subplots(1, 2, figsize=(12, 5))
|
138 |
|
139 |
+
# Plot the errors on a log scale in the first subplot
|
140 |
+
axs[0].semilogy(errors)
|
141 |
+
axs[0].set_xlabel('Iteration')
|
142 |
+
axs[0].set_ylabel('Error (log scale)')
|
143 |
+
axs[0].set_title('Error Convergence')
|
144 |
+
axs[0].grid(True)
|
|
|
|
|
|
|
145 |
|
146 |
+
# Plot the histogram in the second subplot
|
147 |
+
axs[1].hist(scalar_products, bins=30, edgecolor='black')
|
148 |
+
axs[1].set_xlabel('Absolute Values Squared of Scalar Products')
|
149 |
+
axs[1].set_ylabel('Frequency')
|
150 |
+
axs[1].set_title('Histogram of Absolute Values Squared of Scalar Products')
|
151 |
+
axs[1].grid(True)
|
152 |
|
153 |
+
# Adjust layout to prevent overlap
|
154 |
+
plt.tight_layout()
|
155 |
+
plt.gca()
|
156 |
+
return axs, fig
|
|
|
157 |
|
|
|
|
|
158 |
|
159 |
+
@app.cell
|
160 |
+
def _(mo):
|
161 |
+
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.""")
|
|
|
|
|
162 |
return
|
163 |
|
164 |
|
165 |
+
@app.cell
|
166 |
+
def _(mo):
|
167 |
mo.md(
|
168 |
+
r"""
|
169 |
+
## Open questions
|
170 |
+
##
|
171 |
+
|
172 |
+
1. Prove the convergence of the algorithm for generic initializations.
|
173 |
+
2. Find the speed of convergence for arbitrary $n$
|
174 |
+
3. What is the distribution of the scalar products for (large / given) $n$? How about the maximal norm of a commutator
|
175 |
+
$$[u_{ij}, u_{kl}] \, ?$$
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
"""
|
177 |
)
|
178 |
return
|
179 |
|
180 |
|
181 |
+
@app.cell
|
182 |
+
def _(mo):
|
183 |
mo.md(
|
184 |
+
r"""
|
185 |
+
## References
|
186 |
+
##
|
187 |
|
188 |
+
1. S. Wang, “Quantum symmetry groups of finite spaces,” _Communications in mathematical physics_, vol. 195, no. 1, pp. 195–211, 1998.
|
189 |
+
2. T. Banica, J. Bichon, and B. Collins, “Quantum permutation groups: a survey,” _Banach Center Publications_, vol. 78, no. 1, pp. 13–34, 2007.
|
190 |
+
3. T. Banica, I. Nechita, "Flat matrix models for quantum permutation groups," _Adv. Appl. Math._ 83, 24-46 (2017)
|
191 |
"""
|
192 |
)
|
193 |
return
|
194 |
|
195 |
|
196 |
@app.cell
|
197 |
+
def _(np):
|
198 |
+
def generate_random_complex_gaussian_matrix(n):
|
199 |
+
matrix = np.empty((n, n, n), dtype=np.complex128)
|
200 |
+
for i in range(n):
|
201 |
+
for j in range(n):
|
202 |
+
real_part = np.random.normal(size=n)
|
203 |
+
imag_part = np.random.normal(size=n)
|
204 |
+
matrix[i, j] = (real_part + 1j * imag_part) / np.sqrt(2)
|
205 |
+
return matrix
|
206 |
+
|
207 |
+
def orthonormalize_vectors_svd(vectors):
|
208 |
+
u, _, v = np.linalg.svd(vectors)
|
209 |
+
return u @ v
|
210 |
+
|
211 |
+
def error_QPM(u):
|
212 |
+
err = -1;
|
213 |
+
|
214 |
+
for i in range(u.shape[0]):
|
215 |
+
slice = u[i, :, :]
|
216 |
+
err = max(err, np.linalg.norm(slice @ slice.conj().T - np.eye(slice.shape[0])))
|
217 |
+
slice = u[:, i, :]
|
218 |
+
err = max(err, np.linalg.norm(slice @ slice.conj().T - np.eye(slice.shape[0])))
|
219 |
+
|
220 |
+
return err
|
221 |
+
|
222 |
+
def scalar_products_QPM(u):
|
223 |
+
# Calculate the absolute values squared of the scalar products of the vectors in the matrix u
|
224 |
+
scalar_products = []
|
225 |
+
for i in range(u.shape[0]):
|
226 |
+
for j in range(1, u.shape[0]):
|
227 |
+
for k in range(1, u.shape[0]):
|
228 |
+
for l in range(1, u.shape[0]):
|
229 |
+
scalar_product = np.abs(np.dot(u[i,j,:], u[k,l,:].conj()))**2
|
230 |
+
scalar_products.append(scalar_product)
|
231 |
+
|
232 |
+
return scalar_products
|
233 |
+
|
234 |
+
|
235 |
+
return (
|
236 |
+
error_QPM,
|
237 |
+
generate_random_complex_gaussian_matrix,
|
238 |
+
orthonormalize_vectors_svd,
|
239 |
+
scalar_products_QPM,
|
240 |
+
)
|
241 |
|
242 |
|
243 |
+
@app.cell
|
244 |
+
def _():
|
245 |
+
import marimo as mo
|
246 |
+
import numpy as np
|
247 |
+
import matplotlib.pyplot as plt
|
248 |
+
return mo, np, plt
|
249 |
|
250 |
|
251 |
+
@app.cell
|
252 |
+
def _(
|
253 |
+
button,
|
254 |
+
eps_slider,
|
255 |
+
error_QPM,
|
256 |
+
generate_random_complex_gaussian_matrix,
|
257 |
+
max_iter_slider,
|
258 |
+
n_slider,
|
259 |
+
orthonormalize_vectors_svd,
|
260 |
+
scalar_products_QPM,
|
261 |
+
):
|
262 |
+
button
|
263 |
+
|
264 |
+
u = generate_random_complex_gaussian_matrix(n_slider.value)
|
265 |
+
|
266 |
+
error_tolerance = 10**(-eps_slider.value)
|
267 |
+
max_iter = max_iter_slider.value
|
268 |
+
|
269 |
+
iter = 0
|
270 |
+
error = error_QPM(u)
|
271 |
+
errors = [error] # Initialize a list to store errors
|
272 |
+
scalar_products = scalar_products_QPM(u) # Initialize the scalar product list
|
273 |
+
|
274 |
+
while error > error_tolerance and iter < max_iter:
|
275 |
+
|
276 |
+
# orthonormalize rows
|
277 |
+
for i in range(n_slider.value):
|
278 |
+
u[i, :, :] = orthonormalize_vectors_svd(u[i, :, :])
|
279 |
+
|
280 |
+
# orthonormalize columns
|
281 |
+
for j in range(n_slider.value):
|
282 |
+
u[:, j, :] = orthonormalize_vectors_svd(u[:, j, :])
|
283 |
+
|
284 |
+
error = error_QPM(u)
|
285 |
+
errors.append(error) # Append the current error to the list
|
286 |
+
|
287 |
+
if iter % 10 == 0:
|
288 |
+
scalar_products = scalar_products_QPM(u) # Update scalar prodcuts
|
289 |
+
iter += 1
|
290 |
+
return (
|
291 |
+
error,
|
292 |
+
error_tolerance,
|
293 |
+
errors,
|
294 |
+
i,
|
295 |
+
iter,
|
296 |
+
j,
|
297 |
+
max_iter,
|
298 |
+
scalar_products,
|
299 |
+
u,
|
300 |
)
|
|
|
301 |
|
302 |
|
303 |
+
@app.cell
|
304 |
+
def _():
|
305 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
306 |
|
307 |
|
308 |
if __name__ == "__main__":
|
309 |
+
app.run()
|