You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+53-80Lines changed: 53 additions & 80 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,50 +17,45 @@ To introduce the main functionality of IntU, consider the problem of averaging
17
17
$|U_{i,j}|^2$ over the unitary group, i.e., computing $\int dU |U_{i,j}|^2 =
18
18
\int dU U_{i,j} U_{k,l}^* dU$.
19
19
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:
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.
23
38
24
-
**New Feature**: You can now integrate matrix-valued expressions directly!
# 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
+
@integrateabs(U[1, 1])^2dU(d)
57
52
# Output: 1 / d
58
53
```
59
54
60
55
For more complex moments, such as $\int dU |U_{1,1}|^2 |U_{1,2}|^2$, IntU handles the combinatorics (Weingarten functions) automatically:
61
56
62
57
```julia
63
-
integrate(abs(U[1,1])^2*abs(U[1,2])^2, measure)
58
+
@integrateabs(U[1,1])^2*abs(U[1,2])^2dU(d)
64
59
# Output: 1 / (d * (1 + d))
65
60
```
66
61
@@ -75,15 +70,15 @@ can calculate averages over the unitary Haar measure using `dU` and `integrate`.
75
70
76
71
```julia
77
72
# 4-th moment of a diagonal entry
78
-
integrate(abs(U[1,1])^4,dU(U, d))
73
+
@integrateabs(U[1,1])^4dU(d)
79
74
# Output: 2 / (d * (1 + d))
80
75
```
81
76
82
77
### Special Unitary Group
83
78
The Special Unitary group $SU(d)$ consists of unitary matrices with determinant 1. Use `dSU`.
84
79
85
80
```julia
86
-
integrate(abs(U[1,1])^2,dSU(U, d))
81
+
@integrateabs(U[1,1])^2dSU(d)
87
82
# Output: 1/d
88
83
```
89
84
@@ -92,18 +87,16 @@ Orthogonal matrices $O$ are real matrices satisfying $O O^T = I_d$. Averages are
92
87
computed using the `dO` measure.
93
88
94
89
```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]^4dO(d)
97
91
# Output: 3 / (d * (2 + d))
98
92
```
99
93
100
94
### 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`.
103
97
104
98
```julia
105
-
@variables S_mat[1:d, 1:d]::Complex
106
-
integrate(abs(S_mat[1,1])^2, dSp(S_mat, d))
99
+
@integrateabs(Sp[1, 1])^2dSp(d)
107
100
# Output: 1 / d
108
101
```
109
102
@@ -114,12 +107,8 @@ Ginibre ensembles consist of non-Hermitian matrices with i.i.d. Gaussian entries
114
107
-**GinSE (Symplectic Ginibre Ensemble)**: i.i.d. quaternionic Gaussian entries. Use `dGinSE`.
115
108
116
109
```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
+
@integratetr(G * G') dGinUE(d)
123
112
```
124
113
125
114
### Circular Ensembles
@@ -129,9 +118,8 @@ IntU also supports Circular Ensembles (CUE, COE, CSE) which are commonly used in
129
118
-**CSE (Circular Symplectic Ensemble)**: Ensemble of self-dual unitary matrices of even dimension $2n$. Use `dCSE`.
130
119
131
120
```julia
132
-
@variables S_coe[1:d, 1:d]::Complex
133
121
# COE moment E[|S_{1,1}|^2]
134
-
integrate(abs(S_coe[1,1])^2,dCOE(S_coe, d))
122
+
@integrateabs(S[1,1])^2dCOE(d)
135
123
# Output: 2 / (d + 1)
136
124
```
137
125
@@ -140,29 +128,27 @@ IntU can integrate polynomial functions of the components of a Haar-random pure
140
128
state vector $|\psi\rangle$ of dimension $d$.
141
129
142
130
```julia
143
-
@variables dim
144
-
@variables psi[1:dim]::Complex
145
-
measure_psi =dPsi(psi, dim)
146
-
147
131
# Average of |ψ_1|^2
148
-
integrate(abs(psi[1])^2, measure_psi)
149
-
# Output: 1 / dim
132
+
@integrateabs(psi[1, 1])^2dPsi(d)
133
+
# Output: 1 / d
150
134
```
151
135
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.
154
148
155
149
```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)
164
150
# E[Y_11^2]
165
-
integrate(Y[1,1]^2, m_centered)
151
+
@integrateY[1,1]^2dCPerm(d)
166
152
# Output: (d - 1) / d^2
167
153
```
168
154
@@ -171,11 +157,8 @@ IntU supports integration over the group of diagonal unitary matrices, which
171
157
corresponds to independent phase averaging for each diagonal entry.
172
158
173
159
```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
+
@integrateabs(D[1, 1])^2dDiagUnitary(d)
179
162
# Output: 1
180
163
```
181
164
@@ -184,13 +167,8 @@ IntU supports index-free notation for integrating traces of products of random
184
167
matrices, which is often more convenient for quantum information tasks.
185
168
186
169
```julia
187
-
using IntU: tr
188
-
# Define symbolic matrices A, B (constant) and U (random)
189
-
A =SymbolicMatrix(:A)
190
-
B =SymbolicMatrix(:B)
191
170
# 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
+
@integratetr(U * A * U'* B) dU(d)
194
172
# Output: (tr(A)*tr(B)) / d
195
173
```
196
174
@@ -210,13 +188,8 @@ hciz(A, B)
210
188
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$).
211
189
212
190
```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]
0 commit comments