Skip to content

Commit 4efceb8

Browse files
authored
Generalize siteinds constructor (#186)
* Generalize siteinds constructor * Bump to v0.11.12
1 parent 1bd3e44 commit 4efceb8

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ITensorNetworks"
22
uuid = "2919e153-833c-4bdc-8836-1ea460a35fc7"
33
authors = ["Matthew Fishman <[email protected]>, Joseph Tindall <[email protected]> and contributors"]
4-
version = "0.11.11"
4+
version = "0.11.12"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

src/sitetype.jl

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,25 @@ function ITensors.siteind(d::Integer, v; addtags="", kwargs...)
1111
return ITensors.addtags(Index(d; tags="Site, $addtags", kwargs...), vertex_tag(v))
1212
end
1313

14-
function ITensors.siteinds(sitetypes::AbstractDictionary, g::AbstractGraph; kwargs...)
15-
is = IndsNetwork(g)
16-
for v in vertices(g)
17-
is[v] = [siteind(sitetypes[v], vertex_tag(v); kwargs...)]
18-
end
19-
return is
14+
to_siteinds_callable(x) = Returns(x)
15+
function to_siteinds_callable(x::AbstractDictionary)
16+
return Base.Fix1(getindex, x) keytype(x)
17+
end
18+
19+
function ITensors.siteinds(x, g::AbstractGraph; kwargs...)
20+
return siteinds(to_siteinds_callable(x), g; kwargs...)
2021
end
2122

22-
function ITensors.siteinds(sitetype, g::AbstractGraph; kwargs...)
23-
return siteinds(Dictionary(vertices(g), fill(sitetype, nv(g))), g; kwargs...)
23+
function to_siteind(x, vertex; kwargs...)
24+
return [siteind(x, vertex_tag(vertex); kwargs...)]
2425
end
2526

27+
to_siteind(x::Index, vertex; kwargs...) = [x]
28+
2629
function ITensors.siteinds(f::Function, g::AbstractGraph; kwargs...)
27-
return siteinds(Dictionary(vertices(g), map(v -> f(v), vertices(g))), g; kwargs...)
30+
is = IndsNetwork(g)
31+
for v in vertices(g)
32+
is[v] = to_siteind(f(v), v; kwargs...)
33+
end
34+
return is
2835
end

test/test_sitetype.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ using DataGraphs: vertex_data
33
using Dictionaries: Dictionary
44
using Graphs: nv, vertices
55
using ITensorNetworks: IndsNetwork, siteinds
6-
using ITensors: SiteType, hastags, space
6+
using ITensors: Index, SiteType, hastags, space
77
using ITensors.NDTensors: dim
88
using NamedGraphs.NamedGraphGenerators: named_grid
99
using Test: @test, @testset
@@ -18,6 +18,16 @@ using Test: @test, @testset
1818
fdim(v::Tuple) = space(SiteType(ftype(v)))
1919
testtag = "TestTag"
2020

21+
d1 = map(v -> Index(2), vertices(g))
22+
d2 = map(v -> "S=1/2", vertices(g))
23+
for x in (v -> d1[v], d1, v -> d2[v], d2)
24+
s = siteinds(x, g)
25+
@test s[1, 1] isa Vector{<:Index}
26+
@test s[1, 2] isa Vector{<:Index}
27+
@test s[2, 1] isa Vector{<:Index}
28+
@test s[2, 2] isa Vector{<:Index}
29+
end
30+
2131
# uniform string sitetype
2232
s_us = siteinds(sitetypes[1], g; addtags=testtag)
2333
@test s_us isa IndsNetwork

0 commit comments

Comments
 (0)