Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "BlockTensorKit"
uuid = "5f87ffc2-9cf1-4a46-8172-465d160bd8cd"
version = "0.3.23"
version = "0.3.24"
authors = ["Lukas Devos <ldevos98@gmail.com> and contributors"]

[deps]
Expand Down Expand Up @@ -36,7 +36,7 @@ MatrixAlgebraKit = "0.6"
Random = "1"
SafeTestsets = "0.1"
Strided = "2.3.3"
TensorKit = "0.17.2"
TensorKit = "0.17.4"
TensorOperations = "5"
Test = "1"
TestExtras = "0.2, 0.3"
Expand Down
41 changes: 41 additions & 0 deletions src/tensors/tensoroperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,47 @@ function TO.tensoradd!(
return C
end

# permute_twist!
# --------------
# unwrap an adjoint source as TensorKit does, and permute/twist blockwise in a single pass
function TK.permute_twist!(
tdst::AbstractBlockTensorMap, tsrc::BlockTensorOrAdjoint, p::Index2Tuple,
twistmask, backend, allocator
)
@boundscheck TK.spacecheck_transform(TK.permute, space(tdst), space(tsrc), p)
tsrc′, p′, _, conjsrc = TK._unwrap_source(tsrc, p, nothing, false)
return _permute_twist_blocks!(tdst, tsrc′, p′, conjsrc, twistmask, backend, allocator)
end
function _permute_twist_blocks!(
tdst::BlockTensorMap, tsrc::BlockTensorMap, p::Index2Tuple, conjsrc::Bool,
twistmask, backend, allocator
)
Cdata = parent(tdst)
Adata = permutedims(StridedView(parent(tsrc)), (p[1]..., p[2]...))
for I in eachindex(Cdata, Adata)
Cdata[I] = _permute_twist_block!(Cdata[I], Adata[I], p, conjsrc, twistmask, backend, allocator)
end
return tdst
end
function _permute_twist_blocks!(
tdst::AbstractBlockTensorMap, tsrc::AbstractBlockTensorMap, p::Index2Tuple, conjsrc::Bool,
twistmask, backend, allocator
)
p_lin = (p[1]..., p[2]...)
# set blocks of tdst that permuting tsrc doesn't touch to zero
_scale_untouched!(tdst, tsrc, Zero(), p_lin)
for (I, v) in nonzero_pairs(tsrc)
I′ = CartesianIndex(TT.getindices(I.I, p_lin))
_addblock!(tdst, I′, Zero(), allocator) do c, _
_permute_twist_block!(c, v, p, conjsrc, twistmask, backend, allocator)
end
end
return tdst
end
function _permute_twist_block!(c, v, p, conjsrc, twistmask, backend, allocator)
return TK._braid!(c, v, p, conjsrc, allind(v), twistmask, One(), Zero(), backend, allocator)
end

function TO.tensoradd_type(
TC, A::AbstractBlockTensorMap, ::Index2Tuple{N₁, N₂}, ::Bool
) where {N₁, N₂}
Expand Down
19 changes: 19 additions & 0 deletions test/linalg/tensoroperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,25 @@ end
end
end

# testing `permute_twist!` in `blas_contract!` since contracted z is in A's codomain
# tested for a selfdual ℝ^d and fermionic space where permutes also twist
@testset "tensorcontract with permuted operands ($(spacetype(a)))" for (a, b, c) in (
(SumSpace(ℝ^2, ℝ^1), SumSpace(ℝ^1, ℝ^2, ℝ^1), SumSpace(ℝ^2, ℝ^3)),
(
SumSpace(Vect[FermionParity](0 => 1, 1 => 1), Vect[FermionParity](0 => 2)),
SumSpace(Vect[FermionParity](1 => 2), Vect[FermionParity](0 => 1, 1 => 1)),
SumSpace(Vect[FermionParity](0 => 1), Vect[FermionParity](1 => 1)),
),
)
for Asparse in (false, true), Bsparse in (false, true)
A = Asparse ? sprand(Float64, a ⊗ b ← c, 0.5) : randn(Float64, a ⊗ b ← c)
B = Bsparse ? sprand(Float64, c ⊗ b' ← a, 0.5) : randn(Float64, c ⊗ b' ← a)
@tensor C1[x; y] := A[x z; w] * B[w z; y]
@tensor C2[x; y] := convert(TensorMap, A)[x z; w] * convert(TensorMap, B)[w z; y]
@test convert(TensorMap, C1) ≈ C2
end
end

##

# temporaries are `PtrArray`s, such that converting them into other storage is detected
Expand Down
Loading