Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix for remove_zero_valued_subgraphs! #186

Merged
merged 6 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ docs/assets
# committed for packages, but should be committed for applications that require a static
# environment.
Manifest.toml
docs/Manifest.toml

# Ignore intermediate latex files
*.aux
Expand Down
97 changes: 0 additions & 97 deletions docs/Manifest.toml

This file was deleted.

2 changes: 2 additions & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ makedocs(;
canonical="https://numericaleft.github.io/FeynmanDiagram.jl",
assets=["assets/custom.css"]
),
checkdocs=:exports, # check only exported names within the modules
pages=[
"Home" => "index.md",
"Manual" => [
Expand All @@ -29,6 +30,7 @@ makedocs(;
"lib/GV.md",
"lib/parquet.md",
"lib/backend.md",
"lib/utility.md"
]
]
)
Expand Down
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Pages = [
"lib/GV.md",
"lib/parquet.md",
"lib/backend.md",
"lib/utility.md",
]
Depth = 2
```
7 changes: 7 additions & 0 deletions docs/src/lib/utility.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Utility

## API

```@autodocs
Modules = [FeynmanDiagram.Utility]
```
2 changes: 1 addition & 1 deletion docs/src/manual/counterterms.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Since the order of differentiation w.r.t. $\mu$ and $\lambda$ does not matter, i

An example of the interaction counterterm evaluation for a diagram with $n_\lambda = 3$ and $m$ interaction lines. Since the Julia implementation evaluates the interaction counterterms of a given diagram as $\frac{(-\lambda)^n}{n!}\partial^n_\lambda V^m_\lambda$, we pick up an extra factor of $l!$ on each $l$th-order derivative in the chain rule.

![An example of the representation of interaction counterterm diagrams via differentiation.](../../assets/derivative_example.svg#derivative_example)
![An example of the representation of interaction counterterm diagrams via differentiation.](../assets/derivative_example.svg)

## Benchmark of counterterms in the UEG

Expand Down
12 changes: 6 additions & 6 deletions docs/src/manual/feynman_rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ G = g - g\Sigma g + g\Sigma g \Sigma g - ...
## Perturbative Expansion of the Green's Function


![Sign rule for the Wick contractions.](../../assets/diagrams/green0.svg#green0)
![Sign rule for the Wick contractions.](../assets/diagrams/green0.svg)

![Diagrammatic expansion of the Green's function.](../../assets/diagrams/green.svg#green)
![Diagrammatic expansion of the Green's function.](../assets/diagrams/green.svg)

The sign of a Green's function diagram is given by ``(-1)^{n_v} \xi^{n_F}``, where

Expand All @@ -95,7 +95,7 @@ The sign of a Green's function diagram is given by ``(-1)^{n_v} \xi^{n_F}``, whe
From the Green's function diagrams, one can derive the __negative__ self-energy diagram,


![Diagrammatic expansion of the self-energy.](../../assets/diagrams/sigma.svg#sigma)
![Diagrammatic expansion of the self-energy.](../assets/diagrams/sigma.svg)

```math
\begin{aligned}
Expand Down Expand Up @@ -123,7 +123,7 @@ where the indices $x, y$ could be different from diagrams to diagrams, and $\Gam
\Sigma_{3, x} -\Sigma^{Hartree}_{3, x} = G_{3,y} \cdot V_{3, 4} \cdot \Gamma^3_{4,y,x},
```

![Diagrammatic expansion of the 3-point vertex function.](../../assets/diagrams/gamma3.svg#gamma3)
![Diagrammatic expansion of the 3-point vertex function.](../assets/diagrams/gamma3.svg)

The diagram weights are given by,

Expand All @@ -146,7 +146,7 @@ The 4-point vertex function is related to the 3-point vertex function through an

where the indices $x, y, s, t$ could be different from diagrams to diagrams.

![Diagrammatic expansion of the 4-point vertex function.](../../assets/diagrams/gamma4.svg#gamma4)
![Diagrammatic expansion of the 4-point vertex function.](../assets/diagrams/gamma4.svg)

The diagram weights are given by,

Expand All @@ -169,7 +169,7 @@ The susceptibility can be derived from ``\Gamma^{(4)}``.
\chi_{1,2} \equiv \left<\mathcal{T} n_1 n_2\right>_{\text{connected}} = \xi G_{1,2} G_{2, 1} + \xi G_{1,s} G_{t, 1} \Gamma^{(4)}_{s, t, y, x} G_{2,y} G_{x, 2}
```

![Diagrammatic expansion of the susceptibility.](../../assets/diagrams/susceptibility.svg#susceptibility)
![Diagrammatic expansion of the susceptibility.](../assets/diagrams/susceptibility.svg)

We define the polarization ``P`` as the one-interaction irreducible (or proper) vertex function,

Expand Down
2 changes: 2 additions & 0 deletions src/computational_graph/feynmangraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ mutable struct FeynmanGraph{F<:Number,W} <: AbstractGraph # FeynmanGraph
@assert length(external_indices) == length(external_legs)
if typeof(operator) <: Power
@assert length(subgraphs) == 1 "FeynmanGraph with Power operator must have one and only one subgraph."
elseif typeof(operator) <: Unitary
@assert length(subgraphs) == 0 "FeynmanGraph with Unitary operator must have no subgraphs."
end
# @assert allunique(subgraphs) "all subgraphs must be distinct."
if isnothing(vertices)
Expand Down
2 changes: 2 additions & 0 deletions src/computational_graph/graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ mutable struct Graph{F<:Number,W} <: AbstractGraph # Graph
)
if typeof(operator) <: Power
@assert length(subgraphs) == 1 "Graph with Power operator must have one and only one subgraph."
elseif typeof(operator) <: Unitary
@assert length(subgraphs) == 0 "Graph with Unitary operator must have no subgraphs."
end
# @assert allunique(subgraphs) "all subgraphs must be distinct."
g = new{ftype,wtype}(uid(), String(name), orders, subgraphs, subgraph_factors, typeof(operator), weight, properties)
Expand Down
52 changes: 47 additions & 5 deletions src/computational_graph/transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,47 @@ end
"""
flatten_chains(g::AbstractGraph) = flatten_chains!(deepcopy(g))

"""
function mask_zero_subgraph_factors(operator::Type{<:AbstractOperator}, subg_fac::Vector{F}) where {F}

Returns a list of indices that should be considered when performing the operation (e.g., Sum, Prod, Power), effectively masking out zero values as appropriate.

The behavior of the function depends on the operator type:
- `Sum`: Returns all indices that are not equal to zero.
- `Prod`: Returns the index of the first zero value, or all indices if none are found.
- `Power`: Returns `[1]`, or error if the power is negative.
- Other `AbstractOperator`: Defaults to return all indices.
"""
function mask_zero_subgraph_factors(::Type{Sum}, subg_fac::Vector{F}) where {F}
mask_zeros = findall(x -> x != zero(x), subg_fac)
if isempty(mask_zeros)
mask_zeros = [1]
end
return mask_zeros
end
function mask_zero_subgraph_factors(::Type{Prod}, subg_fac::Vector{F}) where {F}
idx = findfirst(x -> x == zero(x), subg_fac)
if isnothing(idx)
mask_zeros = eachindex(subg_fac)
else
mask_zeros = [idx]
end
return mask_zeros
end
function mask_zero_subgraph_factors(::Type{Power{N}}, subg_fac::Vector{F}) where {N,F}
if N >= 0
return [1]
else
error("0^$N is illegal!")
end
end
function mask_zero_subgraph_factors(::Type{<:AbstractOperator}, subg_fac::Vector{F}) where {F}
@info("Masking zero-valued subgraphs when the node operator is $operator is not implemented. Defaulted to no mask! \n" *
"It's better to define a method `mask_zero_subgraph_factors(operator::Type, subg_fac::Vector{F})`."
)
return eachindex(subg_fac)
end

"""
function remove_zero_valued_subgraphs!(g::AbstractGraph)

Expand All @@ -391,15 +432,16 @@ function remove_zero_valued_subgraphs!(g::AbstractGraph)
zero_sgf = zero(subg_fac[1]) # F(0)
# Find subgraphs with all-zero subgraph_factors and propagate subfactor one level up
for (i, sub_g) in enumerate(subg)
if has_zero_subfactors(sub_g)
if isleaf(sub_g)
continue
end
if has_zero_subfactors(sub_g, sub_g.operator)
subg_fac[i] = zero_sgf
end
end

# Remove marked zero subgraph factor subgraph(s) of g
mask_zeros = findall(x -> x != zero(x), subg_fac)
if isempty(mask_zeros)
mask_zeros = [1] # retain eldest(g) if all subfactors are zero
end
mask_zeros = mask_zero_subgraph_factors(g.operator, subg_fac)
set_subgraphs!(g, subg[mask_zeros])
set_subgraph_factors!(g, subg_fac[mask_zeros])
return g
Expand Down
39 changes: 28 additions & 11 deletions src/computational_graph/tree_properties.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,39 @@ function ischain(g::AbstractGraph)
end

"""
function has_zero_subfactors(g)
function has_zero_subfactors(g::AbstractGraph, operator_type::Type{<:AbstractOperator})

Returns whether the graph g has only zero-valued subgraph factor(s).
Note that this function does not recurse through subgraphs of g, so that one may have, e.g.,
`has_zero_subfactors(g) == true` but `has_zero_subfactors(eldest(g)) == false`.
By convention, returns `false` if g is a leaf.
Determines whether the graph `g` has only zero-valued subgraph factors based on the specified operator type.
This function does not recurse through the subgraphs of `g`, so it only checks the immediate subgraph factors.
If `g` is a leaf (i.e., has no subgraphs), the function returns `false` by convention.

The behavior of the function depends on the operator type:
- `Sum`: Checks if all subgraph factors are zero.
- `Prod`: Checks if any subgraph factor is zero.
- `Power{N}`: Checks if the first subgraph factor is zero.
- Other `AbstractOperator`: Defaults to return `false`.
# Arguments:
- `g::AbstractGraph`: graph to be analyzed
- `operator`: the operator used in graph `g`
"""
function has_zero_subfactors(g::AbstractGraph)
if isleaf(g)
return false # convention: subgraph_factors = [] ⟹ subfactorless = false
else
return iszero(subgraph_factors(g))
end
function has_zero_subfactors(g::AbstractGraph, ::Type{Sum})
@assert g.operator == Sum "Operator must be Sum"
return iszero(subgraph_factors(g))
end

function has_zero_subfactors(g::AbstractGraph, ::Type{Prod})
@assert g.operator == Prod "Operator must be Prod"
return 0 in subgraph_factors(g)
end

function has_zero_subfactors(g::AbstractGraph, ::Type{Power{N}}) where {N}
@assert g.operator <: Power "Operator must be a Power"
return iszero(subgraph_factors(g)[1])
end

function has_zero_subfactors(g::AbstractGraph, ::Type{<:AbstractOperator})
@info "has_zero_subfactors: Operator type $operator is not specifically defined. Defaults to return false."
return false
end

"""
Expand Down
Loading
Loading