Skip to content

Commit 4cc89df

Browse files
committed
format relevant files and small changes; add TODO
1 parent 5060bd7 commit 4cc89df

7 files changed

Lines changed: 120 additions & 160 deletions

File tree

src/planar/analyzers.jl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ function get_possible_planar_indices(ex)
66
if !TO.istensorexpr(ex)
77
return [[]]
88
elseif TO.isgeneraltensor(ex)
9-
_,leftind,rightind = TO.decomposegeneraltensor(ex)
9+
_, leftind, rightind = TO.decomposegeneraltensor(ex)
1010
ind = planar_unique2(vcat(leftind, reverse(rightind)))
1111
return length(ind) == length(unique(ind)) ? Any[ind] : Any[]
1212
elseif isexpr(ex, :call) && (ex.args[1] == :+ || ex.args[1] == :-)
1313
inds = get_possible_planar_indices(ex.args[2])
1414
keep = fill(true, length(inds))
15-
for i = 3:length(ex.args)
15+
for i in 3:length(ex.args)
1616
inds′ = get_possible_planar_indices(ex.args[i])
1717
keepᵢ = fill(false, length(inds))
1818
for (j, ind) in enumerate(inds), ind′ in inds′
@@ -53,7 +53,7 @@ function planar_unique2(allind)
5353
removing = false
5454
i = 1
5555
while i <= length(oind) && length(oind) > 1
56-
j = mod1(i+1, length(oind))
56+
j = mod1(i + 1, length(oind))
5757
if oind[i] == oind[j]
5858
deleteat!(oind, i)
5959
deleteat!(oind, mod1(i, length(oind)))
@@ -76,21 +76,21 @@ function possible_planar_complements(ind1, ind2)
7676
# general case:
7777
j1 = findfirst(in(ind2), ind1)
7878
if j1 === nothing # disconnected diagrams, can be made planar in various ways
79-
return Any[(circshift(ind1, i-1), circshift(ind2, j-1), Any[], Any[])
80-
for i eachindex(ind1), j eachindex(ind2)]
79+
return Any[(circshift(ind1, i - 1), circshift(ind2, j - 1), Any[], Any[])
80+
for i in eachindex(ind1), j in eachindex(ind2)]
8181
else # genuine contraction
8282
N1, N2 = length(ind1), length(ind2)
8383
j2 = findfirst(==(ind1[j1]), ind2)
8484
jmax1 = j1
8585
jmin2 = j2
86-
while jmax1 < N1 && ind1[jmax1+1] == ind2[mod1(jmin2-1, N2)]
86+
while jmax1 < N1 && ind1[jmax1 + 1] == ind2[mod1(jmin2 - 1, N2)]
8787
jmax1 += 1
8888
jmin2 -= 1
8989
end
9090
jmin1 = j1
9191
jmax2 = j2
9292
if j1 == 1 && jmax1 < N1
93-
while ind1[mod1(jmin1-1, N1)] == ind2[mod1(jmax2 + 1, N2)]
93+
while ind1[mod1(jmin1 - 1, N1)] == ind2[mod1(jmax2 + 1, N2)]
9494
jmin1 -= 1
9595
jmax2 += 1
9696
end
@@ -99,11 +99,12 @@ function possible_planar_complements(ind1, ind2)
9999
jmax2 -= N2
100100
jmin2 -= N2
101101
end
102-
indo1 = jmin1 < 1 ? ind1[(jmax1+1):mod1(jmin1-1, N1)] :
103-
vcat(ind1[(jmax1+1):N1], ind1[1:(jmin1-1)])
104-
cind1 = jmin1 < 1 ? vcat(ind1[mod1(jmin1, N1):N1], ind1[1:jmax1]) : ind1[jmin1:jmax1]
105-
indo2 = jmin2 < 1 ? ind2[(jmax2+1):mod1(jmin2-1, N2)] :
106-
vcat(ind2[(jmax2+1):N2], ind2[1:(jmin2-1)])
102+
indo1 = jmin1 < 1 ? ind1[(jmax1 + 1):mod1(jmin1 - 1, N1)] :
103+
vcat(ind1[(jmax1 + 1):N1], ind1[1:(jmin1 - 1)])
104+
cind1 = jmin1 < 1 ? vcat(ind1[mod1(jmin1, N1):N1], ind1[1:jmax1]) :
105+
ind1[jmin1:jmax1]
106+
indo2 = jmin2 < 1 ? ind2[(jmax2 + 1):mod1(jmin2 - 1, N2)] :
107+
vcat(ind2[(jmax2 + 1):N2], ind2[1:(jmin2 - 1)])
107108
cind2 = reverse(cind1)
108109
return isempty(intersect(indo1, indo2)) ? Any[(indo1, indo2, cind1, cind2)] : Any[]
109110
end

src/planar/macros.jl

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ macro planar(args::Vararg{Expr})
44

55
planarexpr = args[end]
66
kwargs = TO.parse_tensor_kwargs(args[1:(end - 1)])
7-
parser = planarparser(planarexpr, kwargs... )
7+
parser = planarparser(planarexpr, kwargs...)
88

99
return esc(parser(planarexpr))
1010
end
@@ -17,8 +17,8 @@ function planarparser(planarexpr, kwargs...)
1717
push!(parser.preprocessors, _extract_tensormap_objects)
1818

1919
temporaries = Vector{Symbol}()
20-
push!(parser.postprocessors, ex->_annotate_temporaries(ex, temporaries))
21-
push!(parser.postprocessors, ex->_free_temporaries(ex, temporaries))
20+
push!(parser.postprocessors, ex -> _annotate_temporaries(ex, temporaries))
21+
push!(parser.postprocessors, ex -> _free_temporaries(ex, temporaries))
2222
push!(parser.postprocessors, _insert_planar_operations)
2323

2424
for kw in kwargs
@@ -28,7 +28,8 @@ function planarparser(planarexpr, kwargs...)
2828
isexpr(val, :tuple) ||
2929
throw(ArgumentError("Invalid use of `order`, should be `order=(...,)`"))
3030
indexorder = map(normalizeindex, val.args)
31-
parser.contractiontreebuilder = network -> TO.indexordertree(network, indexorder)
31+
parser.contractiontreebuilder = network -> TO.indexordertree(network,
32+
indexorder)
3233

3334
elseif name == :contractcheck
3435
val isa Bool ||
@@ -66,11 +67,12 @@ function planarparser(planarexpr, kwargs...)
6667
treebuilder = parser.contractiontreebuilder
6768
treesorter = parser.contractiontreesorter
6869
costcheck = parser.contractioncostcheck
69-
push!(parser.preprocessors, ex->TO.processcontractions(ex, treebuilder, treesorter, costcheck))
70+
push!(parser.preprocessors,
71+
ex -> TO.processcontractions(ex, treebuilder, treesorter, costcheck))
7072
parser.contractioncostcheck = nothing
71-
push!(parser.preprocessors, ex->_check_planarity(ex))
72-
push!(parser.preprocessors, ex->_decompose_planar_contractions(ex, temporaries))
73-
73+
push!(parser.preprocessors, ex -> _check_planarity(ex))
74+
push!(parser.preprocessors, ex -> _decompose_planar_contractions(ex, temporaries))
75+
7476
return parser
7577
end
7678

@@ -87,11 +89,11 @@ function _plansor(expr, kwargs...)
8789
newtensors = TO.getnewtensorobjects(expr)
8890

8991
# find the first non-braiding tensor to determine the braidingstyle
90-
targetobj = inputtensors[findfirst(x->x != , inputtensors)]
92+
targetobj = inputtensors[findfirst(x -> x != , inputtensors)]
9193
if !isa(targetobj, Symbol)
9294
targetsym = gensym(string(targetobj))
9395
expr = TO.replacetensorobjects(expr) do obj, leftind, rightind
94-
obj == targetobj ? targetsym : obj
96+
return obj == targetobj ? targetsym : obj
9597
end
9698
args = Any[(:($targetsym = $targetobj))]
9799
else
@@ -105,7 +107,9 @@ function _plansor(expr, kwargs...)
105107
tensorex = tparser(expr)
106108
planarex = pparser(expr)
107109

108-
push!(args, Expr(:if, :(BraidingStyle(sectortype($targetsym)) isa Bosonic), tensorex, planarex))
110+
push!(args,
111+
Expr(:if, :(BraidingStyle(sectortype($targetsym)) isa Bosonic), tensorex,
112+
planarex))
109113
if !isa(targetobj, Symbol) && targetobj newtensors
110114
push!(args, :($targetobj = $targetsym))
111115
end

src/planar/planaroperations.jl

Lines changed: 14 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
function planaradd!(C::AbstractTensorMap{S,N₁,N₂},
33
A::AbstractTensorMap{S},
44
p::Index2Tuple{N₁,N₂},
5-
α,
6-
β,
5+
α::Number,
6+
β::Number,
77
backend::Backend...) where {S,N₁,N₂}
88
return add_transpose!(C, A, p, α, β, backend...)
99
end
1010

11-
function planartrace!(C::AbstractTensorMap{S,N₁,N₂},
11+
function planartrace!(C::AbstractTensorMap{S,N₁,N₂},
1212
A::AbstractTensorMap{S},
1313
p::Index2Tuple{N₁,N₂},
1414
q::Index2Tuple{N₃,N₃},
15-
α,
16-
β,
15+
α::Number,
16+
β::Number,
1717
backend::Backend...) where {S,N₁,N₂,N₃}
1818
if BraidingStyle(sectortype(S)) == Bosonic()
1919
return trace_permute!(C, A, p, q, α, β, backend...)
@@ -44,21 +44,25 @@ function planartrace!(C::AbstractTensorMap{S,N₁,N₂},
4444
return C
4545
end
4646

47-
48-
function planarcontract!(C::AbstractTensorMap{S,N₁,N₂},
47+
function planarcontract!(C::AbstractTensorMap{S,N₁,N₂},
4948
A::AbstractTensorMap{S},
5049
pA::Index2Tuple,
5150
B::AbstractTensorMap{S},
5251
pB::Index2Tuple,
5352
pAB::Index2Tuple{N₁,N₂},
54-
α,
55-
β,
53+
α::Number,
54+
β::Number,
5655
backend::Backend...) where {S,N₁,N₂}
56+
if BraidingStyle(sectortype(S)) == Bosonic()
57+
return contract(C, A, pA, B, pB, pAB, α, β, backend...)
58+
end
59+
5760
codA, domA = codomainind(A), domainind(A)
5861
codB, domB = codomainind(B), domainind(B)
5962
oindA, cindA = pA
6063
cindB, oindB = pB
61-
oindA, cindA, oindB, cindB = reorder_indices(codA, domA, codB, domB, oindA, cindA, oindB, cindB, pAB...)
64+
oindA, cindA, oindB, cindB = reorder_indices(codA, domA, codB, domB, oindA, cindA,
65+
oindB, cindB, pAB...)
6266

6367
if oindA == codA && cindA == domA
6468
A′ = A
@@ -80,81 +84,6 @@ function planarcontract!(C::AbstractTensorMap{S,N₁,N₂},
8084
return C
8185
end
8286

83-
# function planaradd!(α, tsrc::AbstractTensorMap{S},
84-
# β, tdst::AbstractTensorMap{S,N₁,N₂},
85-
# p1::IndexTuple{N₁}, p2::IndexTuple{N₂}) where {S,N₁,N₂}
86-
# return add_transpose!(α, tsrc, β, tdst, p1, p2)
87-
# end
88-
89-
# function planar_trace!(α, tsrc::AbstractTensorMap{S},
90-
# β, tdst::AbstractTensorMap{S,N₁,N₂},
91-
# p1::IndexTuple{N₁}, p2::IndexTuple{N₂},
92-
# q1::IndexTuple{N₃}, q2::IndexTuple{N₃}) where {S,N₁,N₂,N₃}
93-
# if BraidingStyle(sectortype(S)) == Bosonic()
94-
# return trace!(α, tsrc, β, tdst, p1, p2, q1, q2)
95-
# end
96-
97-
# @boundscheck begin
98-
# all(i -> space(tsrc, p1[i]) == space(tdst, i), 1:N₁) ||
99-
# throw(SpaceMismatch("trace: tsrc = $(codomain(tsrc))←$(domain(tsrc)),
100-
# tdst = $(codomain(tdst))←$(domain(tdst)), p1 = $(p1), p2 = $(p2)"))
101-
# all(i -> space(tsrc, p2[i]) == space(tdst, N₁ + i), 1:N₂) ||
102-
# throw(SpaceMismatch("trace: tsrc = $(codomain(tsrc))←$(domain(tsrc)),
103-
# tdst = $(codomain(tdst))←$(domain(tdst)), p1 = $(p1), p2 = $(p2)"))
104-
# all(i -> space(tsrc, q1[i]) == dual(space(tsrc, q2[i])), 1:N₃) ||
105-
# throw(SpaceMismatch("trace: tsrc = $(codomain(tsrc))←$(domain(tsrc)),
106-
# q1 = $(q1), q2 = $(q2)"))
107-
# end
108-
109-
# if iszero(β)
110-
# fill!(tdst, β)
111-
# elseif β != 1
112-
# rmul!(tdst, β)
113-
# end
114-
# pdata = (p1..., p2...)
115-
# for (f₁, f₂) in fusiontrees(tsrc)
116-
# for ((f₁′, f₂′), coeff) in planar_trace(f₁, f₂, p1, p2, q1, q2)
117-
# TO._trace!(α * coeff, tsrc[f₁, f₂], true, tdst[f₁′, f₂′], pdata, q1, q2)
118-
# end
119-
# end
120-
# return tdst
121-
# end
122-
123-
# function planar_contract!(α, A::AbstractTensorMap{S}, B::AbstractTensorMap{S},
124-
# β, C::AbstractTensorMap{S},
125-
# oindA::IndexTuple, cindA::IndexTuple,
126-
# oindB::IndexTuple, cindB::IndexTuple,
127-
# p1::IndexTuple, p2::IndexTuple,
128-
# syms::Union{Nothing,NTuple{3,Symbol}}=nothing) where {S}
129-
# codA, domA = codomainind(A), domainind(A)
130-
# codB, domB = codomainind(B), domainind(B)
131-
# oindA, cindA, oindB, cindB = reorder_indices(codA, domA, codB, domB, oindA, cindA,
132-
# oindB, cindB, p1, p2)
133-
134-
# if oindA == codA && cindA == domA
135-
# A′ = A
136-
# else
137-
# if isnothing(syms)
138-
# A′ = TO.similar_from_indices(eltype(A), oindA, cindA, A, :N)
139-
# else
140-
# A′ = TO.cached_similar_from_indices(syms[1], eltype(A), oindA, cindA, A, :N)
141-
# end
142-
# add_transpose!(true, A, false, A′, oindA, cindA)
143-
# end
144-
# if cindB == codB && oindB == domB
145-
# B′ = B
146-
# else
147-
# if isnothing(syms)
148-
# B′ = TO.similar_from_indices(eltype(B), cindB, oindB, B, :N)
149-
# else
150-
# B′ = TO.cached_similar_from_indices(syms[2], eltype(B), cindB, oindB, B, :N)
151-
# end
152-
# add_transpose!(true, B, false, B′, cindB, oindB)
153-
# end
154-
# mul!(C, A′, B′, α, β)
155-
# return C
156-
# end
157-
15887
# auxiliary routines
15988
_cyclicpermute(t::Tuple) = (Base.tail(t)..., t[1])
16089
_cyclicpermute(t::Tuple{}) = ()

src/planar/postprocessors.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
# to correct for this by adding the `istemp = true` flag.
66
function _annotate_temporaries(ex, temporaries)
77
if isexpr(ex, :(=)) && isexpr(ex.args[2], :call) &&
8-
ex.args[2].args[1] (:tensoralloc_add, :tensoralloc_contract)
8+
ex.args[2].args[1] (:tensoralloc_add, :tensoralloc_contract)
99
lhs = ex.args[1]
1010
i = findfirst(==(lhs), temporaries)
1111
if i !== nothing
1212
rhs = ex.args[2]
1313
# add `istemp = true` flag
14-
newrhs = Expr(:call, rhs.args[1:end-1]..., true)
14+
newrhs = Expr(:call, rhs.args[1:(end - 1)]..., true)
1515
return Expr(:(=), lhs, newrhs)
1616
end
1717
elseif ex isa Expr
@@ -40,7 +40,7 @@ function _free_temporaries(ex, temporaries)
4040
push!(newargs, Expr(:call, :tensorfree!, t))
4141
push!(newargs, lhs)
4242
else
43-
newargs = insert!(newargs, i+1, Expr(:call, :tensorfree!, t))
43+
newargs = insert!(newargs, i + 1, Expr(:call, :tensorfree!, t))
4444
end
4545
end
4646
return Expr(:block, newargs...)
@@ -53,7 +53,9 @@ end
5353
# NOTE: work around a somewhat unfortunate interface choice in TensorOperations, which we will correct in the future.
5454
_planaradd!(C, p, A, α, β, backend...) = planaradd!(C, A, p, α, β, backend...)
5555
_planartrace!(C, p, A, q, α, β, backend...) = planartrace!(C, A, p, q, α, β, backend...)
56-
_planarcontract!(C, pAB, A, pA, B, pB, α, β, backend...) = planarcontract!(C, A, pA, B, pB, pAB, α, β, backend...)
56+
function _planarcontract!(C, pAB, A, pA, B, pB, α, β, backend...)
57+
return planarcontract!(C, A, pA, B, pB, pAB, α, β, backend...)
58+
end
5759
# TODO: replace _planarmethod with planarmethod in everything below
5860
const _PLANAR_OPERATIONS = (:_planaradd!, :_planartrace!, :_planarcontract!)
5961

@@ -97,4 +99,4 @@ function insert_operationbackend(ex, backend)
9799
else
98100
return ex
99101
end
100-
end
102+
end

0 commit comments

Comments
 (0)