# Definition of a new bosonic state

**URL:** <https://itensor.discourse.group/t/definition-of-a-new-bosonic-state/1613>\
**Category:** ITensor Julia Questions\
**Created:** [April 3, 2024, 9:11am UTC](https://itensor.discourse.group/t/definition-of-a-new-bosonic-state/1613 "2024-04-03T09:11:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![YotamKa](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/yotamka/32/546_2.png) [@YotamKa](https://itensor.discourse.group/u/YotamKa)\
**Post date:** [April 3, 2024, 9:11am UTC](https://itensor.discourse.group/t/definition-of-a-new-bosonic-state/1613/1 "2024-04-03T09:11:57Z")

</div>

Dear ITensor team,

I am trying to define a coherent state as an initial state for a 1d bosonic system. I thought of two equivalent ways of doing that:

1. Begin in the bosonic ground state and apply a displacement operator in MPO form to it. This approach works with the following code:

```auto
N = 2 # number of ions
dim = 5 # local oscillator cutoff dimension
α = 0.01 # the initial coherent state's parameter
sites = siteinds("Boson", N, dim=dim); # create the site indices for MPS representation
a_op(d) = diagm(1 => [sqrt(i) for i in 1:(d-1)]) # annihilation operator
D(α, d) = exp(α*a_op(d)'-α'*a_op(d)) # matrix elements of the displacement operator
ITensors.op(::OpName"disp", ::SiteType"Boson", d::Int) = D(α, d);
""" Phonos ground state """
ψgs = MPS(sites, "0");

""" Multiply each site by displacement """
disp_mpo = MPO(sites, "disp")
ψ₀ = apply(disp_mpo, ψgs)

```

1. Directly define a coherent state. I do it using the following code:

```auto
N = 2 # number of bosons
dim = 5 # local oscillator cutoff dimension
sites = siteinds("Boson", N, dim=dim); # create the site indices for MPS representation
α = 0.01 # the coherent state's parameter
a_op(d) = diagm(1 => [sqrt(i) for i in 1:(d-1)]) # annihilation operator
D(α, d) = exp(α*a_op(d)'-α'*a_op(d)) # matrix elements of the displacement operator
ITensors.state(::StateName"coherent", ::SiteType"Boson", d::Int) = D(α, d)*[if n==1 1 else 0 end for n=1:d]
ψ₀ = MPS(sites, "coherent")

```

However, after running the second code I get the error:

```auto
ArgumentError: invalid base 10 digit 'c' in "coherent"

```

Can you please explain why is that?

Thanks,  
Yotam

---

<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:** [April 3, 2024, 10:05am UTC](https://itensor.discourse.group/t/definition-of-a-new-bosonic-state/1613/2 "2024-04-03T10:05:55Z")

</div>

Please overload the `state` function this way instead:  
`ITensors.state(::StateName"coherent", ::SiteType"Boson", s::Index)`  
then obtain the dimension of the site by calling `dim(s)` on the `Index` `s` that gets passed. (Also you’ll need to rename your variable “dim” which is shadowing the `dim` function.)

Here’s a fixed example:

```julia
using ITensors

let
  N = 2 # number of bosons
  d = 5 # local oscillator cutoff dimension
  sites = siteinds("Boson", N; dim=d); # create the site indices for MPS representation
  α = 0.01 # the coherent state's parameter
  a_op(d) = diagm(1 => [sqrt(i) for i in 1:(d-1)]) # annihilation operator
  D(α, d) = exp(α*a_op(d)'-α'*a_op(d)) # matrix elements of the displacement operator
  ITensors.state(::StateName"coherent", ::SiteType"Boson", s::Index) = D(α, dim(s))*[if n==1 1 else 0 end for n=1:dim(s)]
  ψ₀ = MPS(sites, "coherent")
  return
end

```

---

<div class="post-metadata">

**Author:** ![YotamKa](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/yotamka/32/546_2.png) [@YotamKa](https://itensor.discourse.group/u/YotamKa)\
**Post date:** [April 3, 2024, 10:12am UTC](https://itensor.discourse.group/t/definition-of-a-new-bosonic-state/1613/3 "2024-04-03T10:12:44Z")

</div>

Thanks Miles!

Is it also possible to pass \alpha as an argument to the function, or only with some wrapper function over it?

---

<div class="post-metadata">

**Author:** ![kmp5](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/kmp5/32/251_2.png) [@kmp5](https://itensor.discourse.group/u/kmp5)\
**Post date:** [April 3, 2024, 3:31pm UTC](https://itensor.discourse.group/t/definition-of-a-new-bosonic-state/1613/4 "2024-04-03T15:31:30Z")

</div>

Hi Yotam,

I don’t think you need to pass alpha explicitly because its a variable in the function `D`. I ran a quick test

```auto
using ITensors

let
  N = 2 # number of bosons
  d = 5 # local oscillator cutoff dimension
  sites = siteinds("Boson", N; dim=d); # create the site indices for MPS representation
  α = 0.01 # the coherent state's parameter
  a_op(d) = diagm(1 => [sqrt(i) for i in 1:(d-1)]) # annihilation operator
  D(α, d) = exp(α*a_op(d)'-α'*a_op(d)) # matrix elements of the displacement operator
  function ITensors.state(::StateName"coherent", ::SiteType"Boson", s::Index) 
    @show α
    D(α, dim(s))*[if n==1 1 else 0 end for n=1:dim(s)]
  end
  ψ₀ = MPS(sites, "coherent")
  println("Finished with ψ₀")
  α = 0.2
  ϕ = MPS(sites, "coherent")
  println("Finished with ϕ")
  return
end

```

and see this as the output

```auto
α = 0.01
α = 0.01
Finished with ψ₀
α = 0.2
α = 0.2
Finished with ϕ

```

---

<div class="post-metadata">

**Author:** ![YotamKa](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/yotamka/32/546_2.png) [@YotamKa](https://itensor.discourse.group/u/YotamKa)\
**Post date:** [April 4, 2024, 6:44am UTC](https://itensor.discourse.group/t/definition-of-a-new-bosonic-state/1613/5 "2024-04-04T06:44:27Z")

</div>

Got it, thanks 🙂
