Skip to content

Commit

Permalink
[ITensors] Generalize diag_itensor constructors to AbstractVector (#…
Browse files Browse the repository at this point in the history
…1510)

* [ITensors] Generalize diag_itensor constructors to AbstractVector

* [ITensors] Bump to v0.6.15
  • Loading branch information
mtfishman authored Jun 23, 2024
1 parent b00f772 commit 7d5ecf9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
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.6.14"
version = "0.6.15"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
36 changes: 19 additions & 17 deletions src/itensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,14 @@ end

using NDTensors.TypeParameterAccessors: set_eltype, type_parameters, specify_type_parameters
"""
ITensor([ElT::Type, ]A::Array, inds)
ITensor([ElT::Type, ]A::Array, inds::Index...)
ITensor([ElT::Type, ]A::AbstractArray, inds)
ITensor([ElT::Type, ]A::AbstractArray, inds::Index...)
itensor([ElT::Type, ]A::Array, inds)
itensor([ElT::Type, ]A::Array, inds::Index...)
itensor([ElT::Type, ]A::AbstractArray, inds)
itensor([ElT::Type, ]A::AbstractArray, inds::Index...)
Construct an ITensor from an Array `A` and indices `inds`.
The ITensor will be a view of the Array data if possible (if
Construct an ITensor from an AbstractArray `A` and indices `inds`.
The ITensor will be a view of the AbstractArray data if possible (if
no conversion to a different element type is necessary).
If specified, the ITensor will have element type `ElT`.
Expand Down Expand Up @@ -389,13 +389,13 @@ function ITensor(eltype::Type{<:Number}, A::AbstractArray{<:Number}, is...; kwar
end

# For now, it's not well defined to construct an ITensor without indices
# from a non-zero dimensional Array
# from a non-zero dimensional AbstractArray
function ITensor(
as::AliasStyle, eltype::Type{<:Number}, A::AbstractArray{<:Number}; kwargs...
)
if length(A) > 1
error(
"Trying to create an ITensor without any indices from Array $A of dimensions $(size(A)). Cannot construct an ITensor from an Array with more than one element without any indices.",
"Trying to create an ITensor without any indices from $(typeof(A)) $A of dimensions $(size(A)). Cannot construct an ITensor from an $(typeof(A)) with more than one element without any indices.",
)
end
return ITensor(eltype, A[]; kwargs...)
Expand Down Expand Up @@ -448,8 +448,8 @@ diag_itensor(is::Indices) = diag_itensor(Float64, is)
diag_itensor(is...) = diag_itensor(indices(is...))

"""
diag_itensor([ElT::Type, ]v::Vector, inds...)
diagitensor([ElT::Type, ]v::Vector, inds...)
diag_itensor([ElT::Type, ]v::AbstractVector, inds...)
diagitensor([ElT::Type, ]v::AbstractVector, inds...)
Make a sparse ITensor with non-zero elements only along the diagonal.
In general, the diagonal elements will be those stored in `v` and
Expand All @@ -467,29 +467,31 @@ The version `diagitensor` might output an ITensor whose storage data
is an alias of the input vector data in order to minimize operations.
"""
function diag_itensor(
as::AliasStyle, eltype::Type{<:Number}, v::Vector{<:Number}, is::Indices
as::AliasStyle, eltype::Type{<:Number}, v::AbstractVector{<:Number}, is::Indices
)
length(v) mindim(is) && error(
"Length of vector for diagonal must equal minimum of the dimension of the input indices",
)
data = Vector{eltype}(as, v)
data = set_eltype(typeof(v), eltype)(as, v)
return itensor(Diag(data), is)
end

function diag_itensor(as::AliasStyle, eltype::Type{<:Number}, v::Vector{<:Number}, is...)
function diag_itensor(
as::AliasStyle, eltype::Type{<:Number}, v::AbstractVector{<:Number}, is...
)
return diag_itensor(as, eltype, v, indices(is...))
end

function diag_itensor(as::AliasStyle, v::Vector, is...)
function diag_itensor(as::AliasStyle, v::AbstractVector, is...)
return diag_itensor(as, eltype(v), v, is...)
end

function diag_itensor(as::AliasStyle, v::Vector{<:RealOrComplex{Int}}, is...)
function diag_itensor(as::AliasStyle, v::AbstractVector{<:RealOrComplex{Int}}, is...)
return diag_itensor(AllowAlias(), float(eltype(v)), v, is...)
end

diag_itensor(v::Vector{<:Number}, is...) = diag_itensor(NeverAlias(), v, is...)
function diag_itensor(eltype::Type{<:Number}, v::Vector{<:Number}, is...)
diag_itensor(v::AbstractVector{<:Number}, is...) = diag_itensor(NeverAlias(), v, is...)
function diag_itensor(eltype::Type{<:Number}, v::AbstractVector{<:Number}, is...)
return diag_itensor(NeverAlias(), eltype, v, is...)
end

Expand Down

2 comments on commit 7d5ecf9

@mtfishman
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/109610

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.15 -m "<description of version>" 7d5ecf98f0f47167b3a07d8106e5c89511021a7f
git push origin v0.6.15

Please sign in to comment.