# Truncation error in ITensors SVD

**URL:** https://discourse.julialang.org/t/truncation-error-in-itensors-svd/134559
**Category:** General Usage
**Tags:** itensors, svd
**Created:** [December 15, 2025, 11:24am UTC](https://discourse.julialang.org/t/truncation-error-in-itensors-svd/134559 "2025-12-15T11:24:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![diffeqslvr](https://avatars.discourse-cdn.com/v4/letter/d/f475e1/32.png) [@diffeqslvr](https://discourse.julialang.org/u/diffeqslvr)
#### Post date: [December 15, 2025, 11:24am UTC](https://discourse.julialang.org/t/truncation-error-in-itensors-svd/134559/1 "2025-12-15T11:24:34Z")

</div>

I am trying to understand the truncation error when performing an SVD using ITensors. According to [ITensor Examples · ITensors.jl](https://itensor.github.io/ITensors.jl/stable/examples/ITensor.html#Factoring-ITensors-(SVD,-QR,-etc.)), the truncation error is defined as sum\_{discarded n} lambda\_n^2 / sum\_{n} lambda\_n^2, where lambda\_n are the singular values. However, running the following code, I get inconsistent results:

```julia-auto
using ITensors
i = Index(10,"i”);

j = Index(40,"j”);

k = Index(20,"k”);

T = random_itensor(i,j,k)

# Approximate calculation, get truncated spectrum

maxdim = 10

U,S,V,spec = svd(T, (i,k), maxdim = maxdim)
 # The following lines give the same result
@show (norm(U * S * V - T)/norm(T))^2

@show truncerror(spec)

# Exact calculation to get the entire spectrum

U,S,V,spec = svd(T,(i,k))

spec_vals = eigs(spec)
# This line gives a result that does not agree with the truncation errors from above
@show (sum(spec_vals[maxdim+1:end].^2)/sum(spec_vals.^2))

```

What am I missing?

---

<div class="post-metadata">

### Author: ![diffeqslvr](https://avatars.discourse-cdn.com/v4/letter/d/f475e1/32.png) [@diffeqslvr](https://discourse.julialang.org/u/diffeqslvr)
#### Post date: [December 15, 2025, 12:53pm UTC](https://discourse.julialang.org/t/truncation-error-in-itensors-svd/134559/2 "2025-12-15T12:53:52Z")

</div>

I found my mistake: eigs(spec) actually contains the singular values squared, so a working example is:

```julia-auto
using ITensors
using LinearAlgebra
using Test
i = Index(10,"i")
j = Index(40,"j")
k = Index(30,"k")

A = random_itensor(i,j,k)

U,S,V,spec = svd(A, (i,j))

S_diag = diag(Array(S, inds(S)))

@test norm(A)^2 ≈ sum(S_diag.^2)

@test eigs(spec) ≈ S_diag.^2
@test truncerror(spec) == 0.0

maxdim = 7
U,S,V,spec_trunc = svd(A,(i,j); maxdim=maxdim)
@test truncerror(spec_trunc) ≈ sum(S_diag[maxdim+1:end].^2) / sum(S_diag.^2)
eigs_spec = eigs(spec)
@test truncerror(spec_trunc) ≈ sum(eigs_spec[maxdim+1:end]) / sum(eigs_spec)
@test truncerror(spec_trunc) ≈ (norm(U*S*V - A)/norm(A))^2

```

---

<div class="post-metadata">

### Author: ![miles](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles/32/29112_2.png) [@miles](https://discourse.julialang.org/u/miles)
#### Post date: [December 19, 2025, 10:14pm UTC](https://discourse.julialang.org/t/truncation-error-in-itensors-svd/134559/3 "2025-12-19T22:14:05Z")

</div>

Glad you figured out the issue. In the future, please take a look at our ITensor forum at [https://itensor.discourse.group](https://itensor.discourse.group)
