Skip to content

Commit

Permalink
Merge pull request #181 from numericalEFT/computgraph
Browse files Browse the repository at this point in the history
Bugfix in static compiler
  • Loading branch information
houpc authored Feb 23, 2024
2 parents bf73d30 + fae5efa commit 29d1826
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 82 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FeynmanDiagram"
uuid = "e424a512-dbd9-41ff-9883-094748823e72"
authors = ["Kun Chen", "Pengcheng Hou", "Daniel Cerkoney"]
version = "1.0.0"
version = "1.0.1"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
67 changes: 1 addition & 66 deletions src/backend/compiler_python.jl
Original file line number Diff line number Diff line change
@@ -1,68 +1,3 @@
# ms = pyimport("mindspore")

"""
function to_pystatic(operator::Type, subgraphs::AbstractVector{<:AbstractGraph}, subgraph_factors::AbstractVector)
Returns the static representation of a computational graph node `g` with operator `operator`, subgraphs `subgraphs`, and subgraph factors `subgraph_factors` in python.
"""
function to_pystatic(operator::Type, subgraphs::AbstractVector{<:AbstractGraph}, subgraph_factors::AbstractVector)
error(
"Static representation for computational graph nodes with operator $(operator) not yet implemented! " *
"Please define a method `to_static(::Type{$(operator)}, subgraphs::$(typeof(subgraphs)), subgraph_factors::$(typeof(subgraph_factors)))`."
)
end

function to_pystatic(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
if length(subgraphs) == 1
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])"
return "(g$(subgraphs[1].id)$factor_str)"
else
terms = ["g$(g.id)" * (gfactor == 1 ? "" : " * $gfactor") for (g, gfactor) in zip(subgraphs, subgraph_factors)]
return "(" * join(terms, " + ") * ")"
end
end

function to_pystatic(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
if length(subgraphs) == 1
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])"
return "(g$(subgraphs[1].id)$factor_str)"
else
terms = ["g$(g.id)" * (gfactor == 1 ? "" : " * $gfactor") for (g, gfactor) in zip(subgraphs, subgraph_factors)]
return "(" * join(terms, " * ") * ")"
# return "(" * join(["g$(g.id)" for g in subgraphs], " * ") * ")"
end
end

function to_pystatic(::Type{ComputationalGraphs.Power{N}}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {N,F,W}
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])"
return "((g$(subgraphs[1].id))**$N$factor_str)"
end

function to_pystatic(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
if length(subgraphs) == 1
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])"
return "(g$(subgraphs[1].id)$factor_str)"
else
terms = ["g$(g.id)" * (gfactor == 1 ? "" : " * $gfactor") for (g, gfactor) in zip(subgraphs, subgraph_factors)]
return "(" * join(terms, " + ") * ")"
end
end

function to_pystatic(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
if length(subgraphs) == 1
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])"
return "(g$(subgraphs[1].id)$factor_str)"
else
terms = ["g$(g.id)" * (gfactor == 1 ? "" : " * $gfactor") for (g, gfactor) in zip(subgraphs, subgraph_factors)]
return "(" * join(terms, " * ") * ")"
end
end

function to_pystatic(::Type{ComputationalGraphs.Power{N}}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {N,F,W}
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])"
return "((g$(subgraphs[1].id))**$N$factor_str)"
end

"""
function to_python_str(graphs::AbstractVector{<:AbstractGraph})
Expand Down Expand Up @@ -103,7 +38,7 @@ function to_python_str(graphs::AbstractVector{<:AbstractGraph}, framework::Symbo
push!(inds_visitedleaf, g_id)
else
g_id in inds_visitednode && continue
body *= " $target = $(to_pystatic(operator(g), subgraphs(g), subgraph_factors(g)))\n"
body *= " $target = $(to_static(operator(g), subgraphs(g), subgraph_factors(g), lang=:python))\n"
push!(inds_visitednode, g_id)
end
if isroot
Expand Down
43 changes: 32 additions & 11 deletions src/backend/static.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""
function to_static(operator::Type, subgraphs::AbstractVector{<:AbstractGraph}, subgraph_factors::AbstractVector)
Returns the static representation of a computational graph node `g` with operator `operator`, subgraphs `subgraphs`, and subgraph factors `subgraph_factors`.
Returns the static representation of a computational graph node `g` with operator `operator`, subgraphs `subgraphs`, and subgraph factors `subgraph_factors`.
"""
function to_static(operator::Type, subgraphs::AbstractVector{<:AbstractGraph}, subgraph_factors::AbstractVector)
function to_static(operator::Type, subgraphs::AbstractVector{<:AbstractGraph}, subgraph_factors::AbstractVector; lang::Symbol=:julia)
error(
"Static representation for computational graph nodes with operator $(operator) not yet implemented! " *
"Please define a method `to_static(::Type{$(operator)}, subgraphs::$(typeof(subgraphs)), subgraph_factors::$(typeof(subgraph_factors)))`."
)
end

function to_static(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
function to_static(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}; lang::Symbol=:julia) where {F,W}
if length(subgraphs) == 1
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])"
return "(g$(subgraphs[1].id)$factor_str)"
Expand All @@ -20,7 +20,7 @@ function to_static(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{Graph{F,W}
end
end

function to_static(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
function to_static(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}; lang::Symbol=:julia) where {F,W}
if length(subgraphs) == 1
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])"
return "(g$(subgraphs[1].id)$factor_str)"
Expand All @@ -31,12 +31,21 @@ function to_static(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{Graph{F,W
end
end

function to_static(::Type{ComputationalGraphs.Power{N}}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {N,F,W}
function to_static(::Type{ComputationalGraphs.Power{N}}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}; lang::Symbol=:julia) where {N,F,W}
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])"
return "((g$(subgraphs[1].id))^$N$factor_str)"
if lang == :julia
op_str = "^"
elseif lang == :c
return "pow(g$(subgraphs[1].id), $N)$factor_str"
elseif lang == :python
op_str = "**"
else
error("Unsupported language")
end
return "((g$(subgraphs[1].id))$(op_str)$N$factor_str)"
end

function to_static(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
function to_static(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}; lang::Symbol=:julia) where {F,W}
if length(subgraphs) == 1
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])"
return "(g$(subgraphs[1].id)$factor_str)"
Expand All @@ -46,7 +55,7 @@ function to_static(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{FeynmanGra
end
end

function to_static(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
function to_static(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}; lang::Symbol=:julia) where {F,W}
if length(subgraphs) == 1
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])"
return "(g$(subgraphs[1].id)$factor_str)"
Expand All @@ -56,9 +65,18 @@ function to_static(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{FeynmanGr
end
end

function to_static(::Type{ComputationalGraphs.Power{N}}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {N,F,W}
function to_static(::Type{ComputationalGraphs.Power{N}}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}; lang::Symbol=:julia) where {N,F,W}
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])"
return "((g$(subgraphs[1].id))^$N$factor_str)"
if lang == :julia
op_str = "^"
elseif lang == :c
return "pow(g$(subgraphs[1].id), $N)$factor_str"
elseif lang == :python
op_str = "**"
else
error("Unsupported language")
end
return "((g$(subgraphs[1].id))$(op_str)$N$factor_str)"
end

"""
Expand Down Expand Up @@ -165,7 +183,7 @@ function to_Cstr(graphs::AbstractVector{<:AbstractGraph}; root::AbstractVector{I
else
g_id in inds_visitednode && continue
declare *= " g$g_id,"
body *= " $target = $(to_static(operator(g), subgraphs(g), subgraph_factors(g)));\n"
body *= " $target = $(to_static(operator(g), subgraphs(g), subgraph_factors(g), lang=:c));\n"
push!(inds_visitednode, g_id)
end
if isroot
Expand Down Expand Up @@ -252,6 +270,9 @@ function compile_C(graphs::AbstractVector{<:AbstractGraph}, filename::String;
datatype::DataType=_dtype.weight, root::AbstractVector{Int}=[id(g) for g in graphs], func_name="eval_graph")
func_string, leafmap = to_Cstr(graphs; datatype=datatype, root=root, name=func_name)
open(filename, "a") do f
if position(f) == 0
write(f, "#include <math.h>\n")
end
write(f, func_string)
end
return leafmap
Expand Down
8 changes: 4 additions & 4 deletions src/frontend/frontends.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module FrontEnds

import ..ComputationalGraphs
import ..ComputationalGraphs
import ..ComputationalGraphs: Graph, FeynmanGraph, _dtype
import ..QuantumOperators
import ..Taylor
Expand Down Expand Up @@ -134,11 +134,11 @@ function leafstates(leaf_maps::Vector{Dict{Int,G}}, labelProd::LabelProduct) whe
for idx in 1:len_leaves
g = leafmap[idx]
vertices = g.properties.vertices
if IR.diagram_type(g) == IR.Interaction
if ComputationalGraphs.diagram_type(g) == ComputationalGraphs.Interaction
In = Out = vertices[1][1].label
push!(leafType[ikey], 0)
push!(leafLoopIndex[ikey], 1)
elseif IR.diagram_type(g) == IR.Propagator
elseif ComputationalGraphs.diagram_type(g) == ComputationalGraphs.Propagator
if (Op.isfermionic(vertices[1]))
In, Out = vertices[2][1].label, vertices[1][1].label
# push!(leafType[ikey], g.orders[1] * 2 + 1)
Expand Down Expand Up @@ -197,7 +197,7 @@ function leafstates(leaf_maps::Vector{Dict{Int,G}}, maxloopNum::Int) where {G<:G

for idx in 1:len_leaves
leaf = leafmap[idx]
@assert IR.isleaf(leaf)
@assert ComputationalGraphs.isleaf(leaf)
diagId, leaf_orders = leaf.properties, leaf.orders
loopmom = copy(diagId.extK)
len = length(loopmom)
Expand Down

2 comments on commit 29d1826

@houpc
Copy link
Member Author

@houpc houpc commented on 29d1826 Feb 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/101541

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.1 -m "<description of version>" 29d18269fb58822e92abd161a2f6e2a5032d1a45
git push origin v1.0.1

Please sign in to comment.