# Stiff methods throw \`Cannot create a dual over scalar type ComplexF64\` in \`SDEProblem\`

**URL:** https://discourse.julialang.org/t/stiff-methods-throw-cannot-create-a-dual-over-scalar-type-complexf64-in-sdeproblem/106321
**Category:** Modelling & Simulations
**Tags:** differentialequation
**Created:** [November 16, 2023, 3:08pm UTC](https://discourse.julialang.org/t/stiff-methods-throw-cannot-create-a-dual-over-scalar-type-complexf64-in-sdeproblem/106321 "2023-11-16T15:08:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ymardoukhi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ymardoukhi/32/216992_2.png) [@ymardoukhi](https://discourse.julialang.org/u/ymardoukhi)
#### Post date: [November 16, 2023, 3:08pm UTC](https://discourse.julialang.org/t/stiff-methods-throw-cannot-create-a-dual-over-scalar-type-complexf64-in-sdeproblem/106321/1 "2023-11-16T15:08:25Z")

</div>

I am solving the following von Neumann master equation with a stochastic term

\dot{\rho} = -i[H(t), \rho]

where [., .] is the commutator operator and H(t) is given by

H(t) = s(t)\hat{\sigma\_x} + \hat{\sigma\_z} W\_t

s(t) is a control signal and is \sim10^8.  
Here is the snippet of the code to solve this using `SDEProblem`

```julia
noise_factor = 1e3 # a prefactor that scales the Wiener process
sigma_x = Matrix{ComplexF64}([[0., 1.] [1., 0.]]);
sigma_z = Matrix{ComplexF64}([[1., 0.] [0., -1.]]);
ham_z = noise_factor * sigma_z
    
# von Neumann master equation
function von_neumann_eq!(dρ, ρ, p, t)
    ham(t) = p(t) * sigma_x
    commutator = ham(t) * ρ .- ρ * ham(t) 
    dρ .= -im * commutator
end

function stochastic_von_neumann!(dρ, ρ, p, t)
    commutator = ham_z * ρ .- ρ * ham_z
    dρ .= -im * commutator
end

ρ0 = Matrix{ComplexF64}([[1., 0] [0., 0.]])
tspan = (0.0, T) 

prob = SDEProblem(von_neumann_eq!, stochastic_von_neumann!, ρ0, tspan, st)
sol = solve(prob, ImplicitRKMil(), save_everystep=false);

```

When I specifically choose one of the stiff methods to solve the problem I get the following error

```julia
ERROR: LoadError: ArgumentError: Cannot create a dual over scalar type ComplexF64. If the type behaves
 as a scalar, define ForwardDiff.can_dual(::Type{ComplexF64}) = true.

```

However, when I use `alg_hints=[:stiff]` without explicitly providing the method, I can see that the algorithm in `sol.alg` is `ImplicitRKMil`.

Any suggestions on how to choose explicitly a stiff method in this case?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [November 16, 2023, 5:56pm UTC](https://discourse.julialang.org/t/stiff-methods-throw-cannot-create-a-dual-over-scalar-type-complexf64-in-sdeproblem/106321/2 "2023-11-16T17:56:06Z")

</div>

> [@ymardoukhi](#):
>
> `Cannot create a dual over scalar type ComplexF64`

The [suggestion I’ve seen](https://github.com/JuliaDynamics/DynamicalSystems.jl/issues/181) is to re-write your equation as two coupled equations in the real and imaginary parts of your solution, so that you are giving the code real variables and autodiff works better.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [November 16, 2023, 7:14pm UTC](https://discourse.julialang.org/t/stiff-methods-throw-cannot-create-a-dual-over-scalar-type-complexf64-in-sdeproblem/106321/3 "2023-11-16T19:14:13Z")

</div>

You should be able to use `ImplicitRKMil(autodiff=false)` to disable autodiff and use finite differencing instead.

---

<div class="post-metadata">

### Author: ![ymardoukhi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ymardoukhi/32/216992_2.png) [@ymardoukhi](https://discourse.julialang.org/u/ymardoukhi)
#### Post date: [November 17, 2023, 7:11am UTC](https://discourse.julialang.org/t/stiff-methods-throw-cannot-create-a-dual-over-scalar-type-complexf64-in-sdeproblem/106321/4 "2023-11-17T07:11:43Z")

</div>

Thanks! I will consider that.

---

<div class="post-metadata">

### Author: ![ymardoukhi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ymardoukhi/32/216992_2.png) [@ymardoukhi](https://discourse.julialang.org/u/ymardoukhi)
#### Post date: [November 17, 2023, 7:15am UTC](https://discourse.julialang.org/t/stiff-methods-throw-cannot-create-a-dual-over-scalar-type-complexf64-in-sdeproblem/106321/5 "2023-11-17T07:15:02Z")

</div>

This seems nice for the sake of just simulating the problem. My ultimate goal is to optimise the signal s(t). But it was nice to know that there is an option to disable differentiability for a stiff solver. Could you point me to the documentation?
