Skip to content

Format, add format CI #61

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

Merged
merged 1 commit into from
Feb 2, 2023
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
35 changes: 35 additions & 0 deletions .github/workflows/format_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Format check
on:
push:
branches: [main]
tags: [v*]
pull_request:

jobs:
format:
name: "Format Check"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
with:
version: 1
- name: Install JuliaFormatter and format
run: |
julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))'
julia -e 'using JuliaFormatter; format(".", verbose=true)'
- name: Check format
run: |
julia -e '
out = Cmd(`git diff --name-only`) |> read |> String
if out == ""
exit(0)
else
@error "The following files have not been formatted:"
write(stdout, out)
out_diff = Cmd(`git diff`) |> read |> String
@error "Diff:"
write(stdout, out_diff)
exit(1)
@error ""
end'
29 changes: 29 additions & 0 deletions .github/workflows/format_pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: format-pr
on:
schedule:
- cron: '0 0 * * *'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install JuliaFormatter and format
run: |
julia -e 'import Pkg; Pkg.add("JuliaFormatter")'
julia -e 'using JuliaFormatter; format(".")'
# https://github.com/marketplace/actions/create-pull-request
# https://github.com/peter-evans/create-pull-request#reference-example
- name: Create Pull Request
id: cpr
uses: peter-evans/create-pull-request@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: Format .jl files
title: 'Automatic JuliaFormatter.jl run'
branch: auto-juliaformatter-pr
delete-branch: true
labels: formatting, automated pr, no changelog
- name: Check outputs
run: |
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"
27 changes: 27 additions & 0 deletions .github/workflows/format_suggestions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Format suggestions

on:
pull_request:

concurrency:
# Skip intermediate builds: always.
# Cancel intermediate builds: only if it is a pull request build.
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }}

jobs:
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@latest
with:
version: 1
- run: |
julia -e 'using Pkg; Pkg.add("JuliaFormatter")'
julia -e 'using JuliaFormatter; format("."; verbose=true)'
- uses: reviewdog/action-suggester@v1
with:
tool_name: JuliaFormatter
fail_on_error: true
filter_mode: added
14 changes: 14 additions & 0 deletions .github/workflows/register.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Register Package
on:
workflow_dispatch:
inputs:
version:
description: Version to register or component to bump
required: true
jobs:
register:
runs-on: ubuntu-latest
steps:
- uses: julia-actions/RegisterAction@latest
with:
token: ${{ secrets.GITHUB_TOKEN }}
36 changes: 18 additions & 18 deletions src/Graphs/abstractgraph.jl
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
"""Determine if an edge involves a leaf (at src or dst)"""
function is_leaf_edge(g::AbstractGraph, e)
return is_leaf(g, src(e)) || is_leaf(g, dst(e))
end
return is_leaf(g, src(e)) || is_leaf(g, dst(e))
end

"""Determine if a node has any neighbors which are leaves"""
function has_leaf_neighbor(g::AbstractGraph, v)
for vn in neighbors(g, v)
if(is_leaf(g, vn))
return true
end
for vn in neighbors(g, v)
if (is_leaf(g, vn))
return true
end
return false
end
return false
end

"""Get all edges which do not involve a leaf"""
function internal_edges(g::AbstractGraph)
return filter(e -> !is_leaf_edge(g, e), edges(g))
return filter(e -> !is_leaf_edge(g, e), edges(g))
end

"""Get all vertices which are leaves of a graph"""
function leaf_vertices(g::AbstractGraph)
return vertices(g)[findall(==(1), [is_leaf(g,v) for v in vertices(g)])]
return vertices(g)[findall(==(1), [is_leaf(g, v) for v in vertices(g)])]
end

"""Get distance of a vertex from a leaf"""
function distance_to_leaf(g::AbstractGraph, v)
leaves = leaf_vertices(g)
if(isempty(leaves))
println("ERROR: GRAPH DOES NTO CONTAIN LEAVES")
return NaN
end
return minimum([length(a_star(g, v, leaf)) for leaf in leaves])
leaves = leaf_vertices(g)
if (isempty(leaves))
println("ERROR: GRAPH DOES NTO CONTAIN LEAVES")
return NaN
end

return minimum([length(a_star(g, v, leaf)) for leaf in leaves])
end

"""Return all vertices which are within a certain pathlength `dist` of the leaves of the graph"""
function distance_from_roots(g::AbstractGraph, dist::Int64)
return vertices(g)[findall(<=(dist), [distance_to_leaf(g, v) for v in vertices(g)])]
end
return vertices(g)[findall(<=(dist), [distance_to_leaf(g, v) for v in vertices(g)])]
end
21 changes: 11 additions & 10 deletions src/abstractitensornetwork.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ siteinds(tn::AbstractITensorNetwork) = external_indsnetwork(tn)
# External indsnetwork of the flattened network, with vertices
# mapped back to `tn1`.
function flatten_external_indsnetwork(
tn1::AbstractITensorNetwork,
tn2::AbstractITensorNetwork,
tn1::AbstractITensorNetwork, tn2::AbstractITensorNetwork
)
is = external_indsnetwork(sim(tn1; sites=[]) ⊗ tn2)
flattened_is = IndsNetwork(underlying_graph(tn1))
Expand Down Expand Up @@ -359,7 +358,9 @@ function isapprox(
error("Not implemented")
d = norm(x - y)
if !isfinite(d)
error("In `isapprox(x::AbstractITensorNetwork, y::AbstractITensorNetwork)`, `norm(x - y)` is not finite")
error(
"In `isapprox(x::AbstractITensorNetwork, y::AbstractITensorNetwork)`, `norm(x - y)` is not finite",
)
end
return d <= max(atol, rtol * max(norm(x), norm(y)))
end
Expand Down Expand Up @@ -501,11 +502,7 @@ function factorize(
return tn
end

function factorize(
tn::AbstractITensorNetwork,
edge::Pair;
kwargs...,
)
function factorize(tn::AbstractITensorNetwork, edge::Pair; kwargs...)
return factorize(tn, edgetype(tn)(edge); kwargs...)
end

Expand Down Expand Up @@ -790,7 +787,9 @@ function site_combiners(tn::AbstractITensorNetwork{V}) where {V}
return Cs
end

function insert_missing_internal_inds(tn::AbstractITensorNetwork, edges; internal_inds_space=trivial_space(tn))
function insert_missing_internal_inds(
tn::AbstractITensorNetwork, edges; internal_inds_space=trivial_space(tn)
)
tn = copy(tn)
for e in edges
if !hascommoninds(tn[src(e)], tn[dst(e)])
Expand All @@ -803,7 +802,9 @@ function insert_missing_internal_inds(tn::AbstractITensorNetwork, edges; interna
return tn
end

function insert_missing_internal_inds(tn::AbstractITensorNetwork; internal_inds_space=trivial_space(tn))
function insert_missing_internal_inds(
tn::AbstractITensorNetwork; internal_inds_space=trivial_space(tn)
)
return insert_internal_inds(tn, edges(tn); internal_inds_space)
end

Expand Down
98 changes: 55 additions & 43 deletions src/contraction_tree_to_graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,78 @@ define a tripartition of the graph and thus are named as an n = 3 element tuples
Edges connect parents/children within the contraction sequence.
"""
function contraction_sequence_to_graph(contract_sequence)

g = fill_contraction_sequence_graph_vertices(contract_sequence)
g = fill_contraction_sequence_graph_vertices(contract_sequence)

#Now we have the vertices we need to figure out the edges
for v in vertices(g)
#Only add edges from a parent (which defines a tripartition and thus has length 3) to its children
if(length(v) == 3)
#Work out which vertices it connects to
concat1, concat2, concat3 =[v[1]..., v[2]...], [v[2]..., v[3]...], [v[1]..., v[3]...]
for vn in setdiff(vertices(g), [v])
vn_set = [Set(vni) for vni in vn]
if(Set(concat1) ∈ vn_set || Set(concat2) ∈ vn_set || Set(concat3) ∈ vn_set)
add_edge!(g, v => vn)
end
end
#Now we have the vertices we need to figure out the edges
for v in vertices(g)
#Only add edges from a parent (which defines a tripartition and thus has length 3) to its children
if (length(v) == 3)
#Work out which vertices it connects to
concat1, concat2, concat3 = [v[1]..., v[2]...], [v[2]..., v[3]...], [v[1]..., v[3]...]
for vn in setdiff(vertices(g), [v])
vn_set = [Set(vni) for vni in vn]
if (Set(concat1) ∈ vn_set || Set(concat2) ∈ vn_set || Set(concat3) ∈ vn_set)
add_edge!(g, v => vn)
end
end
end
end


return g
return g
end


function fill_contraction_sequence_graph_vertices(contract_sequence)
g = NamedGraph()
leaves = collect(Leaves(contract_sequence))
fill_contraction_sequence_graph_vertices!(g, contract_sequence[1], leaves)
fill_contraction_sequence_graph_vertices!(g, contract_sequence[2], leaves)
return g
g = NamedGraph()
leaves = collect(Leaves(contract_sequence))
fill_contraction_sequence_graph_vertices!(g, contract_sequence[1], leaves)
fill_contraction_sequence_graph_vertices!(g, contract_sequence[2], leaves)
return g
end

"""Given a contraction sequence which is a subsequence of some larger sequence (with leaves `leaves`) which is being built on g
Spawn `contract sequence' as a vertex on `current_g' and continue on with its children """
function fill_contraction_sequence_graph_vertices!(g, contract_sequence, leaves)
if(isa(contract_sequence, Array))
group1 = collect(Leaves(contract_sequence[1]))
group2 = collect(Leaves(contract_sequence[2]))
remaining_verts = setdiff(leaves, vcat(group1, group2))
add_vertex!(g, (group1, group2, remaining_verts))
fill_contraction_sequence_graph_vertices!(g, contract_sequence[1], leaves)
fill_contraction_sequence_graph_vertices!(g, contract_sequence[2], leaves)
else
add_vertex!(g, ([contract_sequence], setdiff(leaves, [contract_sequence])))
end
if (isa(contract_sequence, Array))
group1 = collect(Leaves(contract_sequence[1]))
group2 = collect(Leaves(contract_sequence[2]))
remaining_verts = setdiff(leaves, vcat(group1, group2))
add_vertex!(g, (group1, group2, remaining_verts))
fill_contraction_sequence_graph_vertices!(g, contract_sequence[1], leaves)
fill_contraction_sequence_graph_vertices!(g, contract_sequence[2], leaves)
else
add_vertex!(g, ([contract_sequence], setdiff(leaves, [contract_sequence])))
end
end

"""Get the vertex bi-partition that a given edge represents"""
function contraction_tree_leaf_bipartition(g::AbstractGraph, e)
if (!is_leaf_edge(g, e))
vsrc_set, vdst_set = [Set(vni) for vni in src(e)], [Set(vni) for vni in dst(e)]
c1, c2, c3 = [src(e)[1]..., src(e)[2]...],
[src(e)[2]..., src(e)[3]...],
[src(e)[1]..., src(e)[3]...]
left_bipartition = if Set(c1) ∈ vdst_set
c1
elseif Set(c2) ∈ vdst_set
c2
else
c3
end

if(!is_leaf_edge(g, e))
vsrc_set, vdst_set = [Set(vni) for vni in src(e)], [Set(vni) for vni in dst(e)]
c1, c2, c3 = [src(e)[1]..., src(e)[2]...], [src(e)[2]..., src(e)[3]...], [src(e)[1]..., src(e)[3]...]
left_bipartition = Set(c1) ∈ vdst_set ? c1 : Set(c2) ∈ vdst_set ? c2 : c3

c1, c2, c3 = [dst(e)[1]..., dst(e)[2]...], [dst(e)[2]..., dst(e)[3]...], [dst(e)[1]..., dst(e)[3]...]
right_bipartition = Set(c1) ∈ vsrc_set ? c1 : Set(c2) ∈ vsrc_set ? c2 : c3
c1, c2, c3 = [dst(e)[1]..., dst(e)[2]...],
[dst(e)[2]..., dst(e)[3]...],
[dst(e)[1]..., dst(e)[3]...]
right_bipartition = if Set(c1) ∈ vsrc_set
c1
elseif Set(c2) ∈ vsrc_set
c2
else
left_bipartition = filter(vs -> Set(vs) ∈ [Set(vni) for vni in dst(e)], src(e))[1]
right_bipartition = setdiff(src(e), left_bipartition)
c3
end
else
left_bipartition = filter(vs -> Set(vs) ∈ [Set(vni) for vni in dst(e)], src(e))[1]
right_bipartition = setdiff(src(e), left_bipartition)
end

return left_bipartition, right_bipartition
return left_bipartition, right_bipartition
end
20 changes: 9 additions & 11 deletions src/indsnetwork.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,23 @@ function IndsNetwork(data_graph::DataGraph)
return IndsNetwork{vertextype(data_graph)}(data_graph)
end

@traitfn function IndsNetwork{V,I}(g::G) where {G<:DataGraph,V,I; !IsUnderlyingGraph{G}}
@traitfn function IndsNetwork{V,I}(g::G) where {G<:DataGraph,V,I;!IsUnderlyingGraph{G}}
return _IndsNetwork(V, I, g)
end

function IndsNetwork{V,I}(
g::AbstractNamedGraph, link_space, site_space
) where {V,I}
function IndsNetwork{V,I}(g::AbstractNamedGraph, link_space, site_space) where {V,I}
link_space_dictionary = link_space_map(V, I, g, link_space)
site_space_dictionary = site_space_map(V, I, g, site_space)
return IndsNetwork{V,I}(g, link_space_dictionary, site_space_dictionary)
end

function IndsNetwork{V,I}(
g::AbstractSimpleGraph, link_space, site_space
) where {V,I}
function IndsNetwork{V,I}(g::AbstractSimpleGraph, link_space, site_space) where {V,I}
return IndsNetwork{V,I}(NamedGraph(g), link_space, site_space)
end

@traitfn function IndsNetwork{V}(g::G, link_space, site_space) where {G,V; IsUnderlyingGraph{G}}
@traitfn function IndsNetwork{V}(
g::G, link_space, site_space
) where {G,V;IsUnderlyingGraph{G}}
I = indtype(link_space, site_space)
return IndsNetwork{V,I}(g, link_space, site_space)
end
Expand Down Expand Up @@ -137,17 +135,17 @@ end

@traitfn function IndsNetwork{V,I}(
g::G; link_space=nothing, site_space=nothing
) where {G,V,I; IsUnderlyingGraph{G}}
) where {G,V,I;IsUnderlyingGraph{G}}
return IndsNetwork{V,I}(g, link_space, site_space)
end

@traitfn function IndsNetwork{V}(
g::G; link_space=nothing, site_space=nothing
) where {G,V; IsUnderlyingGraph{G}}
) where {G,V;IsUnderlyingGraph{G}}
return IndsNetwork{V}(g, link_space, site_space)
end

@traitfn function IndsNetwork(g::G; kwargs...) where{G; IsUnderlyingGraph{G}}
@traitfn function IndsNetwork(g::G; kwargs...) where {G; IsUnderlyingGraph{G}}
return IndsNetwork{vertextype(g)}(g; kwargs...)
end

Expand Down
Loading