Skip to content

Commit

Permalink
Merge pull request #16 from JoeyT1994/SiteIndexNaming
Browse files Browse the repository at this point in the history
Improved tags for site indices and construction of site indices
  • Loading branch information
JoeyT1994 authored Apr 9, 2024
2 parents d5d9007 + 5f10a83 commit 2d0678c
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 35 deletions.
2 changes: 1 addition & 1 deletion examples/2d_laplace_solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ using UnicodePlots
seed!(1234)
L = 14
g = NamedGraph(SimpleGraph(uniform_tree(L)))
s = siteinds("S=1/2", g)

vertex_to_dimension_map = Dictionary(vertices(g), [(v[1] % 2) + 1 for v in vertices(g)])
vertex_to_bit_map = Dictionary(vertices(g), [ceil(Int64, v[1] * 0.5) for v in vertices(g)])
bit_map = BitMap(vertex_to_bit_map, vertex_to_dimension_map)
s = siteinds(g, bit_map)

ψ_fxy = 0.1 * rand_itn(s, bit_map; link_space=2)
= laplacian_operator(s, bit_map; scale=false)
Expand Down
8 changes: 4 additions & 4 deletions src/ITensorNumericalAnalysis.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module ITensorNumericalAnalysis

include("itensornetworksutils.jl")
include("bitmaps.jl")
include("bitmap.jl")
include("utils.jl")
include("itensornetworkfunction.jl")
include("itensornetworks_elementary_functions.jl")
include("itensornetworks_elementary_operators.jl")
include("elementary_functions.jl")
include("elementary_operators.jl")

export ITensorNetworkFunction, itensornetwork
export BitMap,
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,33 +1,51 @@
using Graphs: is_tree
using NamedGraphs: undirected_graph
using ITensors:
sim,
OpSum,
@OpName_str,
@SiteType_str,
SiteType,
siteinds,
noprime,
op,
truncate,
replaceinds!,
delta,
add!,
prime,
sim,
noprime!,
contract
using ITensorNetworks: IndsNetwork, ITensorNetwork, TreeTensorNetwork, combine_linkinds, ttn

function ITensors.op(::OpName"D+", ::SiteType"Digit", s::Index)
d = dim(s)
o = zeros(d, d)
o[2, 1] = 1
return ITensor(o, s, s')
end
function ITensors.op(::OpName"D-", ::SiteType"Digit", s::Index)
d = dim(s)
o = zeros(d, d)
o[1, 2] = 1
return ITensor(o, s, s')
end

function plus_shift_ttn(
s::IndsNetwork, bit_map; dimension=default_dimension(), boundary_value=[0.0]
)
@assert is_tree(s)
@assert base(bit_map) == 2
ttn_op = OpSum()
dim_vertices = vertices(bit_map, dimension)
L = length(dim_vertices)

string_site = [("S+", vertex(bit_map, dimension, L))]
add!(ttn_op, 1.0, "S+", vertex(bit_map, dimension, L))
string_site = [("D+", vertex(bit_map, dimension, L))]
add!(ttn_op, 1.0, "D+", vertex(bit_map, dimension, L))
for i in L:-1:2
pop!(string_site)
push!(string_site, ("S-", vertex(bit_map, dimension, i)))
push!(string_site, ("S+", vertex(bit_map, dimension, i - 1)))
push!(string_site, ("D-", vertex(bit_map, dimension, i)))
push!(string_site, ("D+", vertex(bit_map, dimension, i - 1)))
add!(ttn_op, 1.0, (string_site...)...)
end

Expand All @@ -36,16 +54,17 @@ end

function minus_shift_ttn(s::IndsNetwork, bit_map; dimension=default_dimension())
@assert is_tree(s)
@assert base(bit_map) == 2
ttn_op = OpSum()
dim_vertices = vertices(bit_map, dimension)
L = length(dim_vertices)

string_site = [("S-", vertex(bit_map, dimension, L))]
add!(ttn_op, 1.0, "S-", vertex(bit_map, dimension, L))
string_site = [("D-", vertex(bit_map, dimension, L))]
add!(ttn_op, 1.0, "D-", vertex(bit_map, dimension, L))
for i in L:-1:2
pop!(string_site)
push!(string_site, ("S+", vertex(bit_map, dimension, i)))
push!(string_site, ("S-", vertex(bit_map, dimension, i - 1)))
push!(string_site, ("D+", vertex(bit_map, dimension, i)))
push!(string_site, ("D-", vertex(bit_map, dimension, i - 1)))
add!(ttn_op, 1.0, (string_site...)...)
end

Expand Down
2 changes: 1 addition & 1 deletion src/itensornetworkfunction.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ITensorNetworks: ITensorNetworks, AbstractITensorNetwork, data_graph
using ITensorNetworks: ITensorNetworks, AbstractITensorNetwork, data_graph, data_graph_type
using ITensors: ITensor, dim, contract, siteinds, onehot
using Graphs: Graphs

Expand Down
21 changes: 19 additions & 2 deletions src/itensornetworksutils.jl → src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ITensors: Index, dim, inds
using ITensorNetworks: random_tensornetwork, IndsNetwork
using Graphs: AbstractGraph
using ITensors: ITensors, Index, dim, inds, siteinds
using ITensorNetworks: random_tensornetwork, IndsNetwork, vertex_tag

"""Build the order L tensor corresponding to fx(x): x ∈ [0,1], default decomposition is binary"""
function build_full_rank_tensor(L::Int64, fx::Function; base::Int64=2)
Expand Down Expand Up @@ -29,3 +30,19 @@ function c_tensor(phys_ind::Index, virt_inds::Vector)

return T
end

"""Tag for a vertex based on its dimension (Dim) and digit (N)"""
function digit_tag(bm::BitMap, v)
dig = digit(bm, v)
dim = dimension(bm, v)
return "N $dig, Dim $dim"
end

"""Generate network of physical indices given a graph and a bitmap"""
function ITensors.siteinds(g::AbstractGraph, bm::BitMap)
is = IndsNetwork(g)
for v in vertices(g)
is[v] = [Index(base(bm), "Digit, $(digit_tag(bm, v)), V$(vertex_tag(v))")]
end
return is
end
3 changes: 2 additions & 1 deletion test/test_bitmaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ using Dictionaries: Dictionary
L = 4

g = named_grid((L, L))
s = siteinds("S=1/2", g)
bit_map = BitMap(g)

s = siteinds(g, bit_map)

@test dimension(bit_map) == 1
@test Set(vertices(bit_map)) == Set(vertices(g))
@test base(bit_map) == 2
Expand Down
24 changes: 12 additions & 12 deletions test/test_itensorfunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ end
@testset "test const" begin
L = 3
g = named_grid((L, L))
s = siteinds("S=1/2", g)
bit_map = BitMap(g)
s = siteinds(g, bit_map)
c = 1.5

bit_map = BitMap(g)
ψ_fx = const_itn(s, bit_map; c)

x = 0.5
Expand All @@ -60,9 +60,9 @@ end
g = named_comb_tree((2, 3))
a = 1.2
k = 0.125
s = siteinds("S=1/2", g)

bit_map = BitMap(g)
s = siteinds(g, bit_map)

x = 0.625
ψ_fx = net_func(s, bit_map; k, a)
Expand All @@ -85,9 +85,9 @@ end
a = 1.2
k = 0.125
b = 3
s = siteinds("S=1", g)

bit_map = BitMap(g; base=b)
s = siteinds(g, bit_map)

x = (5.0 / 9.0)
ψ_fx = net_func(s, bit_map; k, a)
Expand All @@ -103,9 +103,9 @@ end
a = 1.3
k = 0.15
nterms = 50
s = siteinds("S=1/2", g)

bit_map = BitMap(g)
s = siteinds(g, bit_map)

x = 0.625
ψ_fx = tanh_itn(s, bit_map; k, a, nterms)
Expand All @@ -123,11 +123,11 @@ end
seed!(1234 * deg)
g = NamedGraph(SimpleGraph(uniform_tree(L)))
g = rename_vertices(g, Dict(zip(vertices(g), [(v, 1) for v in vertices(g)])))
s = siteinds("S=1/2", g)
bit_map = BitMap(g)
s = siteinds(g, bit_map)

coeffs = [rand(Uniform(-2, 2)) for i in 1:(deg + 1)]

bit_map = BitMap(g)
x = 0.875
ψ_fx = poly_itn(s, bit_map, coeffs)
fx_x = calculate_fx(ψ_fx, x)
Expand All @@ -142,12 +142,13 @@ end
#Constant function but represented in three dimension
@testset "test const" begin
g = named_grid((3, 3))
s = siteinds("S=1/2", g)
c = 1.5

vertex_to_dimension_map = Dictionary(vertices(g), [v[1] for v in vertices(g)])
vertex_to_bit_map = Dictionary(vertices(g), [v[2] for v in vertices(g)])
bit_map = BitMap(vertex_to_bit_map, vertex_to_dimension_map)
s = siteinds(g, bit_map)

c = 1.5

ψ_fxyz = const_itn(s, bit_map; c)

Expand Down Expand Up @@ -179,7 +180,7 @@ end
@testset "test $name" begin
a = 1.2
k = 0.125
s = siteinds("S=1/2", g)
s = siteinds(g, bit_map)

ψ_fx = net_func(s, bit_map; k, a, dimension=1)
ψ_fy = net_func(s, bit_map; k, a, dimension=2)
Expand All @@ -197,11 +198,10 @@ end
a = 1.3
k = 0.15
nterms = 10
s = siteinds("S=1/2", g)

vertex_to_dimension_map = Dictionary(vertices(g), [v[2] for v in vertices(g)])
vertex_to_bit_map = Dictionary(vertices(g), [v[1] for v in vertices(g)])
bit_map = BitMap(vertex_to_bit_map, vertex_to_dimension_map)
s = siteinds(g, bit_map)

x, y = 0.625, 0.875
ψ_fx = tanh_itn(s, bit_map; k, a, nterms, dimension=1)
Expand Down
11 changes: 6 additions & 5 deletions test/test_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ using Dictionaries: Dictionary
@testset "test laplacian in 1D on MPS" begin
g = named_grid((12, 1))
L = nv(g)
s = siteinds("S=1/2", g)
bit_map = BitMap(g)
s = siteinds(g, bit_map)

∇sq = laplacian_operator(s, bit_map; cutoff=1e-10)
@test maxlinkdim(∇sq) == 3
Expand All @@ -30,8 +30,8 @@ end
@testset "test derivative in 1D on tree" begin
g = named_comb_tree((4, 4))
L = nv(g)
s = siteinds("S=1/2", g)
bit_map = BitMap(g)
s = siteinds(g, bit_map)

∂_∂x = derivative_operator(s, bit_map; cutoff=1e-10)

Expand All @@ -48,8 +48,8 @@ end
@testset "test multiplication_operator_in_1D" begin
g = named_comb_tree((4, 4))
L = nv(g)
s = siteinds("S=1/2", g)
bit_map = BitMap(g)
s = siteinds(g, bit_map)

ψ_gx = sin_itn(s, bit_map; k=0.5 * Float64(pi))
ψ_fx = cos_itn(s, bit_map; k=0.25 * Float64(pi))
Expand All @@ -65,7 +65,6 @@ end
@testset "test multiplication_operator_in_2D" begin
L = 10
g = NamedGraph(SimpleGraph(uniform_tree(L)))
s = siteinds("S=1/2", g)

vertex_to_dimension_map = Dictionary(
vertices(g), [(first(v) % 2) + 1 for v in vertices(g)]
Expand All @@ -74,6 +73,7 @@ end
vertices(g), [ceil(Int64, first(v) * 0.5) for v in vertices(g)]
)
bit_map = BitMap(vertex_to_bit_map, vertex_to_dimension_map)
s = siteinds(g, bit_map)

ψ_fx = cos_itn(s, bit_map; k=0.25 * Float64(pi), dimension=1)
ψ_gy = sin_itn(s, bit_map; k=0.5 * Float64(pi), dimension=2)
Expand All @@ -98,7 +98,6 @@ end
@testset "test differentiation_operator_on_3D_function" begin
L = 60
g = named_grid((L, 1))
s = siteinds("S=1/2", g)

vertex_to_dimension_map = Dictionary(
vertices(g), [(first(v) % 3) + 1 for v in vertices(g)]
Expand All @@ -107,6 +106,8 @@ end
vertices(g), [ceil(Int64, first(v) / 3) for v in vertices(g)]
)
bit_map = BitMap(vertex_to_bit_map, vertex_to_dimension_map)
s = siteinds(g, bit_map)

ψ_fx = sin_itn(s, bit_map; k=Float64(pi), dimension=1)
ψ_gy = sin_itn(s, bit_map; k=Float64(pi), dimension=2)
ψ_hz = sinh_itn(s, bit_map; k=Float64(pi), dimension=3)
Expand Down

0 comments on commit 2d0678c

Please sign in to comment.