Skip to content

Commit

Permalink
Change order @planar preprocessors
Browse files Browse the repository at this point in the history
The braidingtensors are now instantiated first, such that contractcheck can now be used.
Fixes #92.
  • Loading branch information
lkdvos committed Nov 28, 2023
1 parent 2a90b1c commit 518e520
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
11 changes: 5 additions & 6 deletions src/planar/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ function planarparser(planarexpr, kwargs...)
push!(parser.postprocessors, ex -> _free_temporaries(ex, temporaries))
push!(parser.postprocessors, _insert_planar_operations)

for kw in kwargs
name, val = kw

# braiding tensors need to be instantiated before kwargs are processed
push!(parser.preprocessors, _construct_braidingtensors)

for (name, val) in kwargs
if name == :order
isexpr(val, :tuple) ||
throw(ArgumentError("Invalid use of `order`, should be `order=(...,)`"))
Expand Down Expand Up @@ -61,9 +62,7 @@ function planarparser(planarexpr, kwargs...)
throw(ArgumentError("Unknown keyword argument `name`."))
end
end
braidingtensors = Vector{Any}()

push!(parser.preprocessors, _construct_braidingtensors)

treebuilder = parser.contractiontreebuilder
treesorter = parser.contractiontreesorter
costcheck = parser.contractioncostcheck
Expand Down
2 changes: 1 addition & 1 deletion src/planar/preprocessors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function _construct_braidingtensors(ex::Expr)
push!(construct_expr.args, indexmap[i1b])
push!(construct_expr.args, indexmap[i2b])
s = gensym()
push!(pre.args, Expr(:(=), s, construct_expr))
push!(pre.args, :(@notensor $s = $construct_expr))
ex = TO.replacetensorobjects(ex) do o, l, r
if o == obj && l == leftind && r == rightind
return obj == ? s : Expr(TO.prime, s)
Expand Down
23 changes: 23 additions & 0 deletions test/planar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ end

@testset "@planar" verbose = true begin
T = ComplexF64

@testset "contractcheck" begin
V =^2
A = TensorMap(rand, T, V V V)
B = TensorMap(rand, T, V V V')
@tensor C1[i j; k l] := A[i j; m] * B[k l; m]
@tensor contractcheck=true C2[i j; k l] := A[i j; m] * B[k l; m]
@test C1 C2
B2 = TensorMap(rand, T, V V V) # wrong duality for third space
@test_throws SpaceMismatch("incompatible spaces for m: $V$(V')") begin
@tensor contractcheck = true C3[i j; k l] := A[i j; m] * B2[k l; m]
end

A = TensorMap(rand, T, V V V)
B = TensorMap(rand, T, V V V)
@planar C1[i; j] := A[i; k l] * τ[k l; m n] * B[m n; j]
@planar contractcheck=true C2[i; j] := A[i; k l] * τ[k l; m n] * B[m n; j]
@test C1 C2
@test_throws SpaceMismatch("incompatible spaces for l: $V$(V')") begin
@planar contractcheck=true C3[i; j] := A[i; k l] * τ[k l; m n] * B[n j; m]
end
end

@testset "MPS networks" begin
P =^2
Vmps =^12
Expand Down

0 comments on commit 518e520

Please sign in to comment.