From 5f10a83ce675cd8bd016baba7286e2e4a9e1ed09 Mon Sep 17 00:00:00 2001 From: Joey Date: Tue, 9 Apr 2024 14:47:37 -0400 Subject: [PATCH] Improved tags for site indices and construction of site indices from bitmap --- examples/2d_laplace_solver.jl | 2 +- src/ITensorNumericalAnalysis.jl | 8 ++-- src/{bitmaps.jl => bitmap.jl} | 0 ...y_functions.jl => elementary_functions.jl} | 0 ...y_operators.jl => elementary_operators.jl} | 37 ++++++++++++++----- src/itensornetworkfunction.jl | 2 +- src/{itensornetworksutils.jl => utils.jl} | 21 ++++++++++- test/test_bitmaps.jl | 3 +- test/test_itensorfunction.jl | 24 ++++++------ test/test_operators.jl | 11 +++--- 10 files changed, 73 insertions(+), 35 deletions(-) rename src/{bitmaps.jl => bitmap.jl} (100%) rename src/{itensornetworks_elementary_functions.jl => elementary_functions.jl} (100%) rename src/{itensornetworks_elementary_operators.jl => elementary_operators.jl} (83%) rename src/{itensornetworksutils.jl => utils.jl} (59%) diff --git a/examples/2d_laplace_solver.jl b/examples/2d_laplace_solver.jl index 6d7cb2b..4dc7619 100644 --- a/examples/2d_laplace_solver.jl +++ b/examples/2d_laplace_solver.jl @@ -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) diff --git a/src/ITensorNumericalAnalysis.jl b/src/ITensorNumericalAnalysis.jl index f177a53..ad59261 100644 --- a/src/ITensorNumericalAnalysis.jl +++ b/src/ITensorNumericalAnalysis.jl @@ -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, diff --git a/src/bitmaps.jl b/src/bitmap.jl similarity index 100% rename from src/bitmaps.jl rename to src/bitmap.jl diff --git a/src/itensornetworks_elementary_functions.jl b/src/elementary_functions.jl similarity index 100% rename from src/itensornetworks_elementary_functions.jl rename to src/elementary_functions.jl diff --git a/src/itensornetworks_elementary_operators.jl b/src/elementary_operators.jl similarity index 83% rename from src/itensornetworks_elementary_operators.jl rename to src/elementary_operators.jl index b8a5845..b70b615 100644 --- a/src/itensornetworks_elementary_operators.jl +++ b/src/elementary_operators.jl @@ -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 @@ -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 diff --git a/src/itensornetworkfunction.jl b/src/itensornetworkfunction.jl index bf81398..229f983 100644 --- a/src/itensornetworkfunction.jl +++ b/src/itensornetworkfunction.jl @@ -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 diff --git a/src/itensornetworksutils.jl b/src/utils.jl similarity index 59% rename from src/itensornetworksutils.jl rename to src/utils.jl index 05ac429..ebb661d 100644 --- a/src/itensornetworksutils.jl +++ b/src/utils.jl @@ -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) @@ -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 diff --git a/test/test_bitmaps.jl b/test/test_bitmaps.jl index 37ecde0..e6d7d78 100644 --- a/test/test_bitmaps.jl +++ b/test/test_bitmaps.jl @@ -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 diff --git a/test/test_itensorfunction.jl b/test/test_itensorfunction.jl index abd59fe..464a195 100644 --- a/test/test_itensorfunction.jl +++ b/test/test_itensorfunction.jl @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/test/test_operators.jl b/test/test_operators.jl index 0d345c7..a29c2d8 100644 --- a/test/test_operators.jl +++ b/test/test_operators.jl @@ -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 @@ -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) @@ -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)) @@ -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)] @@ -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) @@ -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)] @@ -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)