Skip to content

Commit

Permalink
add $(TYPEDSIGNATURES)
Browse files Browse the repository at this point in the history
  • Loading branch information
annamariadziubyna committed Oct 3, 2023
1 parent 1614f46 commit 425a4be
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 29 deletions.
6 changes: 3 additions & 3 deletions docs/src/userguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ instance = Dict((1, 1) => 1.0, (2, 2) => 0.5, (3, 3) => -0.25,
(1, 2) => -1.0, (2, 3) => 1.0)
ig = ising_graph(instance)
# Create factor graph.
# Create clustered Hamiltonian.
cl_h = clustered_hamiltonian(
ig,
cluster_assignment_rule = super_square_lattice((3, 1, 1))
Expand Down Expand Up @@ -95,7 +95,7 @@ cl_h = clustered_hamiltonian(
cluster_assignment_rule = pegasus_lattice((m, n, t))
)
println("Number of nodes in oryginal instance: ", length(LabelledGraphs.vertices(ig)), "\n", " Number of nodes in factor graph: ", length(LabelledGraphs.vertices(cl_h)))
println("Number of nodes in oryginal instance: ", length(LabelledGraphs.vertices(ig)), "\n", " Number of nodes in clustered Hamiltonian: ", length(LabelledGraphs.vertices(cl_h)))
```


Expand All @@ -121,5 +121,5 @@ cl_h = clustered_hamiltonian(
cluster_assignment_rule = zephyr_lattice((m, n, t))
)
println("Number of nodes in oryginal instance: ", length(LabelledGraphs.vertices(ig)), "\n", " Number of nodes in factor graph: ", length(LabelledGraphs.vertices(cl_h)))
println("Number of nodes in oryginal instance: ", length(LabelledGraphs.vertices(ig)), "\n", " Number of nodes in clustered Hamiltonian: ", length(LabelledGraphs.vertices(cl_h)))
```
20 changes: 20 additions & 0 deletions src/bp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export
SparseCSC

"""
$(TYPEDSIGNATURES)
Perform belief propagation on a given clustered hamiltonian.
# Arguments:
Expand Down Expand Up @@ -89,6 +90,7 @@ function belief_propagation(cl_h::LabelledGraph{S, T}, beta::Real; tol=1e-6, ite
end

"""
$(TYPEDSIGNATURES)
Returns the neighbors of a given vertex in a clustered Hamiltonian.
# Arguments:
Expand Down Expand Up @@ -123,6 +125,8 @@ function get_neighbors(cl_h::LabelledGraph{S, T}, vertex::NTuple) where {S, T}
end

"""
$(TYPEDSIGNATURES)
A custom Julia struct representing energy values in a merged format for use in specific calculations.
# Fields:
Expand All @@ -145,6 +149,8 @@ end
Base.adjoint(s::MergedEnergy) = MergedEnergy(s.e11', s.e21', s.e12', s.e22')

"""
$(TYPEDSIGNATURES)
Update a message using energy values and temperature.
# Arguments:
Expand All @@ -165,6 +171,8 @@ function update_message(E_bond::AbstractArray, message::Vector, beta::Real)
end

"""
$(TYPEDSIGNATURES)
Update a message using energy values and temperature in a merged energy format.
# Arguments:
Expand Down Expand Up @@ -225,6 +233,8 @@ function update_message(E_bond::MergedEnergy, message::Vector, beta::Real)
end

"""
$(TYPEDSIGNATURES)
Constructs a clustered Hamiltonian for a given clustered Hamiltonian with a 2-site cluster approximation.
# Arguments:
Expand Down Expand Up @@ -278,6 +288,8 @@ function clustered_hamiltonian_2site(cl_h::LabelledGraph{S, T}, beta::Real) wher
end

"""
$(TYPEDSIGNATURES)
Merge two vertices in a clustered Hamiltonian to create a single merged vertex.
# Arguments:
Expand Down Expand Up @@ -333,6 +345,8 @@ function merge_vertices_cl_h(cl_h::LabelledGraph{S, T}, β::Real, node1::NTuple{
end

"""
$(TYPEDSIGNATURES)
Get the local energy associated with a vertex in a clustered Hamiltonian.
# Arguments:
Expand All @@ -351,6 +365,8 @@ function local_energy(cl_h::LabelledGraph{S, T}, v::NTuple{3, Int64}) where {S,
end

"""
$(TYPEDSIGNATURES)
Get the interaction energy between two vertices in a clustered Hamiltonian.
# Arguments:
Expand All @@ -375,6 +391,8 @@ function interaction_energy(cl_h::LabelledGraph{S, T}, v::NTuple{3, Int64}, w::N
end

"""
$(TYPEDSIGNATURES)
Get the projector associated with an edge between two vertices in a clustered Hamiltonian.
# Arguments:
Expand Down Expand Up @@ -410,6 +428,8 @@ function outer_projector(p1::Array{T, 1}, p2::Array{T, 1}) where T <: Number
end

"""
$(TYPEDSIGNATURES)
Create a sparse column-compressed (CSC) matrix with specified column indices and values.
# Arguments:
Expand Down
40 changes: 32 additions & 8 deletions src/clustered_hamiltonian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ export
cluster_size

"""
Group spins into clusters based on an assignment rule, mapping factor graph coordinates to groups of spins in the Ising graph.
Dict(factor graph coordinates -> group of spins in Ising graph)
$(TYPEDSIGNATURES)
Group spins into clusters based on an assignment rule, mapping clustered Hamiltonian coordinates to groups of spins in the Ising graph.
Dict(clustered Hamiltonian coordinates -> group of spins in Ising graph)
# Arguments:
- `ig::LabelledGraph{G, L}`: The Ising graph represented as a labeled graph.
- `assignment_rule`: A mapping that assigns Ising graph vertices to clusters based on factor graph coordinates.
- `assignment_rule`: A mapping that assigns Ising graph vertices to clusters based on clustered Hamiltonian coordinates.
# 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 factor graph 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.
Expand All @@ -34,6 +36,8 @@ function split_into_clusters(ig::LabelledGraph{G, L}, assignment_rule) where {G,
end

"""
$(TYPEDSIGNATURES)
Create a clustered Hamiltonian.
This function constructs a clustered Hamiltonian from an Ising graph by introducing a natural order in clustered Hamiltonian coordinates.
Expand Down Expand Up @@ -62,6 +66,8 @@ function clustered_hamiltonian(
end

"""
$(TYPEDSIGNATURES)
Create a clustered Hamiltonian.
This function constructs a clustered Hamiltonian from an Ising graph by introducing a natural order in clustered Hamiltonian coordinates.
Expand Down Expand Up @@ -128,6 +134,8 @@ function clustered_hamiltonian(
end

"""
$(TYPEDSIGNATURES)
Create a clustered Hamiltonian with optional cluster sizes.
This function constructs a clustered Hamiltonian from an Ising graph by introducing a natural order in clustered Hamiltonian coordinates.
Expand Down Expand Up @@ -155,6 +163,8 @@ function clustered_hamiltonian(
end

"""
$(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.
Expand All @@ -180,6 +190,8 @@ function rank_reveal(energy, order=:PE) #TODO: add type
end

"""
$(TYPEDSIGNATURES)
TODO: check the order consistency over external packages.
Decode a clustered Hamiltonian state into Ising graph spin values.
Expand All @@ -193,7 +205,7 @@ 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 factor graph (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}()
Expand All @@ -209,6 +221,8 @@ function decode_clustered_hamiltonian_state(cl_h::LabelledGraph{S, T}, state::Ve
end

"""
$(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.
Expand Down Expand Up @@ -237,6 +251,8 @@ function energy(cl_h::LabelledGraph{S, T}, σ::Dict{T, Int}) where {S, T}
end

"""
$(TYPEDSIGNATURES)
Calculate the interaction energy between two nodes in a clustered Hamiltonian.
This function computes the interaction energy between two specified nodes in a clustered Hamiltonian, represented as a labeled graph.
Expand Down Expand Up @@ -274,6 +290,8 @@ function energy_2site(cl_h::LabelledGraph{S, T}, i::Int, j::Int) where {S, T}
end

"""
$(TYPEDSIGNATURES)
Calculate the bond energy between two clusters in a clustered Hamiltonian.
This function computes the bond energy between two specified clusters (cluster nodes) in a clustered Hamiltonian, represented as a labeled graph.
Expand All @@ -282,10 +300,10 @@ This function computes the bond energy between two specified clusters (cluster n
- `cl_h::LabelledGraph{S, T}`: The clustered Hamiltonian represented as a labeled graph.
- `cl_h_u::NTuple{N, Int64}`: The coordinates of the first cluster.
- `cl_h_v::NTuple{N, Int64}`: The coordinates of the second cluster.
- `σ::Int`: The Ising spin value for which the bond energy is calculated.
- `σ::Int`: Index for which the bond energy is calculated.
# Returns:
- `energies::AbstractVector{T}`: The bond energy vector between the two clusters for the specified Ising spin value.
- `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.
"""
Expand Down Expand Up @@ -315,6 +333,8 @@ function bond_energy(
end

"""
$(TYPEDSIGNATURES)
Get the size of a cluster in a clustered Hamiltonian.
This function returns the size (number of states) of a cluster in a clustered Hamiltonian, represented as a labeled graph.
Expand All @@ -333,9 +353,11 @@ function cluster_size(clustered_hamiltonian::LabelledGraph{S, T}, vertex::T) whe
end

"""
$(TYPEDSIGNATURES)
Calculate the exact conditional probability of a target state in a clustered Hamiltonian.
This function computes the exact conditional probability of a specified target state in a clustered Hamiltonian, represented as a labeled graph.
This function computes the exact conditional probability of a specified target state in a clustered Hamiltonian, represented as a labelled graph.
# Arguments:
- `clustered_hamiltonian::LabelledGraph{S, T}`: The clustered Hamiltonian represented as a labeled graph.
Expand All @@ -359,6 +381,8 @@ function exact_cond_prob(clustered_hamiltonian::LabelledGraph{S, T}, beta, targe
end

"""
$(TYPEDSIGNATURES)
Truncate a clustered Hamiltonian based on specified states.
This function truncates a given clustered Hamiltonian by selecting a subset of states for each cluster based on the provided `states` dictionary.
Expand Down
8 changes: 8 additions & 0 deletions src/ising.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ function unique_nodes(ising_tuples)
end

"""
$(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.
Expand Down Expand Up @@ -71,6 +73,8 @@ basis_size(ig::IsingGraph) = prod(rank_vec(ig))
biases(ig::IsingGraph) = get_prop.(Ref(ig), vertices(ig), :h)

"""
$(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`.
Expand All @@ -95,6 +99,8 @@ end
cluster(ig::IsingGraph, verts) = induced_subgraph(ig, collect(verts))

"""
$(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`.
Expand All @@ -121,6 +127,8 @@ function inter_cluster_edges(ig::IsingGraph{T}, cl1::IsingGraph{T}, cl2::IsingGr
end

"""
$(TYPEDSIGNATURES)
Used only in MPS_search, would be obsolete if MPS_search uses QMps.
Remove non-existing spins from an Ising graph.
Expand Down
Loading

0 comments on commit 425a4be

Please sign in to comment.