Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] ChainRule for norm function #95

Open
davidsmp opened this issue Nov 18, 2024 · 4 comments
Open

[BUG] ChainRule for norm function #95

davidsmp opened this issue Nov 18, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@davidsmp
Copy link

davidsmp commented Nov 18, 2024

Description of bug

Using AD to differentiate the norm gives error: "MethodError: Cannot convert an object of type ChainRulesCore.Tangent{Any, @NamedTuple{data::Vector{Any}, llim::ChainRulesCore.ZeroTangent, rlim::ChainRulesCore.ZeroTangent}} to an object of type MPS"

Minimal code demonstrating the bug or unexpected behavior

Minimal runnable code

using ITensors, ITensorMPS
using LinearAlgebra
using Zygote


function randhaar(s1,s2)
    U = reshape(Matrix(qr(rand(ComplexF64,4,4)).Q),2,2,2,2)
    return ITensor(U,s1,s2,s1',s2')
  end

N = 6
d = 2
sites = siteinds(d,N)
M = randomMPS(sites,2)
function F(us::Vector{ITensor})
  return norm(apply(us,M))
end

us = ITensor[randhaar(s1,s2) for (s1,s2) in zip(sites[1:end-1],sites[2:end])]
#F(us) ≈ 1
gradient(F,us) #fails

Expected output or behavior

Using the function inner instead of norm works, so the problem is probably the rrule for norm

Version information

 ~/.julia/environments/v1.10/Project.toml ⁠
⌃ [082447d4] ChainRules v1.71.0
[d360d2e6] ChainRulesCore v1.25.0
⌃ [0d1a4710] ITensorMPS v0.3.1
⌃ [9136182c] ITensors v0.7.3
⌃ [e88e6eb3] Zygote v0.6.72

@davidsmp davidsmp added the bug Something isn't working label Nov 18, 2024
@mtfishman
Copy link
Member

Thanks for the report. As a workaround, can you use:

function F(us::Vector{ITensor})
  x = apply(us,M)
  return sqrt(real(inner(x, x)))
end

?

@mtfishman mtfishman transferred this issue from ITensor/ITensors.jl Nov 18, 2024
@mtfishman
Copy link
Member

Also, I moved the issue to the ITensorMPS.jl repository since that is where our MPS/MPO code lives now.

@mtfishman mtfishman changed the title [ITensors] [BUG] ChainRule for norm function [BUG] ChainRule for norm function Nov 18, 2024
@davidsmp
Copy link
Author

Using the inner function works, yes.

Thanks for the report. As a workaround, can you use:

function F(us::Vector{ITensor})
  x = apply(us,M)
  return sqrt(real(inner(x, x)))
end

?

@mtfishman
Copy link
Member

Great, thanks for confirming. Ideally norm would work in the same way, if it was defined as sqrt(real(inner(x, x))) internally it would work, but I think we have extra logic to handle potential overflow/underflow in the case of very big or small norms which is tripping up Zygote.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants