We know that in C++ ITensor to initialize, say a chain of spinhalf particles with every other spin to be “Dn” (down). The code is as following
const int N = 100;
auto sites = SpinHalf(N);
//First set all spins to be "Up"
auto state = InitState(sites,"Up");
//Now set every other spin to be "Dn" (down)
for(int j = 2; j <= N; j += 2)
{
state.set(j,"Dn");
}
auto neel_state = MPS(state);
My question is that what is the corresponding code snippet in Julia to realize the same functionality?
Good question - in Julia instead of an InitState object, you can just use a Julia array of numbers or of strings like "Up" and “Dn”`. The documentation for the MPS constructor taking a state array is here and includes a code example. Here’s the example it shows:
N = 10
sites = siteinds("S=1/2",N)
states = [isodd(n) ? "Up" : "Dn" for n=1:N]
psi = MPS(sites,states)
Hi Miles! Thanks for your answer! Just another quick question I forgot to include in the original question. So for bosons, if I want to initialize a state, say there is 1 particle in each site, then the code snippet will be
N = 10
sites = siteinds("Qudit",N)
states = ["1" for i in 1:N]
psi = MPS(sites,states)
Yes, that is correct. You can always check the state afterward to see if it has the right properties. For example, to measure the density on each site you can do this:
dens = expect(psi,"N")
@show dens
which will compute a vector dens of the values \langle N_j\rangle for each site which should all be 1.0.
Also if you are simulating bosons, you can use "Boson" instead of "Qudit". They are exactly the same – just aliases for each other – but thought I would mention that.