# Density Matrix to MPO

**URL:** <https://itensor.discourse.group/t/density-matrix-to-mpo/1058>\
**Category:** ITensor Julia Questions\
**Created:** [July 26, 2023, 5:53pm UTC](https://itensor.discourse.group/t/density-matrix-to-mpo/1058 "2023-07-26T17:53:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AElton363](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/aelton363/32/182_2.png) [@AElton363](https://itensor.discourse.group/u/AElton363)\
**Post date:** [July 26, 2023, 5:53pm UTC](https://itensor.discourse.group/t/density-matrix-to-mpo/1058/1 "2023-07-26T17:53:02Z")

</div>

Hi,

in your examples, you describe a simple way to generate an MPS with N sites from a d^N dimensional vector (representing e.g. a quantum state): [MPS and MPO Examples · ITensors.jl](https://itensor.github.io/ITensors.jl/dev/examples/MPSandMPO.html#Julia-Tensor-to-MPS-Example)

Is there a similarly simple way to construct an MPO from a d^N x d^N dense matrix (representing e.g. a Hamiltonian or a density matrix)?

Thanks and best wishes!

---

<div class="post-metadata">

**Author:** ![JanReimers](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/janreimers/32/171_2.png) [@JanReimers](https://itensor.discourse.group/u/JanReimers)\
**Post date:** [July 27, 2023, 3:20pm UTC](https://itensor.discourse.group/t/density-matrix-to-mpo/1058/2 "2023-07-27T15:20:28Z")

</div>

I think you can do it with two extra steps as follows:

```auto
using ITensors

d=2
N=5
sites=siteinds(d,N)
sites2=[prime.(sites)...,dag.(sites)...] #Make an index set for the doubled Hilbert space.

A2=randn(d^N,d^N)
A2T=ITensor(A2,sites2) #Extra step, convert array to a tensor with the correct indices.
H=MPO(A2T,sites,cutoff=1e-8,maxdim=10)

```

I hope this helps and that you keep N small 🙂  
JR
