-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathproduct.jl
More file actions
378 lines (339 loc) · 13.9 KB
/
product.jl
File metadata and controls
378 lines (339 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# Deligne tensor product of different sectors: ⊠
#------------------------------------------------------------------------------#
const SectorTuple = Tuple{Vararg{Sector}}
"""
struct ProductSector{T <: SectorTuple}
ProductSector((s₁, s₂, ...))
Represents the Deligne tensor product of sectors. The type parameter `T` is a tuple of the
component sectors. The recommended way to construct a `ProductSector` is using the
[`deligneproduct`](@ref ⊠) (`⊠`) operator on the components.
"""
struct ProductSector{T <: SectorTuple} <: Sector
sectors::T
end
Base.Tuple(a::ProductSector) = a.sectors
Base.getindex(s::ProductSector, i::Int) = getindex(s.sectors, i)
Base.length(s::ProductSector) = length(s.sectors)
Base.iterate(s::ProductSector, args...) = iterate(s.sectors, args...)
Base.indexed_iterate(s::ProductSector, args...) = Base.indexed_iterate(s.sectors, args...)
_sectors(::Type{Tuple{}}) = ()
Base.@pure function _sectors(::Type{T}) where {T <: SectorTuple}
return (Base.tuple_type_head(T), _sectors(Base.tuple_type_tail(T))...)
end
function Base.IteratorSize(::Type{SectorValues{ProductSector{T}}}) where {T <: SectorTuple}
return Base.IteratorSize(Base.Iterators.product(map(values, _sectors(T))...))
end
function Base.size(::SectorValues{ProductSector{T}}) where {T <: SectorTuple}
return map(s -> length(values(s)), _sectors(T))
end
Base.length(P::SectorValues{<:ProductSector}) = *(size(P)...)
function _length(iter::SectorValues{I}) where {I <: Sector}
return Base.IteratorSize(iter) === Base.IsInfinite() ? typemax(Int) : length(iter)
end
function _size(::SectorValues{ProductSector{T}}) where {T <: SectorTuple}
return map(s -> _length(values(s)), _sectors(T))
end
function Base.getindex(P::SectorValues{ProductSector{T}}, i::Int) where {T <: SectorTuple}
I = manhattan_to_multidimensional_index(i, _size(P))
return ProductSector{T}(getindex.(values.(_sectors(T)), I))
end
function findindex(
P::SectorValues{ProductSector{T}},
c::ProductSector{T}
) where {T <: SectorTuple}
return to_manhattan_index(findindex.(values.(_sectors(T)), c.sectors), _size(P))
end
function Base.iterate(P::SectorValues{ProductSector{T}}, i = 1) where {T <: SectorTuple}
Base.IteratorSize(P) != Base.IsInfinite() && i > length(P) && return nothing
return getindex(P, i), i + 1
end
ProductSector{T}(args...) where {T <: SectorTuple} = ProductSector{T}(args)
function Base.convert(::Type{ProductSector{T}}, t::Tuple) where {T <: SectorTuple}
return ProductSector{T}(convert(T, t))
end
function unit(::Type{T}) where {T <: ProductSector}
UnitStyle(T) === GenericUnit() && throw_genericunit_error(T)
return only(allunits(T))
end
function allunits(::Type{ProductSector{T}}) where {T}
iterators = map(allunits, _sectors(T))
return SectorSet{ProductSector{T}}(Base.Iterators.product(iterators...))
end
function leftunit(a::P) where {P <: ProductSector}
return P(map(leftunit, a.sectors))
end
function rightunit(a::P) where {P <: ProductSector}
return P(map(rightunit, a.sectors))
end
dual(p::ProductSector) = ProductSector(map(dual, p.sectors))
function ⊗(p1::P, p2::P) where {P <: ProductSector}
if FusionStyle(P) isa UniqueFusion
(P(first(product(map(⊗, p1.sectors, p2.sectors)...))),)
else
return SectorSet{P}(product(map(⊗, p1.sectors, p2.sectors)...))
end
end
function Nsymbol(a::P, b::P, c::P) where {P <: ProductSector}
return prod(map(Nsymbol, a.sectors, b.sectors, c.sectors))
end
_firstsector(x::ProductSector) = x.sectors[1]
_tailsector(x::ProductSector) = ProductSector(Base.tail(x.sectors))
function Fsymbol(a::P, b::P, c::P, d::P, e::P, f::P) where {P <: ProductSector}
heads = map(_firstsector, (a, b, c, d, e, f))
tails = map(_tailsector, (a, b, c, d, e, f))
F₁ = Fsymbol(heads...)
F₂ = Fsymbol(tails...)
if F₁ isa Number && F₂ isa Number
return F₁ * F₂
elseif F₁ isa Number
a₁, b₁, c₁, d₁, e₁, f₁ = heads
sz₁ = (
Nsymbol(a₁, b₁, e₁), Nsymbol(e₁, c₁, d₁), Nsymbol(b₁, c₁, f₁), Nsymbol(a₁, f₁, d₁),
)
F₁′ = fill(F₁, sz₁)
return _kron(F₁′, F₂)
elseif F₂ isa Number
a₂, b₂, c₂, d₂, e₂, f₂ = tails
sz₂ = (
Nsymbol(a₂, b₂, e₂), Nsymbol(e₂, c₂, d₂), Nsymbol(b₂, c₂, f₂), Nsymbol(a₂, f₂, d₂),
)
F₂′ = fill(F₂, sz₂)
return _kron(F₁, F₂′)
else
return _kron(F₁, F₂)
end
end
function Fsymbol(
a::P, b::P, c::P, d::P, e::P, f::P
) where {P <: ProductSector{<:Tuple{Sector}}}
return Fsymbol(map(_firstsector, (a, b, c, d, e, f))...)
end
function Rsymbol(a::P, b::P, c::P) where {P <: ProductSector}
heads = map(_firstsector, (a, b, c))
tails = map(_tailsector, (a, b, c))
R₁ = Rsymbol(heads...)
R₂ = Rsymbol(tails...)
if R₁ isa Number && R₂ isa Number
R₁ * R₂
elseif R₁ isa Number
a₁, b₁, c₁ = heads
sz₁ = (Nsymbol(a₁, b₁, c₁), Nsymbol(b₁, a₁, c₁)) # 0 x 0 or 1 x 1
R₁′ = fill(R₁, sz₁)
return _kron(R₁′, R₂)
elseif R₂ isa Number
a₂, b₂, c₂ = tails
sz₂ = (Nsymbol(a₂, b₂, c₂), Nsymbol(b₂, a₂, c₂)) # 0 x 0 or 1 x 1
R₂′ = fill(R₂, sz₂)
return _kron(R₁, R₂′)
else
return _kron(R₁, R₂)
end
end
function Rsymbol(a::P, b::P, c::P) where {P <: ProductSector{<:Tuple{Sector}}}
return Rsymbol(map(_firstsector, (a, b, c))...)
end
function Bsymbol(a::P, b::P, c::P) where {P <: ProductSector}
heads = map(_firstsector, (a, b, c))
tails = map(_tailsector, (a, b, c))
B₁ = Bsymbol(heads...)
B₂ = Bsymbol(tails...)
if B₁ isa Number && B₂ isa Number
B₁ * B₂
elseif B₁ isa Number
a₁, b₁, c₁ = heads
sz₁ = (Nsymbol(a₁, b₁, c₁), Nsymbol(c₁, dual(b₁), a₁)) # 0 x 0 or 1 x 1
B₁′ = fill(B₁, sz₁)
return _kron(B₁′, B₂)
elseif B₂ isa Number
a₂, b₂, c₂ = tails
sz₂ = (Nsymbol(a₂, b₂, c₂), Nsymbol(c₂, dual(b₂), a₂)) # 0 x 0 or 1 x 1
B₂′ = fill(B₂, sz₂)
return _kron(B₁, B₂′)
else
return _kron(B₁, B₂)
end
end
function Bsymbol(a::P, b::P, c::P) where {P <: ProductSector{<:Tuple{Sector}}}
return Bsymbol(map(_firstsector, (a, b, c))...)
end
function Asymbol(a::P, b::P, c::P) where {P <: ProductSector}
heads = map(_firstsector, (a, b, c))
tails = map(_tailsector, (a, b, c))
A₁ = Asymbol(heads...)
A₂ = Asymbol(tails...)
if A₁ isa Number && A₂ isa Number
A₁ * A₂
elseif A₁ isa Number
a₁, b₁, c₁ = heads
sz₁ = (Nsymbol(a₁, b₁, c₁), Nsymbol(dual(a₁), c₁, b₁)) # 0 x 0 or 1 x 1
A₁′ = fill(A₁, sz₁)
return _kron(A₁′, A₂)
elseif A₂ isa Number
a₂, b₂, c₂ = tails
sz₂ = (Nsymbol(a₂, b₂, c₂), Nsymbol(dual(a₂), c₂, b₂)) # 0 x 0 or 1 x 1
A₂′ = fill(A₂, sz₂)
return _kron(A₁, A₂′)
else
return _kron(A₁, A₂)
end
end
function Asymbol(a::P, b::P, c::P) where {P <: ProductSector{<:Tuple{Sector}}}
return Asymbol(map(_firstsector, (a, b, c))...)
end
frobenius_schur_phase(p::ProductSector) = prod(frobenius_schur_phase, p.sectors)
frobenius_schur_indicator(p::ProductSector) = prod(frobenius_schur_indicator, p.sectors)
function fusiontensor(a::P, b::P, c::P) where {P <: ProductSector}
return _kron(
fusiontensor(map(_firstsector, (a, b, c))...),
fusiontensor(map(_tailsector, (a, b, c))...)
)
end
function fusiontensor(a::P, b::P, c::P) where {P <: ProductSector{<:Tuple{Sector}}}
return fusiontensor(map(_firstsector, (a, b, c))...)
end
function FusionStyle(::Type{<:ProductSector{T}}) where {T <: SectorTuple}
return mapreduce(FusionStyle, &, _sectors(T))
end
function FusionDataStyle(::Type{<:ProductSector{T}}) where {T <: SectorTuple}
return mapreduce(FusionDataStyle, &, _sectors(T))
end
function UnitStyle(::Type{<:ProductSector{T}}) where {T <: SectorTuple}
return mapreduce(UnitStyle, &, _sectors(T))
end
function BraidingStyle(::Type{<:ProductSector{T}}) where {T <: SectorTuple}
return mapreduce(BraidingStyle, &, _sectors(T))
end
function Base.isreal(::Type{<:ProductSector{T}}) where {T <: SectorTuple}
return mapreduce(isreal, &, _sectors(T))
end
fermionparity(P::ProductSector) = mapreduce(fermionparity, xor, P.sectors)
dim(p::ProductSector) = *(dim.(p.sectors)...)
Base.isequal(p1::ProductSector, p2::ProductSector) = isequal(p1.sectors, p2.sectors)
Base.hash(p::ProductSector, h::UInt) = hash(p.sectors, h)
function Base.isless(p1::ProductSector{T}, p2::ProductSector{T}) where {T <: SectorTuple}
I1 = findindex.(values.(_sectors(T)), p1.sectors)
I2 = findindex.(values.(_sectors(T)), p2.sectors)
d1 = sum(I1) - length(I1)
d2 = sum(I2) - length(I2)
d1 < d2 && return true
d1 > d2 && return false
return isless(I1, I2)
end
# Default construction from tensor product of sectors
#-----------------------------------------------------
⊠(s1, s2, s3, s4...) = ⊠(⊠(s1, s2), s3, s4...)
const deligneproduct = ⊠
"""
⊠(s₁::Sector, s₂::Sector)
deligneproduct(s₁::Sector, s₂::Sector)
Given two sectors `s₁` and `s₂`, which label an isomorphism class of simple objects in a
fusion category ``C₁`` and ``C₂``, `s1 ⊠ s2` (obtained as `\\boxtimes+TAB`) labels the
isomorphism class of simple objects in the Deligne tensor product category ``C₁ ⊠ C₂``.
The Deligne tensor product also works in the type domain and for spaces and tensors. For
group representations, we have `Irrep[G₁] ⊠ Irrep[G₂] == Irrep[G₁ × G₂]`.
"""
⊠(s1::Sector, s2::Sector) = ProductSector((s1, s2))
⊠(s1::Trivial, s2::Trivial) = s1
⊠(s1::Sector, s2::Trivial) = s1
⊠(s1::Trivial, s2::Sector) = s2
⊠(p1::ProductSector, s2::Trivial) = p1
⊠(p1::ProductSector, s2::Sector) = ProductSector(tuple(p1.sectors..., s2))
⊠(s1::Trivial, p2::ProductSector) = p2
⊠(s1::Sector, p2::ProductSector) = ProductSector(tuple(s1, p2.sectors...))
⊠(p1::ProductSector, p2::ProductSector) = ProductSector(tuple(p1.sectors..., p2.sectors...))
⊠(I1::Type{Trivial}, I2::Type{Trivial}) = Trivial
⊠(I1::Type{Trivial}, I2::Type{<:ProductSector}) = I2
⊠(I1::Type{Trivial}, I2::Type{<:Sector}) = I2
⊠(I1::Type{<:ProductSector}, I2::Type{Trivial}) = I1
@static if VERSION >= v"1.8"
Base.@assume_effects :foldable function ⊠(
I1::Type{<:ProductSector}, I2::Type{<:ProductSector}
)
T1 = I1.parameters[1]
T2 = I2.parameters[1]
return ProductSector{Tuple{T1.parameters..., T2.parameters...}}
end
else
Base.@pure function ⊠(I1::Type{<:ProductSector}, I2::Type{<:ProductSector})
T1 = I1.parameters[1]
T2 = I2.parameters[1]
return ProductSector{Tuple{T1.parameters..., T2.parameters...}}
end
end
⊠(I1::Type{<:ProductSector}, I2::Type{<:Sector}) = I1 ⊠ ProductSector{Tuple{I2}}
⊠(I1::Type{<:Sector}, I2::Type{Trivial}) = I1
⊠(I1::Type{<:Sector}, I2::Type{<:ProductSector}) = ProductSector{Tuple{I1}} ⊠ I2
⊠(I1::Type{<:Sector}, I2::Type{<:Sector}) = ProductSector{Tuple{I1, I2}}
function Base.show(io::IO, P::ProductSector)
sectors = P.sectors
compact = get(io, :typeinfo, nothing) === typeof(P)
sep = compact ? ", " : " ⊠ "
print(io, "(")
for i in 1:length(sectors)
i == 1 || print(io, sep)
io2 = compact ? IOContext(io, :typeinfo => typeof(sectors[i])) : io
print(io2, sectors[i])
end
return print(io, ")")
end
function type_repr(P::Type{<:ProductSector})
sectors = P.parameters[1].parameters
if length(sectors) == 1
s = "ProductSector{Tuple{" * type_repr(sectors[1]) * "}}"
else
s = "("
for i in 1:length(sectors)
if i != 1
s *= " ⊠ "
end
s *= type_repr(sectors[i])
end
s *= ")"
end
return s
end
#==============================================================================
TODO: the following would implement pretty-printing of product sectors, i.e.
`ProductSector{Tuple{Irrep[G]}}` would be printed as `Irrep[G]`, and
`ProductSector{Tuple{Irrep[G]}}(x)` would be printed as `Irrep[G](x)`.
However, defining show for a type is considered type piracy/treason, and can lead
to unexpected behavior. While we can avoid this by only defining show for the
instances, this would lead to the following behavior:
```julia-repl
julia> [Irrep[ℤ₂ × U₁](0, 0)]
1-element Vector{TensorKit.ProductSector{Tuple{Z2Irrep, U1Irrep}}}:
(0, 0)
```
See Julia issues #29988, #29428, #22363, #28983.
Base.show(io::IO, P::Type{<:ProductSector}) = print(io, type_repr(P))
==============================================================================#
function Base.show(io::IO, P::ProductSector{T}) where {T <: Tuple{Vararg{AbstractIrrep}}}
sectors = P.sectors
get(io, :typeinfo, nothing) === typeof(P) || print(io, type_repr(typeof(P)))
print(io, "(")
for i in 1:length(sectors)
i == 1 || print(io, ", ")
print(IOContext(io, :typeinfo => typeof(sectors[i])), sectors[i])
end
return print(io, ")")
end
function type_repr(::Type{ProductSector{T}}) where {T <: Tuple{Vararg{AbstractIrrep}}}
sectors = T.parameters
s = "Irrep["
for i in 1:length(sectors)
if i != 1
s *= " × "
end
s *= type_repr(supertype(sectors[i]).parameters[1])
end
s *= "]"
return s
end
function Base.getindex(::IrrepTable, ::Type{ProductGroup{Gs}}) where {Gs <: GroupTuple}
G1 = tuple_type_head(Gs)
Grem = tuple_type_tail(Gs)
return ProductSector{Tuple{Irrep[G1]}} ⊠ Irrep[ProductGroup{tuple_type_tail(Gs)}]
end
function Base.getindex(::IrrepTable, ::Type{ProductGroup{Tuple{G}}}) where {G <: Group}
return ProductSector{Tuple{Irrep[G]}}
end