# ArgumentError: matrix contains Infs or NaNs in constructing MPO

**URL:** <https://itensor.discourse.group/t/argumenterror-matrix-contains-infs-or-nans-in-constructing-mpo/1855>\
**Category:** ITensor Julia Questions\
**Created:** [July 5, 2024, 3:51pm UTC](https://itensor.discourse.group/t/argumenterror-matrix-contains-infs-or-nans-in-constructing-mpo/1855 "2024-07-05T15:51:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lin.zhang](https://avatars.discourse-cdn.com/v4/letter/l/e274bd/32.png) [@lin.zhang](https://itensor.discourse.group/u/lin.zhang)\
**Post date:** [July 5, 2024, 3:51pm UTC](https://itensor.discourse.group/t/argumenterror-matrix-contains-infs-or-nans-in-constructing-mpo/1855/1 "2024-07-05T15:51:44Z")

</div>

Hello,

I am trying to construct a density matrix with a weak domain wall-like imbalance in magnetization for the spin 1 system using MPO. The code is as follows:

> using LinearAlgebra  
> using ITensors, ITensorMPS
> 
> L = 340  
> μ = 0.001
> 
> sites = siteinds(“S=1”, L)
> 
> rho = MPO(sites, n → (A = (n ≤ L ÷ 2 ? [1.0 + μ, 1.0, 1.0 - μ] : [1.0 - μ, 1.0, 1.0 + μ]);  
> A = A / sum(A);  
> diagm(0 =\> A)))

This code works for L \<= 339 but breaks when L \>= 340, and the following errors appear:

> ArgumentError: matrix contains Infs or NaNs
> 
> Stacktrace:  
> [1] chkuplofinite(A::Matrix{Float64}, uplo::Char)  
> @ LinearAlgebra.LAPACK ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/LinearAlgebra/src/lapack.jl:98  
> [2] syevr!(jobz::Char, range::Char, uplo::Char, A::Matrix{Float64}, vl::Float64, vu::Float64, il::Int64, iu::Int64, abstol::Float64)  
> @ LinearAlgebra.LAPACK ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/LinearAlgebra/src/lapack.jl:5222  
> [3] eigen!  
> @ ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/LinearAlgebra/src/symmetriceigen.jl:8 [inlined]  
> [4] eigen(A::Hermitian{Float64, Matrix{Float64}}; sortby::Nothing)  
> @ LinearAlgebra ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/LinearAlgebra/src/symmetriceigen.jl:13  
> [5] eigen  
> @ ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/LinearAlgebra/src/symmetriceigen.jl:11 [inlined]  
> [6] eigen  
> @ ~/.julia/packages/NDTensors/gdQSG/src/lib/Expose/src/functions/linearalgebra.jl:24 [inlined]  
> [7] svd\_recursive(M::Transpose{Float64, Matrix{Float64}}; thresh::Float64, north\_pass::Int64)  
> @ NDTensors ~/.julia/packages/NDTensors/gdQSG/src/linearalgebra/svd.jl:40  
> [8] svd\_recursive(M::Transpose{Float64, Matrix{Float64}})  
> @ NDTensors ~/.julia/packages/NDTensors/gdQSG/src/linearalgebra/svd.jl:29  
> [9] svd\_recursive(M::Matrix{Float64}; thresh::Float64, north\_pass::Int64)  
> @ NDTensors ~/.julia/packages/NDTensors/gdQSG/src/linearalgebra/svd.jl:32  
> [10] svd\_recursive  
> @ ~/.julia/packages/NDTensors/gdQSG/src/linearalgebra/svd.jl:29 [inlined]  
> [11] svd(T::NDTensors.DenseTensor{Float64, 2, Tuple{Index{Int64}, Index{Int64}}, NDTensors.Dense{Float64, Vector{Float64}}}; mindim::Nothing, maxdim::Nothing, cutoff::Float64, use\_absolute\_cutoff::Nothing, use\_relative\_cutoff::Nothing, alg::Nothing, min\_blockdim::Nothing)  
> @ NDTensors ~/.julia/packages/NDTensors/gdQSG/src/linearalgebra/linearalgebra.jl:108  
> [12] svd(A::ITensor, Linds::Vector{Index{Int64}}; leftdir::Nothing, rightdir::Nothing, lefttags::ITensors.TagSets.GenericTagSet{BitIntegers.UInt256, 4}, righttags::Nothing, mindim::Nothing, maxdim::Nothing, cutoff::Float64, alg::Nothing, use\_absolute\_cutoff::Nothing, use\_relative\_cutoff::Nothing, min\_blockdim::Nothing, utags::ITensors.TagSets.GenericTagSet{BitIntegers.UInt256, 4}, vtags::Nothing)  
> @ ITensors ~/.julia/packages/ITensors/oOwvi/src/tensor\_operations/matrix\_decomposition.jl:162  
> [13] truncate!(::NDTensors.BackendSelection.Algorithm{:frobenius, @NamedTuple{}}, M::MPO; site\_range::UnitRange{Int64}, kwargs::@Kwargs{cutoff::Float64})  
> @ ITensors.ITensorMPS ~/.julia/packages/ITensors/oOwvi/src/lib/ITensorMPS/src/abstractmps.jl:1692  
> [14] truncate!  
> @ ~/.julia/packages/ITensors/oOwvi/src/lib/ITensorMPS/src/abstractmps.jl:1679 [inlined]  
> [15] truncate!(M::MPO; alg::String, kwargs::@Kwargs{cutoff::Float64})  
> @ ITensors.ITensorMPS ~/.julia/packages/ITensors/oOwvi/src/lib/ITensorMPS/src/abstractmps.jl:1676  
> [16] MPO(::Type{Float64}, sites::Vector{Index{Int64}}, ops::Vector{Matrix{Float64}})  
> @ ITensors.ITensorMPS ~/.julia/packages/ITensors/oOwvi/src/lib/ITensorMPS/src/mpo.jl:87  
> [17] MPO(::Type{Float64}, sites::Vector{Index{Int64}}, fops::var"#149#150")  
> @ ITensors.ITensorMPS ~/.julia/packages/ITensors/oOwvi/src/lib/ITensorMPS/src/mpo.jl:94  
> [18] MPO(sites::Vector{Index{Int64}}, ops::Function)  
> @ ITensors.ITensorMPS ~/.julia/packages/ITensors/oOwvi/src/lib/ITensorMPS/src/mpo.jl:97

From the definition of density matrix (just being a tensor product of local density matrices), there should be no Infs and NaNs. But somehow they appear in constructing the MPO. Do you know how to fix this? Thanks very much in advance for your help.

---

<div class="post-metadata">

**Author:** ![jzHan](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@jzHan](https://itensor.discourse.group/u/jzHan)\
**Post date:** [July 6, 2024, 3:05am UTC](https://itensor.discourse.group/t/argumenterror-matrix-contains-infs-or-nans-in-constructing-mpo/1855/2 "2024-07-06T03:05:35Z")

</div>

In numerical calculations, I believe it is advisable to avoid using such small parameters as much as possible, otherwise there is a high possibility of generating infinite numbers. I suggest you try increasing the \mu.

---

<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:** [July 14, 2024, 2:34pm UTC](https://itensor.discourse.group/t/argumenterror-matrix-contains-infs-or-nans-in-constructing-mpo/1855/3 "2024-07-14T14:34:43Z")

</div>

Hi Lin,  
I found the issue and it’s actually a bug or weakness in one of the ITensor MPO constructors. Please use the following code below as a workaround for your case. The code below calls a different constructor that avoids the issue.

```auto
using LinearAlgebra
using ITensors, ITensorMPS

let
  L = 340
  μ = 0.001

  sites = siteinds("S=1", L)

  function makeA(n)
    A = (n ≤ L ÷ 2 ? [1.0 + μ, 1.0, 1.0 - μ] : [1.0 - μ, 1.0, 1.0 + μ])
    A = A / sum(A)
    return diagm(0 => A)
  end

  ops = [makeA(n) for n=1:L]
  op_prod = Prod{Op}()
  for n in 1:L
    op_prod *= Op(ops[n], n)
  end
  rho = MPO(op_prod, sites)

  return
end

```

For more details about what the issue is, one of our ITensor MPO constructors does a numerical truncation of the MPO after constructing it. For some types of MPOs, even ones which contain all reasonably-sized numbers, doing a standard MPS-like truncation can lead to extremely large numerical values due to the difference between how states and operators are normalized. So really we should be truncating the MPO in a different way in that code. Please for now use the style of the code above.

Thanks, Miles

---

<div class="post-metadata">

**Author:** ![lin.zhang](https://avatars.discourse-cdn.com/v4/letter/l/e274bd/32.png) [@lin.zhang](https://itensor.discourse.group/u/lin.zhang)\
**Post date:** [July 15, 2024, 2:02pm UTC](https://itensor.discourse.group/t/argumenterror-matrix-contains-infs-or-nans-in-constructing-mpo/1855/4 "2024-07-15T14:02:35Z")

</div>

Hi Miles,

Thanks very much for the code. It works!

Best wishes,  
Lin

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/itensor/original/1X/d3072b13e047cd06df7f3981547c0917d940dfa4.png) [@system](https://itensor.discourse.group/u/system)\
**Post date:** [July 25, 2024, 2:03pm UTC](https://itensor.discourse.group/t/argumenterror-matrix-contains-infs-or-nans-in-constructing-mpo/1855/5 "2024-07-25T14:03:22Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
