From 690964df586ac6adb0c69934d78038ddf564d83c Mon Sep 17 00:00:00 2001 From: houpc Date: Wed, 10 Jan 2024 20:30:46 +0800 Subject: [PATCH] remove factor field in AbstractGraph and clean up --- example/to_dot_parquetV2.jl | 8 +- src/backend/compiler.jl | 2 +- src/backend/compiler_python.jl | 14 +- src/backend/static.jl | 12 +- src/backend/to_dot.jl | 186 ++++------------------- src/computational_graph/abstractgraph.jl | 18 +-- src/computational_graph/feynmangraph.jl | 26 +--- src/computational_graph/graph.jl | 26 +--- src/computational_graph/io.jl | 9 +- src/computational_graph/operation.jl | 8 +- src/computational_graph/optimize.jl | 13 +- src/frontend/parquet/operation.jl | 1 - src/utility.jl | 7 - test/computational_graph.jl | 5 - 14 files changed, 75 insertions(+), 260 deletions(-) diff --git a/example/to_dot_parquetV2.jl b/example/to_dot_parquetV2.jl index 5831809a..21fdc14d 100644 --- a/example/to_dot_parquetV2.jl +++ b/example/to_dot_parquetV2.jl @@ -3,7 +3,7 @@ using FeynmanDiagram function recursive_print(diag) if typeof(diag) <: FeynmanDiagram.ComputationalGraphs.Graph if !isempty(diag.subgraphs) - print("$(diag.id) $(diag.factor) $(diag.subgraph_factors)\n") + print("$(diag.id) $(diag.subgraph_factors)\n") for subdiag in diag.subgraphs recursive_print(subdiag) end @@ -24,7 +24,7 @@ function main() KinL, KoutL, KinR = zeros(16), zeros(16), zeros(16) KinL[1], KoutL[2], KinR[3] = 1.0, 1.0, 1.0 # para = GV.diagPara(SigmaDiag, false, spin, order, [NoHartree], KinL - KoutL) - para = DiagParaF64(type=SigmaDiag, innerLoopNum=order, interaction=[Interaction(UpUp, [Instant,])], hasTau=true) + para = DiagPara(type=SigmaDiag, innerLoopNum=order, interaction=[Interaction(UpUp, [Instant,])], hasTau=true) # para = DiagParaF64(type=SigmaDiag, innerLoopNum=2, interaction=[Interaction(ChargeCharge, [Instant,])], hasTau=true) parquet_builder = Parquet.build(para) diag = parquet_builder.diagram @@ -38,8 +38,8 @@ function main() # print("new diag2\n") # recursive_print(eachd) # end - G = FrontEnds.Graph!(d[1]) - G = [eldest(G)] # drop extraneous Add node at root + # G = FrontEnds.Graph!(d[1]) + G = [eldest(d[1])] # drop extraneous Add node at root # for d in G # print("graph1\n") # recursive_print(d) diff --git a/src/backend/compiler.jl b/src/backend/compiler.jl index 95af7fb8..f2566a85 100644 --- a/src/backend/compiler.jl +++ b/src/backend/compiler.jl @@ -1,7 +1,7 @@ module Compilers using PyCall using ..ComputationalGraphs -import ..ComputationalGraphs: id, name, set_name!, operator, subgraphs, subgraph_factors, factor, FeynmanProperties +import ..ComputationalGraphs: id, name, set_name!, operator, subgraphs, subgraph_factors, FeynmanProperties using ..Parquet using ..Parquet: PropagatorId, BareGreenId, BareInteractionId diff --git a/src/backend/compiler_python.jl b/src/backend/compiler_python.jl index 1fbea267..a7a49822 100644 --- a/src/backend/compiler_python.jl +++ b/src/backend/compiler_python.jl @@ -96,15 +96,13 @@ function to_python_str(graphs::AbstractVector{<:AbstractGraph}, framework::Symbo end if isempty(subgraphs(g)) #leaf g_id in inds_visitedleaf && continue - factor_str = factor(g) == 1 ? "" : " * $(factor(g))" - body *= " $target = leaf[$(leafidx)]$factor_str\n" + body *= " $target = leaf[$(leafidx)]\n" gid_to_leafid[target] = leafidx leafidx += 1 push!(inds_visitedleaf, g_id) else g_id in inds_visitednode && continue - factor_str = factor(g) == 1 ? "" : " * $(factor(g))" - body *= " $target = $(to_pystatic(operator(g), subgraphs(g), subgraph_factors(g)))$factor_str\n" + body *= " $target = $(to_pystatic(operator(g), subgraphs(g), subgraph_factors(g)))\n" push!(inds_visitednode, g_id) end if isroot @@ -115,18 +113,18 @@ function to_python_str(graphs::AbstractVector{<:AbstractGraph}, framework::Symbo end head *= "def graphfunc(leaf):\n" output = ["root$(i)" for i in 0:rootidx-1] - output = join(output,",") + output = join(output, ",") tail = " return $output\n\n" if framework == :jax - tail *="graphfunc_jit = jit(graphfunc)" + tail *= "graphfunc_jit = jit(graphfunc)" end expr = head * body * tail - return expr, leafidx , gid_to_leafid + return expr, leafidx, gid_to_leafid end function compile_python(graphs::AbstractVector{<:AbstractGraph}, framework::Symbol=:jax, filename::String="GraphFunc.py") - py_string, leafnum, leafmap = to_python_str(graphs,framework) + py_string, leafnum, leafmap = to_python_str(graphs, framework) println("The number of leaves: $leafnum") open(filename, "w") do f write(f, py_string) diff --git a/src/backend/static.jl b/src/backend/static.jl index 11959749..40df4cc5 100644 --- a/src/backend/static.jl +++ b/src/backend/static.jl @@ -96,15 +96,13 @@ function to_julia_str(graphs::AbstractVector{<:AbstractGraph}; root::AbstractVec end if isempty(subgraphs(g)) #leaf g_id in inds_visitedleaf && continue - factor_str = factor(g) == 1 ? "" : " * $(factor(g))" - body *= " $target = leafVal[$idx_leafVal]$factor_str\n" + body *= " $target = leafVal[$idx_leafVal]\n" map_validx_leaf[idx_leafVal] = g idx_leafVal += 1 push!(inds_visitedleaf, g_id) else g_id in inds_visitednode && continue - factor_str = factor(g) == 1 ? "" : " * $(factor(g))" - body *= " $target = $(to_static(operator(g), subgraphs(g), subgraph_factors(g)))$factor_str\n" + body *= " $target = $(to_static(operator(g), subgraphs(g), subgraph_factors(g)))\n" push!(inds_visitednode, g_id) end if isroot @@ -160,16 +158,14 @@ function to_Cstr(graphs::AbstractVector{<:AbstractGraph}; root::AbstractVector{I if isempty(subgraphs(g)) #leaf g_id in inds_visitedleaf && continue declare *= " g$g_id," - factor_str = factor(g) == 1 ? "" : " * $(factor(g))" - body *= " $target = leafVal[$idx_leafVal]$factor_str;\n" + body *= " $target = leafVal[$idx_leafVal];\n" idx_leafVal += 1 map_validx_leaf[idx_leafVal] = g push!(inds_visitedleaf, g_id) else g_id in inds_visitednode && continue declare *= " g$g_id," - factor_str = factor(g) == 1 ? "" : " * $(factor(g))" - body *= " $target = $(to_static(operator(g), subgraphs(g), subgraph_factors(g)))$factor_str;\n" + body *= " $target = $(to_static(operator(g), subgraphs(g), subgraph_factors(g)));\n" push!(inds_visitednode, g_id) end if isroot diff --git a/src/backend/to_dot.jl b/src/backend/to_dot.jl index 5228a818..410e63ab 100644 --- a/src/backend/to_dot.jl +++ b/src/backend/to_dot.jl @@ -1,33 +1,17 @@ -function to_dotstatic(operator::Type, id::Int, factor, subgraphs::AbstractVector{<:AbstractGraph}, subgraph_factors::AbstractVector) +function to_dotstatic(operator::Type, id::Int, subgraphs::AbstractVector{<:AbstractGraph}, subgraph_factors::AbstractVector) error( "Static representation for computational graph nodes with operator $(operator) not yet implemented! " ) end -function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, factor::F, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W} +function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W} node_temp = "" arrow_temp = "" - if factor != 1 - # opr_fac = "factor$(id)[label=$(factor), style=filled, fillcolor=lavender]\n" - # opr_name = "g$(id)_t" - # node_str = "g$(id)[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,]\n" - # arrow_temp *= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n" - # node_temp *= opr_fac * node_str - opr_node = "g$(id)[shape=box, label = <($factor)*⊕>, style=filled, fillcolor=cyan,fontsize=18]" - else - opr_node = "g$(id)[shape=box, label = <⊕>, style=filled, fillcolor=cyan,fontsize=18]" - # opr_name = "g$id" - end - opr_name = "g$id" - # opr_node = opr_name * "[shape=box, label = <⊕>, style=filled, fillcolor=cyan,]\n" + # opr_node = "g$(id)[shape=box, label = <($factor)*⊕>, style=filled, fillcolor=cyan,fontsize=18]" + opr_node = "g$(id)[shape=box, label = <⊕>, style=filled, fillcolor=cyan,fontsize=18]" node_temp *= opr_node - for (gix, (g, gfactor)) in enumerate(zip(subgraphs, subgraph_factors)) + for (g, gfactor) in zip(subgraphs, subgraph_factors) if gfactor != 1 - # factor_str = "factor$(g.id)_$(id)_$gix[label=$(gfactor), style=filled, fillcolor=lavender]\n" - # subg_str = "g$(g.id)_$(id)_$gix[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,]\n" - # node_temp *= factor_str * subg_str - # arrow_temp *= "factor$(g.id)_$(id)_$gix->g$(g.id)_$(id)_$gix[arrowhead=vee,]\ng$(g.id)->g$(g.id)_$(id)_$gix[arrowhead=vee,]\n" - # arrow_temp *= "g$(g.id)_$(id)_$gix->$opr_name[arrowhead=vee,]\n" arrow_temp *= "g$(g.id)->g$(id)[arrowhead=vee,label=$gfactor,fontsize=16]\n" else arrow_temp *= "g$(g.id)->g$(id)[arrowhead=vee,]\n" @@ -36,37 +20,14 @@ function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, factor::F, subgr return node_temp, arrow_temp end -function to_dotstatic(::Type{ComputationalGraphs.Prod}, id::Int, factor::F, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W} +function to_dotstatic(::Type{ComputationalGraphs.Prod}, id::Int, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W} node_temp = "" arrow_temp = "" - if factor != 1 - # opr_fac = "factor$(id)[label=$(factor), style=filled, fillcolor=lavender]\n" - # opr_name = "g$(id)_t" - # node_str = "g$(id)[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,]\n" - # arrow_temp *= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n" - # node_temp *= opr_fac * node_str - opr_node = "g$id[shape=box, label = <($factor)⊗>, style=filled, fillcolor=cornsilk,fontsize=18]\n" - else - opr_node = "g$id[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,fontsize=18]\n" - end - # opr_node = opr_name * "[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,]\n" + # opr_node = "g$id[shape=box, label = <($factor)⊗>, style=filled, fillcolor=cornsilk,fontsize=18]\n" + opr_node = "g$id[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,fontsize=18]\n" node_temp *= opr_node - # if length(subgraphs) == 1 - # if subgraph_factors[1] == 1 - # arrow_temp *= "g$(subgraphs[1].id)->$opr_name[arrowhead=vee,]\n" - # else - # factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n" - # node_temp *= factor_str - # arrow_temp *= "factor$(subgraphs[1].id)_$(id)->$opr_name[arrowhead=vee,]\ng$(subgraphs[1].id)->$opr_name[arrowhead=vee,]\n" - # end - # else - for (gix, (g, gfactor)) in enumerate(zip(subgraphs, subgraph_factors)) + for (g, gfactor) in zip(subgraphs, subgraph_factors) if gfactor != 1 - # factor_str = "factor$(g.id)_$(id)_$gix[label=$(gfactor), style=filled, fillcolor=lavender]\n" - # subg_str = "g$(g.id)_$(id)_$gix[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,]\n" - # node_temp *= factor_str * subg_str - # arrow_temp *= "factor$(g.id)_$(id)_$gix->g$(g.id)_$(id)_$gix[arrowhead=vee,]\ng$(g.id)->g$(g.id)_$(id)_$gix[arrowhead=vee,]\n" - # arrow_temp *= "g$(g.id)_$(id)_$gix->$opr_name[arrowhead=vee,]\n" arrow_temp *= "g$(g.id)->g$(id)[arrowhead=vee,label=$gfactor,fontsize=16]\n" else arrow_temp *= "g$(g.id)->g$(id)[arrowhead=vee,]\n" @@ -76,29 +37,13 @@ function to_dotstatic(::Type{ComputationalGraphs.Prod}, id::Int, factor::F, subg return node_temp, arrow_temp end -function to_dotstatic(::Type{ComputationalGraphs.Power{N}}, id::Int, factor::F, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {N,F,W} +function to_dotstatic(::Type{ComputationalGraphs.Power{N}}, id::Int, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {N,F,W} node_temp = "" arrow_temp = "" - if factor != 1 - # opr_fac = "factor$(id)[label=$(factor), style=filled, fillcolor=lavender]\n" - # opr_name = "g$(id)_t" - # node_str = "g$(id)[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,]\n" - # arrow_temp *= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n" - # node_temp *= opr_fac * node_str - opr_node = "g$id[shape=box, label = <($factor)*Pow($N)>, style=filled, fillcolor=darkolivegreen,fontsize=18]\n" - else - opr_node = "g$id[shape=box, label = , style=filled, fillcolor=darkolivegreen,fontsize=18]\n" - end - # opr_node = "g$id[shape=box, label = , style=filled, fillcolor=darkolivegreen,]\n" - # order_node = "order$(id)[label=$N, style=filled, fillcolor=lavender]\n" - # node_temp *= opr_node * order_node + # opr_node = "g$id[shape=box, label = <($factor)*Pow($N)>, style=filled, fillcolor=darkolivegreen,fontsize=18]\n" + opr_node = "g$id[shape=box, label = , style=filled, fillcolor=darkolivegreen,fontsize=18]\n" node_temp *= opr_node - # arrow_temp *= "order$(id)->$opr_name[arrowhead=vee,]\n" if subgraph_factors[1] != 1 - # factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n" - # subg_str = "g$(subgraphs[1].id)_$(id)[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,]\n" - # node_temp *= factor_str * subg_str - # arrow_temp *= "factor$(subgraphs[1].id)_$(id)->g$(subgraphs[1].id)_$(id)[arrowhead=vee,]\ng$(subgraphs[1].id)->g$(subgraphs[1].id)_$(id)[arrowhead=vee,]\n" arrow_temp *= "g$(subgraphs[1].id)->$opr_name[arrowhead=vee,label=$gfactor,fontsize=16]\n" else arrow_temp *= "g$(subgraphs[1].id)->$opr_name[arrowhead=vee,]\n" @@ -106,30 +51,15 @@ function to_dotstatic(::Type{ComputationalGraphs.Power{N}}, id::Int, factor::F, return node_temp, arrow_temp end -function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, factor::F, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W} +function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W} node_temp = "" arrow_temp = "" - if factor != 1 - # opr_fac = "factor$(id)[label=$(factor), style=filled, fillcolor=lavender]\n" - # opr_name = "g$(id)_t" - # node_str = "g$(id)[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,]\n" - # arrow_temp *= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n" - # node_temp *= opr_fac * node_str - opr_node = "g$(id)[shape=box, label = <($factor)*⊕>, style=filled, fillcolor=cyan,fontsize=18]" - else - opr_node = "g$(id)[shape=box, label = <⊕>, style=filled, fillcolor=cyan,fontsize=18]" - # opr_name = "g$id" - end + # opr_node = "g$(id)[shape=box, label = <($factor)*⊕>, style=filled, fillcolor=cyan,fontsize=18]" + opr_node = "g$(id)[shape=box, label = <⊕>, style=filled, fillcolor=cyan,fontsize=18]" opr_name = "g$id" - # opr_node = opr_name * "[shape=box, label = <⊕>, style=filled, fillcolor=cyan,]\n" node_temp *= opr_node - for (gix, (g, gfactor)) in enumerate(zip(subgraphs, subgraph_factors)) + for (g, gfactor) in zip(subgraphs, subgraph_factors) if gfactor != 1 - # factor_str = "factor$(g.id)_$(id)_$gix[label=$(gfactor), style=filled, fillcolor=lavender]\n" - # subg_str = "g$(g.id)_$(id)_$gix[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,]\n" - # node_temp *= factor_str * subg_str - # arrow_temp *= "factor$(g.id)_$(id)_$gix->g$(g.id)_$(id)_$gix[arrowhead=vee,]\ng$(g.id)->g$(g.id)_$(id)_$gix[arrowhead=vee,]\n" - # arrow_temp *= "g$(g.id)_$(id)_$gix->$opr_name[arrowhead=vee,]\n" arrow_temp *= "g$(g.id)->$opr_name[arrowhead=vee,label=$gfactor,fontsize=16]\n" else arrow_temp *= "g$(g.id)->$opr_name[arrowhead=vee,]\n" @@ -138,37 +68,14 @@ function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, factor::F, subgr return node_temp, arrow_temp end -function to_dotstatic(::Type{ComputationalGraphs.Prod}, id::Int, factor::F, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W} +function to_dotstatic(::Type{ComputationalGraphs.Prod}, id::Int, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W} node_temp = "" arrow_temp = "" - if factor != 1 - # opr_fac = "factor$(id)[label=$(factor), style=filled, fillcolor=lavender]\n" - # opr_name = "g$(id)_t" - # node_str = "g$(id)[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,]\n" - # arrow_temp *= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n" - # node_temp *= opr_fac * node_str - opr_node = "g$id[shape=box, label = <($factor)⊗>, style=filled, fillcolor=cornsilk,fontsize=18]\n" - else - opr_node = "g$id[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,fontsize=18]\n" - end - # opr_node = opr_name * "[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,]\n" + # opr_node = "g$id[shape=box, label = <($factor)⊗>, style=filled, fillcolor=cornsilk,fontsize=18]\n" + opr_node = "g$id[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,fontsize=18]\n" node_temp *= opr_node - # if length(subgraphs) == 1 - # if subgraph_factors[1] == 1 - # arrow_temp *= "g$(subgraphs[1].id)->$opr_name[arrowhead=vee,]\n" - # else - # factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n" - # node_temp *= factor_str - # arrow_temp *= "factor$(subgraphs[1].id)_$(id)->$opr_name[arrowhead=vee,]\ng$(subgraphs[1].id)->$opr_name[arrowhead=vee,]\n" - # end - # else - for (gix, (g, gfactor)) in enumerate(zip(subgraphs, subgraph_factors)) + for (g, gfactor) in zip(subgraphs, subgraph_factors) if gfactor != 1 - # factor_str = "factor$(g.id)_$(id)_$gix[label=$(gfactor), style=filled, fillcolor=lavender]\n" - # subg_str = "g$(g.id)_$(id)_$gix[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,]\n" - # node_temp *= factor_str * subg_str - # arrow_temp *= "factor$(g.id)_$(id)_$gix->g$(g.id)_$(id)_$gix[arrowhead=vee,]\ng$(g.id)->g$(g.id)_$(id)_$gix[arrowhead=vee,]\n" - # arrow_temp *= "g$(g.id)_$(id)_$gix->$opr_name[arrowhead=vee,]\n" arrow_temp *= "g$(g.id)->g$(id)[arrowhead=vee,label=$gfactor,fontsize=16]\n" else arrow_temp *= "g$(g.id)->g$(id)[arrowhead=vee,]\n" @@ -178,30 +85,14 @@ function to_dotstatic(::Type{ComputationalGraphs.Prod}, id::Int, factor::F, subg return node_temp, arrow_temp end -function to_dotstatic(::Type{ComputationalGraphs.Power{N}}, id::Int, factor::F, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {N,F,W} +function to_dotstatic(::Type{ComputationalGraphs.Power{N}}, id::Int, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {N,F,W} node_temp = "" arrow_temp = "" - if factor != 1 - # opr_fac = "factor$(id)[label=$(factor), style=filled, fillcolor=lavender]\n" - # opr_name = "g$(id)_t" - # node_str = "g$(id)[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,]\n" - # arrow_temp *= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n" - # node_temp *= opr_fac * node_str - opr_node = "g$id[shape=box, label = <($factor)*Pow($N)>, style=filled, fillcolor=darkolivegreen,fontsize=18]\n" - else - opr_node = "g$id[shape=box, label = , style=filled, fillcolor=darkolivegreen,fontsize=18]\n" - end - # opr_node = "g$id[shape=box, label = , style=filled, fillcolor=darkolivegreen,]\n" - # order_node = "order$(id)[label=$N, style=filled, fillcolor=lavender]\n" - # node_temp *= opr_node * order_node + # opr_node = "g$id[shape=box, label = <($factor)*Pow($N)>, style=filled, fillcolor=darkolivegreen,fontsize=18]\n" + opr_node = "g$id[shape=box, label = , style=filled, fillcolor=darkolivegreen,fontsize=18]\n" node_temp *= opr_node - # arrow_temp *= "order$(id)->$opr_name[arrowhead=vee,]\n" if subgraph_factors[1] != 1 - # factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n" - # subg_str = "g$(subgraphs[1].id)_$(id)[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,]\n" - # node_temp *= factor_str * subg_str - # arrow_temp *= "factor$(subgraphs[1].id)_$(id)->g$(subgraphs[1].id)_$(id)[arrowhead=vee,]\ng$(subgraphs[1].id)->g$(subgraphs[1].id)_$(id)[arrowhead=vee,]\n" - arrow_temp *= "g$(subgraphs[1].id)->$opr_name[arrowhead=vee,label=$gfactor,fontsize=16]\n" + arrow_temp *= "g$(subgraphs[1].id)->$opr_name[arrowhead=vee,label=$(subgraph_factors[1]),fontsize=16]\n" else arrow_temp *= "g$(subgraphs[1].id)->$opr_name[arrowhead=vee,]\n" end @@ -237,31 +128,13 @@ function to_dot_str(graphs::AbstractVector{<:AbstractGraph}, name::String="") if isempty(subgraphs(g)) #leaf g_id in inds_visitedleaf && continue leafname = get_leafname(g, leafidx) - if factor(g) == 1 - gnode_str = "g$g_id[label=<$leafname>, style=filled, fillcolor=paleturquoise,fontsize=18]\n" - body_node *= gnode_str - elseif factor(g) == -1 - # println("BareInteraction with -1 factor!") - # @assert typeof(g.properties) == BareInteractionId - # leafname = "<-V$leafidx>" - # gnode_str = "g$g_id[label=$leafname, style=filled, fillcolor=paleturquoise]\n" - # body_node *= gnode_str - gnode_str = "g$g_id[label=<-$leafname>, style=filled, fillcolor=paleturquoise,fontsize=18]\n" - body_node *= gnode_str - else - # factor_str = "factor$(leafidx)_inp[label=$(factor(g)), style=filled, fillcolor=lavender]\n" - # leaf_node = "l$(leafidx)[label=$leafname, style=filled, fillcolor=paleturquoise]\n" - # gnode_str = "g$g_id[shape=box, label = <⊗>, style=filled, fillcolor=cornsilk,]\n" - gnode_str = "l$(leafidx)[label=<$(factor(g))$leafname>, style=filled, fillcolor=paleturquoise,fontsize=18]\n" - # body_node *= factor_str * leaf_node * gnode_str - # body_node *= gnode_str - # body_arrow *= "factor$(leafidx)_inp->g$g_id[arrowhead=vee,]\nl$(leafidx)->g$g_id[arrowhead=vee,]\n" - end + gnode_str = "g$g_id[label=<$leafname>, style=filled, fillcolor=paleturquoise,fontsize=18]\n" + body_node *= gnode_str leafidx += 1 push!(inds_visitedleaf, g_id) else g_id in inds_visitednode && continue - temp_node, temp_arrow = to_dotstatic(operator(g), g_id, factor(g), subgraphs(g), subgraph_factors(g)) + temp_node, temp_arrow = to_dotstatic(operator(g), g_id, subgraphs(g), subgraph_factors(g)) body_node *= temp_node body_arrow *= temp_arrow push!(inds_visitednode, g_id) @@ -297,10 +170,9 @@ function get_leafname(g, leafidx) if leaftype == BareGreenId leafname = "G$leafidx" - println(leaftype, ": ", leafidx, " ", g.factor, " ", " ", g.properties.extK, " ", g.properties.extT, " ", g.properties.order) + println(leaftype, ": ", leafidx, " ", g.properties.extK, " ", g.properties.extT) elseif leaftype == BareInteractionId - println(leaftype, ": ", leafidx, " ", g.factor, " ", g.properties.response, " ", g.properties.type, - " ", g.properties.permutation, " ", g.properties.extK, " ", g.properties.extT, " ", g.properties.order) + println(leaftype, ": ", leafidx, " ", g.properties.response, " ", g.properties.type, " ", g.properties.extK, " ", g.properties.extT) leafname = "V$leafidx" elseif leaftype == PolarId leafname = "Π$leafidx" diff --git a/src/computational_graph/abstractgraph.jl b/src/computational_graph/abstractgraph.jl index b5dd2f47..5891a8d1 100644 --- a/src/computational_graph/abstractgraph.jl +++ b/src/computational_graph/abstractgraph.jl @@ -3,7 +3,7 @@ abstract type AbstractGraph end abstract type AbstractOperator end struct Sum <: AbstractOperator end struct Prod <: AbstractOperator end -struct Constant <: AbstractOperator end +struct Unitary <: AbstractOperator end struct Power{N} <: AbstractOperator function Power(N::Int) @assert N ∉ [0, 1] "Power{$N} makes no sense." @@ -19,7 +19,7 @@ apply(o::AbstractOperator, diags) = error("not implemented!") Base.show(io::IO, o::AbstractOperator) = print(io, typeof(o)) Base.show(io::IO, ::Type{Sum}) = print(io, "⨁") Base.show(io::IO, ::Type{Prod}) = print(io, "Ⓧ") -Base.show(io::IO, ::Type{Constant}) = print(io, "C") +Base.show(io::IO, ::Type{Unitary}) = print(io, "𝟙") Base.show(io::IO, ::Type{Power{N}}) where {N} = print(io, "^$N") # Is the unary form of operator 𝓞 trivial: 𝓞(G) ≡ G? @@ -82,13 +82,6 @@ function operator(g::AbstractGraph) """ function operator(g::AbstractGraph) end -""" -function factor(g::AbstractGraph) - - Returns the fixed scalar-multiplicative factor of the computational graph `g`. -""" -function factor(g::AbstractGraph) end - """ function weight(g::AbstractGraph) @@ -184,13 +177,6 @@ function set_operator!(g::AbstractGraph, operator::Type{<:AbstractOperator}) """ function set_operator!(g::AbstractGraph, operator::Type{<:AbstractOperator}) end -""" -function set_factor!(g::AbstractGraph, factor) - - Update the factor of graph `g` to `factor`. -""" -function set_factor!(g::AbstractGraph, factor) end - """ function set_weight!(g::AbstractGraph, weight) diff --git a/src/computational_graph/feynmangraph.jl b/src/computational_graph/feynmangraph.jl index c33e848a..d3a38206 100644 --- a/src/computational_graph/feynmangraph.jl +++ b/src/computational_graph/feynmangraph.jl @@ -55,7 +55,6 @@ drop_topology(p::FeynmanProperties) = FeynmanProperties(p.diagtype, p.vertices, - `subgraphs::Vector{FeynmanGraph{F,W}}` vector of sub-diagrams - `subgraph_factors::Vector{F}` scalar multiplicative factors associated with each subdiagram - `operator::DataType` node operation (Sum, Prod, etc.) -- `factor::F` a number representing the total scalar multiplicative factor for the diagram. - `weight::W` weight of the diagram # Example: @@ -80,7 +79,6 @@ mutable struct FeynmanGraph{F<:Number,W} <: AbstractGraph # FeynmanGraph subgraph_factors::Vector{F} operator::DataType - factor::F weight::W """ @@ -119,13 +117,12 @@ mutable struct FeynmanGraph{F<:Number,W} <: AbstractGraph # FeynmanGraph vertices = [external_operators(g) for g in subgraphs if diagram_type(g) != Propagator] end properties = FeynmanProperties(typeof(diagtype), vertices, topology, external_indices, external_legs) - # return new{ftype,wtype}(uid(), name, orders, properties, subgraphs, subgraph_factors, typeof(operator), factor, weight) - g = new{ftype,wtype}(uid(), String(name), orders, properties, subgraphs, subgraph_factors, typeof(operator), one(ftype), weight) + g = new{ftype,wtype}(uid(), String(name), orders, properties, subgraphs, subgraph_factors, typeof(operator), weight) if factor ≈ one(ftype) return g else - return new{ftype,wtype}(uid(), String(name), orders, properties, [g,], [factor,], Prod, one(ftype), weight * factor) + return new{ftype,wtype}(uid(), String(name), orders, properties, [g,], [factor,], Prod, weight * factor) end end @@ -156,13 +153,11 @@ mutable struct FeynmanGraph{F<:Number,W} <: AbstractGraph # FeynmanGraph @assert length(subgraphs) == 1 "FeynmanGraph with Power operator must have one and only one subgraph." end # @assert allunique(subgraphs) "all subgraphs must be distinct." - # return new{ftype,wtype}(uid(), name, orders, properties, subgraphs, subgraph_factors, typeof(operator), factor, weight) - g = new{ftype,wtype}(uid(), String(name), orders, properties, subgraphs, subgraph_factors, typeof(operator), one(ftype), weight) - + g = new{ftype,wtype}(uid(), String(name), orders, properties, subgraphs, subgraph_factors, typeof(operator), weight) if factor ≈ one(ftype) return g else - return new{ftype,wtype}(uid(), String(name), orders, properties, [g,], [factor,], Prod, one(ftype), weight * factor) + return new{ftype,wtype}(uid(), String(name), orders, properties, [g,], [factor,], Prod, weight * factor) end end @@ -178,8 +173,8 @@ mutable struct FeynmanGraph{F<:Number,W} <: AbstractGraph # FeynmanGraph function FeynmanGraph(g::Graph{F,W}, properties::FeynmanProperties) where {F,W} @assert length(properties.external_indices) == length(properties.external_legs) # @assert allunique(subgraphs) "all subgraphs must be distinct." - # return new{F,W}(uid(), g.name, g.orders, properties, g.subgraphs, g.subgraph_factors, g.operator, g.factor, g.weight) - return new{F,W}(uid(), g.name, g.orders, properties, [FeynmanGraph(subg, subg.properties) for subg in g.subgraphs], g.subgraph_factors, g.operator, g.factor, g.weight) + # return new{F,W}(uid(), g.name, g.orders, properties, g.subgraphs, g.subgraph_factors, g.operator, g.weight) + return new{F,W}(uid(), g.name, g.orders, properties, [FeynmanGraph(subg, subg.properties) for subg in g.subgraphs], g.subgraph_factors, g.operator, g.weight) end end @@ -190,7 +185,6 @@ id(g::FeynmanGraph) = g.id name(g::FeynmanGraph) = g.name orders(g::FeynmanGraph) = g.orders operator(g::FeynmanGraph) = g.operator -factor(g::FeynmanGraph) = g.factor weight(g::FeynmanGraph) = g.weight subgraph(g::FeynmanGraph, i=1) = g.subgraphs[i] subgraphs(g::FeynmanGraph) = g.subgraphs @@ -205,7 +199,6 @@ set_name!(g::FeynmanGraph, name::String) = (g.name = name) set_orders!(g::FeynmanGraph, orders::Vector{Int}) = (g.orders = orders) set_operator!(g::FeynmanGraph, operator::Type{<:AbstractOperator}) = (g.operator = operator) set_operator!(g::FeynmanGraph, operator::AbstractOperator) = (g.operator = typeof(operator)) -set_factor!(g::FeynmanGraph{F,W}, factor) where {F,W} = (g.factor = F(factor)) set_weight!(g::FeynmanGraph{F,W}, weight) where {F,W} = (g.weight = W(weight)) set_subgraph!(g::FeynmanGraph{F,W}, subgraph::FeynmanGraph{F,W}, i=1) where {F,W} = (g.subgraphs[i] = subgraph) set_subgraphs!(g::FeynmanGraph{F,W}, subgraphs::Vector{FeynmanGraph{F,W}}) where {F,W} = (g.subgraphs = subgraphs) @@ -313,7 +306,6 @@ function Base.:*(g1::FeynmanGraph{F,W}, c2) where {F,W} # Convert trivial unary link to in-place form if unary_istrivial(g1) && onechild(g1) g.subgraph_factors[1] *= g1.subgraph_factors[1] - # g.subgraph_factors[1] *= g1.subgraph_factors[1] * g1.factor g.subgraphs = g1.subgraphs end return g @@ -333,7 +325,6 @@ function Base.:*(c1, g2::FeynmanGraph{F,W}) where {F,W} # Convert trivial unary link to in-place form if unary_istrivial(g2) && onechild(g2) g.subgraph_factors[1] *= g2.subgraph_factors[1] - # g.subgraph_factors[1] *= g2.subgraph_factors[1] * g2.factor g.subgraphs = g2.subgraphs end return g @@ -366,12 +357,10 @@ function linear_combination(g1::FeynmanGraph{F,W}, g2::FeynmanGraph{F,W}, c1=F(1 # Convert trivial unary links to in-place form if unary_istrivial(g1) && onechild(g1) subgraph_factors[1] *= g1.subgraph_factors[1] - # subgraph_factors[1] *= g1.subgraph_factors[1] * g1.factor subgraphs[1] = g1.subgraphs[1] end if unary_istrivial(g2) && onechild(g2) subgraph_factors[2] *= g2.subgraph_factors[1] - # subgraph_factors[2] *= g2.subgraph_factors[1] * g2.factor subgraphs[2] = g2.subgraphs[1] end @@ -418,14 +407,13 @@ function linear_combination(graphs::Vector{FeynmanGraph{F,W}}, constants::Abstra for (i, sub_g) in enumerate(graphs) if unary_istrivial(sub_g) && onechild(sub_g) subgraph_factors[i] *= sub_g.subgraph_factors[1] - # subgraph_factors[i] *= sub_g.subgraph_factors[1] * sub_g.factor subgraphs[i] = sub_g.subgraphs[1] end end unique_graphs = FeynmanGraph{F,W}[] unique_factors = F[] for (idx, g) in enumerate(subgraphs) - i = findfirst(isequal(g), unique_graphs) + i = findfirst(isequal(g.id), id.(unique_graphs)) if isnothing(i) push!(unique_graphs, g) push!(unique_factors, subgraph_factors[idx]) diff --git a/src/computational_graph/graph.jl b/src/computational_graph/graph.jl index fe188489..ce0e61d3 100644 --- a/src/computational_graph/graph.jl +++ b/src/computational_graph/graph.jl @@ -10,7 +10,6 @@ - `subgraphs::Vector{Graph{F,W}}` vector of sub-diagrams - `subgraph_factors::Vector{F}` scalar multiplicative factors associated with each subgraph. Note that the subgraph factors may be manipulated algebraically. To associate a fixed multiplicative factor with this graph which carries some semantic meaning, use the `factor` argument instead. - `operator::DataType` node operation. Addition and multiplication are natively supported via operators Sum and Prod, respectively. Should be a concrete subtype of `AbstractOperator`. -- `factor::F` a number representing the total scalar multiplicative factor for the diagram. - `weight::W` the weight of this node - `properties::Any` extra information of Green's functions. @@ -35,7 +34,6 @@ mutable struct Graph{F<:Number,W} <: AbstractGraph # Graph subgraph_factors::Vector{F} operator::DataType - factor::F weight::W properties::Any @@ -63,12 +61,12 @@ mutable struct Graph{F<:Number,W} <: AbstractGraph # Graph @assert length(subgraphs) == 1 "Graph with Power operator must have one and only one subgraph." end # @assert allunique(subgraphs) "all subgraphs must be distinct." - g = new{ftype,wtype}(uid(), String(name), orders, subgraphs, subgraph_factors, typeof(operator), one(ftype), weight, properties) + g = new{ftype,wtype}(uid(), String(name), orders, subgraphs, subgraph_factors, typeof(operator), weight, properties) if factor ≈ one(ftype) return g else - return new{ftype,wtype}(uid(), String(name), orders, [g,], [factor,], Prod, one(ftype), weight * factor, properties) + return new{ftype,wtype}(uid(), String(name), orders, [g,], [factor,], Prod, weight * factor, properties) end end end @@ -80,7 +78,6 @@ id(g::Graph) = g.id name(g::Graph) = g.name orders(g::Graph) = g.orders operator(g::Graph) = g.operator -factor(g::Graph) = g.factor weight(g::Graph) = g.weight properties(g::Graph) = g.properties subgraph(g::Graph, i=1) = g.subgraphs[i] @@ -96,7 +93,6 @@ set_name!(g::Graph, name::String) = (g.name = name) set_orders!(g::Graph, orders::Vector{Int}) = (g.orders = orders) set_operator!(g::Graph, operator::Type{<:AbstractOperator}) = (g.operator = operator) set_operator!(g::Graph, operator::AbstractOperator) = (g.operator = typeof(operator)) -set_factor!(g::Graph{F,W}, factor) where {F,W} = (g.factor = F(factor)) set_weight!(g::Graph{F,W}, weight) where {F,W} = (g.weight = W(weight)) set_properties!(g::Graph, properties) = (g.properties = properties) set_subgraph!(g::Graph{F,W}, subgraph::Graph{F,W}, i=1) where {F,W} = (g.subgraphs[i] = subgraph) @@ -117,7 +113,7 @@ set_subgraph_factors!(g::Graph{F,W}, subgraph_factors::AbstractVector, indices:: - `f`: constant factor """ function constant_graph(factor=one(_dtype.factor)) - g = Graph([]; operator=Constant(), ftype=_dtype.factor, wtype=_dtype.weight, weight=one(_dtype.weight)) + g = Graph([]; operator=Unitary(), ftype=_dtype.factor, wtype=_dtype.weight, weight=one(_dtype.weight)) if factor ≈ one(_dtype.factor) return g else @@ -139,7 +135,6 @@ function Base.:*(g1::Graph{F,W}, c2) where {F,W} # Convert trivial unary link to in-place form if unary_istrivial(g1) && onechild(g1) g.subgraph_factors[1] *= g1.subgraph_factors[1] - # g.subgraph_factors[1] *= g1.subgraph_factors[1] * g1.factor g.subgraphs = g1.subgraphs end return g @@ -159,7 +154,6 @@ function Base.:*(c1, g2::Graph{F,W}) where {F,W} # Convert trivial unary link to in-place form if unary_istrivial(g2) && onechild(g2) g.subgraph_factors[1] *= g2.subgraph_factors[1] - # g.subgraph_factors[1] *= g2.subgraph_factors[1] * g2.factor g.subgraphs = g2.subgraphs end return g @@ -193,16 +187,14 @@ function linear_combination(g1::Graph{F,W}, g2::Graph{F,W}, c1=F(1), c2=F(1)) wh # Convert trivial unary links to in-place form if unary_istrivial(g1) && onechild(g1) subgraph_factors[1] *= g1.subgraph_factors[1] - # subgraph_factors[1] *= g1.subgraph_factors[1] * g1.factor subgraphs[1] = g1.subgraphs[1] end if unary_istrivial(g2) && onechild(g2) subgraph_factors[2] *= g2.subgraph_factors[1] - # subgraph_factors[2] *= g2.subgraph_factors[1] * g2.factor subgraphs[2] = g2.subgraphs[1] end - if subgraphs[1] == subgraphs[2] + if subgraphs[1].id == subgraphs[2].id g = Graph([subgraphs[1]]; subgraph_factors=[sum(subgraph_factors)], operator=Sum(), orders=orders(g1), ftype=F, wtype=W) else g = Graph(subgraphs; subgraph_factors=subgraph_factors, operator=Sum(), orders=orders(g1), ftype=F, wtype=W) @@ -243,7 +235,6 @@ function linear_combination(graphs::Vector{Graph{F,W}}, constants::AbstractVecto for (i, sub_g) in enumerate(graphs) if unary_istrivial(sub_g) && onechild(sub_g) subgraph_factors[i] *= sub_g.subgraph_factors[1] - # subgraph_factors[i] *= sub_g.subgraph_factors[1] * sub_g.factor subgraphs[i] = sub_g.subgraphs[1] end end @@ -251,7 +242,7 @@ function linear_combination(graphs::Vector{Graph{F,W}}, constants::AbstractVecto unique_graphs = Graph{F,W}[] unique_factors = F[] for (idx, g) in enumerate(subgraphs) - i = findfirst(isequal(g), unique_graphs) + i = findfirst(isequal(g.id), id.(unique_graphs)) if isnothing(i) push!(unique_graphs, g) push!(unique_factors, subgraph_factors[idx]) @@ -316,16 +307,14 @@ function multi_product(g1::Graph{F,W}, g2::Graph{F,W}, c1=F(1), c2=F(1)) where { # Convert trivial unary links to in-place form if unary_istrivial(g1) && onechild(g1) subgraph_factors[1] *= g1.subgraph_factors[1] - # subgraph_factors[1] *= g1.subgraph_factors[1] * g1.factor subgraphs[1] = g1.subgraphs[1] end if unary_istrivial(g2) && onechild(g2) subgraph_factors[2] *= g2.subgraph_factors[1] - # subgraph_factors[2] *= g2.subgraph_factors[1] * g2.factor subgraphs[2] = g2.subgraphs[1] end - if subgraphs[1] == subgraphs[2] + if subgraphs[1].id == subgraphs[2].id g = Graph([subgraphs[1]]; subgraph_factors=[prod(subgraph_factors)], operator=Power(2), orders=2 * orders(g1), ftype=F, wtype=W) else if length(g1.orders) > length(g2.orders) @@ -367,7 +356,6 @@ function multi_product(graphs::Vector{Graph{F,W}}, constants::AbstractVector=one for (i, sub_g) in enumerate(graphs) if unary_istrivial(sub_g) && onechild(sub_g) subgraph_factors[i] *= sub_g.subgraph_factors[1] - # subgraph_factors[i] *= sub_g.subgraph_factors[1] * sub_g.factor subgraphs[i] = sub_g.subgraphs[1] end sub_g.orders = [orders(sub_g); zeros(Int, maxlen_orders - length(orders(sub_g)))] @@ -378,7 +366,7 @@ function multi_product(graphs::Vector{Graph{F,W}}, constants::AbstractVector=one unique_factors = F[] repeated_counts = Int[] for (idx, g) in enumerate(subgraphs) - loc = findfirst(isequal(g), unique_graphs) + loc = findfirst(isequal(g.id), id.(unique_graphs)) if isnothing(loc) push!(unique_graphs, g) push!(unique_factors, subgraph_factors[idx]) diff --git a/src/computational_graph/io.jl b/src/computational_graph/io.jl index 7d321d02..112105f1 100644 --- a/src/computational_graph/io.jl +++ b/src/computational_graph/io.jl @@ -34,21 +34,22 @@ function _idstring(graph::AbstractGraph) return string(id(graph), _namestr(graph)) end -function _idstring(graph::FeynmanGraph) +function _idstring(graph::FeynmanGraph) return string(id(graph), _namestr(graph), ":", _ops_to_str(vertices(graph))) end function _stringrep(graph::AbstractGraph, color=true) idstr = _idstring(graph) - fstr = short(factor(graph), one(factor(graph))) + # fstr = short(factor(graph), one(factor(graph))) wstr = short(weight(graph)) ostr = short_orders(orders(graph)) # =$(node.weight*(2π)^(3*node.id.para.innerLoopNum)) if length(subgraphs(graph)) == 0 - return isempty(fstr) ? "$(idstr)$(ostr)=$wstr" : "$(idstr)⋅$(fstr)=$wstr" + # return isempty(fstr) ? "$(idstr)$(ostr)=$wstr" : "$(idstr)⋅$(fstr)=$wstr" + return "$(idstr)$(ostr)=$wstr" else - return "$(idstr)$(ostr)=$wstr=$(fstr)$(operator(graph)) " + return "$(idstr)$(ostr)=$wstr=$(operator(graph)) " end end diff --git a/src/computational_graph/operation.jl b/src/computational_graph/operation.jl index 99940911..4cc1907f 100644 --- a/src/computational_graph/operation.jl +++ b/src/computational_graph/operation.jl @@ -35,9 +35,8 @@ function linear_combination_number_with_graph(g::Vector{Union{F,Graph{F,W}}}, co push!(subcoeff, 1.0) end result = linear_combination(subgraphs, subcoeff) - #result.factor *= d.factor elseif !isnothing(subnumber) #if only numbers appear in derivative, return a number - result = subnumber #* d.factor + result = subnumber end return result #return subgraphs, subnumber, subcoeff @@ -257,7 +256,7 @@ function backAD(diag::Graph{F,W}, debug::Bool=false) where {F,W} result = Dict{Tuple{Int,Int},Graph{F,W}}() parents = all_parent(diag) for d in Leaves(diag)#PreOrderDFS(diag) # preorder traversal will visit all parents first - if d.operator == Constant || haskey(dual, d.id) + if d.operator == Unitary || haskey(dual, d.id) continue end recursive_backAD!(d, parents, dual, result, diag.id) @@ -391,7 +390,6 @@ function forwardAD_root!(graphs::AbstractVector{G}, idx::Int=1, dual[key_node].subgraph_factors = node.subgraph_factors dual[key_node].name = node.name else - # dual[key_node] = Graph(nodes_deriv; subgraph_factors=node.subgraph_factors, factor=node.factor) dual[key_node] = Graph(nodes_deriv; subgraph_factors=node.subgraph_factors) end elseif node.operator == Prod @@ -417,7 +415,6 @@ function forwardAD_root!(graphs::AbstractVector{G}, idx::Int=1, dual[key_node].subgraph_factors = one.(eachindex(nodes_deriv)) dual[key_node].name = node.name else - # dual[key_node] = Graph(nodes_deriv; factor=node.factor) dual[key_node] = Graph(nodes_deriv) end elseif node.operator <: Power # node with Power operator has only one subgraph! @@ -439,7 +436,6 @@ function forwardAD_root!(graphs::AbstractVector{G}, idx::Int=1, dual[key_node].name = node.name dual.operator = Prod else - # dual[key_node] = Graph(nodes_deriv; subgraph_factors=[1, node.subgraph_factors[1]], operator=Prod(), factor=node.factor) dual[key_node] = Graph(nodes_deriv; subgraph_factors=[1, node.subgraph_factors[1]], operator=Prod()) end end diff --git a/src/computational_graph/optimize.jl b/src/computational_graph/optimize.jl index 50592bda..67ccfa26 100644 --- a/src/computational_graph/optimize.jl +++ b/src/computational_graph/optimize.jl @@ -435,16 +435,19 @@ function burn_from_targetleaves!(graphs::AbstractVector{G}, targetleaves_id::Abs end end - g_c0 = constant_graph(ftype(0)) + # g_c0 = constant_graph(ftype(0)) + g_c1 = constant_graph(ftype(1)) has_c0 = false for g in graphs if name(g) == "BURNING" has_c0 = true - set_id!(g, id(g_c0)) - set_operator!(g, Constant) - set_factor!(g, ftype(0)) + set_id!(g, id(g_c1)) + set_operator!(g, Unitary) + # set_subgraphs!(g, subgraphs(g_c0)) + # set_subgraph_factors!(g, subgraph_factors(g_c0)) + set_weight!(g, 0.0) end end - has_c0 ? (return id(g_c0)) : (return nothing) + has_c0 ? (return id(g_c1)) : (return nothing) end \ No newline at end of file diff --git a/src/frontend/parquet/operation.jl b/src/frontend/parquet/operation.jl index 6207ee50..d8a2df0d 100644 --- a/src/frontend/parquet/operation.jl +++ b/src/frontend/parquet/operation.jl @@ -130,7 +130,6 @@ function mergeby(diags::Vector{Graph{F,W}}; # if there is only one diagram, and the new id is either GenericId or the id of the existing diagram, # then simply return the current diagram without creating a new diagram # ! the new factor will be multiplied to the factor of the exisiting diagram! - # diags[1].factor *= factor return diags end diag = Graph(diags; properties=id, operator=operator, name=name) diff --git a/src/utility.jl b/src/utility.jl index 12b2b296..c23a156b 100644 --- a/src/utility.jl +++ b/src/utility.jl @@ -44,7 +44,6 @@ function taylorexpansion!(graph::G, var_dependence::Dict{Int,Vector{Bool}}=Dict{ if sum(o) == 0 # For a graph the zero order taylor coefficient is just itself. result.coeffs[o] = graph else - # coeff = Graph([]; operator=ComputationalGraphs.Sum(), factor=graph.factor, properties=graph.properties, orders=o) coeff = Graph([]; operator=ComputationalGraphs.Sum(), properties=graph.properties, orders=o) result.coeffs[o] = coeff end @@ -52,7 +51,6 @@ function taylorexpansion!(graph::G, var_dependence::Dict{Int,Vector{Bool}}=Dict{ to_coeff_map[graph.id] = result return result, to_coeff_map else - # to_coeff_map[graph.id] = graph.factor * apply(graph.operator, [taylorexpansion!(sub, var_dependence; to_coeff_map=to_coeff_map)[1] for sub in graph.subgraphs], graph.subgraph_factors) to_coeff_map[graph.id] = apply(graph.operator, [taylorexpansion!(sub, var_dependence; to_coeff_map=to_coeff_map)[1] for sub in graph.subgraphs], graph.subgraph_factors) return to_coeff_map[graph.id], to_coeff_map end @@ -82,14 +80,12 @@ function taylorexpansion!(graph::FeynmanGraph{F,W}, var_dependence::Dict{Int,Vec result = TaylorSeries{Graph{F,W}}() for order in collect(Iterators.product(ordtuple...)) #varidx specifies the variables graph depends on. Iterate over all taylor coefficients of those variables. o = collect(order) - # coeff = Graph([]; operator=ComputationalGraphs.Sum(), factor=graph.factor, properties=graph.properties, orders=o) coeff = Graph([]; operator=ComputationalGraphs.Sum(), properties=graph.properties, orders=o) result.coeffs[o] = coeff end to_coeff_map[graph.id] = result return result, to_coeff_map else - # to_coeff_map[graph.id] = graph.factor * apply(graph.operator, [taylorexpansion!(sub, var_dependence; to_coeff_map=to_coeff_map)[1] for sub in graph.subgraphs], graph.subgraph_factors) to_coeff_map[graph.id] = apply(graph.operator, [taylorexpansion!(sub, var_dependence; to_coeff_map=to_coeff_map)[1] for sub in graph.subgraphs], graph.subgraph_factors) return to_coeff_map[graph.id], to_coeff_map end @@ -208,11 +204,8 @@ function taylorexpansion_withmap(g::G; coeffmode=true, var::Vector{Bool}=fill(tr if ordernew[idx] <= get_orders(idx) if !haskey(result.coeffs, ordernew) if coeffmode - # funcAD = Graph([]; operator=ComputationalGraphs.Sum(), factor=g.factor) funcAD = Graph([]; operator=ComputationalGraphs.Sum()) else - #funcAD = taylor_factorial(ordernew) * Graph([]; operator=ComputationalGraphs.Sum(), factor=g.factor) - # funcAD = Graph([]; operator=ComputationalGraphs.Sum(), factor=taylor_factorial(ordernew) * g.factor) funcAD = Graph([]; operator=ComputationalGraphs.Sum(), factor=taylor_factorial(ordernew)) end new_func[ordernew] = funcAD diff --git a/test/computational_graph.jl b/test/computational_graph.jl index 27472ea7..bf1853ac 100644 --- a/test/computational_graph.jl +++ b/test/computational_graph.jl @@ -77,7 +77,6 @@ Graphs.unary_istrivial(::Type{O}) where {O<:Union{O1,O2,O3}} = true @test Graphs.name(g) == "" @test Graphs.orders(g) == zeros(Int, 0) @test Graphs.operator(g) == O - # @test Graphs.factor(g) == 1.0 @test Graphs.weight(g) == 1.0 @test Graphs.subgraph(g) == g1 @test Graphs.subgraph(g, 2) == g2 @@ -147,7 +146,6 @@ end end @testset "Addition" begin g3 = g1 + g2 - # @test g3.factor == 1 @test g3.subgraphs == [g1] @test g3.subgraph_factors == [3] # @test g3.subgraphs == [g1, g1] @@ -156,7 +154,6 @@ end end @testset "Subtraction" begin g4 = g1 - g2 - # @test g4.factor == 1 @test g4.subgraphs == [g1] @test g4.subgraph_factors == [-1] @test g4.subgraphs[1] == g1 @@ -525,7 +522,6 @@ end g3 = g1 + g2 @test vertices(g3) == vertices(g1) @test external_operators(g3) == external_operators(g1) - # @test g3.factor == 1 @test g3.subgraphs == [g1] @test g3.subgraph_factors == [3] # @test g3.subgraphs == [g1, g1] @@ -536,7 +532,6 @@ end g4 = g1 - g2 @test vertices(g4) == vertices(g1) @test external_operators(g4) == external_operators(g1) - # @test g4.factor == 1 @test g4.subgraphs == [g1,] @test g4.subgraph_factors == [-1,] # @test g4.subgraphs == [g1, g1]