When using complex valued numbers in the objective function, get an error that I struggle to understand. This minimal example illustrates it very well. Let’s say we want to find parameters of a parametrised unitay matrix such that it agrees with a given unitary.
using LinearAlgebra
using JuMP
import HiGHS
const X = ComplexF64[0 1; 1 0]
const Y = ComplexF64[0 -im; im 0]
const Z = ComplexF64[1 0; 0 -1]
rot(op, angle) = exp(-im * angle / 2 * op)
function unitary(theta)
return exp(-i * theta[3] * kron(Z, X)) + exp(-i * theta[2] * kron(Z, Y)) * exp(-i * theta[1] * kron(Z, X))
end
function opt(theta)
u_target = exp(-im * pi / 4 * kron(Z, X))
u = u_lstar(theta)
fid = abs(tr(u_target' * u))^2 / 4
return fid
end
model = Model(HiGHS.Optimizer)
@variable(model, -pi <= theta[1:3] <= pi)
@objective(model, Max, opt(theta))
JuMP.optimize!(model)
value(theta)
It gives the following error:
ERROR: Cannot build `GenericNonlinearExpr` because a term is complex-valued: `(0)::GenericAffExpr{ComplexF64, VariableRef}`
Stacktrace:
[1] error(s::String)
@ Base .\error.jl:44
[2] _throw_if_not_real(x::GenericAffExpr{ComplexF64, VariableRef})
@ JuMP C:\Users\xyz\.julia\packages\JuMP\7eD71\src\nlp_expr.jl:344
[3] /(x::GenericAffExpr{ComplexF64, VariableRef}, y::NonlinearExpr)
@ JuMP C:\Users\xyz\.julia\packages\JuMP\7eD71\src\nlp_expr.jl:401
[4] eigtype(T::Type)
@ LinearAlgebra C:\Users\xyz\.julia\juliaup\julia-1.12.4+0.x64.w64.mingw32\share\julia\stdlib\v1.12\LinearAlgebra\src\eigen.jl:319
[5] exp(A::Matrix{GenericAffExpr{ComplexF64, VariableRef}})
@ LinearAlgebra C:\Users\xyz\.julia\juliaup\julia-1.12.4+0.x64.w64.mingw32\share\julia\stdlib\v1.12\LinearAlgebra\src\dense.jl:671
...
The value returned by the optimization objective (and everything else except for the matrix product) is real, so I struggle to understand what causes the problem. Is it not allowed to use complex numbers altogether?