# defining custom states

**URL:** <https://itensor.discourse.group/t/defining-custom-states/499>\
**Category:** ITensor Julia Questions\
**Tags:** mps, mpo\
**Created:** [November 5, 2022, 6:33pm UTC](https://itensor.discourse.group/t/defining-custom-states/499 "2022-11-05T18:33:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![metapuff](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@metapuff](https://itensor.discourse.group/u/metapuff)\
**Post date:** [November 5, 2022, 6:33pm UTC](https://itensor.discourse.group/t/defining-custom-states/499/1 "2022-11-05T18:33:26Z")

</div>

I am trying to play around with the S=3/2 [example](https://itensor.github.io/ITensors.jl/stable/examples/Physics.html) provided in the documentation by running DMRG on a simple S=3/2 Hamiltonian. Defining the Hamiltonian can be done by defining custom S=3/2 operators as detailed in the documentation. I am however unsure about how to define an initial product state MPS with a particular magnetization, which requires defining custom state types.

For example, I would like to be able to initialize an MPS by calling `randomMPS(sites,states)`, where `states` is a vector of strings (e.g. `["-1/2","3/2",...]` in this example). Does ITensor support the ability to define custom states in this way?

I was also trying an alternative approach, whereby I simply use the state types provided by the `Qudit` site type. In this case however I was not able to define appropriate S=3/2 operators. For example, if I naively do

```auto
ITensors.op(::OpName"Sz",::SiteType"Qudit") = [3/2 0 0 0; 0 1/2 0 0; 0 0 -1/2 0; 0 0 0 -3/2]

```

then I am unable to construct an MPO built from `Sz` operators acting on siteinds initialized from `siteinds("Qudit",L;dim=4)` (attempting to build the MPO throws the error `MethodError: no method matching op(::OpName{:Sz}, ::SiteType{Qudit}, ::Int64)`).

Probably I need to reference the dimension of the qudits in the definition of `Sz`, but I am not sure what the correct syntax to use here is.

Thanks for the help!

---

<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:** [November 6, 2022, 3:45am UTC](https://itensor.discourse.group/t/defining-custom-states/499/2 "2022-11-06T03:45:03Z")

</div>

Yes, the `states` vector that `randomMPS` takes can be (and usually is) a vector of strings. For example, in [this tutorial](https://itensor.github.io/ITensors.jl/stable/tutorials/QN_DMRG.html) you can see that it is a vector of strings like `["Up","Dn","Up","Dn"]` for the case of spin 1/2 used there.

For your custom S=3/2 site type, you can define the states that you want, following [this code example](https://itensor.github.io/ITensors.jl/dev/examples/Physics.html#Making-a-Custom-state-Definition) (which right now only appears in the dev branch of the documentation). So you can define your states by strings and then pass those strings in your `states` vector.

It may be easier to just pass integers as states though too, which also works. If you pass `1` that corresponds to the first setting of the Index, and `2` to the second setting, etc.

Lastly, I’m not sure I understand the part of your question about the “Qudit” site type. If you are making and using sites with the site type of `"S=3/2"` then nothing about the `"Qudit"` site type will apply or work for a different site type. Or do you mean you are actually making indices with the “Qudit” site type?

---

<div class="post-metadata">

**Author:** ![metapuff](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@metapuff](https://itensor.discourse.group/u/metapuff)\
**Post date:** [November 6, 2022, 7:06am UTC](https://itensor.discourse.group/t/defining-custom-states/499/3 "2022-11-06T07:06:31Z")

</div>

Thanks Miles! Apologies for not seeing that earlier, I wasn’t aware that there was a dev version. For posterity’s sake, the syntax looks like e.g.

`ITensors.state(::StateName"-3/2", ::SiteType"S=3/2") = [0, 0, 0, 1]`

Good to know about passing integers though; that is indeed easier!

Re qudits: I was trying to define custom operators for the `Qudit` site type. For example, I was trying to naively do

`ITensors.op(::OpName"Z",::SiteType"Qudit") = [1 0 0; 0 exp(2π * im/3) 0; 0 0 exp(-2π * im/3)]`

and to then build an MPO defined on `sites = siteinds("Qudit",L;dim=3)` by taking `ampo = OpSum()` and doing

```nohighlight
for i in 1:L 
     ampo += 1,"Z",i
end 
H = MPO(ampo,sites)

```

which throws the error mentioned in the previous post. Probably I need to include the dimension of qudits I am targeting somewhere in the definition of `Z`, but am not exactly sure where to do so.

---

<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:** [November 7, 2022, 4:27am UTC](https://itensor.discourse.group/t/defining-custom-states/499/4 "2022-11-07T04:27:50Z")

</div>

I see - it’s a really good question about the “Qudit” site type. Your guess is right that you have to include the dimension – we need to figure out how to document that because it’s different from the other site types that way.

Here is the example of how to do it for your “Z” operator

`ITensors.op(::OpName"Z",::SiteType"Qudit", d::Int)`

Then of course you also need to define the operator matrix to be a d \times d matrix and put the appropriate entries. If that’s too much work and you only want to do the case of a certain d value, then you could always just put a line in the function that checks d is the right value and throws an error otherwise (just to prevent accidentally passing another value of d).

Please let me know if that doesn’t work. If you’d like to see more examples of how the “Qudit” operators included with ITensor are defined you can have a look here:  
[https://github.com/ITensor/ITensors.jl/blob/main/src/physics/site\_types/qudit.jl](https://github.com/ITensor/ITensors.jl/blob/main/src/physics/site_types/qudit.jl)
