+1 vote
asked by (130 points)

Hello. I'm new to both Julia and ITensor, so I have some rudimentary questions.
The first is that I thought that Julia was a functional language in the sense it did not have objects. Why is ITensor then built off of objects? How do we get the speed-ups I've heard Julia has then? Why Julia instead of Python or something then?

The next question is more detailed. I understand that TN algorithms often just rely on random ansatz, but I have a naive view that I should be able to treat an ITensor() obj as an array. Is there a method to convert an ITensor() obj to a Julia array? Or even better, set a ITensor() obj equal to some array that has already been created?

For example, it seems like I should be able to write

i = Index(2);
j = Index(2);
k = Index(2);
Aten =ITensor(i,j,k);
A = rand(Float64,2,2,2)
Aten = A

and this would be the same as

i = Index(2);
j = Index(2);
k = Index(2);
Aten = randomITensor(i,j,k);
commented by (70.1k points)
Hi Andrew,
I see you posted a little more in your question with some code examples.

In your first block of code above, when you say it should be the same, is the goal of the code to do a comparison between Aten and A to check that they are equal in some sense? Or to construct Aten by filling it with the numbers inside A? (E.g. note that your second example doesn't involve A so I'm not totally sure what you mean by "should be the same as".)

Thanks,
Miles

1 Answer

0 votes
answered by (70.1k points)

Hi Andrew,
These are good questions. Let me give some brief answers and we could discuss any followup questions you may have in the comments below:

  • technically you are right and Julia does not have objects but "structs". However, these are very similar concepts to each other, so we casually say objects and structs interchangeably when talking about Julia variables. The only difference between an object and a struct (even in C++) is that an object in C++ can have "class methods" or functions 'bound' to it which have certain access privileges to private data inside the class, whereas in Julia methods are always external, separate functions and there is no concept of public or private data. Because there's no private data in Julia, external methods can do all of the same things that class methods can do in C++.

  • the speedups of Julia are unrelated to whether it has structs or classes or similar. Likewise C++ is not fast because it has object-oriented features. What makes both C++ and Julia fast is that (1) they are compiled languages where (2) the compiler can reason about the types of variables that occur in function definitions in order to optimize code specifically for each type that might get passed to a function. In practice, well written Julia code gets compiled to highly optimized assembly code the first time it is run and is thus very fast every time after that.

  • we prefer Julia to python for many reasons, the main one being the speed. I already mentioned above the main reason (though not the only one!) why Julia is so fast. Basically python is a very slow language because it does not get compiled to assembly code and for other crucial reasons related to how data is handled. So essentially all fast python code is really C/C++ code underneath (software like numpy, TensorFlow, PyTorch, etc.). We did not want to have to program in two languages (the "two language problem") as is required for fast python code, and also Julia is arguably a nicer and more expressive language (this is a long discussion but Julia's multiple-dispatch feature solves what is called the 'expression problem' which lets code be arbitrarily extensible whereas Python and C++'s object oriented approach only solves one half of the expression problem).

  • Yes, you can construct ITensors from arrays and arrays from ITensors. Here is the section in the docs showing examples of making ITensor from arrays:
    https://itensor.github.io/ITensors.jl/stable/examples/ITensor.html#Constructing-ITensors-from-Arrays
    To make an array from an ITensor, you can use one of these functions:
    https://itensor.github.io/ITensors.jl/stable/ITensorType.html#Convert-to-Array

  • It is sometimes true that tensor network algorithms involve random tensors, but there are a lot of algorithms that do not

Hope that helps!

Miles

commented by (130 points)
My code example was an attempt to ask about the synatx in the documentation " Constructing ITensors from Arrays" in case you wanted a different random distribution or something. Your link answered everything. Thank you for such a detailed explanation. Thanks!
commented by (70.1k points)
I see, yes so if you are asking about whether the two-step procedure of making a random Julia array and passing it to the ITensor constructor is the same as calling the function `randomITensor` then yes, they are the same (with a minor caveat about owning/view behavior of the data which you can ignore to a first approximation).

We made the `randomITensor` function so that one could directly make ITensors with random elements without having to go to the trouble of first making an array with the same dimension as a separate step.
Welcome to ITensor Support Q&A, where you can ask questions and receive answers from other members of the community.

Formatting Tips:
  • To format code, indent by four spaces
  • To format inline LaTeX, surround it by @@ on both sides
  • To format LaTeX on its own line, surround it by $$ above and below
  • For LaTeX, it may be necessary to backslash-escape underscore characters to obtain proper formatting. So for example writing \sum\_i to represent a sum over i.
If you cannot register due to firewall issues (e.g. you cannot see the capcha box) please email Miles Stoudenmire to ask for an account.

To report ITensor bugs, please use the issue tracker.

Categories

...