Skip to content

Commit

Permalink
Generalize siteinds constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed May 22, 2024
1 parent 1bd3e44 commit cac1619
Show file tree
Hide file tree
Showing 3 changed files with 29 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
26 changes: 17 additions & 9 deletions src/sitetype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,26 @@ 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...)
@show x
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

0 comments on commit cac1619

Please sign in to comment.