# What is the InitState correspondence in ITensor julia?

**URL:** <https://itensor.discourse.group/t/what-is-the-initstate-correspondence-in-itensor-julia/158>\
**Category:** ITensor Julia Questions\
**Created:** [May 19, 2022, 2:41pm UTC](https://itensor.discourse.group/t/what-is-the-initstate-correspondence-in-itensor-julia/158 "2022-05-19T14:41:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![coolgas](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/coolgas/32/72_2.png) [@coolgas](https://itensor.discourse.group/u/coolgas)\
**Post date:** [May 19, 2022, 2:41pm UTC](https://itensor.discourse.group/t/what-is-the-initstate-correspondence-in-itensor-julia/158/1 "2022-05-19T14:41:05Z")

</div>

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![miles](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/miles/32/6_2.png) [@miles](https://itensor.discourse.group/u/miles)\
**Post date:** [May 19, 2022, 3:03pm UTC](https://itensor.discourse.group/t/what-is-the-initstate-correspondence-in-itensor-julia/158/2 "2022-05-19T15:03:06Z")

</div>

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](https://itensor.github.io/ITensors.jl/stable/MPSandMPO.html#MPS-Constructors) and includes a code example. Here’s the example it shows:

```auto
N = 10
sites = siteinds("S=1/2",N)
states = [isodd(n) ? "Up" : "Dn" for n=1:N]
psi = MPS(sites,states)

```

---

<div class="post-metadata">

**Author:** ![coolgas](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/coolgas/32/72_2.png) [@coolgas](https://itensor.discourse.group/u/coolgas)\
**Post date:** [May 19, 2022, 3:40pm UTC](https://itensor.discourse.group/t/what-is-the-initstate-correspondence-in-itensor-julia/158/3 "2022-05-19T15:40:41Z")

</div>

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

```auto
N = 10
sites = siteinds("Qudit",N)
states = ["1" for i in 1:N]
psi = MPS(sites,states)

```

So Am I doing right in this case?

---

<div class="post-metadata">

**Author:** ![miles](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/miles/32/6_2.png) [@miles](https://itensor.discourse.group/u/miles)\
**Post date:** [May 20, 2022, 1:52pm UTC](https://itensor.discourse.group/t/what-is-the-initstate-correspondence-in-itensor-julia/158/4 "2022-05-20T13:52:59Z")

</div>

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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![coolgas](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/coolgas/32/72_2.png) [@coolgas](https://itensor.discourse.group/u/coolgas)\
**Post date:** [May 21, 2022, 10:00am UTC](https://itensor.discourse.group/t/what-is-the-initstate-correspondence-in-itensor-julia/158/5 "2022-05-21T10:00:18Z")

</div>

Got it! Really appreciate!
