# MPO matrix representations

**URL:** <https://itensor.discourse.group/t/mpo-matrix-representations/1658>\
**Category:** ITensor Julia Questions\
**Tags:** julia, mps, mpo\
**Created:** [April 25, 2024, 1:51pm UTC](https://itensor.discourse.group/t/mpo-matrix-representations/1658 "2024-04-25T13:51:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bhargava\_Balaganchi](https://avatars.discourse-cdn.com/v4/letter/b/a5b964/32.png) [@Bhargava\_Balaganchi](https://itensor.discourse.group/u/Bhargava_Balaganchi)\
**Post date:** [April 25, 2024, 1:51pm UTC](https://itensor.discourse.group/t/mpo-matrix-representations/1658/1 "2024-04-25T13:51:26Z")

</div>

Hi,  
is there a way to print the matrices of an MPO/MPS? What I mean by that is, an MPO is a formed by multiplying matrices on each site. However, ITensors, stores these matrices as 4 indexed objects in case of MPOs and 3 indexed objects in case of MPS. Now, what I want is to print these 4 indexed and 3 indexed objects as matrices. In other words, I want to print the constituent matrices of MPO/MPS.

---

<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 25, 2024, 3:52pm UTC](https://itensor.discourse.group/t/mpo-matrix-representations/1658/2 "2024-04-25T15:52:28Z")

</div>

Hello,

You can easily print the constituent matrices using Julia array access notation. For example

```auto
julia> siteinds("S=1/2", 10)
julia> psi = randomMPS(s)
julia> @show psi[5]
psi[5] = ITensor ord=3
    Dim 1: (dim=1|id=416|"Link,l=4")
    Dim 2: (dim=2|id=192|"S=1/2,Site,n=5")
    Dim 3: (dim=1|id=967|"Link,l=5")
    Dense{Float64, Vector{Float64}}
     1×2×1
    [:, :, 1] =
     -0.8970638949108982 0.4419008581653681
    ITensor ord=3 (dim=1|id=416|"Link,l=4") (dim=2|id=192|"S=1/2,Site,n=5") (dim=1|id=967|"Link,l=5")
    Dense{Float64, Vector{Float64}}

```

---

<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 25, 2024, 6:40pm UTC](https://itensor.discourse.group/t/mpo-matrix-representations/1658/3 "2024-04-25T18:40:45Z")

</div>

Hi Bhargava,  
Karl’s answer is right, but you may want to see the output organized so that the printing always shows the “matrices” in the sense of how a “matrix product state” is a collection of d matrices on each site, one for every setting of the “physical index” of dimension d. And similar for an MPO thought of as a collection of d^2 indices on each site.

So I made a small function that permutes the index ordering of each tensor to put the physical or Site indices last. This way the printout shows each matrix grouped together. Please give it a try:

```julia
function print_matrices(A::AbstractMPS)
  s = siteinds(A)
  for j=1:length(s)
    Aj = A[j]
    links = uniqueinds(Aj,s[j])
    Aj = permute(Aj,links...,collect(s[j])...)
    println(Aj)
  end
end

```

For example, when I use it to print a Hamiltonian MPO (XY model for S=1/2) I get:

```ITensor
Dim 1: (dim=4|id=324|"Link,l=1")
Dim 2: (dim=2|id=640|"Qubit,Site,n=1")'
Dim 3: (dim=2|id=640|"Qubit,Site,n=1")
NDTensors.Dense{Float64, Vector{Float64}}
 4×2×2
[:, :, 1] =
 0.0 0.0
 0.0 0.0
 0.0 0.5
 1.0 0.0

[:, :, 2] =
 0.0 0.0
 0.5 0.0
 0.0 0.0
 0.0 1.0
ITensor ord=4
Dim 1: (dim=4|id=324|"Link,l=1")
Dim 2: (dim=4|id=576|"Link,l=2")
Dim 3: (dim=2|id=762|"Qubit,Site,n=2")'
Dim 4: (dim=2|id=762|"Qubit,Site,n=2")
NDTensors.Dense{Float64, Vector{Float64}}
 4×4×2×2
[:, :, 1, 1] =
 1.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 1.0

[:, :, 2, 1] =
 0.0 0.0 0.0 0.0
 1.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0
 0.0 0.0 0.5 0.0

[:, :, 1, 2] =
 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0
 1.0 0.0 0.0 0.0
 0.0 0.5 0.0 0.0

[:, :, 2, 2] =
 1.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 1.0

...

```

---

<div class="post-metadata">

**Author:** ![Bhargava\_Balaganchi](https://avatars.discourse-cdn.com/v4/letter/b/a5b964/32.png) [@Bhargava\_Balaganchi](https://itensor.discourse.group/u/Bhargava_Balaganchi)\
**Post date:** [April 26, 2024, 1:51pm UTC](https://itensor.discourse.group/t/mpo-matrix-representations/1658/4 "2024-04-26T13:51:41Z")

</div>

Hi Karl and Miles,  
Thank you very much for the detailed reply. Your answers were very useful. 🙂
