qojulia / QuantumOptics.jl

Library for the numerical simulation of closed as well as open quantum systems.
http://qojulia.org
Other
533 stars 104 forks source link

Data type of `Ket()` sometimes incompatible with `timeevolution` methods #317

Closed dehond closed 3 years ago

dehond commented 3 years ago

The problem Whenever I define a basis state using the Ket() method I've noticed that the data type of the state vector is left to be the same as what I put in, see the following example:

basis = SpinBasis(1//2)
typeof( Ket(basis, [1, 0]).data )
    # = Array{Int64,1}
typeof( Ket(basis, [1.0, 0.0]).data )
    # = Array{Float64,1}

I imagine it is maintained to use optimized methods for matrix multiplication and diagonalization, and most of the time it works great. When trying to solve time dependent problems I run into a problem, though. Trying the following, for instance

basis = SpinBasis(1//2)
H = sigmax(basis)
ψ0 = Ket(basis, [1.0, 0.0])
tspan = [0:0.01:1;]
_, ψt = timeevolution.schroedinger(tspan, ψ0, H)

results in an InexactError: Float64(0 - 1im) with an enormous stacktrace that I have no hope of understanding. The problem goes away if I enforce the coefficients of the state vector to be complex, i.e. setting

ψ0 = Ket(basis, complex([1.0, 0.0]))

I've run into this problem several times but keep forgetting how to solve it because the error message is opaque to me. Every time I figure it out I think: "Darn it, I should really remember this next time!" but that invariably doesn't happen.

Proposed solution Would it be possible to have the timeevolution methods perform a check on the data type of the initial state, and have them convert it to complex if need be? Or at least throw an intelligible error message into the user's lap? In my view this would make the package more user friendly.

I would be happy to give this a go and file a pull request.

Version data QuantumOptics v0.8.4 Julia 1.5.3

ChristophHotter commented 3 years ago

Hi @dehond, Yes, I think your proposed solution would make sense. A specific error message would be sufficient in my opinion.

I would appreciate it if you send a pull request.

Best, Christoph

dehond commented 3 years ago

Ok, @ChristophHotter, will do!