Skip to content

Commit

Permalink
some more planar fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jutho committed Dec 1, 2023
1 parent 596b038 commit 8a2df70
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/planar/analyzers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ function get_possible_planar_indices(ex)
end
end
return inds
elseif isexpr(ex, :call) && ex.args[1] == :/
return get_possible_planar_indices(ex.args[2])
elseif isexpr(ex, :call) && ex.args[1] == :\
return get_possible_planar_indices(ex.args[3])

Check warning on line 42 in src/planar/analyzers.jl

View check run for this annotation

Codecov / codecov/patch

src/planar/analyzers.jl#L41-L42

Added lines #L41 - L42 were not covered by tests
else
return Any[]
end
Expand Down
21 changes: 17 additions & 4 deletions src/planar/preprocessors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ end
_construct_braidingtensors(x) = x

function _construct_braidingtensors!(ex, preargs, indexmap) # ex is guaranteed to be a single tensor expression
if TO.istensor(ex)
if TO.isscalarexpr(ex)
return ex, true
elseif TO.istensor(ex)
obj, leftind, rightind = TO.decomposetensor(ex)
if _remove_adjoint(obj) ==
# try to construct a braiding tensor
Expand Down Expand Up @@ -216,9 +218,14 @@ function _construct_braidingtensors!(ex, preargs, indexmap) # ex is guaranteed t
end
newex = Expr(ex.head, newargs...)
return newex, success

Check warning on line 220 in src/planar/preprocessors.jl

View check run for this annotation

Codecov / codecov/patch

src/planar/preprocessors.jl#L219-L220

Added lines #L219 - L220 were not covered by tests
elseif isexpr(ex, :call) && ex.args[1] == :/ && length(ex.args) == 3
newarg, success = _construct_braidingtensors!(ex.args[2], preargs, indexmap)
return Expr(:call, :/, newarg, ex.args[3]), success
elseif isexpr(ex, :call) && ex.args[1] == :\ && length(ex.args) == 3
newarg, success = _construct_braidingtensors!(ex.args[3], preargs, indexmap)
return Expr(:call, :\, ex.args[2], newarg), success

Check warning on line 226 in src/planar/preprocessors.jl

View check run for this annotation

Codecov / codecov/patch

src/planar/preprocessors.jl#L224-L226

Added lines #L224 - L226 were not covered by tests
else
@show("huh?")
return ex, true
error("unexpected expression $ex")

Check warning on line 228 in src/planar/preprocessors.jl

View check run for this annotation

Codecov / codecov/patch

src/planar/preprocessors.jl#L228

Added line #L228 was not covered by tests
end
end

Expand Down Expand Up @@ -520,8 +527,14 @@ function _extract_contraction_pairs(rhs, lhs, pre, temporaries)
for
a in rhs.args[2:end]]
return Expr(rhs.head, rhs.args[1], args...)
elseif isexpr(rhs, :call) && rhs.args[1] == :/
newarg = _extract_contraction_pairs(rhs.args[2], lhs, pre, temporaries)
return Expr(:call, :/, newarg, rhs.args[3])
elseif isexpr(rhs, :call) && rhs.args[1] == :\
newarg = _extract_contraction_pairs(rhs.args[3], lhs, pre, temporaries)
return Expr(:call, :\, rhs.args[2], newarg)

Check warning on line 535 in src/planar/preprocessors.jl

View check run for this annotation

Codecov / codecov/patch

src/planar/preprocessors.jl#L533-L535

Added lines #L533 - L535 were not covered by tests
else
throw(ArgumentError("unknown tensor expression"))
throw(ArgumentError("unknown tensor expression $ex"))

Check warning on line 537 in src/planar/preprocessors.jl

View check run for this annotation

Codecov / codecov/patch

src/planar/preprocessors.jl#L537

Added line #L537 was not covered by tests
end
end

Expand Down
23 changes: 23 additions & 0 deletions src/tensors/braidingtensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ function planarcontract!(C::AbstractTensorMap{S,N₁,N₂},
end
return C
end
function planarcontract!(C::AbstractTensorMap{S,N₁,N₂},

Check warning on line 274 in src/tensors/braidingtensor.jl

View check run for this annotation

Codecov / codecov/patch

src/tensors/braidingtensor.jl#L274

Added line #L274 was not covered by tests
A::BraidingTensor{S},
(oindA, cindA)::Index2Tuple{2,2},
B::BraidingTensor{S},
(cindB, oindB)::Index2Tuple{2,2},
(p1, p2)::Index2Tuple{N₁,N₂},
α::Number, β::Number,
backend::Backend...) where {S,N₁,N₂}
return planarcontract!(C, copy(A), (oindA, cindA), B, (cindB, oindB), (p1, p2), α, β,

Check warning on line 282 in src/tensors/braidingtensor.jl

View check run for this annotation

Codecov / codecov/patch

src/tensors/braidingtensor.jl#L282

Added line #L282 was not covered by tests
backend...)
end

function planarcontract!(C::AbstractTensorMap{S,N₁,N₂},
A::AbstractTensorMap{S},
(oindA, cindA)::Index2Tuple{N₃,2},
Expand Down Expand Up @@ -317,6 +329,17 @@ function planarcontract!(C::AbstractTensorMap{S,N₁,N₂},
end
return C
end
function planarcontract!(C::AbstractTensorMap{S,N₁,N₂},
A::BraidingTensor{S},
(oindA, cindA)::Index2Tuple{2,2},
B::BraidingTensor{S},
(cindB, oindB)::Index2Tuple{2,2},
(p1, p2)::Index2Tuple{N₁,N₂},
α::Number, β::Number,
backend::Backend...) where {S,N₁,N₂}
return planarcontract!(C, copy(A), (oindA, cindA), B, (cindB, oindB), (p1, p2), α, β,
backend...)
end

# Fallback cases for planarcontract!
# TODO: implement specialised cases for contracting 0, 1, 3 and 4 indices
Expand Down
14 changes: 7 additions & 7 deletions test/planar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ end
t1 = TensorMap(rand, T, V1 V2)
t2 = TensorMap(rand, T, V2 V1)

tr1 = @planar opt = true t1[a; b] * t2[b; a]
tr2 = @planar opt = true t1[d; a] * t2[b; c] * τ[c b; a d]
tr3 = @planar opt = true t1[d; a] * t2[b; c] * τ[a c; d b]
tr4 = @planar opt = true t1[f; a] * t2[c; d] * τ[d b; c e] * τ[e b; a f]
tr5 = @planar opt = true t1[f; a] * t2[c; d] * τ[d b; c e] * τ[a e; f b]
tr6 = @planar opt = true t1[f; a] * t2[c; d] * τ[c d; e b] * τ[e b; a f]
tr7 = @planar opt = true t1[f; a] * t2[c; d] * τ[c d; e b] * τ[a e; f b]
tr1 = @planar opt = true t1[a; b] * t2[b; a]/2
tr2 = @planar opt = true t1[d; a] * t2[b; c] * 1/2 * τ[c b; a d]
tr3 = @planar opt = true t1[d; a] * t2[b; c] * τ[a c; d b] /2
tr4 = @planar opt = true t1[f; a] * 1/2 * t2[c; d] * τ[d b; c e] * τ[e b; a f]
tr5 = @planar opt = true t1[f; a] * t2[c; d]/2 * τ[d b; c e] * τ[a e; f b]
tr6 = @planar opt = true t1[f; a] * t2[c; d] * τ[c d; e b]/2 * τ[e b; a f]
tr7 = @planar opt = true t1[f; a] * t2[c; d] * (τ[c d; e b] * τ[a e; f b] /2)

@test tr1 tr2 tr3 tr4 tr5 tr6 tr7
end
Expand Down

0 comments on commit 8a2df70

Please sign in to comment.