You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature request] Lazy evaluation of tensor contractions #329
In ED and diagonalizing transfer matrices (constructed from TNR algorithms), those transfer matrices are usually defined formally by acting on states.
I am wondering whether it is possible for TensorOperations to open an interface such that users can directly access and manipulate the evaluation order of contractions, and define our own lazy evaluations?
Could you elaborate a bit more on what you actually want to see from this interface? I have definitely at some point played around a bit with this, but found it hard to convince myself that just having a closure or callable struct doesn't suffice. For the examples you provide, an anonymous function seems to provide most of the same functionality right?
Just to add some things that might be useful to add to such an interface, although this requires quite a bit of redesigning:
In some of the more modern (and especially GPU) backends, the pair wise contractions happen by first creating a plan, and then executing it. This might be something worth storing/reusing at the network level.
we could store the intermediates if the total memory footprint isn't an issue, such that repeated execution does not allocate.
We could consider some way of accessing the structure of the map, making it less of an opaque closure. I'm thinking of things like querying the structure of the input and output, or even defining the adjoint of the tensor contraction as a map. This does somewhat restrict to networks with one in and output tensor though.
In the example of transfer matrix, a closure indeed can already done this. But we cannot change the contraction and structure of network by hand, nor can we extract the internal structure of the closure, such as matrix ranks and spaces data.
This interface can possibly allow general definition of a contraction of a tensor network, which also potentially be applied to MPS, PEPS, ED and TNR as a unified interface for tensor network.
For example in MPS, the contraction plan can be chosen as totally dense as long as we do not explicitly do evaluate(mps). Different caching strategies become totally encoded into the mps.network part and can be manipulated independently as an object. The mps.network fully encodes the structure of the MPS and therefore is a sufficient description of a tensor network. For general tensor networks, we can define check_consistency(tensor_network) for general space check.
The problem is a bit that as soon as you allow the tensors to be manipulated, you lose the advantage of being able to precompute/cache anything, without having to keep track which parts get invalidated.
At that point the question becomes a bit whether it is actually beneficial to mutate an existing "network", as opposed to just creating a new one.
The main annoyance is though that most of our machinery is very poorly set up for dynamic contractions, so changing the contraction order or ranks or number of tensors at runtime more or less necessarily makes the contraction lose type stability. If it is mostly convenience you are looking for, this might not matter and maybe this can be fine, but if it is performance I don't think it is straightforward to make this work.
Just working this out a little bit, something that could be possible is the following:
@tensornetworkfunctionTransferMatrix(x)
return y[i j] := A[i k l] *conj(A[j k m]) * x[l m]
end
which would then expand to something like:
struct TransferMatrix{TA}
A::TAend
(T::TransferMatrix)(x) =@tensor y[i j] := T.A[i k l] *conj(T.A[j k m]) * x[l m]
with possibly a little bit more bells and whistles for input/output structures.
In a second pass we could also make it store its intermediate tensors so these can be reused, slightly improving the performance of repeated application.
Is this kind of what you are after?
The main problem is that this definitely does not have the ability of changing the rank, since that both breaks the @tensor expression, as well as requires TA = Any, and the same is true for being able to change the eltype.
In principle we could allow changing the spaces, but that would invalidate any intermediates we had stored, and then the question becomes why not just construct a new TransferMatrix object.
Anyways, I'm just thinking out loud here, let me know if any of this makes sense
Thanks for the explanation. I hadn't considered the type-stability issue.
I'm not sure how useful a fully dynamic interface would be if it came with a significant performance cost. Does this mean that, to retain type stability and performance, we would generally need to settle on a contraction structure and order before repeatedly evaluating the network?
If so, would other approaches effectively be equivalent to generating or writing a specialized contraction function for each network structure and then calling it whenever needed?
In the design I had in mind, all tensor types are fixed. The network specifies only the Einstein summation indices, and its connectivity does not change. However, different contraction paths can produce intermediate tensors of different types. Am I understanding correctly that choosing the contraction order at runtime could therefore lead to type instability?
Defining the transfer matrix and its action through a fixed function is indeed already sufficient for this purpose, as in the current implementations in MPSKit and TNRKit. My original idea was to store the index information in a network field of TransferMatrix, so that transfer matrices constructed from different geometrical arrangements of fixed-point tensors could share the same type. A generic implementation of their action on a state could then work by reading the index information stored in network.
If I'm understanding this correctly, indeed the claim is that changing the contraction order loses the type stability.
The problem is just that if we fix the contraction, and its order (as is the case for @tensor expressions), things are type stable because we can figure out the types of all of the intermediates from that.
If the order or network becomes a runtime value instead, the resulting pattern of intermediates can no longer be determined at compile time and therefore we lose type stability.
This is effectively the current trade-off between @tensor and ncon.
Note though that for large contractions this overhead can actually sometimes be negligible, as long as the type instability does not leak into the surrounding code.
For example, just having ncon!(destination, tensors, indices) would mean that we can manage the dynamic dispatches within the ncon! call, with reasonably low overhead for each.
If you are looking for more functionality that prints/manipulates/handles the network topology and contraction order, it might also be reasonable to have a look at OMEinsum.jl, which provides static and dynamic expressions that you can manipulate.
This does have the same trade-offs though.
Ultimately, I've never really found a compelling case to introduce this feature and the associated maintenance over just having closures, although MPSKit is kind of filled with these callable structures like the TransferMatrix etc, but I'm not sure that we can provide much more functionality to improve this.
For completeness, in case anyone in the future wants to have a look at this, one of the things we could do if we really implemented this is to automatically "prepermute" the fixed tensors of the network into their most efficient orientation, such that repeated applications don't have to pay this cost.
Although at the time of writing this, I think most of the MPSKit implementations actually already do this, albeit manually, though this is definitely worth double-checking.
Activity
lkdvos commentedon Nov 19, 2026
Could you elaborate a bit more on what you actually want to see from this interface? I have definitely at some point played around a bit with this, but found it hard to convince myself that just having a closure or callable struct doesn't suffice. For the examples you provide, an anonymous function seems to provide most of the same functionality right?
Just to add some things that might be useful to add to such an interface, although this requires quite a bit of redesigning:
Chenqitrg commentedon Nov 19, 2026
In the example of transfer matrix, a closure indeed can already done this. But we cannot change the contraction and structure of network by hand, nor can we extract the internal structure of the closure, such as matrix ranks and spaces data.
This interface can possibly allow general definition of a contraction of a tensor network, which also potentially be applied to MPS, PEPS, ED and TNR as a unified interface for tensor network.
For example in MPS, the contraction plan can be chosen as totally dense as long as we do not explicitly do
evaluate(mps). Different caching strategies become totally encoded into themps.networkpart and can be manipulated independently as an object. Themps.networkfully encodes the structure of the MPS and therefore is a sufficient description of a tensor network. For general tensor networks, we can definecheck_consistency(tensor_network)for general space check.lkdvos commentedon Nov 19, 2026
The problem is a bit that as soon as you allow the tensors to be manipulated, you lose the advantage of being able to precompute/cache anything, without having to keep track which parts get invalidated.
At that point the question becomes a bit whether it is actually beneficial to mutate an existing "network", as opposed to just creating a new one.
The main annoyance is though that most of our machinery is very poorly set up for dynamic contractions, so changing the contraction order or ranks or number of tensors at runtime more or less necessarily makes the contraction lose type stability. If it is mostly convenience you are looking for, this might not matter and maybe this can be fine, but if it is performance I don't think it is straightforward to make this work.
Just working this out a little bit, something that could be possible is the following:
which would then expand to something like:
with possibly a little bit more bells and whistles for input/output structures.
In a second pass we could also make it store its intermediate tensors so these can be reused, slightly improving the performance of repeated application.
Is this kind of what you are after?
The main problem is that this definitely does not have the ability of changing the rank, since that both breaks the
@tensorexpression, as well as requiresTA = Any, and the same is true for being able to change theeltype.In principle we could allow changing the spaces, but that would invalidate any intermediates we had stored, and then the question becomes why not just construct a new
TransferMatrixobject.Anyways, I'm just thinking out loud here, let me know if any of this makes sense
Chenqitrg commentedon Nov 19, 2026
Thanks for the explanation. I hadn't considered the type-stability issue.
I'm not sure how useful a fully dynamic interface would be if it came with a significant performance cost. Does this mean that, to retain type stability and performance, we would generally need to settle on a contraction structure and order before repeatedly evaluating the network?
If so, would other approaches effectively be equivalent to generating or writing a specialized contraction function for each network structure and then calling it whenever needed?
In the design I had in mind, all tensor types are fixed. The network specifies only the Einstein summation indices, and its connectivity does not change. However, different contraction paths can produce intermediate tensors of different types. Am I understanding correctly that choosing the contraction order at runtime could therefore lead to type instability?
Defining the transfer matrix and its action through a fixed function is indeed already sufficient for this purpose, as in the current implementations in
MPSKitandTNRKit. My original idea was to store the index information in anetworkfield ofTransferMatrix, so that transfer matrices constructed from different geometrical arrangements of fixed-point tensors could share the same type. A generic implementation of their action on a state could then work by reading the index information stored innetwork.lkdvos commentedon Nov 19, 2026
If I'm understanding this correctly, indeed the claim is that changing the contraction order loses the type stability.
The problem is just that if we fix the contraction, and its order (as is the case for
@tensorexpressions), things are type stable because we can figure out the types of all of the intermediates from that.If the order or network becomes a runtime value instead, the resulting pattern of intermediates can no longer be determined at compile time and therefore we lose type stability.
This is effectively the current trade-off between
@tensorandncon.Note though that for large contractions this overhead can actually sometimes be negligible, as long as the type instability does not leak into the surrounding code.
For example, just having
ncon!(destination, tensors, indices)would mean that we can manage the dynamic dispatches within thencon!call, with reasonably low overhead for each.If you are looking for more functionality that prints/manipulates/handles the network topology and contraction order, it might also be reasonable to have a look at OMEinsum.jl, which provides static and dynamic expressions that you can manipulate.
This does have the same trade-offs though.
Ultimately, I've never really found a compelling case to introduce this feature and the associated maintenance over just having closures, although MPSKit is kind of filled with these callable structures like the TransferMatrix etc, but I'm not sure that we can provide much more functionality to improve this.
For completeness, in case anyone in the future wants to have a look at this, one of the things we could do if we really implemented this is to automatically "prepermute" the fixed tensors of the network into their most efficient orientation, such that repeated applications don't have to pay this cost.
Although at the time of writing this, I think most of the MPSKit implementations actually already do this, albeit manually, though this is definitely worth double-checking.