Skip to content

Commit 64430c5

Browse files
authored
Do not allow duplicate levels in anyonic braid (#503)
* define helper function * apply helper function + docstring clarification * docs clarification * avoid duplicate levels * add tests * fix typo * clarifications on duplicate levels + @ref + error strings * check for duplicate levels only when anyonic * forgot to remove old test * deleted too much * remove docstring sentences [skip ci]
1 parent 08f32dd commit 64430c5

6 files changed

Lines changed: 46 additions & 4 deletions

File tree

‎docs/src/man/indexmanipulations.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ removeunit(::AbstractTensorMap, ::Val{i}) where {i}
6767
These operations reorder indices and/or move them between domain and codomain by applying the transposing or braiding isomorphisms of the underlying category.
6868
They form a hierarchy from most general to most restricted:
6969

70-
- [`braid`](@ref) is the most general: it accepts any permutation and requires a `levels` argument — a tuple of heights, one per index — that determines whether each index crosses over or under the others it has to pass.
70+
- [`braid`](@ref) is the most general: it accepts any permutation and requires a `levels` argument — a tuple of depths, one per index — that determines whether each index crosses over or under the others it has to pass. In particular, duplicate levels are not allowed when the braiding is anyonic (`BraidingStyle(I) isa Anyonic`), and the length of `levels` must match the number of indices in the tensor.
7171
- [`permute`](@ref) is a simpler interface for sector types with a symmetric braiding (`BraidingStyle(I) isa SymmetricBraiding`), where over- and under-crossings are equivalent and `levels` is therefore not needed.
7272
- [`transpose`](@ref) is restricted to *cyclic* permutations (indices do not cross).
7373
- [`repartition`](@ref) only moves the codomain/domain boundary without reordering the indices at all.

‎src/auxiliary/auxiliary.jl‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,19 @@ end
7676
else
7777
_allequal(f, xs) = allequal(f, xs)
7878
end
79+
80+
"""
81+
_check_levels(levels::Tuple) -> Nothing
82+
83+
Check whether all elements of a tuple are distinct, throwing an `ArgumentError` if not.
84+
This is used to validate the `levels` of [`braid`](@ref).
85+
For symmetric braidings, this check is skipped since the levels are irrelevant.
86+
"""
87+
_check_levels(::SymmetricBraiding, levels, s) = nothing
88+
@inline function _check_levels(::BraidingStyle, levels, s)
89+
levels[s] == levels[s + 1] && _throw_ambiguous_levels(levels[s])
90+
return nothing
91+
end
92+
@noinline function _throw_ambiguous_levels(l)
93+
throw(ArgumentError(lazy"ambiguous braid: two indices with equal level $l have to cross"))
94+
end

‎src/fusiontrees/braiding_manipulations.jl‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ braid(f::FusionTree{I, N}, p::IndexTuple{N}, levels::IndexTuple{N}) where {I, N}
217217
function braid(f::FusionTree{I, N}, (p, _)::Index2Tuple{N, 0}, (levels, _)::Index2Tuple{N, 0}) where {I, N}
218218
TupleTools.isperm(p) || throw(ArgumentError(lazy"not a valid permutation: $p"))
219219
@assert FusionStyle(I) isa UniqueFusion
220-
if BraidingStyle(I) isa SymmetricBraiding # this assumes Fsymbols are 1!
220+
braid_style = BraidingStyle(I)
221+
if braid_style isa SymmetricBraiding # this assumes Fsymbols are 1!
221222
coeff = one(sectorscalartype(I))
222223
for i in 1:N
223224
for j in 1:(i - 1)
@@ -236,6 +237,7 @@ function braid(f::FusionTree{I, N}, (p, _)::Index2Tuple{N, 0}, (levels, _)::Inde
236237
T = sectorscalartype(I)
237238
c = one(T)
238239
for s in permutation2swaps(p)
240+
_check_levels(braid_style, levels, s)
239241
inv = levels[s] > levels[s + 1]
240242
f, c′ = artin_braid(f, s; inv)
241243
c *= c′
@@ -316,7 +318,9 @@ end
316318

317319
dst, U = repartition(src, numind(src))
318320

321+
braid_style = BraidingStyle(I)
319322
for s in permutation2swaps(p)
323+
_check_levels(braid_style, levels, s)
320324
inv = levels[s] > levels[s + 1]
321325
dst, U_tmp = artin_braid(dst, s; inv)
322326
U = U_tmp * U

‎src/tensors/indexmanipulations.jl‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,10 @@ end
293293
294294
Compute `tdst = β * tdst + α * braid(tsrc, (p₁, p₂), levels)`, writing the result into `tdst`.
295295
The codomain and domain of `tdst` correspond to the indices in `p₁` and `p₂` of `tsrc` respectively.
296-
Here, `levels` is a tuple of length `numind(tsrc)` that assigns a level or height to the indices of `tsrc`,
296+
Here, `levels` is a tuple of length `numind(tsrc)` that assigns a level or depth to the indices of `tsrc`,
297297
which determines whether they will braid over or under any other index with which they have to change places.
298+
In other words, a smaller value in `levels` means that the corresponding index will be braided over any other index with a higher value.
299+
298300
Optionally specify a `backend` and `allocator` for the underlying array operation.
299301
300302
See also [`braid`](@ref) for creating a new tensor.
@@ -323,8 +325,9 @@ end
323325
324326
Return tensor `tdst` obtained by braiding the indices of `tsrc`.
325327
The codomain and domain of `tdst` correspond to the indices in `p₁` and `p₂` of `tsrc` respectively.
326-
Here, `levels` is a tuple of length `numind(tsrc)` that assigns a level or height to the indices of `tsrc`,
328+
Here, `levels` is a tuple of length `numind(tsrc)` that assigns a level or depth to the indices of `tsrc`,
327329
which determines whether they will braid over or under any other index with which they have to change places.
330+
In other words, a smaller value in `levels` means that the corresponding index will be braided over any other index with a higher value.
328331
329332
If `copy=false`, `tdst` might share data with `tsrc` whenever possible. Otherwise, a copy is always made.
330333
Optionally specify a `backend` and `allocator` for the underlying array operation.

‎test/symmetries/doubletree.jl‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ using TensorKitSectors
174174
end
175175

176176
BraidingStyle(I) isa HasBraiding && @testset "Double fusion tree: permutation and braiding" begin
177+
if !(BraidingStyle(I) isa SymmetricBraiding)
178+
eq = ntuple(Returns(1), N) # potential problematic level 1
179+
err_str = "ambiguous braid: two indices with equal level 1 have to cross"
180+
181+
@test TensorKit.braid(src, (ntuple(identity, N), ntuple(i -> i + N, N)), (eq, eq)) isa Pair # equal levels are fine when the indices never meet
182+
@test_throws ArgumentError(err_str) TensorKit.braid(src, ((2, 1, ntuple(i -> i + 2, N - 2)...), ntuple(i -> i + N, N)), (eq, eq)) # and rejected when they do
183+
end
184+
177185
for n in 0:(2N)
178186
p = (randperm(2 * N)...,)
179187
p1, p2 = p[1:n], p[(n + 1):(2N)]

‎test/tensors/indexmanipulations.jl‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ for V in spacelist
137137
t2 = braid(copy(t'), p, levels)
138138
@test t1 ≈ t2
139139
end
140+
hasbraiding && !symmetricbraiding && @timedtestset "Braid: invalid levels" begin
141+
t = rand(ComplexF64, V1 ⊗ V2 ← V3)
142+
p = ((2, 1), (3,))
143+
dupe_levels = (2, 2, 1) # level 2 is the duplicate
144+
bad_lengths = (1, 2)
145+
dupe_err_str = "ambiguous braid: two indices with equal level 2 have to cross"
146+
len_err_str = "length of levels should be $(numind(t)), got $(length(bad_lengths))"
147+
@test_throws ArgumentError(dupe_err_str) braid(t, p, dupe_levels) # duplicate levels
148+
@test_throws ArgumentError(len_err_str) braid(t, p, bad_lengths) # wrong length
149+
@test_throws ArgumentError(dupe_err_str) braid!(similar(t, permute(space(t), p)), t, p, dupe_levels)
150+
end
140151
end
141152
TensorKit.empty_globalcaches!()
142153
end

0 commit comments

Comments
 (0)