# Convert co-variance to Hamiltonian matrix

**URL:** <https://discuss.pennylane.ai/t/convert-co-variance-to-hamiltonian-matrix/280>\
**Category:** Strawberry Fields\
**Created:** [November 28, 2019, 5:55pm UTC](https://discuss.pennylane.ai/t/convert-co-variance-to-hamiltonian-matrix/280 "2019-11-28T17:55:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kareem\_essafty](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kareem_essafty/32/401_2.png) [@kareem\_essafty](https://discuss.pennylane.ai/u/kareem_essafty)\
**Post date:** [November 28, 2019, 5:55pm UTC](https://discuss.pennylane.ai/t/convert-co-variance-to-hamiltonian-matrix/280/1 "2019-11-28T17:55:25Z")

</div>

hey,  
I was trying to reconstruct the Hamiltonian from a covariance matrix but I always get nan values. [this function](https://github.com/XanaduAI/strawberryfields/blob/d4913b698f8b0a1469ac7e1ab3f3c345f9fff84a/strawberryfields/decompositions.py#L684) implements the conversion. I checked and I realized that it’s the “atanh” that causes the problem. is there a way to overcome this issue??  
I’m using the latest version of strawberryfields from the master

here is a simple script to produce the same error:

```
prog = sf.Program(1)
amp = .7
np.random.seed(5)
x = np.random.rand(1)

with prog.context as q:

    sf.ops.DisplacedSqueezed(r=amp,p=x) | q[0]

eng = sf.Engine('gaussian')

result = eng.run(prog)    

cov = result.state.cov()
print(sf.decompositions.covmat_to_hamil(cov))
```

---

<div class="post-metadata">

**Author:** ![nathan](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nathan/32/85_2.png) [@nathan](https://discuss.pennylane.ai/u/nathan)\
**Post date:** [November 29, 2019, 11:30pm UTC](https://discuss.pennylane.ai/t/convert-co-variance-to-hamiltonian-matrix/280/2 "2019-11-29T23:30:09Z")

</div>

Thanks @kareem_essafty for pointing this out. I’m not sure if this is a bug or not. I’ll consult with the team and we’ll get back to you when we understand more what’s causing this issue.

---

<div class="post-metadata">

**Author:** ![kareem\_essafty](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kareem_essafty/32/401_2.png) [@kareem\_essafty](https://discuss.pennylane.ai/u/kareem_essafty)\
**Post date:** [November 30, 2019, 11:20am UTC](https://discuss.pennylane.ai/t/convert-co-variance-to-hamiltonian-matrix/280/3 "2019-11-30T11:20:50Z")

</div>

hey, i have read the [paper](https://arxiv.org/pdf/1507.01941.pdf) that is pointed out in the docs and I believe by changing only this part of this [line](https://github.com/XanaduAI/strawberryfields/blob/d4913b698f8b0a1469ac7e1ab3f3c345f9fff84a/strawberryfields/decompositions.py#L720)

` H = (1j * omega @ (v @ np.diag(np.arctanh(1.0/l.real)) @ np.linalg.inv(v))).real`

to

` H = (1j * omega @ (v @ np.diag(np.arctanh(1.0/l)) @ np.linalg.inv(v))).real`

it will lead to overcoming the issue of nan values

is that right?

---

<div class="post-metadata">

**Author:** ![Nicolas\_Quesada](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nicolas_quesada/32/118_2.png) [@Nicolas\_Quesada](https://discuss.pennylane.ai/u/Nicolas_Quesada)\
**Post date:** [December 2, 2019, 5:28pm UTC](https://discuss.pennylane.ai/t/convert-co-variance-to-hamiltonian-matrix/280/5 "2019-12-02T17:28:58Z")

</div>

Hi @kareem_essafty — Thanks for the question!  
I believe sf is giving you the correct output, in that a Gibbs state, i.e., a state of the form \exp(-\beta H) is only pure at infinite inverse temperature \beta = 1/(k\_B T) where k\_B is the Boltzmann constant and T is the temperature.  
A quick solution to this problem is to start with a thermal state with a small mean number of photons and then extrapolate to the zero temperature limit.

---

<div class="post-metadata">

**Author:** ![kareem\_essafty](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kareem_essafty/32/401_2.png) [@kareem\_essafty](https://discuss.pennylane.ai/u/kareem_essafty)\
**Post date:** [December 3, 2019, 9:35pm UTC](https://discuss.pennylane.ai/t/convert-co-variance-to-hamiltonian-matrix/280/6 "2019-12-03T21:35:03Z")

</div>

hey @Nicolas_Quesada, I tried the following:

```
prog = sf.Program(2)
np.random.seed(5)
x = np.random.rand(1)
print(x[0])
with prog.context as q:

    sf.ops.Thermal(n=1e-015) | q[0]
    sf.ops.Thermal(n=1e-015) | q[1]
    #sf.ops.S2gate(r=0.7,phi=0) | q
    #sf.ops.DisplacedSqueezed(alpha=1.,r=.7,p=x[0]) | q[0]
    #sf.ops.DisplacedSqueezed(alpha=2,r=.7,p=x[0]+np.pi) |q[1]
    sf.ops.Sgate(r=0.7,phi=x[0]) | q[0]
    sf.ops.Sgate(r=0.7,phi=x[0]+np.pi/4) | q[1]
    sf.ops.Dgate(7.2) | q[0]
    sf.ops.Dgate(-0.6-0.3j) | q[1]

eng = sf.Engine('gaussian')

result = eng.run(prog)    

cov = result.state.cov()
print(cov)
print(" ")
print(sf.decompositions.covmat_to_hamil(cov)) 

```

the result didn’t include any nan and was perfect except for the fact that the hamiltonian was not symmetric.  
besides that, if i used a displacedsqueezed operation instead of the S and D gates i get nan values

---

<div class="post-metadata">

**Author:** ![Nicolas\_Quesada](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nicolas_quesada/32/118_2.png) [@Nicolas\_Quesada](https://discuss.pennylane.ai/u/Nicolas_Quesada)\
**Post date:** [December 3, 2019, 9:52pm UTC](https://discuss.pennylane.ai/t/convert-co-variance-to-hamiltonian-matrix/280/7 "2019-12-03T21:52:29Z")

</div>

Hi — I think you should use bigger values of the mean photon number in the thermal state. Maybe try 10^-3

---

<div class="post-metadata">

**Author:** ![kareem\_essafty](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kareem_essafty/32/401_2.png) [@kareem\_essafty](https://discuss.pennylane.ai/u/kareem_essafty)\
**Post date:** [December 3, 2019, 9:57pm UTC](https://discuss.pennylane.ai/t/convert-co-variance-to-hamiltonian-matrix/280/8 "2019-12-03T21:57:51Z")

</div>

I still get nan when I use the displacedsqueezed preparetion after the thermal operation

---

<div class="post-metadata">

**Author:** ![kareem\_essafty](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kareem_essafty/32/401_2.png) [@kareem\_essafty](https://discuss.pennylane.ai/u/kareem_essafty)\
**Post date:** [December 3, 2019, 10:03pm UTC](https://discuss.pennylane.ai/t/convert-co-variance-to-hamiltonian-matrix/280/9 "2019-12-03T22:03:01Z")

</div>

another question but please bare with me I’m sorry,

mean photon number for S gate is sinh(squezzing amp)\*\*2 and the phase doesn’t affect right?  
and for the D gate is abs(alpha)\*\*2 and alpha is a complex number right?

---

<div class="post-metadata">

**Author:** ![kareem\_essafty](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kareem_essafty/32/401_2.png) [@kareem\_essafty](https://discuss.pennylane.ai/u/kareem_essafty)\
**Post date:** [December 7, 2019, 12:40am UTC](https://discuss.pennylane.ai/t/convert-co-variance-to-hamiltonian-matrix/280/10 "2019-12-07T00:40:12Z")

</div>

@Nicolas_Quesada  
the thermal state solved the problem  
thanks a lot
