Skip to content

Commit

Permalink
GraphsFlows extension
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Apr 24, 2024
1 parent 13c5b40 commit a1d14bf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
GraphsFlows = "06909019-6f44-4949-96fc-b9d9aaa02889"
ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5"
IsApprox = "28f27b66-4bd8-47e7-9110-e2746eb8bed7"
IterTools = "c8e1da08-722c-5040-9ed9-7db0dc04731e"
Expand All @@ -36,11 +35,13 @@ TupleTools = "9d95972d-f1c8-5527-a6e0-b4b365fa01f6"

[weakdeps]
EinExprs = "b1794770-133b-4de1-afb4-526377e9f4c5"
GraphsFlows = "06909019-6f44-4949-96fc-b9d9aaa02889"
Observers = "338f10d5-c7f1-4033-a7d1-f9dec39bcaa0"
OMEinsumContractionOrders = "6f22d1fd-8eed-4bb7-9776-e7d684900715"

[extensions]
ITensorNetworksEinExprsExt = "EinExprs"
ITensorNetworksGraphsFlowsExt = "GraphsFlows"
ITensorNetworksObserversExt = "Observers"
ITensorNetworksOMEinsumContractionOrdersExt = "OMEinsumContractionOrders"

Expand Down Expand Up @@ -78,6 +79,7 @@ julia = "1.10"

[extras]
EinExprs = "b1794770-133b-4de1-afb4-526377e9f4c5"
GraphsFlows = "06909019-6f44-4949-96fc-b9d9aaa02889"
Observers = "338f10d5-c7f1-4033-a7d1-f9dec39bcaa0"
OMEinsumContractionOrders = "6f22d1fd-8eed-4bb7-9776-e7d684900715"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
19 changes: 19 additions & 0 deletions ext/ITensorNetworksGraphsFlowsExt/ITensorNetworksGraphsFlowsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module ITensorNetworksGraphsFlowsExt
using Graphs: AbstractGraph
using GraphsFlows: GraphsFlows
using ITensorNetworks: ITensorNetworks
using NDTensors.AlgorithmSelection: @Algorithm_str

function ITensorNetworks.mincut(
::Algorithm"GraphsFlows",
graph::AbstractGraph,
source_vertex,
target_vertex;
capacity_matrix,
alg=GraphsFlows.PushRelabelAlgorithm(),
)
# TODO: Replace with `Backend(backend)`.
return GraphsFlows.mincut(graph, source_vertex, target_vertex, capacity_matrix, alg)
end

end
16 changes: 14 additions & 2 deletions src/contract_approx/mincut.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using AbstractTrees: Leaves, PostOrderDFS
using Combinatorics: powerset
using Graphs: dijkstra_shortest_paths, weights
using GraphsFlows: GraphsFlows
using NamedGraphs: NamedDiGraph
using NDTensors.AlgorithmSelection: Algorithm

# a large number to prevent this edge being a cut
MAX_WEIGHT = 1e32
Expand Down Expand Up @@ -37,6 +37,18 @@ function binary_tree_structure(tn::ITensorNetwork, outinds::Vector)
return _binary_tree_structure(tn, outinds; maximally_unbalanced=false)
end

function mincut(graph::AbstractGraph, source_vertex, target_vertex; backend, kwargs...)
# TODO: Replace with `Backend(backend)`.
return mincut(Algorithm(backend), graph, source_vertex, target_vertex; kwargs...)
end

# TODO: Replace with `backend::Backend`.
function mincut(
backend::Algorithm, graph::AbstractGraph, source_vertex, target_vertex; kwargs...
)
return error("Backend `$backend` not implemented for `mincut`.")
end

"""
Calculate the mincut between two subsets of the uncontracted inds
(source_inds and terminal_inds) of the input tn.
Expand All @@ -52,7 +64,7 @@ function _mincut(tn::ITensorNetwork, source_inds::Vector, terminal_inds::Vector)
tn = disjoint_union(
ITensorNetwork([ITensor(source_inds...), ITensor(terminal_inds...)]), tn
)
return GraphsFlows.mincut(tn, (1, 1), (2, 1), weights(tn))
return mincut(tn, (1, 1), (2, 1); backend="GraphsFlows", capacity_matrix=weights(tn))
end

"""
Expand Down

0 comments on commit a1d14bf

Please sign in to comment.