Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize siteinds constructor #186

Merged
merged 2 commits into from
May 22, 2024
Merged
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
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
Loading