|
54 | 54 | # TODO: replace _planarmethod with planarmethod in everything below |
55 | 55 | const _PLANAR_OPERATIONS = (:planaradd!, :planartrace!, :planarcontract!) |
56 | 56 | const _PLANAR_ALLOCATIONS = (:planaralloc_contract,) |
| 57 | +const _PLANAR_CONTRACTIONS = (:planarcontract!, :planaralloc_contract) |
57 | 58 |
|
58 | 59 | function _insert_planar_operations(ex) |
59 | 60 | if isexpr(ex, :call) |
@@ -99,6 +100,29 @@ function _insert_planar_operations(ex) |
99 | 100 | return ex |
100 | 101 | end |
101 | 102 |
|
| 103 | +""" |
| 104 | + canonicalizeplanarindices(ex, partitions) |
| 105 | +
|
| 106 | +Replace the index tuples of every planar contraction in `ex` by their canonical form, as |
| 107 | +obtained from [`planar_contract_indices`](@ref) and the index partitions in `partitions`. |
| 108 | +Contractions whose operands have no known partition are left to be canonicalized at runtime. |
| 109 | +""" |
| 110 | +function canonicalizeplanarindices(ex, partitions) |
| 111 | + if isexpr(ex, :call) && length(ex.args) ≥ 7 && ex.args[1] isa GlobalRef && |
| 112 | + ex.args[1].mod === TensorKit && ex.args[1].name ∈ _PLANAR_CONTRACTIONS |
| 113 | + A, pA, B, pB, pAB = ex.args[3], ex.args[4], ex.args[5], ex.args[6], ex.args[7] |
| 114 | + WA, WB = get(partitions, A, nothing), get(partitions, B, nothing) |
| 115 | + if !isnothing(WA) && !isnothing(WB) && pA isa Index2Tuple && |
| 116 | + pB isa Index2Tuple && pAB isa Index2Tuple |
| 117 | + args = copy(ex.args) |
| 118 | + args[4], args[6], args[7] = planar_contract_indices(WA, pA, WB, pB, pAB) |
| 119 | + return Expr(:call, args...) |
| 120 | + end |
| 121 | + end |
| 122 | + return ex isa Expr ? |
| 123 | + Expr(ex.head, (canonicalizeplanarindices(a, partitions) for a in ex.args)...) : ex |
| 124 | +end |
| 125 | + |
102 | 126 | # like `TO.insertargument`, but matching `GlobalRef`s into `TensorKit` |
103 | 127 | function _insertargument(ex, arg, methods) |
104 | 128 | if isexpr(ex, :call) && ex.args[1] isa GlobalRef && |
|
0 commit comments