Skip to content

Commit 435f775

Browse files
committed
refactor shared_product_sectors_fusion_rule
1 parent 00fd5f8 commit 435f775

File tree

2 files changed

+40
-68
lines changed

2 files changed

+40
-68
lines changed

NDTensors/src/lib/SymmetrySectors/src/sector_product.jl

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -122,33 +122,6 @@ product_sectors_fusion_rule(::NamedTuple{()}, sects::Tuple) = SectorProduct(sect
122122
product_sectors_fusion_rule(sects::NamedTuple, ::Tuple{}) = SectorProduct(sects)
123123
product_sectors_fusion_rule(::Tuple{}, sects::NamedTuple) = SectorProduct(sects)
124124

125-
function fix_fused_product_type(Sectors::Type, fused)
126-
return fix_fused_product_type(product_sectors_symmetrystyle(Sectors), Sectors, fused)
127-
end
128-
129-
function fix_fused_product_type(::AbelianStyle, Sectors::Type, fused)
130-
return recover_sector_product_type(Sectors, fused)
131-
end
132-
133-
function fix_fused_product_type(::NotAbelianStyle, Sectors::Type, fused)
134-
# convert e.g. Tuple{GradedUnitRange{SU2}, GradedUnitRange{SU2}} into GradedUnitRange{SU2×SU2}
135-
g = reduce(×, fused)
136-
# convention: keep unsorted blocklabels as produced by F order loops in ×
137-
return recover_gradedaxis_product_type(Sectors, g)
138-
end
139-
140-
function recover_gradedaxis_product_type(Sectors::Type, g0::AbstractGradedUnitRange)
141-
old_labels = blocklabels(g0)
142-
old_sects = product_sectors.(SectorProduct.(old_labels))
143-
new_labels = recover_sector_product_type.(Sectors, old_sects)
144-
new_blocklengths = labelled.(unlabel.(blocklengths(g0)), new_labels)
145-
return gradedrange(new_blocklengths)
146-
end
147-
148-
function recover_sector_product_type(Sectors::Type, sects)
149-
return SectorProduct(Sectors(sects))
150-
end
151-
152125
# ================================= Cartesian Product ====================================
153126
×(c1::AbstractSector, c2::AbstractSector) = ×(SectorProduct(c1), SectorProduct(c2))
154127
function ×(p1::SectorProduct, p2::SectorProduct)
@@ -228,8 +201,13 @@ function product_sectors_diff(t1::Tuple, t2::Tuple)
228201
end
229202

230203
function shared_product_sectors_fusion_rule(shared1::T, shared2::T) where {T<:Tuple}
231-
fused = map(fusion_rule, shared1, shared2)
232-
return fix_fused_product_type(T, fused)
204+
return mapreduce(
205+
to_gradedrange fusion_rule,
206+
×,
207+
shared1,
208+
shared2;
209+
init=to_gradedrange(SectorProduct(())),
210+
)
233211
end
234212

235213
function product_sectors_insert_unspecified(t1::Tuple, t2::Tuple)
@@ -282,7 +260,11 @@ end
282260

283261
product_sectors_diff(nt1::NamedTuple, nt2::NamedTuple) = symdiff_keys(nt1, nt2)
284262

263+
function map_blocklabels(f, r::AbstractUnitRange)
264+
return gradedrange(labelled.(unlabel.(blocklengths(r)), f.(blocklabels(r))))
265+
end
266+
285267
function shared_product_sectors_fusion_rule(shared1::NT, shared2::NT) where {NT<:NamedTuple}
286-
fused = map(fusion_rule, values(shared1), values(shared2))
287-
return fix_fused_product_type(NT, fused)
268+
tuple_fused = shared_product_sectors_fusion_rule(values(shared1), values(shared2))
269+
return map_blocklabels(SectorProduct NT product_sectors SectorProduct, tuple_fused)
288270
end

NDTensors/src/lib/SymmetrySectors/test/test_sector_product.jl

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ using NDTensors.SymmetrySectors:
1212
Z,
1313
block_dimensions,
1414
quantum_dimension,
15-
recover_sector_product_type,
1615
product_sectors,
1716
trivial
1817
using NDTensors.GradedAxes: dual, fusion_product, space_isequal, gradedrange
@@ -43,9 +42,6 @@ using Test: @inferred, @test, @testset, @test_throws
4342
@test product_sectors(s)[2] == SU2(1//2)
4443
@test product_sectors(s)[3] == U1(3)
4544
@test (@inferred trivial(s)) == SectorProduct(U1(0), SU2(0), U1(0))
46-
@test (@inferred recover_sector_product_type(
47-
typeof(product_sectors(s)), product_sectors(s)
48-
)) == s
4945

5046
s = U1(3) × SU2(1//2) × Fib("τ")
5147
@test length(product_sectors(s)) == 3
@@ -146,14 +142,14 @@ using Test: @inferred, @test, @testset, @test_throws
146142
@test (@inferred p1 p2) == SectorProduct(U1(3))
147143

148144
p11 = U1(1) × U1(1)
149-
@test (@inferred p11 p11) == U1(2) × U1(2)
145+
@test p11 p11 == U1(2) × U1(2)
150146

151147
p123 = U1(1) × U1(2) × U1(3)
152-
@test (@inferred p123 p123) == U1(2) × U1(4) × U1(6)
148+
@test p123 p123 == U1(2) × U1(4) × U1(6)
153149

154150
s1 = SectorProduct(U1(1), Z{2}(1))
155151
s2 = SectorProduct(U1(0), Z{2}(0))
156-
@test (@inferred s1 s2) == U1(1) × Z{2}(1)
152+
@test s1 s2 == U1(1) × Z{2}(1)
157153
end
158154

159155
@testset "Fusion of NonAbelian products" begin
@@ -177,7 +173,7 @@ using Test: @inferred, @test, @testset, @test_throws
177173
]),
178174
)
179175
@test space_isequal(
180-
(@inferred phh phh),
176+
phh phh,
181177
gradedrange([
182178
(SU2(0) × SU2(0)) => 1,
183179
(SU2(1) × SU2(0)) => 1,
@@ -191,12 +187,11 @@ using Test: @inferred, @test, @testset, @test_throws
191187
ı = Fib("1")
192188
τ = Fib("τ")
193189
s = ı × ı
194-
@test space_isequal((@inferred s s), gradedrange([s => 1]))
190+
@test space_isequal(s s, gradedrange([s => 1]))
195191

196192
s = τ × τ
197193
@test space_isequal(
198-
(@inferred s s),
199-
gradedrange([(ı × ı) => 1, (τ × ı) => 1, (ı × τ) => 1, (τ × τ) => 1]),
194+
s s, gradedrange([(ı × ı) => 1, (τ × ı) => 1, (ı × τ) => 1, (τ × τ) => 1])
200195
)
201196

202197
σ = Ising("σ")
@@ -205,27 +200,27 @@ using Test: @inferred, @test, @testset, @test_throws
205200
g = gradedrange([
206201
× Ising("1")) => 1, (τ × Ising("1")) => 1, (ı × ψ) => 1, (τ × ψ) => 1
207202
])
208-
@test space_isequal((@inferred s s), g)
203+
@test space_isequal(s s, g)
209204
end
210205

211206
@testset "Fusion of mixed Abelian and NonAbelian products" begin
212207
p2h = U1(2) × SU2(1//2)
213208
p1h = U1(1) × SU2(1//2)
214209
@test space_isequal(
215-
(@inferred p2h p1h), gradedrange([(U1(3) × SU2(0)) => 1, (U1(3) × SU2(1)) => 1])
210+
p2h p1h, gradedrange([(U1(3) × SU2(0)) => 1, (U1(3) × SU2(1)) => 1])
216211
)
217212

218213
p1h1 = U1(1) × SU2(1//2) × Z{2}(1)
219214
@test space_isequal(
220-
(@inferred p1h1 p1h1),
215+
p1h1 p1h1,
221216
gradedrange([(U1(2) × SU2(0) × Z{2}(0)) => 1, (U1(2) × SU2(1) × Z{2}(0)) => 1]),
222217
)
223218
end
224219

225220
@testset "Fusion of fully mixed products" begin
226221
s = U1(1) × SU2(1//2) × Ising("σ")
227222
@test space_isequal(
228-
(@inferred s s),
223+
s s,
229224
gradedrange([
230225
(U1(2) × SU2(0) × Ising("1")) => 1,
231226
(U1(2) × SU2(1) × Ising("1")) => 1,
@@ -238,7 +233,7 @@ using Test: @inferred, @test, @testset, @test_throws
238233
τ = Fib("τ")
239234
s = SU2(1//2) × U1(1) × τ
240235
@test space_isequal(
241-
(@inferred s s),
236+
s s,
242237
gradedrange([
243238
(SU2(0) × U1(2) × ı) => 1,
244239
(SU2(1) × U1(2) × ı) => 1,
@@ -248,9 +243,7 @@ using Test: @inferred, @test, @testset, @test_throws
248243
)
249244

250245
s = U1(1) × ı × τ
251-
@test space_isequal(
252-
(@inferred s s), gradedrange([(U1(2) × ı × ı) => 1, (U1(2) × ı × τ) => 1])
253-
)
246+
@test space_isequal(s s, gradedrange([(U1(2) × ı × ı) => 1, (U1(2) × ı × τ) => 1]))
254247
end
255248

256249
@testset "Fusion of different length Categories" begin
@@ -282,7 +275,7 @@ using Test: @inferred, @test, @testset, @test_throws
282275
g1 = gradedrange([s1 => 2])
283276
g2 = gradedrange([s2 => 1])
284277
@test space_isequal(
285-
(@inferred fusion_product(g1, g2)),
278+
fusion_product(g1, g2),
286279
gradedrange([U1(1) × SU2(0) × Ising("σ") => 2, U1(1) × SU2(1) × Ising("σ") => 2]),
287280
)
288281
end
@@ -305,9 +298,6 @@ end
305298
@test (@inferred quantum_dimension(s)) == 5
306299
@test (@inferred dual(s)) == (A=U1(-1),) × (B=SU2(2),)
307300
@test (@inferred trivial(s)) == (A=U1(0),) × (B=SU2(0),)
308-
@test (@inferred recover_sector_product_type(
309-
typeof(product_sectors(s)), Tuple(product_sectors(s))
310-
)) == s
311301
@test s == (B=SU2(2),) × (A=U1(1),)
312302

313303
s = s × (C=Ising("ψ"),)
@@ -418,15 +408,15 @@ end
418408
@test (@inferred q01 q00) == q01
419409
@test (@inferred q00 q01) == q01
420410
@test (@inferred q10 q01) == q11
421-
@test (@inferred q11 q11) == SectorProduct(; A=U1(2), B=U1(2))
411+
@test q11 q11 == SectorProduct(; A=U1(2), B=U1(2))
422412

423413
s11 = SectorProduct(; A=U1(1), B=Z{2}(1))
424414
s10 = SectorProduct(; A=U1(1))
425415
s01 = SectorProduct(; B=Z{2}(1))
426416
@test (@inferred s01 q00) == s01
427417
@test (@inferred q00 s01) == s01
428418
@test (@inferred s10 s01) == s11
429-
@test (@inferred s11 s11) == SectorProduct(; A=U1(2), B=Z{2}(0))
419+
@test s11 s11 == SectorProduct(; A=U1(2), B=Z{2}(0))
430420
end
431421

432422
@testset "Fusion of NonAbelian products" begin
@@ -444,7 +434,7 @@ end
444434
@test space_isequal((@inferred pha phb), gradedrange([phab => 1]))
445435

446436
@test space_isequal(
447-
(@inferred phab phab),
437+
phab phab,
448438
gradedrange([
449439
SectorProduct(; A=SU2(0), B=SU2(0)) => 1,
450440
SectorProduct(; A=SU2(1), B=SU2(0)) => 1,
@@ -458,11 +448,11 @@ end
458448
ı = Fib("1")
459449
τ = Fib("τ")
460450
s = SectorProduct(; A=ı, B=ı)
461-
@test space_isequal((@inferred s s), gradedrange([s => 1]))
451+
@test space_isequal(s s, gradedrange([s => 1]))
462452

463453
s = SectorProduct(; A=τ, B=τ)
464454
@test space_isequal(
465-
(@inferred s s),
455+
s s,
466456
gradedrange([
467457
SectorProduct(; A=ı, B=ı) => 1,
468458
SectorProduct(; A=τ, B=ı) => 1,
@@ -480,7 +470,7 @@ end
480470
SectorProduct(; A=ı, B=ψ) => 1,
481471
SectorProduct(; A=τ, B=ψ) => 1,
482472
])
483-
@test space_isequal((@inferred s s), g)
473+
@test space_isequal(s s, g)
484474
end
485475

486476
@testset "Fusion of mixed Abelian and NonAbelian products" begin
@@ -494,16 +484,16 @@ end
494484
q21 = (N=U1(2),) × (J=SU2(1),)
495485
q22 = (N=U1(2),) × (J=SU2(2),)
496486

497-
@test space_isequal((@inferred q1h q1h), gradedrange([q20 => 1, q21 => 1]))
498-
@test space_isequal((@inferred q10 q1h), gradedrange([q2h => 1]))
487+
@test space_isequal(q1h q1h, gradedrange([q20 => 1, q21 => 1]))
488+
@test space_isequal(q10 q1h, gradedrange([q2h => 1]))
499489
@test space_isequal((@inferred q0h q1h), gradedrange([q10 => 1, q11 => 1]))
500-
@test space_isequal((@inferred q11 q11), gradedrange([q20 => 1, q21 => 1, q22 => 1]))
490+
@test space_isequal(q11 q11, gradedrange([q20 => 1, q21 => 1, q22 => 1]))
501491
end
502492

503493
@testset "Fusion of fully mixed products" begin
504494
s = SectorProduct(; A=U1(1), B=SU2(1//2), C=Ising("σ"))
505495
@test space_isequal(
506-
(@inferred s s),
496+
s s,
507497
gradedrange([
508498
SectorProduct(; A=U1(2), B=SU2(0), C=Ising("1")) => 1,
509499
SectorProduct(; A=U1(2), B=SU2(1), C=Ising("1")) => 1,
@@ -516,7 +506,7 @@ end
516506
τ = Fib("τ")
517507
s = SectorProduct(; A=SU2(1//2), B=U1(1), C=τ)
518508
@test space_isequal(
519-
(@inferred s s),
509+
s s,
520510
gradedrange([
521511
SectorProduct(; A=SU2(0), B=U1(2), C=ı) => 1,
522512
SectorProduct(; A=SU2(1), B=U1(2), C=ı) => 1,
@@ -527,7 +517,7 @@ end
527517

528518
s = SectorProduct(; A=τ, B=U1(1), C=ı)
529519
@test space_isequal(
530-
(@inferred s s),
520+
s s,
531521
gradedrange([
532522
SectorProduct(; B=U1(2), A=ı, C=ı) => 1, SectorProduct(; B=U1(2), A=τ, C=ı) => 1
533523
]),
@@ -540,14 +530,14 @@ end
540530
g2 = gradedrange([s2 => 1])
541531
s3 = SectorProduct(; A=U1(1), B=SU2(0), C=Ising("σ"))
542532
s4 = SectorProduct(; A=U1(1), B=SU2(1), C=Ising("σ"))
543-
@test space_isequal((@inferred fusion_product(g1, g2)), gradedrange([s3 => 2, s4 => 2]))
533+
@test space_isequal(fusion_product(g1, g2), gradedrange([s3 => 2, s4 => 2]))
544534

545535
sA = SectorProduct(; A=U1(1))
546536
sB = SectorProduct(; B=SU2(1//2))
547537
sAB = SectorProduct(; A=U1(1), B=SU2(1//2))
548538
gA = gradedrange([sA => 2])
549539
gB = gradedrange([sB => 1])
550-
@test space_isequal((@inferred fusion_product(gA, gB)), gradedrange([sAB => 2]))
540+
@test space_isequal(fusion_product(gA, gB), gradedrange([sAB => 2]))
551541
end
552542
end
553543

0 commit comments

Comments
 (0)