From 3d9fddd764477d4dc06b825f57e28a09febcb22f Mon Sep 17 00:00:00 2001 From: Bartek Date: Sun, 29 Oct 2023 11:24:02 +0000 Subject: [PATCH] clean up --- src/contractor.jl | 63 +++++++----- src/search.jl | 171 ++++++++++++++++---------------- src/square_cross_double_node.jl | 12 +-- src/square_cross_single_node.jl | 10 +- src/square_double_node.jl | 37 +++---- src/tensors.jl | 13 +-- 6 files changed, 144 insertions(+), 162 deletions(-) diff --git a/src/contractor.jl b/src/contractor.jl index 2baa1afb..a87cf5e3 100644 --- a/src/contractor.jl +++ b/src/contractor.jl @@ -52,7 +52,7 @@ A struct representing different layers of a Matrix Product Operator (MPO) used i - `main::Dict{Site, Sites}`: A dictionary mapping sites to the main layers of the MPO. - `dress::Dict{Site, Sites}`: A dictionary mapping sites to the dress layers of the MPO. - `right::Dict{Site, Sites}`: A dictionary mapping sites to the right layers of the MPO. - + The `MpoLayers` struct distinguishes the various layers of an MPO, which is often used in tensor network contraction algorithms. MPOs are commonly employed in quantum many-body physics and condensed matter physics to represent operators acting on quantum states in a factorized form. """ struct MpoLayers @@ -75,7 +75,7 @@ A struct representing control parameters for the MPO-MPS (Matrix Product Operato - `iters_var::Int`: The number of iterations for variational optimization. - `Dtemp_multiplier::Int`: A multiplier for the bond dimension when temporary bond dimensions are computed. - `method::Symbol`: The contraction method to use (e.g., `:psvd_sparse`). - + The `MpsParameters` struct encapsulates various control parameters that influence the behavior and accuracy of the MPO-MPS contraction scheme used for PEPS network calculations. """ struct MpsParameters @@ -87,7 +87,17 @@ struct MpsParameters iters_var::Int Dtemp_multiplier::Int method::Symbol - MpsParameters(bd=typemax(Int), ϵ=1E-8, sw=4, ts=1E-16, is=1, iv=1, dm=2, m=:psvd_sparse) = new(bd, ϵ, sw, ts, is, iv, dm, m) # TODO: split the line + + MpsParameters( + bd = typemax(Int), + ϵ = 1E-8, + sw = 4, + ts = 1E-16, + is = 1, + iv = 1, + dm = 2, + m = :psvd_sparse + ) = new(bd, ϵ, sw, ts, is, iv, dm, m) end """ @@ -96,11 +106,11 @@ A function that provides the layout used to construct the PEPS (Projected Entang # Arguments - `net::PEPSNetwork{T, S}`: The PEPS network for which the layout is provided. - + # Returns - The layout type `T` used to construct the PEPS network. - -The `layout` function returns the layout type used in the construction of a PEPS network. This layout type specifies the geometric arrangement and sparsity pattern of the tensors in the PEPS network. + +The `layout` function returns the layout type used in the construction of a PEPS network. This layout type specifies the geometric arrangement and sparsity pattern of the tensors in the PEPS network. """ layout(net::PEPSNetwork{T, S}) where {T, S} = T @@ -110,10 +120,10 @@ A function that provides the sparsity used to construct the PEPS (Projected Enta # Arguments - `net::PEPSNetwork{T, S}`: The PEPS network for which the sparsity is provided. - + # Returns - The sparsity type `S` used to construct the PEPS network. - + The `sparsity` function returns the sparsity type used in the construction of a PEPS network. This sparsity type specifies the pattern of zero elements in the tensors of the PEPS network, which can affect the computational efficiency and properties of the network. """ sparsity(net::PEPSNetwork{T, S}) where {T, S} = S @@ -134,8 +144,8 @@ A mutable struct representing a contractor for contracting a PEPS (Projected Ent - `node_search_index::Dict{Node, Int}`: A mapping of nodes to their search indices. - `current_node::Node`: The current node being processed during contraction. - `onGPU::Bool`: A flag indicating whether the contraction is performed on a GPU. - -The `MpsContractor` struct defines the contractor responsible for contracting a PEPS network using the MPO-MPS scheme. + +The `MpsContractor` struct defines the contractor responsible for contracting a PEPS network using the MPO-MPS scheme. It encapsulates various components and settings required for the contraction process. """ mutable struct MpsContractor{T <: AbstractStrategy, R <: AbstractGauge} <: AbstractContractor @@ -357,7 +367,7 @@ This function constructs the top Matrix Product State (MPS) using the Zipper (tr W = transpose(mpo(ctr, ctr.layers.main, i, indβ)) canonise!(ψ, :left) - ψ0 = zipper(W, ψ; method=method, Dcut=Dcut, tol=tolS, iters_svd=iters_svd, + ψ0 = zipper(W, ψ; method=method, Dcut=Dcut, tol=tolS, iters_svd=iters_svd, iters_var=iters_var, Dtemp_multiplier = Dtemp_multiplier) canonise!(ψ0, :left) variational_compress!(ψ0, W, ψ, tolV, max_sweeps) @@ -396,7 +406,7 @@ This function constructs the (bottom) Matrix Product State (MPS) using the Zippe ψ = mps(ctr, i+1, indβ) W = mpo(ctr, ctr.layers.main, i, indβ) canonise!(ψ, :left) - ψ0 = zipper(W, ψ; method=method, Dcut=Dcut, tol=tolS, iters_svd=iters_svd, + ψ0 = zipper(W, ψ; method=method, Dcut=Dcut, tol=tolS, iters_svd=iters_svd, iters_var=iters_var, Dtemp_multiplier=Dtemp_multiplier) canonise!(ψ0, :left) variational_compress!(ψ0, W, ψ, tolV, max_sweeps) @@ -633,8 +643,8 @@ end $(TYPEDSIGNATURES) Clear all memoization caches used by the PEPS network contraction. -This function clears all memoization caches that store previously computed results for various operations and environments in the PEPS network contraction. -Memoization is used to optimize the contraction process by avoiding redundant computations. +This function clears all memoization caches that store previously computed results for various operations and environments in the PEPS network contraction. +Memoization is used to optimize the contraction process by avoiding redundant computations. Calling this function removes all cached results, which can be useful when you want to free up memory or ensure that the caches are refreshed with updated data. """ function clear_memoize_cache() @@ -645,8 +655,8 @@ end $(TYPEDSIGNATURES) Clear memoization caches for specific operations after processing a row. -This function clears the memoization caches for specific operations used in the PEPS network contraction after processing a row. -The cleared operations include `left_env`, `right_env`, `mpo`, and `dressed_mps`. Memoization is used to optimize the contraction process by avoiding redundant computations. +This function clears the memoization caches for specific operations used in the PEPS network contraction after processing a row. +The cleared operations include `left_env`, `right_env`, `mpo`, and `dressed_mps`. Memoization is used to optimize the contraction process by avoiding redundant computations. Calling this function allows you to clear the caches for these specific operations, which can be useful when you want to free up memory or ensure that the caches are refreshed with updated data after processing a row in the contraction. """ function clear_memoize_cache_after_row() @@ -657,16 +667,16 @@ end $(TYPEDSIGNATURES) Clear memoization cache for specific operations for a given row and index beta. -This function clears the memoization cache for specific operations used in the PEPS network contraction for a given row and index beta (indβ). -The cleared operations include `mps_top`, `mps`, `mpo`, `dressed_mps`, and related operations. -Memoization is used to optimize the contraction process by avoiding redundant computations. +This function clears the memoization cache for specific operations used in the PEPS network contraction for a given row and index beta (indβ). +The cleared operations include `mps_top`, `mps`, `mpo`, `dressed_mps`, and related operations. +Memoization is used to optimize the contraction process by avoiding redundant computations. Calling this function allows you to clear the cache for these specific operations for a particular row and index beta, which can be useful when you want to free up memory or ensure that the cache is refreshed with updated data for a specific computation. - + # Arguments - `ctr::MpsContractor{T, S}`: The PEPS network contractor object. - `row::Site`: The row for which the cache should be cleared. - `indβ::Int`: The index beta for which the cache should be cleared. - + """ function clear_memoize_cache(ctr::MpsContractor{T, S}, row::Site, indβ::Int) where {T, S} for ind ∈ 1:indβ # indbeta a vector? @@ -711,7 +721,7 @@ function sweep_gauges!( ψ_top = deepcopy(ψ_top) ψ_bot = deepcopy(ψ_bot) - + onGPU = ψ_top.onGPU && ψ_bot.onGPU gauges = optimize_gauges_for_overlaps!!(ψ_top, ψ_bot, tol, max_sweeps) @@ -861,8 +871,8 @@ $(TYPEDSIGNATURES) boundary index formed from outer product of two projectors """ function boundary_indices( - ctr::MpsContractor{T}, - nodes::Union{NTuple{4, S}, Tuple{S, NTuple{2, S}, S, NTuple{2, S}}}, + ctr::MpsContractor{T}, + nodes::Union{NTuple{4, S}, Tuple{S, NTuple{2, S}, S, NTuple{2, S}}}, states::Vector{Vector{Int}} ) where {S, T} v, w, k, l = nodes @@ -872,10 +882,9 @@ function boundary_indices( (j .- 1) .* maximum(pv) .+ i end - function boundary_indices( - ctr::MpsContractor{T}, - nodes::Tuple{S, NTuple{2, S}, S, NTuple{2, S}, S, NTuple{2, S}, S, NTuple{2, S}}, + ctr::MpsContractor{T}, + nodes::Tuple{S, NTuple{2, S}, S, NTuple{2, S}, S, NTuple{2, S}, S, NTuple{2, S}}, states::Vector{Vector{Int}} ) where {S, T} v1, v2, v3, v4, v5, v6, v7, v8 = nodes diff --git a/src/search.jl b/src/search.jl index 4aa79f41..814f83e3 100644 --- a/src/search.jl +++ b/src/search.jl @@ -21,13 +21,13 @@ export $(TYPEDSIGNATURES) A struct representing search parameters for low-energy spectrum search. -## Fields +## Fields - `max_states::Int`: The maximum number of states to be considered during the search. Default is 1, indicating a single state search. - `cut_off_prob::Real`: The cutoff probability for terminating the search. Default is 0.0, meaning no cutoff based on probability. - -SearchParameters encapsulates parameters that control the behavior of low-energy spectrum search algorithms in the SpinGlassPEPS package. + +SearchParameters encapsulates parameters that control the behavior of low-energy spectrum search algorithms in the SpinGlassPEPS package. Users can customize these parameters to adjust the search strategy and resource usage according to their specific needs. - + """ struct SearchParameters max_states::Int @@ -47,51 +47,56 @@ Base.copy(s::NoDroplets) = s A data structure representing the properties and criteria for single-layer droplets in the context of the SpinGlassPEPS package. A `SingleLayerDroplets` object is used to specify the maximum energy, minimum size, and metric for single-layer droplets in the SpinGlassPEPS system. - + ## Fields - `max_energy::Real`: The maximum allowed excitation energy for single-layer droplets. It is typically a real number. - `min_size::Int`: The minimum size (number of sites) required for a single-layer droplet to be considered significant. -- `metric::Symbol`: The metric used to evaluate the significance of a single-layer droplet. +- `metric::Symbol`: The metric used to evaluate the significance of a single-layer droplet. This can be `:no_metric` or other custom metrics defined in the package. - + ## Constructors -- `SingleLayerDroplets(max_energy::Real = 1.0, min_size::Int = 1, metric::Symbol = :no_metric)`: Creates a new +- `SingleLayerDroplets(max_energy::Real = 1.0, min_size::Int = 1, metric::Symbol = :no_metric)`: Creates a new `SingleLayerDroplets` object with the specified maximum energy, minimum size, and metric. - + """ struct SingleLayerDroplets - max_energy :: Real - min_size :: Int - metric :: Symbol - SingleLayerDroplets(max_energy = 1.0, min_size = 1, metric = :no_metric) = new(max_energy, min_size, metric) + max_energy::Real + min_size::Int + metric::Symbol + + SingleLayerDroplets( + max_energy = 1.0, + min_size = 1, + metric = :no_metric + ) = new(max_energy, min_size, metric) end """ A data structure representing a set of flips or changes in states for nodes in the SpinGlassPEPS package. A `Flip` object contains information about the support, state changes, and spinxor values for a set of node flips in the SpinGlassPEPS system. - + ## Fields - `support::Vector{Int}`: An array of integers representing the indices of nodes where flips occur. - `state::Vector{Int}`: An array of integers representing the new states for the nodes in the `support`. - `spinxor::Vector{Int}`: An array of integers representing the spin-xor values for the nodes in the `support`. - -## Constructors -- `Flip(support::Vector{Int}, state::Vector{Int}, spinxor::Vector{Int})`: + +## Constructors +- `Flip(support::Vector{Int}, state::Vector{Int}, spinxor::Vector{Int})`: Creates a new `Flip` object with the specified support, state changes, and spinxor values. - + """ struct Flip - support :: Vector{Int} - state :: Vector{Int} - spinxor :: Vector{Int} + support::Vector{Int} + state::Vector{Int} + spinxor::Vector{Int} end """ $(TYPEDSIGNATURES) A data structure representing a droplet in the context of the SpinGlassPEPS package. -A `Droplet` represents an excitation in the SpinGlassPEPS system. It contains information about the excitation energy, -the site where the droplet starts, the site where it ends, the states of nodes flipped by the droplet, +A `Droplet` represents an excitation in the SpinGlassPEPS system. It contains information about the excitation energy, +the site where the droplet starts, the site where it ends, the states of nodes flipped by the droplet, and any sub-droplets on top of the current droplet. ## Fields @@ -99,17 +104,17 @@ and any sub-droplets on top of the current droplet. - `first::Int`: The site index where the droplet starts. - `last::Int`: The site index where the droplet ends. - `flip::Flip`: The states of nodes flipped by the droplet, often represented using a `Flip` type. -- `droplets::Union{NoDroplets, Vector{Droplet}}`: A field that can be either `NoDroplets()` if there are no sub-droplets -on top of the current droplet or a vector of `Droplet` objects representing sub-droplets. +- `droplets::Union{NoDroplets, Vector{Droplet}}`: A field that can be either `NoDroplets()` if there are no sub-droplets +on top of the current droplet or a vector of `Droplet` objects representing sub-droplets. This field may be used to build a hierarchy of droplets in more complex excitations. """ mutable struct Droplet - denergy :: Real # excitation energy - first :: Int # site where droplet starts - last :: Int - flip :: Flip # states of nodes flipped by droplet - droplets :: Union{NoDroplets, Vector{Droplet}} # subdroplets on top of the current droplet; can be empty + denergy::Real # excitation energy + first::Int # site where droplet starts + last::Int + flip::Flip # states of nodes flipped by droplet + droplets::Union{NoDroplets, Vector{Droplet}} # subdroplets on top of the current droplet; can be empty end Droplets = Union{NoDroplets, Vector{Droplet}} # Can't be defined before Droplet struct @@ -128,10 +133,10 @@ A struct representing a solution obtained from a low-energy spectrum search. - `largest_discarded_probability::Real`: The probability of the largest discarded state. - `droplets::Vector{Droplets}`: A vector of droplets associated with each state. - `spins::Vector{Vector{Int}}`: The spin configurations corresponding to each state. - -The `Solution` struct holds information about the results of a low-energy spectrum search, including the energy levels, -state configurations, probabilities, degeneracy, and additional details such as droplets and spin configurations. -Users can access this information to analyze and interpret the search results. + +The `Solution` struct holds information about the results of a low-energy spectrum search, including the energy levels, +state configurations, probabilities, degeneracy, and additional details such as droplets and spin configurations. +Users can access this information to analyze and interpret the search results. """ struct Solution energies::Vector{<:Real} @@ -162,14 +167,14 @@ An empty `Solution` object with default field values, ready to store search resu $(TYPEDSIGNATURES) Create a new `Solution` object by selecting specific states from an existing `Solution`. -This constructor allows you to create a new `Solution` object by selecting specific states from an existing `Solution`. -It copies the energies, states, probabilities, degeneracy, droplets, and spins information for the selected states +This constructor allows you to create a new `Solution` object by selecting specific states from an existing `Solution`. +It copies the energies, states, probabilities, degeneracy, droplets, and spins information for the selected states while allowing you to set a custom `largest_discarded_probability`. ## Arguments - `sol::Solution`: The original `Solution` object from which states are selected. - `idx::Vector{Int}`: A vector of indices specifying the states to be selected from the original `Solution`. -- `ldp::Real=sol.largest_discarded_probability`: (Optional) The largest discarded probability for the new `Solution`. +- `ldp::Real=sol.largest_discarded_probability`: (Optional) The largest discarded probability for the new `Solution`. By default, it is set to the largest discarded probability of the original `Solution`. ## Returns @@ -193,15 +198,15 @@ end $(TYPEDSIGNATURES) Calculates the energy contribution of a branch given a base energy and a spin configuration. -This function calculates the energy contribution of a branch in a SpinGlassPEPS calculation. -It takes a `MpsContractor` object `ctr` and a tuple `eσ` containing a base energy as the first element -and a spin configuration represented as a vector of integers as the second element. -The function calculates the branch energy by adding the base energy to the energy contribution +This function calculates the energy contribution of a branch in a SpinGlassPEPS calculation. +It takes a `MpsContractor` object `ctr` and a tuple `eσ` containing a base energy as the first element +and a spin configuration represented as a vector of integers as the second element. +The function calculates the branch energy by adding the base energy to the energy contribution of the given spin configuration obtained from the `update_energy` function. ## Arguments - `ctr::MpsContractor{T}`: An instance of the `MpsContractor` type parameterized by the strategy type `T`. -- `eσ::Tuple{<:Real, Vector{Int}}`: A tuple containing the base energy as the first element (a real number) +- `eσ::Tuple{<:Real, Vector{Int}}`: A tuple containing the base energy as the first element (a real number) and the spin configuration as the second element (a vector of integers). ## Returns @@ -280,22 +285,22 @@ end # $(TYPEDSIGNATURES) # """ (method::NoDroplets)( - ctr::MpsContractor{T}, - best_idx::Int, - energies::Vector{<:Real}, - states::Vector{Vector{Int}}, - droplets::Vector{Droplets}, + ctr::MpsContractor{T}, + best_idx::Int, + energies::Vector{<:Real}, + states::Vector{Vector{Int}}, + droplets::Vector{Droplets}, spins::Vector{Vector{Int}} - ) where T= NoDroplets() +) where T = NoDroplets() function (method::SingleLayerDroplets)( - ctr::MpsContractor{T}, - best_idx::Int, - energies::Vector{<:Real}, - states::Vector{Vector{Int}}, - droplets::Vector{Droplets}, + ctr::MpsContractor{T}, + best_idx::Int, + energies::Vector{<:Real}, + states::Vector{Vector{Int}}, + droplets::Vector{Droplets}, spins::Vector{Vector{Int}} - ) where T +) where T ndroplets = copy(droplets[best_idx]) bstate = states[best_idx] benergy = energies[best_idx] @@ -332,7 +337,7 @@ function filter_droplets(all_droplets::Vector{Droplet}, method::SingleLayerDropl else #method.metric == :no_metric cutoff = -Inf end - + filtered_droplets = Droplet[] for droplet in sorted_droplets should_push = true @@ -346,7 +351,6 @@ function filter_droplets(all_droplets::Vector{Droplet}, method::SingleLayerDropl push!(filtered_droplets, droplet) end end - filtered_droplets end @@ -361,15 +365,13 @@ end function diversity_metric(drop1::Droplet, drop2::Droplet, metric::Symbol) if metric == :hamming d = hamming_distance(drop1.flip, drop2.flip) - else + else d = Inf end d end -function hamming_distance(flip::Flip) - sum(count_ones(st) for st ∈ flip.spinxor) -end +hamming_distance(flip::Flip) = sum(count_ones(st) for st ∈ flip.spinxor) function hamming_distance(flip1::Flip, flip2::Flip) n1, n2, hd = 1, 1, 0 @@ -396,7 +398,7 @@ function hamming_distance(flip1::Flip, flip2::Flip) while n2 <= l2 hd += count_ones(flip2.spinxor[n2]) n2 += 1 - end + end hd end @@ -455,7 +457,6 @@ function merge_droplets(method::SingleLayerDroplets, droplet::Droplet, subdrople Droplet(denergy, first, last, flip, NoDroplets()) end - function flip_state(state::Vector{Int}, flip::Flip) new_state = copy(state) new_state[flip.support] .= flip.state @@ -490,23 +491,22 @@ function unpack_droplets(sol, β) # have β in sol ? inds = sortperm(energies) Solution( - energies[inds], - states[inds], - probs[inds], - degeneracy[inds], - sol.largest_discarded_probability, - droplets[inds], + energies[inds], + states[inds], + probs[inds], + degeneracy[inds], + sol.largest_discarded_probability, + droplets[inds], spins[inds] - ) + ) end - """ $(TYPEDSIGNATURES) Merge branches of a contractor based on specified merge type and droplet update strategy. -This function merges branches of a contractor (`ctr`) based on a specified merge type (`merge_type`) -and an optional droplet update strategy (`update_droplets`). +This function merges branches of a contractor (`ctr`) based on a specified merge type (`merge_type`) +and an optional droplet update strategy (`update_droplets`). It returns a function `_merge` that can be used to merge branches in a solution. ## Arguments @@ -583,7 +583,6 @@ function merge_branches(ctr::MpsContractor{T}, merge_type::Symbol=:nofit, update _merge end - function merge_branches_blur(ctr::MpsContractor{T}, hamming_cutoff::Int, merge_type::Symbol=:nofit, update_droplets=NoDroplets()) where {T} function _merge_blur(psol::Solution) psol = merge_branches(ctr, merge_type, update_droplets)(psol) @@ -613,7 +612,7 @@ hamming_distance(state1, state2) = sum(state1 .!== state2) $(TYPEDSIGNATURES) No-op merge function that returns the input `partial_sol` as is. -This function is a no-op merge function that takes a `Solution` object `partial_sol` as input and returns it unchanged. +This function is a no-op merge function that takes a `Solution` object `partial_sol` as input and returns it unchanged. It is used as a merge strategy when you do not want to perform any merging of branches in a solution. ## Arguments @@ -628,19 +627,19 @@ no_merge(partial_sol::Solution) = partial_sol $(TYPEDSIGNATURES) Bound the solution to a specified number of states while discarding low-probability states. -This function takes a `Solution` object `psol`, bounds it to a specified number of states `max_states`, -and discards low-probability states based on the probability threshold `δprob`. +This function takes a `Solution` object `psol`, bounds it to a specified number of states `max_states`, +and discards low-probability states based on the probability threshold `δprob`. You can specify a `merge_strategy` for merging branches in the `psol` object. - + ## Arguments - `psol::Solution`: A `Solution` object representing the solution to be bounded. - `max_states::Int`: The maximum number of states to retain in the bounded solution. - `δprob::Real`: The probability threshold for discarding low-probability states. - `merge_strategy=no_merge`: (Optional) Merge strategy for branches. Defaults to `no_merge`. - + ## Returns A `Solution` object representing the bounded solution with a maximum of `max_states` states. - + """ function bound_solution( psol::Solution, max_states::Int, δprob::Real, merge_strategy=no_merge @@ -655,9 +654,7 @@ end # """ # $(TYPEDSIGNATURES) # """ -function sampling( - psol::Solution, max_states::Int, δprob::Real, merge_strategy=no_merge -) +function sampling(psol::Solution, max_states::Int, δprob::Real, merge_strategy=no_merge) prob = exp.(psol.probabilities) new_prob = cumsum(reshape(prob, :, max_states), dims = 1) @@ -678,8 +675,8 @@ end $(TYPEDSIGNATURES) Compute the low-energy spectrum of a spin glass PEPS network using branch-and-bound search. -This function computes the low-energy spectrum of a spin glass PEPS (Projected Entangled Pair State) network using a branch-and-bound search algorithm. -It takes as input a `ctr` object representing the PEPS network and the parameters for controlling its contraction, `sparams` specifying search parameters, `merge_strategy` for merging branches, +This function computes the low-energy spectrum of a spin glass PEPS (Projected Entangled Pair State) network using a branch-and-bound search algorithm. +It takes as input a `ctr` object representing the PEPS network and the parameters for controlling its contraction, `sparams` specifying search parameters, `merge_strategy` for merging branches, and `symmetry` indicating any symmetry constraints. Optionally, you can disable caching using the `no_cache` flag. ## Arguments @@ -764,7 +761,7 @@ function low_energy_spectrum( ctr.peps.clustered_hamiltonian.reverse_label_map[idx] for idx ∈ ctr.peps.vertex_map.(ctr.nodes_search_order) ]) - + inner_perm_inv = zeros(Int, length(inner_perm)) inner_perm_inv[inner_perm] = collect(1:length(inner_perm)) @@ -792,7 +789,7 @@ perm_droplet(drop::NoDroplets, perm::Vector{Int}) = drop function perm_droplet(drops::Vector{Droplet}, perm::Vector{Int}) [perm_droplet(drop, perm) for drop in drops] -end +end function perm_droplet(drop::Droplet, perm::Vector{Int}) flip = drop.flip @@ -853,7 +850,7 @@ function gibbs_sampling( ctr.peps.clustered_hamiltonian.reverse_label_map[idx] for idx ∈ ctr.peps.vertex_map.(ctr.nodes_search_order) ]) - + inner_perm_inv = zeros(Int, length(inner_perm)) inner_perm_inv[inner_perm] = collect(1:length(inner_perm)) diff --git a/src/square_cross_double_node.jl b/src/square_cross_double_node.jl index b5796743..f069cae0 100644 --- a/src/square_cross_double_node.jl +++ b/src/square_cross_double_node.jl @@ -94,7 +94,7 @@ function MpoLayers(::Type{T}, ncols::Int) where T <: SquareCrossDoubleNode{Energ MpoLayers( Dict(site(i) => (-1//6, 0, 3//6, 4//6) for i ∈ 1//2:1//2:ncols), Dict(site(i) => (3//6, 4//6) for i ∈ 1//2:1//2:ncols), - Dict(site(i) => (-3//6, 0) for i ∈ 1//2:1//2:ncols) + Dict(site(i) => (-3//6, 0) for i ∈ 1//2:1//2:ncols), ) end @@ -111,7 +111,6 @@ function MpoLayers(::Type{T}, ncols::Int) where T <: SquareCrossDoubleNode{Gauge ) end - @memoize Dict function precompute_conditional( ::Type{T}, ctr::MpsContractor{S}, current_node ) where {T <: SquareCrossDoubleNode, S} @@ -206,14 +205,10 @@ end prf2 = CuArray(prf2) pd2 = CuArray(pd2) end - return (eng_loc, eng_l, eng_lu, eng_u, eng_12, plb2, prf2, pd2, splb1, splb2, sprf2, spd2) end end - - - """ $(TYPEDSIGNATURES) """ @@ -301,7 +296,6 @@ function conditional_probability( normalize_probability(probs) end - """ $(TYPEDSIGNATURES) """ @@ -450,7 +444,8 @@ function tensor( #TODO VirtualTensor( net.lp, CentralTensor(net, β, (i, j), (i, j+1)), - (p_lb, p_l, p_lt, p_rb, p_r, p_rt)) + (p_lb, p_l, p_lt, p_rb, p_r, p_rt), + ) end """ @@ -474,7 +469,6 @@ function tensor( B end - """ $(TYPEDSIGNATURES) """ diff --git a/src/square_cross_single_node.jl b/src/square_cross_single_node.jl index 6f4e2950..624d7281 100644 --- a/src/square_cross_single_node.jl +++ b/src/square_cross_single_node.jl @@ -31,7 +31,6 @@ function tensor_map( ::Type{SquareCrossSingleNode{T}}, ::Type{S}, nrows::Int, ncols::Int ) where {T <: Union{EnergyGauges, GaugesEnergy}, S <: AbstractSparsity} map = Dict{PEPSNode, Symbol}() - for i ∈ 1:nrows, j ∈ 1:ncols push!( map, @@ -54,7 +53,6 @@ function tensor_map( ::Type{SquareCrossSingleNode{T}}, ::Type{S}, nrows::Int, ncols::Int ) where {T <: EngGaugesEng, S <: AbstractSparsity} map = Dict{PEPSNode, Symbol}() - for i ∈ 1:nrows, j ∈ 1:ncols push!( map, @@ -129,7 +127,7 @@ function MpoLayers(::Type{T}, ncols::Int) where T <: SquareCrossSingleNode{Energ MpoLayers( Dict(site(i) => (-1//6, 0, 3//6, 4//6) for i ∈ 1//2:1//2:ncols), Dict(site(i) => (3//6, 4//6) for i ∈ 1//2:1//2:ncols), - Dict(site(i) => (-3//6, 0) for i ∈ 1//2:1//2:ncols) + Dict(site(i) => (-3//6, 0) for i ∈ 1//2:1//2:ncols), ) end @@ -142,7 +140,7 @@ function MpoLayers(::Type{T}, ncols::Int) where T <: SquareCrossSingleNode{Gauge MpoLayers( Dict(site(i) => (-4//6, -1//2, 0, 1//6) for i ∈ 1//2:1//2:ncols), Dict(site(i) => (1//6,) for i ∈ 1//2:1//2:ncols), - Dict(site(i) => (-3//6, 0) for i ∈ 1//2:1//2:ncols) + Dict(site(i) => (-3//6, 0) for i ∈ 1//2:1//2:ncols), ) end @@ -155,7 +153,7 @@ function MpoLayers(::Type{T}, ncols::Int) where T <: SquareCrossSingleNode{EngGa MpoLayers( Dict(site(i) => (-2//5, -1//5, 0, 1//5, 2//5) for i ∈ 1//2:1//2:ncols), Dict(site(i) => (1//5, 2//5) for i ∈ 1//2:1//2:ncols), - Dict(site(i) => (-4//5, -1//5, 0) for i ∈ 1//2:1//2:ncols) + Dict(site(i) => (-4//5, -1//5, 0) for i ∈ 1//2:1//2:ncols), ) end @@ -205,8 +203,6 @@ function conditional_probability( normalize_probability(probs) end - - """ $(TYPEDSIGNATURES) """ diff --git a/src/square_double_node.jl b/src/square_double_node.jl index bdf4605b..2cb4eb49 100644 --- a/src/square_double_node.jl +++ b/src/square_double_node.jl @@ -11,7 +11,9 @@ $(TYPEDSIGNATURES) function SquareDoubleNode(m::Int, n::Int) labels = [(i, j, k) for j ∈ 1:n for i ∈ 1:m for k ∈ 1:2] lg = LabelledGraph(labels) - for i ∈ 1:m, j ∈ 1:n add_edge!(lg, (i, j, 1), (i, j, 2)) end + for i ∈ 1:m, j ∈ 1:n + add_edge!(lg, (i, j, 1), (i, j, 2)) + end for i ∈ 1:m-1, j ∈ 1:n add_edge!(lg, (i, j, 1), (i+1, j, 1)) @@ -26,7 +28,6 @@ function SquareDoubleNode(m::Int, n::Int) add_edge!(lg, (i, j, 1), (i, j+1, 2)) add_edge!(lg, (i, j, 1), (i, j+1, 1)) end - lg end @@ -47,7 +48,6 @@ function tensor_map( ::Type{SquareDoubleNode{T}}, ::Type{S}, nrows::Int, ncols::Int ) where {T <: Union{GaugesEnergy, EnergyGauges}, S <: AbstractSparsity} map = Dict{PEPSNode, Symbol}() - for i ∈ 1:nrows, j ∈ 1:ncols push!(map, PEPSNode(i, j) => site_double_node(S)) if j < ncols push!(map, PEPSNode(i, j + 1//2) => :central_h_double_node) end @@ -95,15 +95,14 @@ Defines the MPO layers for the SquareSingleNode geometry with the EnergyGauges l """ function MpoLayers(::Type{T}, ncols::Int) where T <: SquareDoubleNode{EnergyGauges} main = Dict{Site, Sites}(i => (-1//6, 0, 3//6, 4//6) for i ∈ 1:ncols) - #main = Dict{Site, Sites}(i => (0, 3//6) for i ∈ 1:ncols) - for i ∈ 1:ncols - 1 push!(main, i + 1//2 => (0,)) end - + for i ∈ 1:ncols - 1 + push!(main, i + 1//2 => (0,)) + end right = Dict{Site, Sites}(i => (-3//6, 0) for i ∈ 1:ncols) - for i ∈ 1:ncols - 1 push!(right, i + 1//2 => (0,)) end - - MpoLayers(main, Dict(i => (3//6, 4//6) for i ∈ 1:ncols), right) # Check if gauge works? - #MpoLayers(main, Dict(i => (3//6,) for i ∈ 1:ncols), right) - + for i ∈ 1:ncols - 1 + push!(right, i + 1//2 => (0,)) + end + MpoLayers(main, Dict(i => (3//6, 4//6) for i ∈ 1:ncols), right) end """ @@ -138,7 +137,7 @@ function conditional_probability( if ctr.onGPU R = CuArray(R) end - + eng_loc = [local_energy(ctr.peps, (i, j, k)) for k ∈ 1:2] el = [interaction_energy(ctr.peps, (i, j, k), (i, j-1, m)) for k ∈ 1:2, m ∈ 1:2] pl = [projector(ctr.peps, (i, j, k), (i, j-1, m)) for k ∈ 1:2, m ∈ 1:2] @@ -279,7 +278,7 @@ function tensor( SiteTensor( net.lp, exp.(-β .* (eloc12 .- minimum(eloc12))), - projectors_site_tensor(net, Node(node)) + projectors_site_tensor(net, Node(node)), ) end @@ -339,8 +338,9 @@ function tensor( end -function CentralTensor(network::PEPSNetwork{T, S}, β::Real, node1::NTuple{2, Int64}, node2::NTuple{2, Int64} - ) where {T <: AbstractGeometry, S} +function CentralTensor( + network::PEPSNetwork{T, S}, β::Real, node1::NTuple{2, Int64}, node2::NTuple{2, Int64} +) where {T <: AbstractGeometry, S} i1, j1 = node1 i2, j2 = node2 @@ -375,12 +375,7 @@ function CentralTensor(network::PEPSNetwork{T, S}, β::Real, node1::NTuple{2, In le12 = exp.(-β .* (e12 .- minimum(e12))) le22 = exp.(-β .* (e22 .- minimum(e22))) - SpinGlassTensors.CentralTensor( - le11, - le12, - le21, - le22 - ) + SpinGlassTensors.CentralTensor(le11, le12, le21, le22) end function CentralTensor_size(network::PEPSNetwork{T, S}, node1::NTuple{2, Int64}, node2::NTuple{2, Int64} diff --git a/src/tensors.jl b/src/tensors.jl index 71828185..b91c1e04 100644 --- a/src/tensors.jl +++ b/src/tensors.jl @@ -32,15 +32,6 @@ function tensor( ) end -# TODO: This is how the engine should work -#= -function (peps::PEPSNetwork)(::Type{SparseCentralTensor}, v::PEPSNode, β::Real) - # ... -end - -ten = peps(SparseCentralTensor, v, β) -=# - """ $(TYPEDSIGNATURES) @@ -62,8 +53,8 @@ $(TYPEDSIGNATURES) """ function Base.size( - network::PEPSNetwork{T, S}, - v::PEPSNode, + network::PEPSNetwork{T, S}, + v::PEPSNode, ::Union{Val{:site}, Val{:sparse_site}, Val{:sparse_site_square_double_node}} ) where {T <: AbstractGeometry, S <: AbstractSparsity} maximum.(projectors_site_tensor(network, Node(v)))