From 6e69a70e23df9366ea10f6c75fcd33b54ee6f6a3 Mon Sep 17 00:00:00 2001 From: annamariadziubyna <73058800+annamariadziubyna@users.noreply.github.com> Date: Thu, 5 Oct 2023 21:05:30 +0200 Subject: [PATCH] split the lines --- src/bp.jl | 69 +++++++++++++++++++++++++----------- src/clustered_hamiltonian.jl | 51 ++++++++++++++++++-------- src/ising.jl | 25 +++++++++---- src/lattice.jl | 24 ++++++++----- src/projectors.jl | 29 +++++++++------ src/spectrum.jl | 12 ++++--- src/truncate.jl | 6 ++-- 7 files changed, 149 insertions(+), 67 deletions(-) diff --git a/src/bp.jl b/src/bp.jl index b9af2c7..bc8286f 100644 --- a/src/bp.jl +++ b/src/bp.jl @@ -18,11 +18,13 @@ Perform belief propagation on a given clustered hamiltonian. # Arguments: - `cl_h::LabelledGraph{S, T}`: The clustered hamiltonian represented as a labeled graph. - `beta::Real`: The temperature parameter for the belief propagation algorithm. -- `tol::Real (optional, default=1e-6)`: The convergence tolerance. The algorithm stops when the message updates between iterations are smaller than this value. +- `tol::Real (optional, default=1e-6)`: The convergence tolerance. +The algorithm stops when the message updates between iterations are smaller than this value. - `iter::Int (optional, default=1)`: The maximum number of iterations to perform. # Returns: -- `beliefs::Dict`: A dictionary where keys are vertices of clustered hamiltonian, and values are the resulting beliefs after belief propagation. +- `beliefs::Dict`: A dictionary where keys are vertices of clustered hamiltonian, and values are the +resulting beliefs after belief propagation. The function implements belief propagation on the given clustered hamiltonian `cl_h` to calculate beliefs for each vertex. Belief propagation is an iterative algorithm that computes beliefs by passing messages between vertices and edges of the clustered hamiltonian. @@ -98,12 +100,15 @@ Returns the neighbors of a given vertex in a clustered Hamiltonian. - `vertex::NTuple`: The vertex for which neighbors are to be retrieved. # Returns: -- `neighbors::Vector{Tuple}`: A vector of tuples representing the neighbors of the specified vertex. Each tuple contains the following information: - - `dst_node::T`: The neighboring vertex. - - `pv::Matrix`: The projector associated with the edge connecting the vertex and its neighbor. - - `en::Real`: The energy associated with the edge connecting the vertex and its neighbor. - -This function retrieves the neighbors of a given vertex in a clustered Hamiltonian graph. It iterates through the edges of the graph and identifies edges connected to the specified vertex. For each neighboring edge, it extracts and returns the neighboring vertex, the associated projector, and the energy. +- `neighbors::Vector{Tuple}`: A vector of tuples representing the neighbors of the specified vertex. +Each tuple contains the following information: +- `dst_node::T`: The neighboring vertex. +- `pv::Matrix`: The projector associated with the edge connecting the vertex and its neighbor. +- `en::Real`: The energy associated with the edge connecting the vertex and its neighbor. + +This function retrieves the neighbors of a given vertex in a clustered Hamiltonian graph. +It iterates through the edges of the graph and identifies edges connected to the specified vertex. +For each neighboring edge, it extracts and returns the neighboring vertex, the associated projector, and the energy. """ function get_neighbors(cl_h::LabelledGraph{S, T}, vertex::NTuple) where {S, T} neighbors = [] @@ -135,9 +140,12 @@ A custom Julia struct representing energy values in a merged format for use in s - `e21::AbstractMatrix{T}` - `e22::AbstractMatrix{T}` -The `MergedEnergy` struct is used to represent energy values that are organized in a merged format. This format is often utilized in certain computational tasks, where energy values are categorized based on combinations of left and right factors. +The `MergedEnergy` struct is used to represent energy values that are organized in a merged format. +This format is often utilized in certain computational tasks, where energy values are categorized based on combinations of left and right factors. -Each field of the `MergedEnergy` struct stores energy values as an `AbstractMatrix{T}` of type `T`, where `T` is a subtype of the `Real` abstract type. The specific organization and interpretation of these energy values depend on the context in which this struct is used. +Each field of the `MergedEnergy` struct stores energy values as an `AbstractMatrix{T}` of type `T`, +where `T` is a subtype of the `Real` abstract type. +The specific organization and interpretation of these energy values depend on the context in which this struct is used. """ struct MergedEnergy{T <: Real} e11::AbstractMatrix{T} @@ -161,7 +169,9 @@ Update a message using energy values and temperature. # Returns: - `updated_message::Vector`: The updated message vector after applying the energy-based update. -This function takes energy values `E_bond` associated with a bond or interaction, an input message vector `message`, and a temperature parameter `beta`. It updates the message by first adjusting the energy values relative to their minimum value, exponentiating them with a negative sign and scaling by `beta`, and then multiplying them element-wise with the input message. +This function takes energy values `E_bond` associated with a bond or interaction, an input message vector `message`, +and a temperature parameter `beta`. It updates the message by first adjusting the energy values relative to their minimum value, +exponentiating them with a negative sign and scaling by `beta`, and then multiplying them element-wise with the input message. The result is an updated message that reflects the influence of energy values and temperature. """ @@ -183,9 +193,11 @@ Update a message using energy values and temperature in a merged energy format. # Returns: - `updated_message::Vector`: The updated message vector after applying the energy-based update. -This function takes energy values `E_bond` in a merged energy format, an input message vector `message`, and a temperature parameter `beta`. It updates the message based on the energy values and temperature using a specified algorithm. +This function takes energy values `E_bond` in a merged energy format, an input message vector `message`, +and a temperature parameter `beta`. It updates the message based on the energy values and temperature using a specified algorithm. -The `MergedEnergy` type represents energy values in a merged format, and the function processes these values accordingly to update the message vector. +The `MergedEnergy` type represents energy values in a merged format, and the function processes these values +accordingly to update the message vector. """ function update_message(E_bond::MergedEnergy, message::Vector, beta::Real) e11, e12, e21, e22 = E_bond.e11, E_bond.e12, E_bond.e21, E_bond.e22 @@ -244,9 +256,12 @@ Constructs a clustered Hamiltonian for a given clustered Hamiltonian with a 2-si # Returns: - `new_cl_h::LabelledGraph{MetaDiGraph}`: A new labeled graph representing the 2-site cluster Hamiltonian. - This function constructs a clustered Hamiltonian `cl_h` by applying a 2-site cluster approximation. It combines and merges vertices and edges of the original graph to create a simplified representation of the Hamiltonian. + This function constructs a clustered Hamiltonian `cl_h` by applying a 2-site cluster approximation. + It combines and merges vertices and edges of the original graph to create a simplified representation of the Hamiltonian. -The resulting `new_cl_h` graph represents the 2-site cluster Hamiltonian with simplified interactions between clusters. The energy values, projectors, and spectra associated with the new vertices and edges are computed based on the provided temperature parameter `beta`. +The resulting `new_cl_h` graph represents the 2-site cluster Hamiltonian with simplified interactions between clusters. +The energy values, projectors, and spectra associated with the new vertices and edges are computed based on +the provided temperature parameter `beta`. """ function clustered_hamiltonian_2site(cl_h::LabelledGraph{S, T}, beta::Real) where {S, T} @@ -303,9 +318,12 @@ Merge two vertices in a clustered Hamiltonian to create a single merged vertex. - `pl::AbstractVector`: The merged left projector. - `pr::AbstractVector`: The merged right projector. -This function merges two vertices in a clustered Hamiltonian graph `cl_h` to create a single merged vertex. The merging process combines projectors and energy values associated with the original vertices based on the provided temperature parameter `β`. +This function merges two vertices in a clustered Hamiltonian graph `cl_h` to create a single merged vertex. +The merging process combines projectors and energy values associated with the original vertices based on +the provided temperature parameter `β`. -The merged energy values, left projector `pl`, and right projector `pr` are computed based on the interactions between the original vertices and their respective projectors. +The merged energy values, left projector `pl`, and right projector `pr` are computed based on the interactions +between the original vertices and their respective projectors. """ function merge_vertices_cl_h(cl_h::LabelledGraph{S, T}, β::Real, node1::NTuple{3, Int64}, node2::NTuple{3, Int64} ) where {S, T} @@ -356,7 +374,8 @@ Get the local energy associated with a vertex in a clustered Hamiltonian. # Returns: - `local_energy::AbstractVector`: An abstract vector containing the local energy values associated with the specified vertex. -This function retrieves the local energy values associated with a given vertex `v` in a clustered Hamiltonian graph `cl_h`. If the vertex exists in the graph and has associated energy values, it returns those values; otherwise, it returns a vector of zeros. +This function retrieves the local energy values associated with a given vertex `v` in a clustered Hamiltonian graph `cl_h`. +If the vertex exists in the graph and has associated energy values, it returns those values; otherwise, it returns a vector of zeros. The local energy values are typically obtained from the spectrum associated with the vertex. """ @@ -377,7 +396,10 @@ Get the interaction energy between two vertices in a clustered Hamiltonian. # Returns: - `interaction_energy::AbstractMatrix`: An abstract matrix containing the interaction energy values between the specified vertices. -This function retrieves the interaction energy values between two vertices, `v` and `w`, in a clustered Hamiltonian graph `cl_h`. If there is a directed edge from `w` to `v`, it returns the corresponding energy values; if there is a directed edge from `v` to `w`, it returns the transpose of the energy values; otherwise, it returns a matrix of zeros. +This function retrieves the interaction energy values between two vertices, `v` and `w`, in a clustered Hamiltonian graph `cl_h`. +If there is a directed edge from `w` to `v`, it returns the corresponding energy values; +if there is a directed edge from `v` to `w`, it returns the transpose of the energy values; +otherwise, it returns a matrix of zeros. The interaction energy values represent the energy associated with the interaction or connection between the two vertices. """ function interaction_energy(cl_h::LabelledGraph{S, T}, v::NTuple{3, Int64}, w::NTuple{3, Int64}) where {S, T} @@ -403,7 +425,11 @@ Get the projector associated with an edge between two vertices in a clustered Ha # Returns: - `p::AbstractVector`: An abstract vector representing the projector associated with the specified edge. -This function retrieves the projector associated with an edge between two vertices, `v` and `w`, in a clustered Hamiltonian graph `cl_h`. If there is a directed edge from `w` to `v`, it returns the index of right projector (`:ipr`); if there is a directed edge from `v` to `w`, it returns the index of left projector (`:ipl`). If no edge exists between the vertices, it returns a vector of ones. +This function retrieves the projector associated with an edge between two vertices, `v` and `w`, +in a clustered Hamiltonian graph `cl_h`. +If there is a directed edge from `w` to `v`, it returns the index of right projector (`:ipr`); +if there is a directed edge from `v` to `w`, it returns the index of left projector (`:ipl`). +If no edge exists between the vertices, it returns a vector of ones. """ function projector(cl_h::LabelledGraph{S, T}, v::NTuple{N, Int64}, w::NTuple{N, Int64}) where {S, T, N} if has_edge(cl_h, w, v) @@ -439,7 +465,8 @@ Create a sparse column-compressed (CSC) matrix with specified column indices and # Returns: - `sparse_matrix::SparseMatrixCSC{R}`: A sparse column-compressed matrix with non-zero values at specified columns. -This constructor function creates a sparse column-compressed (CSC) matrix of element type `R` based on the provided column indices `p` and values. The resulting matrix has non-zero values at the specified column indices, while all other elements are zero. +This constructor function creates a sparse column-compressed (CSC) matrix of element type `R` based on the provided +column indices `p` and values. The resulting matrix has non-zero values at the specified column indices, while all other elements are zero. The `SparseCSC` constructor is useful for creating sparse matrices with specific column indices and values efficiently. """ function SparseCSC(::Type{R}, p::Vector{Int64}) where R <: Real diff --git a/src/clustered_hamiltonian.jl b/src/clustered_hamiltonian.jl index c8fd5bd..b7c6d27 100644 --- a/src/clustered_hamiltonian.jl +++ b/src/clustered_hamiltonian.jl @@ -24,7 +24,9 @@ Dict(clustered Hamiltonian coordinates -> group of spins in Ising graph) # Returns: - `clusters::Dict{L, Vertex}`: A dictionary mapping cluster identifiers to representative vertices in the Ising graph. -This function groups spins in the Ising graph into clusters based on an assignment rule. The assignment rule defines how clustered Hamiltonian coordinates correspond to clusters of spins in the Ising graph. Each cluster is represented by a vertex from the Ising graph. +This function groups spins in the Ising graph into clusters based on an assignment rule. +The assignment rule defines how clustered Hamiltonian coordinates correspond to clusters of spins in the Ising graph. +Each cluster is represented by a vertex from the Ising graph. The `split_into_clusters` function is useful for organizing and analyzing spins in complex spin systems, particularly in the context of clustered Hamiltonian. @@ -51,7 +53,10 @@ This function constructs a clustered Hamiltonian from an Ising graph by introduc # Returns: - `cl_h::LabelledGraph{S, T}`: The clustered Hamiltonian represented as a labeled graph. -The `clustered_hamiltonian` function takes an Ising graph (`ig`) as input and constructs a clustered Hamiltonian by introducing a natural order in clustered Hamiltonian coordinates. It allows you to specify the number of states per cluster, a spectrum calculation function, and a cluster assignment rule, which maps Ising graph vertices to clusters. +The `clustered_hamiltonian` function takes an Ising graph (`ig`) as input and constructs a clustered Hamiltonian by +introducing a natural order in clustered Hamiltonian coordinates. +It allows you to specify the number of states per cluster, a spectrum calculation function, +and a cluster assignment rule, which maps Ising graph vertices to clusters. This function is useful for organizing and studying spin systems in a clustered Hamiltonian framework. """ @@ -81,7 +86,9 @@ This function constructs a clustered Hamiltonian from an Ising graph by introduc # Returns: - `cl_h::LabelledGraph{MetaDiGraph}`: The clustered Hamiltonian represented as a labeled graph. -The `clustered_hamiltonian` function takes an Ising graph (`ig`) as input and constructs a clustered Hamiltonian by introducing a natural order in clustered Hamiltonian coordinates. It allows you to specify the number of states per cluster, a spectrum calculation function, and a cluster assignment rule, which maps Ising graph vertices to clusters. +The `clustered_hamiltonian` function takes an Ising graph (`ig`) as input and constructs a clustered Hamiltonian +by introducing a natural order in clustered Hamiltonian coordinates. It allows you to specify the number of +states per cluster, a spectrum calculation function, and a cluster assignment rule, which maps Ising graph vertices to clusters. This function is useful for organizing and studying spin systems in a clustered Hamiltonian framework. @@ -148,9 +155,12 @@ This function constructs a clustered Hamiltonian from an Ising graph by introduc # Returns: - `cl_h::LabelledGraph{MetaDiGraph}`: The clustered Hamiltonian represented as a labeled graph. -The `clustered_hamiltonian` function takes an Ising graph (`ig`) as input and constructs a clustered Hamiltonian by introducing a natural order in clustered Hamiltonian coordinates. You can optionally specify a spectrum calculation function and a cluster assignment rule, which maps Ising graph vertices to clusters. +The `clustered_hamiltonian` function takes an Ising graph (`ig`) as input and constructs a clustered Hamiltonian +by introducing a natural order in clustered Hamiltonian coordinates. +You can optionally specify a spectrum calculation function and a cluster assignment rule, which maps Ising graph vertices to clusters. -If you want to specify custom cluster sizes, use the alternative version of this function by passing a `Dict{T, Int}` containing the number of states per cluster as `num_states_cl`. +If you want to specify custom cluster sizes, use the alternative version of this function by +passing a `Dict{T, Int}` containing the number of states per cluster as `num_states_cl`. This function is useful for organizing and studying spin systems in a clustered Hamiltonian framework. """ @@ -167,11 +177,13 @@ $(TYPEDSIGNATURES) Reveal ranks and energies in a specified order. -This function calculates and reveals the ranks and energies of a set of states in either the 'PE' (Projector Energy) or 'EP' (Energy Projector) order. +This function calculates and reveals the ranks and energies of a set of states in either the +'PE' (Projector Energy) or 'EP' (Energy Projector) order. # Arguments: - `energy`: The energy values of states. -- `order::Symbol`: The order in which to reveal the ranks and energies. It can be either `:PE` for 'Projector Energy)' order (default) or `:EP` for 'Energy Projector' order. +- `order::Symbol`: The order in which to reveal the ranks and energies. +It can be either `:PE` for 'Projector Energy)' order (default) or `:EP` for 'Energy Projector' order. # Returns: - If `order` is `:PE`, the function returns a tuple `(P, E)` where: @@ -196,7 +208,8 @@ TODO: check the order consistency over external packages. Decode a clustered Hamiltonian state into Ising graph spin values. -This function decodes a state from a clustered Hamiltonian into Ising graph spin values and returns a dictionary mapping each Ising graph vertex to its corresponding spin value. +This function decodes a state from a clustered Hamiltonian into Ising graph spin values and +returns a dictionary mapping each Ising graph vertex to its corresponding spin value. # Arguments: - `cl_h::LabelledGraph{S, T}`: The clustered Hamiltonian represented as a labeled graph. @@ -205,7 +218,8 @@ This function decodes a state from a clustered Hamiltonian into Ising graph spin # Returns: - `spin_values::Dict{Int, Int}`: A dictionary mapping each Ising graph vertex to its corresponding spin value. -This function assumes that the state has the same order as the vertices in the clustered Hamiltonian. It decodes the state consistently based on the cluster assignments and spectra of the clustered Hamiltonian. +This function assumes that the state has the same order as the vertices in the clustered Hamiltonian. +It decodes the state consistently based on the cluster assignments and spectra of the clustered Hamiltonian. """ function decode_clustered_hamiltonian_state(cl_h::LabelledGraph{S, T}, state::Vector{Int}) where {S, T} ret = Dict{Int, Int}() @@ -225,7 +239,8 @@ $(TYPEDSIGNATURES) Calculate the energy of a clustered Hamiltonian state. -This function calculates the energy of a given state in a clustered Hamiltonian. The state is represented as a dictionary mapping each Ising graph vertex to its corresponding spin value. +This function calculates the energy of a given state in a clustered Hamiltonian. +The state is represented as a dictionary mapping each Ising graph vertex to its corresponding spin value. # Arguments: - `cl_h::LabelledGraph{S, T}`: The clustered Hamiltonian represented as a labeled graph. @@ -234,7 +249,9 @@ This function calculates the energy of a given state in a clustered Hamiltonian. # Returns: - `en_cl_h::Float64`: The energy of the state in the clustered Hamiltonian. -This function computes the energy by summing the energies associated with individual clusters and the interaction energies between clusters. It takes into account the cluster spectra and projectors stored in the clustered Hamiltonian. +This function computes the energy by summing the energies associated with individual +clusters and the interaction energies between clusters. +It takes into account the cluster spectra and projectors stored in the clustered Hamiltonian. """ function energy(cl_h::LabelledGraph{S, T}, σ::Dict{T, Int}) where {S, T} en_cl_h = 0.0 @@ -265,7 +282,9 @@ This function computes the interaction energy between two specified nodes in a c # Returns: - `int_eng::AbstractMatrix{T}`: The interaction energy matrix between the specified sites. -The function checks if there is an interaction edge between the two sites (i, j) in both directions (i -> j and j -> i). If such edges exist, it retrieves the interaction energy matrix, projectors, and calculates the interaction energy. If no interaction edge is found, it returns a zero matrix. +The function checks if there is an interaction edge between the two sites (i, j) in both directions (i -> j and j -> i). +If such edges exist, it retrieves the interaction energy matrix, projectors, and calculates the interaction energy. +If no interaction edge is found, it returns a zero matrix. """ function energy_2site(cl_h::LabelledGraph{S, T}, i::Int, j::Int) where {S, T} # matrix of interaction energies between two nodes @@ -305,7 +324,9 @@ This function computes the bond energy between two specified clusters (cluster n # Returns: - `energies::AbstractVector{T}`: The bond energy vector between the two clusters for the specified index. -The function checks if there is an edge between the two clusters (u -> v and v -> u). If such edges exist, it retrieves the bond energy matrix and projectors and calculates the bond energy. If no bond edge is found, it returns a zero vector. +The function checks if there is an edge between the two clusters (u -> v and v -> u). +If such edges exist, it retrieves the bond energy matrix and projectors and calculates the bond energy. +If no bond edge is found, it returns a zero vector. """ function bond_energy( cl_h::LabelledGraph{S, T}, @@ -367,7 +388,9 @@ This function computes the exact conditional probability of a specified target s # Returns: - `prob::Float64`: The exact conditional probability of the target state. -The function generates all possible states for the clusters in the clustered Hamiltonian, calculates their energies, and computes the probability distribution based on the given inverse temperature parameter. It then calculates the conditional probability of the specified target state by summing the probabilities of states that match the target state. +The function generates all possible states for the clusters in the clustered Hamiltonian, +calculates their energies, and computes the probability distribution based on the given inverse temperature parameter. +It then calculates the conditional probability of the specified target state by summing the probabilities of states that match the target state. """ function exact_cond_prob(clustered_hamiltonian::LabelledGraph{S, T}, beta, target_state::Dict) where {S, T} # TODO: Not going to work without PoolOfProjectors diff --git a/src/ising.jl b/src/ising.jl index 2b2639e..9672617 100644 --- a/src/ising.jl +++ b/src/ising.jl @@ -25,18 +25,22 @@ $(TYPEDSIGNATURES) Create an Ising graph from interaction data. -This function creates an Ising graph from interaction data provided in the form of an `inst` argument. The Ising graph represents a system of spins, where each spin is associated with a vertex, and interactions between spins are represented as edges with corresponding weights. +This function creates an Ising graph from interaction data provided in the form of an `inst` argument. +The Ising graph represents a system of spins, where each spin is associated with a vertex, and interactions between spins are represented as edges with corresponding weights. # Arguments: - `::Type{T}`: The type of the edge weights, typically `Float64` or `Float32`. -- `inst::Instance`: Interaction data, which can be either a file path to a CSV file or a collection of triples `(i, j, J)` representing interactions between spins, where `i` and `j` are spin indices, and `J` is the interaction strength. +- `inst::Instance`: Interaction data, which can be either a file path to a CSV file or a collection of triples `(i, j, J)` +representing interactions between spins, where `i` and `j` are spin indices, and `J` is the interaction strength. - `scale::Real`: A scaling factor applied to interaction strengths (default is 1). - `rank_override::Dict`: A dictionary specifying the rank (number of states) for each vertex. If not provided, a default rank of 2 is used for all vertices. # Returns: - `ig::IsingGraph{T}`: The Ising graph representing the spin system. -The function reads interaction data and constructs an Ising graph `ig`. It assigns interaction strengths to edges between spins and optionally scales them by the `scale` factor. The `rank_override` dictionary can be used to specify the rank (number of states) for individual vertices, allowing customization of the Ising model. +The function reads interaction data and constructs an Ising graph `ig`. +It assigns interaction strengths to edges between spins and optionally scales them by the `scale` factor. +The `rank_override` dictionary can be used to specify the rank (number of states) for individual vertices, allowing customization of the Ising model. Convention: H = scale * sum_{i, j} (J_{ij} * s_i * s_j + J_{ii} * s_i) """ function ising_graph(::Type{T}, inst::Instance; scale::Real=1, rank_override::Dict=Dict{Int, Int}()) where T @@ -77,7 +81,8 @@ $(TYPEDSIGNATURES) Return the coupling strengths between vertices of an Ising graph. -This function computes and returns the coupling strengths (interaction energies) between pairs of vertices in an Ising graph `ig`. The coupling strengths are represented as a matrix, where each element `(i, j)` corresponds to the interaction energy between vertex `i` and vertex `j`. +This function computes and returns the coupling strengths (interaction energies) between pairs of vertices in an Ising graph `ig`. +The coupling strengths are represented as a matrix, where each element `(i, j)` corresponds to the interaction energy between vertex `i` and vertex `j`. # Arguments: - `ig::IsingGraph{T}`: The Ising graph representing a system of spins with associated interaction strengths. @@ -103,7 +108,10 @@ $(TYPEDSIGNATURES) Return the dense adjacency matrix between clusters of vertices in an Ising graph. -This function computes and returns the dense adjacency matrix `J` between clusters of vertices represented by two Ising graphs, `cl1` and `cl2`, within the context of the larger Ising graph `ig`. The adjacency matrix represents the interaction strengths between clusters of vertices, where each element `(i, j)` corresponds to the interaction strength between cluster `i` in `cl1` and cluster `j` in `cl2`. +This function computes and returns the dense adjacency matrix `J` between clusters of vertices represented by two +Ising graphs, `cl1` and `cl2`, within the context of the larger Ising graph `ig`. +The adjacency matrix represents the interaction strengths between clusters of vertices, +where each element `(i, j)` corresponds to the interaction strength between cluster `i` in `cl1` and cluster `j` in `cl2`. # Arguments: - `ig::IsingGraph{T}`: The Ising graph representing a system of spins with associated interaction strengths. @@ -114,7 +122,8 @@ This function computes and returns the dense adjacency matrix `J` between cluste - `outer_edges::Vector{LabelledEdge}`: A vector of labeled edges representing the interactions between clusters. - `J::Matrix{T}`: A dense adjacency matrix representing interaction strengths between clusters. -The function first identifies the outer edges that connect vertices between the two clusters in the context of the larger Ising graph `ig`. It then computes the interaction strengths associated with these outer edges and populates the dense adjacency matrix `J` accordingly. +The function first identifies the outer edges that connect vertices between the two clusters in the context of the larger Ising graph `ig`. +It then computes the interaction strengths associated with these outer edges and populates the dense adjacency matrix `J` accordingly. """ function inter_cluster_edges(ig::IsingGraph{T}, cl1::IsingGraph{T}, cl2::IsingGraph{T}) where T outer_edges = [LabelledEdge(i, j) for i ∈ vertices(cl1), j ∈ vertices(cl2) if has_edge(ig, i, j)] @@ -132,7 +141,9 @@ $(TYPEDSIGNATURES) Used only in MPS_search, would be obsolete if MPS_search uses QMps. Remove non-existing spins from an Ising graph. -This function removes non-existing spins from the given Ising graph `ig`. Non-existing spins are those that have zero degree (no connections to other spins) and also have an external magnetic field (`h`) that is not approximately equal to zero within the specified tolerance `atol`. +This function removes non-existing spins from the given Ising graph `ig`. +Non-existing spins are those that have zero degree (no connections to other spins) and also have an external +magnetic field (`h`) that is not approximately equal to zero within the specified tolerance `atol`. # Arguments: - `ig::IsingGraph`: The Ising graph to be pruned. diff --git a/src/lattice.jl b/src/lattice.jl index e36df5a..55b7e1d 100644 --- a/src/lattice.jl +++ b/src/lattice.jl @@ -15,7 +15,8 @@ $(TYPEDSIGNATURES) Create a mapping from clustered Hamiltonian coordinates to a super square lattice arrangement. Variable number of Ising graph -> Factor graph coordinate system -This function generates a mapping that relates clustered Hamiltonian coordinates to a super square lattice arrangement. The super square lattice is defined by the size of five dimensions: `(m, um, n, un, t)`. +This function generates a mapping that relates clustered Hamiltonian coordinates to a super square lattice arrangement. +The super square lattice is defined by the size of five dimensions: `(m, um, n, un, t)`. # Arguments: - `size::NTuple{5, Int}`: A tuple specifying the size of the super square lattice in five dimensions: `(m, um, n, un, t)`. @@ -23,7 +24,8 @@ This function generates a mapping that relates clustered Hamiltonian coordinates # Returns: - `coord_map::Dict`: A dictionary that maps clustered Hamiltonian coordinates to the corresponding lattice coordinates. -The `size` tuple represents the dimensions of the super square lattice. The function creates a dictionary where clustered Hamiltonian coordinates are associated with their corresponding lattice coordinates. +The `size` tuple represents the dimensions of the super square lattice. The function creates a dictionary where +clustered Hamiltonian coordinates are associated with their corresponding lattice coordinates. """ function super_square_lattice(size::NTuple{5, Int}) m, um, n, un, t = size @@ -36,7 +38,8 @@ $(TYPEDSIGNATURES) Create a mapping from clustered Hamiltonian coordinates to a simplified super square lattice arrangement. -This function generates a mapping that relates clustered Hamiltonian coordinates to a simplified super square lattice arrangement. The simplified super square lattice is defined by the size of three dimensions: `(m, n, t)`. +This function generates a mapping that relates clustered Hamiltonian coordinates to a simplified super square lattice arrangement. +The simplified super square lattice is defined by the size of three dimensions: `(m, n, t)`. # Arguments: - `size::NTuple{3, Int}`: A tuple specifying the size of the simplified super square lattice in three dimensions: `(m, n, t)`. @@ -44,7 +47,8 @@ This function generates a mapping that relates clustered Hamiltonian coordinates # Returns: - `coord_map::Dict`: A dictionary that maps clustered Hamiltonian coordinates to the corresponding lattice coordinates. -The `size` tuple represents the dimensions of the simplified super square lattice. The function internally adds the required dimensions `(1, 1)` to make it compatible with the `super_square_lattice` function, which deals with five dimensions. +The `size` tuple represents the dimensions of the simplified super square lattice. +The function internally adds the required dimensions `(1, 1)` to make it compatible with the `super_square_lattice` function, which deals with five dimensions. """ function super_square_lattice(size::NTuple{3, Int}) m, n, t = size @@ -58,7 +62,8 @@ $(TYPEDSIGNATURES) Create a mapping from clustered Hamiltonian coordinates to Pegasus lattice coordinates. -This function generates a mapping that relates clustered Hamiltonian coordinates to Pegasus lattice coordinates based on the specified size of the Pegasus lattice in three dimensions: `(m, n, t)`. +This function generates a mapping that relates clustered Hamiltonian coordinates to Pegasus lattice coordinates +based on the specified size of the Pegasus lattice in three dimensions: `(m, n, t)`. # Arguments: - `size::NTuple{3, Int}`: A tuple specifying the size of the Pegasus lattice in three dimensions: `(m, n, t)`. @@ -66,7 +71,8 @@ This function generates a mapping that relates clustered Hamiltonian coordinates # Returns: - `coord_map::Dict`: A dictionary that maps clustered Hamiltonian coordinates to the corresponding Pegasus lattice coordinates. -The Pegasus lattice is a specialized lattice used in quantum computing, and this function allows you to convert between clustered Hamiltonian coordinates and Pegasus lattice coordinates. +The Pegasus lattice is a specialized lattice used in quantum computing, and this function allows you to convert +between clustered Hamiltonian coordinates and Pegasus lattice coordinates. """ function pegasus_lattice(size::NTuple{3, Int}) m, n, t = size @@ -132,7 +138,8 @@ $(TYPEDSIGNATURES) Create a mapping from clustered Hamiltonian coordinates to Zephyr lattice coordinates. -This function generates a mapping that relates clustered Hamiltonian coordinates to Zephyr lattice coordinates based on the specified size of the Zephyr lattice in three dimensions: `(m, n, t)`. +This function generates a mapping that relates clustered Hamiltonian coordinates to Zephyr lattice +coordinates based on the specified size of the Zephyr lattice in three dimensions: `(m, n, t)`. # Arguments: - `size::NTuple{3, Int}`: A tuple specifying the size of the Zephyr lattice in three dimensions: `(m, n, t)`. @@ -140,7 +147,8 @@ This function generates a mapping that relates clustered Hamiltonian coordinates # Returns: - `coord_map::Dict`: A dictionary that maps clustered Hamiltonian coordinates to the corresponding Zephyr lattice coordinates. -The Zephyr lattice is a specialized lattice used in quantum computing, and this function allows you to convert between clustered Hamiltonian coordinates and Zephyr lattice coordinates. +The Zephyr lattice is a specialized lattice used in quantum computing, and this function allows you to +convert between clustered Hamiltonian coordinates and Zephyr lattice coordinates. """ function zephyr_lattice(size::NTuple{3, Int}) m, n, t = size diff --git a/src/projectors.jl b/src/projectors.jl index ec16fcd..5b6f0cf 100644 --- a/src/projectors.jl +++ b/src/projectors.jl @@ -9,16 +9,20 @@ const Proj{T} = Union{Vector{T}, CuArray{T, 1}} """ $(TYPEDSIGNATURES) -`PoolOfProjectors` is a data structure for managing projectors associated with Ising model sites. It allows efficient storage and retrieval of projectors based on their indices and provides support for different computational devices. - - # Fields: - - `data::Dict{Symbol, Dict{Int, Proj{T}}}`: A dictionary that stores projectors associated with different computational devices (`:CPU`, `:GPU`, etc.). The inner dictionary maps site indices to projectors. - - `default_device::Symbol`: A symbol representing the default computational device for projectors in the pool. - - `sizes::Dict{Int, Int}`: A dictionary that maps site indices to the maximum projector size for each site. +`PoolOfProjectors` is a data structure for managing projectors associated with Ising model sites. +It allows efficient storage and retrieval of projectors based on their indices and provides support for different computational devices. + +# Fields: +- `data::Dict{Symbol, Dict{Int, Proj{T}}}`: A dictionary that stores projectors associated with different +computational devices (`:CPU`, `:GPU`, etc.). The inner dictionary maps site indices to projectors. +- `default_device::Symbol`: A symbol representing the default computational device for projectors in the pool. +- `sizes::Dict{Int, Int}`: A dictionary that maps site indices to the maximum projector size for each site. - # Constructors: - - `PoolOfProjectors(data::Dict{Int, Dict{Int, Vector{T}}}) where T`: Create a `PoolOfProjectors` with initial data for projectors. The data is provided as a dictionary that maps site indices to projectors stored in different computational devices. The `sizes` dictionary is automatically populated based on the maximum projector size for each site. - - `PoolOfProjectors{T}() where T`: Create an empty `PoolOfProjectors` with no projectors initially stored. +# Constructors: +- `PoolOfProjectors(data::Dict{Int, Dict{Int, Vector{T}}}) where T`: Create a `PoolOfProjectors` with initial data for projectors. +The data is provided as a dictionary that maps site indices to projectors stored in different computational devices. +The `sizes` dictionary is automatically populated based on the maximum projector size for each site. +- `PoolOfProjectors{T}() where T`: Create an empty `PoolOfProjectors` with no projectors initially stored. """ struct PoolOfProjectors{T <: Integer} data::Dict{Symbol, Dict{Int, Proj{T}}} @@ -69,7 +73,8 @@ TODO This is version for only one GPU Retrieve or create a projector from the `PoolOfProjectors` associated with a specific device. -This function retrieves a projector from the `PoolOfProjectors` if it already exists. If the projector does not exist in the pool, it creates a new one and stores it for future use on the specified computational device. +This function retrieves a projector from the `PoolOfProjectors` if it already exists. +If the projector does not exist in the pool, it creates a new one and stores it for future use on the specified computational device. # Arguments: - `lp::PoolOfProjectors{T}`: The `PoolOfProjectors` object containing projectors. @@ -102,7 +107,9 @@ $(TYPEDSIGNATURES) Add a projector to the `PoolOfProjectors` and associate it with an index. -This function adds a projector `p` to the `PoolOfProjectors`. The `PoolOfProjectors` stores projectors based on their computational device (e.g., CPU or GPU) and assigns a unique index to each projector. The index can be used to retrieve the projector later using `get_projector!`. +This function adds a projector `p` to the `PoolOfProjectors`. +The `PoolOfProjectors` stores projectors based on their computational device (e.g., CPU or GPU) and assigns a unique index to each projector. +The index can be used to retrieve the projector later using `get_projector!`. # Arguments: - `lp::PoolOfProjectors{T}`: The `PoolOfProjectors` object to which the projector should be added. diff --git a/src/spectrum.jl b/src/spectrum.jl index 78c465d..d38df75 100644 --- a/src/spectrum.jl +++ b/src/spectrum.jl @@ -69,7 +69,8 @@ $(TYPEDSIGNATURES) Calculates the energy of a state in an Ising graph. -This function calculates the energy of a given state in the context of an Ising graph. The energy is computed based on the interactions between spins and their associated biases. +This function calculates the energy of a given state in the context of an Ising graph. +The energy is computed based on the interactions between spins and their associated biases. # Arguments: - `σ::AbstractArray{State}`: An array representing the state of spins in the Ising graph. @@ -88,7 +89,8 @@ $(TYPEDSIGNATURES) Calculates the energy of a state in an Ising graph. -This function computes the energy of a given state in the context of an Ising graph. The energy is calculated based on the interactions between spins and their associated biases. +This function computes the energy of a given state in the context of an Ising graph. +The energy is calculated based on the interactions between spins and their associated biases. # Arguments: - `ig::IsingGraph{T}`: The Ising graph defining the interactions and biases. @@ -117,7 +119,8 @@ $(TYPEDSIGNATURES) Generates the energy spectrum for an Ising graph. -This function computes the energy spectrum (energies and corresponding states) for a given Ising graph. The energy spectrum represents all possible energy levels and their associated states in the Ising graph. +This function computes the energy spectrum (energies and corresponding states) for a given Ising graph. +The energy spectrum represents all possible energy levels and their associated states in the Ising graph. # Arguments: - `ig::IsingGraph{T}`: The Ising graph for which the energy spectrum is generated. @@ -145,7 +148,8 @@ $(TYPEDSIGNATURES) Computes the Gibbs tensor for an Ising graph at a given inverse temperature. -This function calculates the Gibbs tensor for an Ising graph at a specified inverse temperature (β). The Gibbs tensor represents the conditional probabilities of states given the inverse temperature and the Ising graph. +This function calculates the Gibbs tensor for an Ising graph at a specified inverse temperature (β). +The Gibbs tensor represents the conditional probabilities of states given the inverse temperature and the Ising graph. # Arguments: - `ig::IsingGraph{T}`: The Ising graph for which the Gibbs tensor is computed. diff --git a/src/truncate.jl b/src/truncate.jl index db298d8..1ae5f63 100644 --- a/src/truncate.jl +++ b/src/truncate.jl @@ -113,7 +113,8 @@ $(TYPEDSIGNATURES) Select a specified number of best states based on energy. -This function selects a specified number of best states from a list of energies based on energy values in two nodes of clustered hamiltonian. It fine-tunes the selection to ensure that the resulting states have the expected number. +This function selects a specified number of best states from a list of energies based on energy values in two nodes of clustered hamiltonian. +It fine-tunes the selection to ensure that the resulting states have the expected number. # Arguments: - `E::Vector{Real}`: A vector of energy values. @@ -121,7 +122,8 @@ This function selects a specified number of best states from a list of energies - `num_states::Int`: The desired number of states to select. # Returns: -- `Tuple{Vector{Int}, Vector{Int}}`: A tuple containing two vectors of indices, `ind1` and `ind2`, which represent the selected states for two nodes of a clustered Hamiltonian. +- `Tuple{Vector{Int}, Vector{Int}}`: A tuple containing two vectors of indices, `ind1` and `ind2`, +which represent the selected states for two nodes of a clustered Hamiltonian. """ function select_numstate_best(E, sx, num_states) low, high = 1, min(num_states, length(E))