Skip to content

Commit 0c8462a

Browse files
authored
Major refactor (#21)
* new consistent interface * update symbolic matrix and its docs * update interface * another iteration * make tests pass * make test and examples pass * almost working * consistency * fix performance, make tests, exampels and benchmarks pass * update manuscript * cleanup comments * update docs * cleanup obsolete macros * fix includes * fix tests * update memory allocations * fix tags * ux improvments and centered permutation optimization * minot optimizations * remove try catch blocks * remove all try catch blocks * clenaup manuscript * update readme * reformat * fix symbols in manuscript * refactor interface * refactor gaussian measures * refactor integration core * refator type hierarchy * minor refactors * fix macro inconsistency * fix symbols in @integrate * fix docs * fix docs * make more consistent * make docs more consistent * fix itensors * update authors * update kronecker products * fix krons and symplectic itnegration * fix evaluate * fix docstring
1 parent 67495cb commit 0c8462a

108 files changed

Lines changed: 5066 additions & 4072 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Project.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
name = "IntU"
22
uuid = "70c8b9a1-b4c5-4d7a-a56e-8e7e6495d68b"
3-
version = "0.8.0"
3+
version = "0.9.0"
44
authors = ["Łukasz Pawela", "Zbigniew Puchała"]
55

66
[deps]
77
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
88
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
99
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
10+
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
1011
Memoization = "6fafb56a-5788-4b4e-91ca-c0cea6611c73"
1112
SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b"
1213
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
1314

1415
[weakdeps]
15-
ITensors = "9136182c-28ba-11e9-034c-db136f33ea42"
16+
ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5"
1617

1718
[extensions]
1819
IntUITensorsExt = "ITensors"
@@ -21,11 +22,12 @@ IntUITensorsExt = "ITensors"
2122
Aqua = "0.8.14"
2223
Combinatorics = "1"
2324
DataStructures = "0.19.3"
25+
ITensors = "0.3, 0.6"
2426
LinearAlgebra = "1.11, 1.12"
27+
MacroTools = "0.5.16"
2528
Memoization = "0.2.2"
2629
SymbolicUtils = "4"
2730
Symbolics = "7"
28-
ITensors = "0.3, 0.6"
2931
Test = "1.11, 1.12"
3032
julia = "1.11, 1.12"
3133

README.md

Lines changed: 53 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,45 @@ To introduce the main functionality of IntU, consider the problem of averaging
1717
$|U_{i,j}|^2$ over the unitary group, i.e., computing $\int dU |U_{i,j}|^2 =
1818
\int dU U_{i,j} U_{k,l}^* dU$.
1919

20-
While numerical approaches (like sampling random matrices) can estimate this,
21-
they are slow and approximate. IntU provides the **exact** analytic result
22-
instantly, even for symbolic dimensions.
20+
IntU provides an exact analytic result instantly, even for symbolic dimensions, using a simple unified interface: `integrate(expr, measure)`. It supports matrix-valued expressions and provides the `@integrate` macro for intuitive symbolic integration.
21+
22+
The `@integrate` macro implicitly identifies random matrices based on the measure:
23+
- `dU`, `dSU` $\rightarrow$ `U` (Unitary)
24+
- `dO` $\rightarrow$ `O` (Orthogonal)
25+
- `dSp` $\rightarrow$ `Sp` (Symplectic)
26+
- `dPerm` $\rightarrow$ `P` (Permutation)
27+
- `dCPerm` $\rightarrow$ `Y` (Centered Permutation)
28+
- `dCOE`, `dCSE` $\rightarrow$ `S` (Circular Orthogonal/Symplectic)
29+
- `dPsi` $\rightarrow$ `psi` (Pure State)
30+
- `dDiagUnitary` $\rightarrow$ `D` (Diagonal Unitary)
31+
- `dStiefel` $\rightarrow$ `V` (Stiefel Manifold)
32+
- Ginibre Ensembles $\rightarrow$ `G`
33+
34+
Unknown symbols (like `A`, `B`, `d`) are automatically treated as constants or dimensions.
35+
36+
> [!NOTE]
37+
> **Symbol Scope and Redefinition**: The `@integrate` macro manages a persistent symbolic state in your session. If you use a symbol (e.g., `U`) as a random matrix in one call and then use it in a different context (e.g., as a constant in an orthogonal integral), the macro automatically re-declares and re-binds the symbol to the correct type and measure context. This "Safety Rebind" mechanism prevents silent math errors when running examples in sequence.
2338
24-
**New Feature**: You can now integrate matrix-valued expressions directly!
2539
```julia
2640
using IntU, Symbolics, LinearAlgebra
2741

28-
d = 3
29-
@variables U[1:d, 1:d]::Complex
30-
measure = dU(U, d)
31-
3242
# Integrate the matrix expression U * U'
33-
# This performs element-wise integration automatically
34-
res = integrate(collect(U * U'), measure)
35-
# Output: Identity Matrix (I)
43+
# The @integrate macro knows 'U' is the random matrix for measure dU(2)
44+
res = @integrate U * U' dU(2)
45+
# Output: [1.0 0.0; 0.0 1.0] (2x2 Identity Matrix)
3646
```
3747

3848
```julia
39-
using IntU, Symbolics
40-
41-
# Define symbolic dimension 'd' and a unitary matrix 'U'
42-
@variables d
43-
@variables U[1:d, 1:d]::Complex
44-
45-
# Define the Haar measure
46-
measure = dU(U, d)
47-
48-
# Compute the integral of |U_{i,j}|^2
49-
# Note: IntU handles symbolic indices automatically if defined,
50-
# but here we use concrete 1,1 for simplicity which yields the same result by symmetry.
51-
integrate(abs(U[1,1])^2, measure)
52-
# Output: 1 / d
53-
54-
# New: Convenient measure constructors
55-
measure = dU(d) # No matrix variable required
56-
integrate(abs(U[1,1])^2, measure)
49+
using IntU
50+
# Compute the integral of |U_{1,1}|^2
51+
@integrate abs(U[1, 1])^2 dU(d)
5752
# Output: 1 / d
5853
```
5954

6055
For more complex moments, such as $\int dU |U_{1,1}|^2 |U_{1,2}|^2$, IntU handles the combinatorics (Weingarten functions) automatically:
6156

6257
```julia
63-
integrate(abs(U[1,1])^2 * abs(U[1,2])^2, measure)
58+
@integrate abs(U[1, 1])^2 * abs(U[1, 2])^2 dU(d)
6459
# Output: 1 / (d * (1 + d))
6560
```
6661

@@ -75,15 +70,15 @@ can calculate averages over the unitary Haar measure using `dU` and `integrate`.
7570

7671
```julia
7772
# 4-th moment of a diagonal entry
78-
integrate(abs(U[1,1])^4, dU(U, d))
73+
@integrate abs(U[1, 1])^4 dU(d)
7974
# Output: 2 / (d * (1 + d))
8075
```
8176

8277
### Special Unitary Group
8378
The Special Unitary group $SU(d)$ consists of unitary matrices with determinant 1. Use `dSU`.
8479

8580
```julia
86-
integrate(abs(U[1,1])^2, dSU(U, d))
81+
@integrate abs(U[1, 1])^2 dSU(d)
8782
# Output: 1/d
8883
```
8984

@@ -92,18 +87,16 @@ Orthogonal matrices $O$ are real matrices satisfying $O O^T = I_d$. Averages are
9287
computed using the `dO` measure.
9388

9489
```julia
95-
@variables O_mat[1:d, 1:d]::Real
96-
integrate(O_mat[1,1]^4, dO(O_mat, d))
90+
@integrate O[1, 1]^4 dO(d)
9791
# Output: 3 / (d * (2 + d))
9892
```
9993

10094
### Symplectic group
101-
Symplectic matrices $S$ are unitary matrices of even dimension $2n$ that
102-
preserve the symplectic form, $S \Omega S^T = \Omega$. Use `dSp`.
95+
Symplectic matrices $Sp$ are unitary matrices of even dimension $2n$ that
96+
preserve the symplectic form, $Sp \Omega Sp^T = \Omega$. Use `dSp`.
10397

10498
```julia
105-
@variables S_mat[1:d, 1:d]::Complex
106-
integrate(abs(S_mat[1,1])^2, dSp(S_mat, d))
99+
@integrate abs(Sp[1, 1])^2 dSp(d)
107100
# Output: 1 / d
108101
```
109102

@@ -114,12 +107,8 @@ Ginibre ensembles consist of non-Hermitian matrices with i.i.d. Gaussian entries
114107
- **GinSE (Symplectic Ginibre Ensemble)**: i.i.d. quaternionic Gaussian entries. Use `dGinSE`.
115108

116109
```julia
117-
@variables d
118-
# Use T=Complex{Num} for GinUE to ensure conj(G) != G
119-
G = [Symbolics.variable(:G, i, j, T=Complex{Num}) for i = 1:2, j = 1:2]
120-
# E[Tr(G G')] = d^2 = 4 (for 2x2 matrix)
121-
integrate(tr(G * G'), dGinUE(G, d))
122-
# Output: 4
110+
# E[Tr(G G')] = d^2
111+
@integrate tr(G * G') dGinUE(d)
123112
```
124113

125114
### Circular Ensembles
@@ -129,9 +118,8 @@ IntU also supports Circular Ensembles (CUE, COE, CSE) which are commonly used in
129118
- **CSE (Circular Symplectic Ensemble)**: Ensemble of self-dual unitary matrices of even dimension $2n$. Use `dCSE`.
130119

131120
```julia
132-
@variables S_coe[1:d, 1:d]::Complex
133121
# COE moment E[|S_{1,1}|^2]
134-
integrate(abs(S_coe[1,1])^2, dCOE(S_coe, d))
122+
@integrate abs(S[1, 1])^2 dCOE(d)
135123
# Output: 2 / (d + 1)
136124
```
137125

@@ -140,29 +128,27 @@ IntU can integrate polynomial functions of the components of a Haar-random pure
140128
state vector $|\psi\rangle$ of dimension $d$.
141129

142130
```julia
143-
@variables dim
144-
@variables psi[1:dim]::Complex
145-
measure_psi = dPsi(psi, dim)
146-
147131
# Average of |ψ_1|^2
148-
integrate(abs(psi[1])^2, measure_psi)
149-
# Output: 1 / dim
132+
@integrate abs(psi[1, 1])^2 dPsi(d)
133+
# Output: 1 / d
150134
```
151135

152-
### Permutation Groups
153-
IntU supports integration over the Symmetric Group $S_d$ (permutation matrices) and centered permutation matrices $Y = P - J/d$.
136+
### Permutation Group
137+
IntU supports integration over the Symmetric group $S_d$ (permutation matrices).
138+
- **dPerm**: Integration over the set of $d \times d$ permutation matrices.
139+
140+
```julia
141+
# E[P_11]
142+
@integrate P[1, 1] dPerm(d)
143+
# Output: 1 / d
144+
```
145+
146+
IntU also supports centered permutation matrices $Y = P - J/d$.
147+
- **dCPerm**: Integration over centered permutation matrices.
154148

155149
```julia
156-
@variables P[1:d, 1:d]
157-
measure = dPerm(P, d)
158-
# E[P_11 * P_22]
159-
integrate(P[1,1] * P[2,2], measure)
160-
# Output: 1 / (d * (d - 1))
161-
162-
@variables Y[1:d, 1:d]
163-
m_centered = dCPerm(Y, d)
164150
# E[Y_11^2]
165-
integrate(Y[1,1]^2, m_centered)
151+
@integrate Y[1, 1]^2 dCPerm(d)
166152
# Output: (d - 1) / d^2
167153
```
168154

@@ -171,11 +157,8 @@ IntU supports integration over the group of diagonal unitary matrices, which
171157
corresponds to independent phase averaging for each diagonal entry.
172158

173159
```julia
174-
@variables V[1:d, 1:d]::Complex
175-
measure = dDiagUnitary(V, d)
176-
177-
# E[|V_11|^2]
178-
integrate(abs(V[1,1])^2, measure)
160+
# E[|D_11|^2]
161+
@integrate abs(D[1, 1])^2 dDiagUnitary(d)
179162
# Output: 1
180163
```
181164

@@ -184,13 +167,8 @@ IntU supports index-free notation for integrating traces of products of random
184167
matrices, which is often more convenient for quantum information tasks.
185168

186169
```julia
187-
using IntU: tr
188-
# Define symbolic matrices A, B (constant) and U (random)
189-
A = SymbolicMatrix(:A)
190-
B = SymbolicMatrix(:B)
191170
# Compute ∫ tr(U A U† B) dU
192-
expr = tr(SymbolicMatrix(:U, false, :U) * A * SymbolicMatrix(:U, true, :U) * B)
193-
integrate(expr, dU(d))
171+
@integrate tr(U * A * U' * B) dU(d)
194172
# Output: (tr(A)*tr(B)) / d
195173
```
196174

@@ -210,13 +188,8 @@ hciz(A, B)
210188
IntU supports integration over the Stiefel manifold $V_k(\mathbb{C}^d)$, which represents the set of $d \times k$ matrices with orthonormal columns. This generalizes Haar-random pure states ($k=1$).
211189

212190
```julia
213-
@variables d
214-
k = 2
215-
V = [Symbolics.variable(Symbol("V_$(i)_$(j)"), T=Complex{Num}) for i=1:3, j=1:k]
216-
measure = dStiefel(V, d, k)
217-
218191
# E[|V_{1,1}|^2]
219-
integrate(abs(V[1,1])^2, measure)
192+
@integrate abs(V[1, 1])^2 dStiefel(d, 2)
220193
# Output: 1 / d
221194
```
222195

0 commit comments

Comments
 (0)