|
3 | 3 | using BlockArrays: blocklengths
|
4 | 4 | using Strided: Strided, @strided
|
5 | 5 |
|
6 |
| -using TensorAlgebra: BlockedPermutation, permmortar, blockpermute |
| 6 | +using GradedArrays: AbelianStyle, NotAbelianStyle, SymmetryStyle, checkspaces |
| 7 | +using TensorAlgebra: AbstractBlockPermutation, permmortar |
7 | 8 |
|
8 |
| -function naive_permutedims(ft, biperm::BlockedPermutation{2}) |
9 |
| - @assert ndims(ft) == length(biperm) |
10 |
| - |
11 |
| - # naive permute: cast to dense, permutedims, cast to FusionTensor |
12 |
| - arr = Array(ft) |
13 |
| - permuted_arr = permutedims(arr, Tuple(biperm)) |
14 |
| - permuted = to_fusiontensor(permuted_arr, blocks(axes(ft)[biperm])...) |
15 |
| - return permuted |
| 9 | +# permutedims with 1 tuple of 2 separate tuples |
| 10 | +function fusiontensor_permutedims(ft, new_leg_dims::Tuple{Tuple,Tuple}) |
| 11 | + return fusiontensor_permutedims(ft, new_leg_dims...) |
16 | 12 | end
|
17 | 13 |
|
18 |
| -# permutedims with 1 tuple of 2 separate tuples |
19 |
| -function fusiontensor_permutedims(ft, new_leg_indices::Tuple{Tuple,Tuple}) |
20 |
| - return fusiontensor_permutedims(ft, new_leg_indices...) |
| 14 | +function fusiontensor_permutedims!(ftdst, ftsrc, new_leg_dims::Tuple{Tuple,Tuple}) |
| 15 | + return fusiontensor_permutedims!(ftdst, ftsrc, new_leg_dims...) |
21 | 16 | end
|
22 | 17 |
|
23 | 18 | # permutedims with 2 separate tuples
|
24 |
| -function fusiontensor_permutedims( |
25 |
| - ft, new_codomain_indices::Tuple, new_domain_indices::Tuple |
26 |
| -) |
27 |
| - biperm = permmortar((new_codomain_indices, new_domain_indices)) |
| 19 | +function fusiontensor_permutedims(ft, new_codomain_dims::Tuple, new_domain_dims::Tuple) |
| 20 | + biperm = permmortar((new_codomain_dims, new_domain_dims)) |
28 | 21 | return fusiontensor_permutedims(ft, biperm)
|
29 | 22 | end
|
30 | 23 |
|
31 |
| -function fusiontensor_permutedims(ft, biperm::BlockedPermutation{2}) |
| 24 | +function fusiontensor_permutedims!( |
| 25 | + ftdst, ftsrc, new_codomain_dims::Tuple, new_domain_dims::Tuple |
| 26 | +) |
| 27 | + biperm = permmortar((new_codomain_dims, new_domain_dims)) |
| 28 | + return fusiontensor_permutedims!(ftdst, ftsrc, biperm) |
| 29 | +end |
| 30 | + |
| 31 | +# permutedims with BlockedPermutation |
| 32 | +function fusiontensor_permutedims(ft, biperm::AbstractBlockPermutation{2}) |
32 | 33 | ndims(ft) == length(biperm) || throw(ArgumentError("Invalid permutation length"))
|
| 34 | + ftdst = similar(ft, axes(ft)[biperm]) |
| 35 | + fusiontensor_permutedims!(ftdst, ft, biperm) |
| 36 | + return ftdst |
| 37 | +end |
| 38 | + |
| 39 | +function fusiontensor_permutedims!(ftdst, ftsrc, biperm::AbstractBlockPermutation{2}) |
| 40 | + ndims(ftsrc) == length(biperm) || throw(ArgumentError("Invalid permutation length")) |
| 41 | + blocklengths(axes(ftdst)) == blocklengths(biperm) || |
| 42 | + throw(ArgumentError("Destination tensor does not match bipermutation")) |
| 43 | + checkspaces(axes(ftdst), axes(ftsrc)[biperm]) |
33 | 44 |
|
34 |
| - # early return for identity operation. Do not copy. Also handle tricky 0-dim case. |
35 |
| - if ndims_codomain(ft) == first(blocklengths(biperm)) # compile time |
36 |
| - if Tuple(biperm) == ntuple(identity, ndims(ft)) |
37 |
| - return ft |
| 45 | + # early return for identity operation. Also handle tricky 0-dim case. |
| 46 | + if ndims_codomain(ftdst) == ndims_codomain(ftsrc) # compile time |
| 47 | + if Tuple(biperm) == ntuple(identity, ndims(ftdst)) |
| 48 | + copy!(data_matrix(ftdst), data_matrix(ftsrc)) |
| 49 | + return ftdst |
38 | 50 | end
|
39 | 51 | end
|
| 52 | + return permute_data!(SymmetryStyle(ftdst), ftdst, ftsrc, Tuple(biperm)) |
| 53 | +end |
40 | 54 |
|
41 |
| - new_ft = FusionTensor{eltype(ft)}(undef, axes(ft)[biperm]) |
42 |
| - fusiontensor_permutedims!(new_ft, ft, Tuple(biperm)) |
43 |
| - return new_ft |
| 55 | +# =============================== Internal ============================================= |
| 56 | +function permute_data!(::AbelianStyle, ftdst, ftsrc, flatperm) |
| 57 | + # abelian case: all unitary blocks are 1x1 identity matrices |
| 58 | + # compute_unitary is only called to get block positions |
| 59 | + unitary = compute_unitary(ftdst, ftsrc, flatperm) |
| 60 | + for ((old_trees, new_trees), _) in unitary |
| 61 | + new_block = view(ftdst, new_trees...) |
| 62 | + old_block = view(ftsrc, old_trees...) |
| 63 | + @strided new_block .= permutedims(old_block, flatperm) |
| 64 | + end |
| 65 | + return ftdst |
44 | 66 | end
|
45 | 67 |
|
46 |
| -function fusiontensor_permutedims!( |
47 |
| - new_ft::FusionTensor{T,N}, old_ft::FusionTensor{T,N}, flatperm::NTuple{N,Integer} |
48 |
| -) where {T,N} |
49 |
| - foreach(m -> fill!(m, zero(T)), eachstoredblock(data_matrix(new_ft))) |
50 |
| - unitary = compute_unitary(new_ft, old_ft, flatperm) |
| 68 | +function permute_data!(::NotAbelianStyle, ftdst, ftsrc, flatperm) |
| 69 | + foreach(m -> fill!(m, zero(eltype(ftdst))), eachstoredblock(data_matrix(ftdst))) |
| 70 | + unitary = compute_unitary(ftdst, ftsrc, flatperm) |
51 | 71 | for ((old_trees, new_trees), coeff) in unitary
|
52 |
| - new_block = view(new_ft, new_trees...) |
53 |
| - old_block = view(old_ft, old_trees...) |
| 72 | + new_block = view(ftdst, new_trees...) |
| 73 | + old_block = view(ftsrc, old_trees...) |
54 | 74 | @strided new_block .+= coeff .* permutedims(old_block, flatperm)
|
55 | 75 | end
|
| 76 | + return ftdst |
56 | 77 | end
|
0 commit comments