From 73680b6e0ba7419427f4ff50f471c073ed94763e Mon Sep 17 00:00:00 2001 From: Joey Date: Wed, 20 Mar 2024 10:30:30 +0000 Subject: [PATCH] Derivative -> Environment --- src/ITensorNetworks.jl | 2 +- src/caches/beliefpropagationcache.jl | 2 -- src/{derivative.jl => environment.jl} | 15 +++++++++------ src/formnetworks/abstractformnetwork.jl | 12 ++++++------ test/test_forms.jl | 8 ++++---- 5 files changed, 20 insertions(+), 19 deletions(-) rename src/{derivative.jl => environment.jl} (74%) diff --git a/src/ITensorNetworks.jl b/src/ITensorNetworks.jl index 9e81a3e6..536db27c 100644 --- a/src/ITensorNetworks.jl +++ b/src/ITensorNetworks.jl @@ -129,7 +129,7 @@ include(joinpath("treetensornetworks", "solvers", "contract.jl")) include(joinpath("treetensornetworks", "solvers", "linsolve.jl")) include(joinpath("treetensornetworks", "solvers", "tree_sweeping.jl")) include("apply.jl") -include("derivative.jl") +include("environment.jl") include("exports.jl") diff --git a/src/caches/beliefpropagationcache.jl b/src/caches/beliefpropagationcache.jl index ab6204b5..92f761c2 100644 --- a/src/caches/beliefpropagationcache.jl +++ b/src/caches/beliefpropagationcache.jl @@ -11,8 +11,6 @@ end return default_bp_maxiter(undirected_graph(underlying_graph(g))) end default_partitioning(ψ::AbstractITensorNetwork) = group(v -> v, vertices(ψ)) -#We could probably do something cleverer here based on graph partitioning algorithms: https://en.wikipedia.org/wiki/Graph_partition. -default_partitioning(f::AbstractFormNetwork) = group(v -> state_vertex(f, v), vertices(f)) default_cache_update_kwargs(cache) = (; maxiter=20, tol=1e-5) function message_diff(message_a::Vector{ITensor}, message_b::Vector{ITensor}) diff --git a/src/derivative.jl b/src/environment.jl similarity index 74% rename from src/derivative.jl rename to src/environment.jl index 4acbd1a8..7d1c2ef1 100644 --- a/src/derivative.jl +++ b/src/environment.jl @@ -1,12 +1,15 @@ -default_derivative_algorithm() = "exact" +default_environment_algorithm() = "exact" -function derivative( - ψ::AbstractITensorNetwork, vertices::Vector; alg=default_derivative_algorithm(), kwargs... +function environment( + ψ::AbstractITensorNetwork, + vertices::Vector; + alg=default_environment_algorithm(), + kwargs..., ) - return derivative(Algorithm(alg), ψ, vertices; kwargs...) + return environment(Algorithm(alg), ψ, vertices; kwargs...) end -function derivative( +function environment( ::Algorithm"exact", ψ::AbstractITensorNetwork, vertices::Vector; @@ -18,7 +21,7 @@ function derivative( return ITensor[contract(ψ_reduced; sequence, kwargs...)] end -function derivative( +function environment( ::Algorithm"bp", ψ::AbstractITensorNetwork, verts::Vector; diff --git a/src/formnetworks/abstractformnetwork.jl b/src/formnetworks/abstractformnetwork.jl index 66b395ad..07a2a0fd 100644 --- a/src/formnetworks/abstractformnetwork.jl +++ b/src/formnetworks/abstractformnetwork.jl @@ -57,22 +57,22 @@ function operator_network(f::AbstractFormNetwork) ) end -function derivative( +function environment( f::AbstractFormNetwork, state_vertices::Vector; - alg=default_derivative_algorithm(), + alg=default_environment_algorithm(), kwargs..., ) - tn_vertices = derivative_vertices(f, state_vertices) + tn_vertices = environment_vertices(f, state_vertices) if alg == "bp" partitions = group(v -> state_vertex(f, v), vertices(f)) - return derivative(tensornetwork(f), tn_vertices; alg, partitions, kwargs...) + return environment(tensornetwork(f), tn_vertices; alg, partitions, kwargs...) else - return derivative(tensornetwork(f), tn_vertices; alg, kwargs...) + return environment(tensornetwork(f), tn_vertices; alg, kwargs...) end end -function derivative_vertices(f::AbstractFormNetwork, state_vertices::Vector; kwargs...) +function environment_vertices(f::AbstractFormNetwork, state_vertices::Vector; kwargs...) return setdiff( vertices(f), vcat(bra_vertices(f, state_vertices), ket_vertices(f, state_vertices)) ) diff --git a/test/test_forms.jl b/test/test_forms.jl index 63686336..0bfa2d02 100644 --- a/test/test_forms.jl +++ b/test/test_forms.jl @@ -12,7 +12,7 @@ using ITensorNetworks: bra_network, ket_network, operator_network, - derivative, + environment, BeliefPropagationCache using Test using Random @@ -52,16 +52,16 @@ using SplitApplyCombine @test underlying_graph(ket_network(qf)) == underlying_graph(ψket) @test underlying_graph(operator_network(qf)) == underlying_graph(A) - ∂qf_∂v = only(derivative(qf, [v])) + ∂qf_∂v = only(environment(qf, [v])) @test (∂qf_∂v) * (qf[ket_vertex(qf, v)] * qf[bra_vertex(qf, v)]) ≈ contract(qf) - ∂qf_∂v_bp = derivative(qf, [v]; alg="bp", update_cache=false) + ∂qf_∂v_bp = environment(qf, [v]; alg="bp", update_cache=false) ∂qf_∂v_bp = contract(∂qf_∂v_bp) ∂qf_∂v_bp /= norm(∂qf_∂v_bp) ∂qf_∂v /= norm(∂qf_∂v) @test ∂qf_∂v_bp != ∂qf_∂v - ∂qf_∂v_bp = derivative(qf, [v]; alg="bp", update_cache=true) + ∂qf_∂v_bp = environment(qf, [v]; alg="bp", update_cache=true) ∂qf_∂v_bp = contract(∂qf_∂v_bp) ∂qf_∂v_bp /= norm(∂qf_∂v_bp) @test ∂qf_∂v_bp ≈ ∂qf_∂v