Skip to content

Commit

Permalink
Generalize siteinds constructor (#186)
Browse files Browse the repository at this point in the history
* Generalize siteinds constructor

* Bump to v0.11.12
  • Loading branch information
mtfishman authored May 22, 2024
1 parent 1bd3e44 commit 4efceb8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ITensorNetworks"
uuid = "2919e153-833c-4bdc-8836-1ea460a35fc7"
authors = ["Matthew Fishman <[email protected]>, Joseph Tindall <[email protected]> and contributors"]
version = "0.11.11"
version = "0.11.12"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
25 changes: 16 additions & 9 deletions src/sitetype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,25 @@ function ITensors.siteind(d::Integer, v; addtags="", kwargs...)
return ITensors.addtags(Index(d; tags="Site, $addtags", kwargs...), vertex_tag(v))
end

function ITensors.siteinds(sitetypes::AbstractDictionary, g::AbstractGraph; kwargs...)
is = IndsNetwork(g)
for v in vertices(g)
is[v] = [siteind(sitetypes[v], vertex_tag(v); kwargs...)]
end
return is
to_siteinds_callable(x) = Returns(x)
function to_siteinds_callable(x::AbstractDictionary)
return Base.Fix1(getindex, x) keytype(x)
end

function ITensors.siteinds(x, g::AbstractGraph; kwargs...)
return siteinds(to_siteinds_callable(x), g; kwargs...)
end

function ITensors.siteinds(sitetype, g::AbstractGraph; kwargs...)
return siteinds(Dictionary(vertices(g), fill(sitetype, nv(g))), g; kwargs...)
function to_siteind(x, vertex; kwargs...)
return [siteind(x, vertex_tag(vertex); kwargs...)]
end

to_siteind(x::Index, vertex; kwargs...) = [x]

function ITensors.siteinds(f::Function, g::AbstractGraph; kwargs...)
return siteinds(Dictionary(vertices(g), map(v -> f(v), vertices(g))), g; kwargs...)
is = IndsNetwork(g)
for v in vertices(g)
is[v] = to_siteind(f(v), v; kwargs...)
end
return is
end
12 changes: 11 additions & 1 deletion test/test_sitetype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using DataGraphs: vertex_data
using Dictionaries: Dictionary
using Graphs: nv, vertices
using ITensorNetworks: IndsNetwork, siteinds
using ITensors: SiteType, hastags, space
using ITensors: Index, SiteType, hastags, space
using ITensors.NDTensors: dim
using NamedGraphs.NamedGraphGenerators: named_grid
using Test: @test, @testset
Expand All @@ -18,6 +18,16 @@ using Test: @test, @testset
fdim(v::Tuple) = space(SiteType(ftype(v)))
testtag = "TestTag"

d1 = map(v -> Index(2), vertices(g))
d2 = map(v -> "S=1/2", vertices(g))
for x in (v -> d1[v], d1, v -> d2[v], d2)
s = siteinds(x, g)
@test s[1, 1] isa Vector{<:Index}
@test s[1, 2] isa Vector{<:Index}
@test s[2, 1] isa Vector{<:Index}
@test s[2, 2] isa Vector{<:Index}
end

# uniform string sitetype
s_us = siteinds(sitetypes[1], g; addtags=testtag)
@test s_us isa IndsNetwork
Expand Down

2 comments on commit 4efceb8

@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/107436

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.11.12 -m "<description of version>" 4efceb86dee7b15bcd37f2bb5b171180c305deb3
git push origin v0.11.12

Please sign in to comment.