ITensor / ITensors.jl

A Julia library for efficient tensor computations and tensor network calculations
https://itensor.org
Apache License 2.0
536 stars 124 forks source link

[ITensors] [ENHANCEMENT] Shorthand ITensor constructors #1037

Open mtfishman opened 1 year ago

mtfishman commented 1 year ago

I'm not sure why I hadn't thought of this before, but we could overload Julia's array constructors for Index inputs to define short-hands for constructing ITensors. For example:

using ITensors
Base.randn(inds::Index...) = itensor(randn(dims(inds)...), inds...)
Base.rand(inds::Index...) = itensor(rand(dims(inds)...), inds...)
Base.ones(inds::Index...) = itensor(ones(dims(inds)...), inds...)
Base.zeros(inds::Index...) = itensor(zeros(dims(inds)...), inds...)
Base.fill(value, inds::Index...) = itensor(fill(value, dims(inds)...), inds...)

I think this is pretty natural, considering we already have specialized constructors like delta(::Index...) and combiner(::Index...).

It would also be useful for making a generic constructor for ITensorNetworks.ITensorNetwork, where I am planning on defining a constructor that accepts a function that generates the tensors in the network given the vertex and the specified indices of the network. For example:

using ITensors
using ITensorNetworks
using NamedGraphs
s = siteinds("S=1/2", named_grid((3, 3)))
tn = ITensorNetwork((v, inds...) -> rand(inds...), s; link_space=3)

@JoeyT1994 this is related to some of the constructors you wrote for specialized tensor networks.

For creating ITensors on GPU, we could then use the shorthands CUDA.zeros, CUDA.ones, CUDA.rand, CUDA.randn, etc. defined in CUDA.jl (e.g. https://cuda.juliagpu.org/stable/usage/array/#Random-numbers). @kmp5VT that would be related to what you're working on making ITensor constructors more generic for GPU data storage.

emstoudenmire commented 1 year ago

Yes, this is a great idea and I also wonder why none of us thought of it. Does seem helpful for generic programming too.