Skip to content

Regression in 1.0, asking for Hessian of a complex matrix function results in infinite loop #756

@araujoms

Description

@araujoms

I've encountered this issue when running the tests of my package ConicQKD, which started to hang when I switched from ForwardDiff 0.10 to 1.0.

It was a lot of work to reduce it to a minimum working example. Apparently the problem appears when you try to compute the Hessian of a function of a complex matrix that sets several elements to zero and asks for its eigenvalues. If I just ask for the eigenvalues without setting anything to zero then it's fine. Real matrices work without any problems, even when setting elements to zero.

To reproduce, call test_hessian(ComplexF64). You can also check the real case test_hessian(Float64), it works fine.

using LinearAlgebra
import ForwardDiff
import GenericLinearAlgebra.eigen
import Random

function test_hessian(::Type{T}) where {T}
    Random.seed!(1)

    d = 3

    function barrier(point)
        d2 = div(d, 2)
        M = mat(point, T)
        M[1:d2, d2+1:end] .= 0
        return sum(eigvals(Hermitian(M)))
    end

    x = randn(T, d, d)
    point = rvec(x * x')
    fd_hess = ForwardDiff.hessian(barrier, point)

    return fd_hess
end

function rvec(M::AbstractMatrix{T}) where {T}
    if T <: Real
        return vec(M)
    else
        return [real(vec(M)); imag(vec(M))]
    end
end

function mat(v::AbstractVector, ::Type{T}) where {T}
    if T <: Real
        n = isqrt(length(v))
        return reshape(v, n, n)
    else
        l2 = div(length(v), 2)
        n = isqrt(l2)
        return reshape(v[1:l2] + im*v[l2+1:end], n, n)
    end
end        

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions