Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy ITensor contraction #248

Closed
mtfishman opened this issue Apr 1, 2020 · 3 comments
Closed

Lazy ITensor contraction #248

mtfishman opened this issue Apr 1, 2020 · 3 comments
Assignees
Labels
broadcasting Issues related to broadcasting ITensors. enhancement New feature or request performance Issues related to performance
Milestone

Comments

@mtfishman
Copy link
Member

This is a reminder that it would be great to have lazy ITensor contraction support and broadcasting working with contractions.

This could be done through the broadcast syntax, for example:

C .= a .* A .* B   # mul!(C, A, B, a)
C .= a .* A .* B .+ b .* C  # mul!(C, A, B, a, b)
D .= A .* B .* C  # lazily contract A, B, and C, optimizing contraction order
D .= A .+ B .+ C  # Fuse into a single call
@mtfishman
Copy link
Member Author

Some of these were addressed by #251. It would still be nice to have lazy contraction of arbitrary numbers of ITensors, with contraction order optimization.

I think that we could even include contraction order optimization with regular tensor contraction, like A*B*C, since:

julia> i = Index(2)
(dim=2|id=53)

julia> A = randomITensor(i,i')
ITensor ord=2 (dim=2|id=53) (dim=2|id=53)' 
Dense{Float64,Array{Float64,1}}

julia> B = randomITensor(i',i'')
ITensor ord=2 (dim=2|id=53)' (dim=2|id=53)'' 
Dense{Float64,Array{Float64,1}}

julia> C = randomITensor(i'', i)
ITensor ord=2 (dim=2|id=53)'' (dim=2|id=53) 
Dense{Float64,Array{Float64,1}}

julia> @which A*B*C
*(a, b, c, xs...) in Base at operators.jl:529

so it looks like we can overload *(::ITensor, ::ITensor, ::ITensor, ::ITensor...) and do contraction order optimization (and other optimizations, for example ones involving delta tensors).

@emstoudenmire
Copy link
Collaborator

Yes, let's definitely look into this. It could be a really killer feature of ITensor. I'm really glad we might be able to do this just with * in addition to broadcasted operations.

One issue to think through is how we can deal with views versus copies. Copying the tensors is safe but costly; not copying is more efficient but trickier for ensuring correctness.

So I imagine what you're thinking of here is a sort of semi-eager mode, where we are lazy about evaluating the separate contractions within R = ABC*D but as soon as we have all of those tensors, we eagerly compute the result R before going on to the next line of code. Correct?

@mtfishman
Copy link
Member Author

In both cases (the broadcasting and non-broadcasting versions), I was thinking it would work semi-eagerly as you say.

In the case of broadcasting, Julia rewrites a call like R = A .* B .* C into a Broadcast.Broadcasted object that stores views of A, B and C (and the operations between them). Then you can control the final operation that is done with that object which gets stored in R, so the idea would be that it would do the contraction of A, B, and C into normal ITensor that gets stored in R. In theory we could output a fancier lazy ITensor storage that doesn't actually do the contraction yet, but I don't see the need right now.

In the case of R = A*B*C, it appears to be a built-in Julia feature that this gets rewritten into a call R = *(A,B,C), which means that we can overload *(A::ITensor, B::ITensor, C::ITensor). Again, in theory we could overload that function to output a lazy ITensor that stores those tensors without doing the actually operation, but it seems simpler for now to have that actually perform the contraction with possible order optimization.

I think both of those should cover many cases of lazy operations that we are interested in (we can just tell people that if they want operations treated lazily, put them on the same line and possibly use the dot syntax for more complicated expressions).

@mtfishman mtfishman self-assigned this Oct 8, 2020
@mtfishman mtfishman added this to the v0.3 milestone Oct 8, 2020
@mtfishman mtfishman added broadcasting Issues related to broadcasting ITensors. enhancement New feature or request performance Issues related to performance labels Oct 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
broadcasting Issues related to broadcasting ITensors. enhancement New feature or request performance Issues related to performance
Projects
None yet
Development

No branches or pull requests

2 participants