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
41 changes: 27 additions & 14 deletions src/tensors/abstractblocktensor/conversion.jl
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
# Conversion
# ----------
function Base.convert(::Type{TensorMap}, t::AbstractBlockTensorMap)
S = spacetype(t)
N₁, N₂ = numout(t), numin(t)
cod = ProductSpace{S, N₁}(oplus.(codomain(t).spaces))
dom = ProductSpace{S, N₂}(oplus.(domain(t).spaces))
tdst = similar(t, cod ← dom)

issparse(t) && zerovector!(tdst)

function _copy_subblocks!(tdst, tsrc)
S = spacetype(tsrc)
N₁, N₂ = numout(tsrc), numin(tsrc)
for ((f₁, f₂), arr) in subblocks(tdst)
blockax = ntuple(N₁ + N₂) do i
return if i <= N₁
blockedrange(map(Base.Fix2(dim, f₁.uncoupled[i]), space(t, i)))
blockedrange(map(Base.Fix2(dim, f₁.uncoupled[i]), space(tsrc, i)))
else
blockedrange(map(Base.Fix2(dim, f₂.uncoupled[i - N₁]), space(t, i)'))
blockedrange(map(Base.Fix2(dim, f₂.uncoupled[i - N₁]), space(tsrc, i)'))
end
end

for (k, v) in nonzero_pairs(t)
for (k, v) in nonzero_pairs(tsrc)
indices = getindex.(blockax, Block.(Tuple(k)))
arr_slice = arr[indices...]
# need to check for empty since fusion tree pair might not be present
isempty(arr_slice) || copy!(arr_slice, v[f₁, f₂])
end
end
return tdst
end

function Base.convert(::Type{TensorMap}, t::AbstractBlockTensorMap)
S = spacetype(t)
N₁, N₂ = numout(t), numin(t)
cod = ProductSpace{S, N₁}(oplus.(codomain(t).spaces))
dom = ProductSpace{S, N₂}(oplus.(domain(t).spaces))
tdst = TensorMapWithStorageType{scalartype(t), storagetype(t)}(undef, cod, dom)

issparse(t) && zerovector!(tdst)
_copy_subblocks!(tdst, t)
return tdst
end

function Base.convert(::Type{T}, t::AbstractBlockTensorMap) where {T <: TensorMap}
tdst = convert(TensorMap, t)
return convert(T, tdst)
function Base.convert(::Type{TT}, t::AbstractBlockTensorMap) where {TT <: TensorMap}
S = spacetype(t)
N₁, N₂ = numout(t), numin(t)
cod = ProductSpace{S, N₁}(oplus.(codomain(t).spaces))
dom = ProductSpace{S, N₂}(oplus.(domain(t).spaces))
tdst = TT(undef, cod ← dom)
issparse(t) && zerovector!(tdst)

_copy_subblocks!(tdst, t)
return tdst
end

function Base.convert(::Type{TT}, t::AbstractTensorMap) where {TT <: AbstractBlockTensorMap}
Expand Down
11 changes: 11 additions & 0 deletions src/tensors/tensoroperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ function TO.tensoradd_type(TC, A::AdjointBlockTensorMap, pA::Index2Tuple, conjA:
return TO.tensoradd_type(TC, A', adjointtensorindices(A, pA), !conjA)
end

# copy blocks back to CPU/collect them into an array
# seems necessary for GPU-backed BlockTensorMaps but
# maybe not the most efficient approach?
function TO.tensorscalar(t::AbstractBlockTensorMap{T, S, 0, 0}) where {T, S}
Bs = TK.blocks(t)
B_ends = collect.(map(b -> collect.(getfield(b, :blocks)), map(last, Bs)))
inds = findall(!iszero ∘ last, B_ends)
isempty(inds) && return zero(TKscalartype(t))
return only(last(B_ends[only(inds)]))
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
end
function TO.tensorscalar(t::AbstractBlockTensorMap{T, S, 0, 0}) where {T, S}
return prod(TO.tensorscalar, nonzero_values(t))

Do you know if this would work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me try it locally and get back to you


# tensoralloc_contract
# --------------------
for TTA in (:AbstractTensorMap, :AbstractBlockTensorMap), TTB in (:AbstractTensorMap, :AbstractBlockTensorMap)
Expand Down
Loading