Skip to content

Commit

Permalink
[ITensorsNamedDimsArraysExt] Convert symmetric tensors
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Nov 12, 2024
1 parent 4996dca commit e4b647c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion NDTensors/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NDTensors"
uuid = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf"
authors = ["Matthew Fishman <[email protected]>"]
version = "0.3.62"
version = "0.3.63"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down
15 changes: 15 additions & 0 deletions NDTensors/src/lib/GradedAxes/src/gradedunitrangedual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ function blockedunitrange_getindices(
return flip_blockvector(v)
end

# Fixes ambiguity error.
# TODO: Write this in terms of `blockedunitrange_getindices(dual(a), indices)`.
function blockedunitrange_getindices(
a::GradedUnitRangeDual, indices::AbstractBlockVector{<:Block{1}}
)
blks = map(bs -> mortar(map(b -> a[b], bs)), blocks(indices))
# We pass `length.(blks)` to `mortar` in order
# to pass block labels to the axes of the output,
# if they exist. This makes it so that
# `only(axes(a[indices])) isa `GradedUnitRange`
# if `a isa `GradedUnitRange`, for example.
v = mortar(blks, labelled_length.(blks))
return flip_blockvector(v)
end

function flip_blockvector(v::BlockVector)
block_axes = flip.(axes(v))
flipped = mortar(vec.(blocks(v)), block_axes)
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ITensors"
uuid = "9136182c-28ba-11e9-034c-db9fb085ebd5"
authors = ["Matthew Fishman <[email protected]>", "Miles Stoudenmire <[email protected]>"]
version = "0.7.4"
version = "0.7.5"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
24 changes: 19 additions & 5 deletions src/lib/ITensorsNamedDimsArraysExt/src/to_nameddimsarray.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using ..NDTensors: data, inds
using ITensors: ITensor
using ..NDTensors: data, inds

# TODO: Delete this, it is a hack to decide
# if an Index is blocked.
Expand Down Expand Up @@ -34,21 +34,35 @@ function to_nameddimsarray(x::DiagTensor)
return named(DiagonalArray(data(x), size(x)), name.(inds(x)))
end

using ..NDTensors: BlockSparseTensor
using ITensors: ITensors, dir, qn
using ..NDTensors: BlockSparseTensor, array, blockdim, datatype, nblocks, nzblocks
using ..NDTensors.BlockSparseArrays: BlockSparseArray
using ..NDTensors.BlockSparseArrays.BlockArrays: BlockArrays, blockedrange
using ..NDTensors.GradedAxes: dual, gradedrange
using ..NDTensors.TypeParameterAccessors: set_ndims
# TODO: Delete once `BlockSparse` is removed.
function to_nameddimsarray(x::BlockSparseTensor)
blockinds = map(i -> [blockdim(i, b) for b in 1:nblocks(i)], inds(x))
blockinds = map(inds(x)) do i
r = gradedrange([qn(i, b) => blockdim(i, b) for b in 1:nblocks(i)])
if dir(i) == ITensors.In
return dual(r)
end
return r
end
blocktype = set_ndims(datatype(x), ndims(x))
# TODO: Make a simpler constructor:
# BlockSparseArray(blocktype, blockinds)
arraystorage = BlockSparseArray{eltype(x),ndims(x),blocktype}(blockinds)
arraystorage = BlockSparseArray{eltype(x),ndims(x),blocktype}(undef, blockinds)
for b in nzblocks(x)
arraystorage[BlockArrays.Block(Tuple(b)...)] = x[b]
arraystorage[BlockArrays.Block(Int.(Tuple(b))...)] = array(x[b])
end
return named(arraystorage, name.(inds(x)))
end

using ITensors: QN
using ..NDTensors.GradedAxes: GradedAxes
GradedAxes.fuse_labels(l1::QN, l2::QN) = l1 + l2

## TODO: Add this back, define `CombinerArrays` library in NDTensors!
## using ..NDTensors: CombinerTensor, CombinerArray, storage
## # TODO: Delete when we directly use `CombinerArray` as storage.
Expand Down

0 comments on commit e4b647c

Please sign in to comment.