-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed support for Vector{ITensor} in contract.jl functions for now
- Loading branch information
Showing
6 changed files
with
191 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
module ITensorsExtensions | ||
using LinearAlgebra: LinearAlgebra, eigen, pinv | ||
using ITensors: | ||
ITensor, | ||
Index, | ||
commonind, | ||
dag, | ||
hasqns, | ||
inds, | ||
isdiag, | ||
itensor, | ||
map_diag, | ||
noncommonind, | ||
noprime, | ||
replaceinds, | ||
space, | ||
sqrt_decomp | ||
using ITensors.NDTensors: | ||
NDTensors, | ||
Block, | ||
Tensor, | ||
blockdim, | ||
blockoffsets, | ||
denseblocks, | ||
diaglength, | ||
getdiagindex, | ||
nzblocks, | ||
setdiagindex!, | ||
svd, | ||
tensor, | ||
DiagBlockSparseTensor, | ||
DenseTensor, | ||
BlockOffsets | ||
using Observers: update!, insert_function! | ||
|
||
function NDTensors.blockoffsets(dense::DenseTensor) | ||
return BlockOffsets{ndims(dense)}([Block(ntuple(Returns(1), ndims(dense)))], [0]) | ||
end | ||
function NDTensors.nzblocks(dense::DenseTensor) | ||
return nzblocks(blockoffsets(dense)) | ||
end | ||
NDTensors.blockdim(ind::Int, ::Block{1}) = ind | ||
NDTensors.blockdim(i::Index{Int}, b::Integer) = blockdim(i, Block(b)) | ||
NDTensors.blockdim(i::Index{Int}, b::Block) = blockdim(space(i), b) | ||
|
||
LinearAlgebra.isdiag(it::ITensor) = isdiag(tensor(it)) | ||
|
||
# Convenience functions | ||
sqrt_diag(it::ITensor) = map_diag(sqrt, it) | ||
inv_diag(it::ITensor) = map_diag(inv, it) | ||
invsqrt_diag(it::ITensor) = map_diag(inv ∘ sqrt, it) | ||
pinv_diag(it::ITensor) = map_diag(pinv, it) | ||
pinvsqrt_diag(it::ITensor) = map_diag(pinv ∘ sqrt, it) | ||
|
||
function map_itensor( | ||
f::Function, A::ITensor, lind=first(inds(A)); regularization=nothing, kwargs... | ||
) | ||
USV = svd(A, lind; kwargs...) | ||
U, S, V, spec, u, v = USV | ||
S = map_diag(s -> f(s + regularization), S) | ||
sqrtDL, δᵤᵥ, sqrtDR = sqrt_decomp(S, u, v) | ||
sqrtDR = denseblocks(sqrtDR) * denseblocks(δᵤᵥ) | ||
L, R = U * sqrtDL, V * sqrtDR | ||
return L * R | ||
end | ||
|
||
# Analagous to `denseblocks`. | ||
# Extract the diagonal entries into a diagonal tensor. | ||
function diagblocks(D::Tensor) | ||
nzblocksD = nzblocks(D) | ||
T = DiagBlockSparseTensor(eltype(D), nzblocksD, inds(D)) | ||
for b in nzblocksD | ||
for n in 1:diaglength(D) | ||
setdiagindex!(T, getdiagindex(D, n), n) | ||
end | ||
end | ||
return T | ||
end | ||
|
||
diagblocks(it::ITensor) = itensor(diagblocks(tensor(it))) | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.