# Krotov’s Method in quantum control

**URL:** <https://discourse.julialang.org/t/krotov-s-method-in-quantum-control/80146>\
**Category:** New to Julia\
**Created:** [April 27, 2022, 3:07pm UTC](https://discourse.julialang.org/t/krotov-s-method-in-quantum-control/80146 "2022-04-27T15:07:22Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [April 27, 2022, 3:07pm UTC](https://discourse.julialang.org/t/krotov-s-method-in-quantum-control/80146/1 "2022-04-27T15:07:22Z")

</div>

Hello everybody. I’m looking for a krotov method in Julia, but I’ve been having doubts with the search result. What is the recommended package for the krotov optimization method? I’m going to use it with quantum control. Thank you all.

---

<div class="post-metadata">

**Author:** ![james3](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@james3](https://discourse.julialang.org/u/james3)\
**Post date:** [May 4, 2022, 1:16am UTC](https://discourse.julialang.org/t/krotov-s-method-in-quantum-control/80146/2 "2022-05-04T01:16:16Z")

</div>

You can try [`Krotov.jl`](https://juliaquantumcontrol.github.io/Krotov.jl/stable/), which is a port of the `krotov` Python package.

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [May 6, 2022, 3:11am UTC](https://discourse.julialang.org/t/krotov-s-method-in-quantum-control/80146/3 "2022-05-06T03:11:27Z")

</div>

But I can’t even test Krotov because there is no Shapes method in QuantumControl (package installed).

```julia
ϵ(t) = 0.2 * QuantumControl.Shapes.flattop(t, T=5, t_rise=0.3, func=:blackman);

```

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [May 6, 2022, 3:22am UTC](https://discourse.julialang.org/t/krotov-s-method-in-quantum-control/80146/4 "2022-05-06T03:22:15Z")

</div>

I was typing ϵ(t), but without the (t):

```julia
ϵ = 0.2 * QuantumControl.Shapes.flattop(t, T=5, t_rise=0.3, func=:blackman);

```

and getting the error: ERROR: UndefVarError: Shapes not defined

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [May 6, 2022, 10:46pm UTC](https://discourse.julialang.org/t/krotov-s-method-in-quantum-control/80146/5 "2022-05-06T22:46:21Z")

</div>

It doesn’t work for me. When I execute the commands in example 1, I still get an error message when I run :  
julia\> fig = plot\_control(H[2][2], tlist)  
ERROR: UndefVarError: Shapes not defined

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [May 6, 2022, 11:34pm UTC](https://discourse.julialang.org/t/krotov-s-method-in-quantum-control/80146/6 "2022-05-06T23:34:40Z")

</div>

It was not immediately obvious you are talking about the Julia package QuantumControl.jl (which seems to use Krotov.jl internally).

I would suggest asking on their issue tracker [GitHub - JuliaQuantumControl/QuantumControl.jl: Julia Framework for Quantum Optimal Control](https://github.com/JuliaQuantumControl/QuantumControl.jl) or pining some of the package authors that are also on this forum, e.g. @goerz

---

<div class="post-metadata">

**Author:** ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)\
**Post date:** [May 7, 2022, 1:28am UTC](https://discourse.julialang.org/t/krotov-s-method-in-quantum-control/80146/7 "2022-05-07T01:28:08Z")

</div>

[`QuantumControl.jl`](https://github.com/JuliaQuantumControl/QuantumControl.jl) (which contains [`Krotov.jl`](https://github.com/JuliaQuantumControl/Krotov.jl)) is definitely the package you’re looking for.

@marllos I just doubled-checked the [Example](https://juliaquantumcontrol.github.io/Krotov.jl/stable/examples/simple_state_to_state/) from a clean environment, and it should work just fine. If you’re missing `QuantumControl.Shapes`, my best guess would be that you’re not running the latest release of `QuantumControl` or its sub-packages. There was some re-organization a month ago or so that would have affected `QuantumControl.Shapes`. Maybe you dev-installed packages from the [JuliaQuantumControl organization](https://github.com/JuliaQuantumControl) at some point in the past? Make sure that you’re running the latest release (or the latest `master` branch, equivalently).

If you’re running into any problems, feel free to ask here (tag me), in the `#quantumcontrol` channel on Slack, or by opening an issue on Github. There might still be a few rough edges in `QuantumControl.jl`: I’d say it’s not _quite_ ready for “production” yet, although everything in the examples definitely works as described (most notably, optimizations with Krotov and GRAPE of standard quantum control problems). Other stuff is still on the roadmap, like better integration with packages such as `QuantumOptics.jl` and a better API for the time propagation (`QuantumPropagators.jl`)

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [May 9, 2022, 3:18pm UTC](https://discourse.julialang.org/t/krotov-s-method-in-quantum-control/80146/8 "2022-05-09T15:18:44Z")

</div>

Hi Goerz, you are right about the versioning of the packages. But I always install with “pkg\> add” and update Julia almost daily. So do I need to install manually?

The versions are:

pkg\> st  
[b05dcdc7] Krotov v0.0.1  
[8a270532] QuantumControl v0.0.1  
[429524aa] Optim v1.7.0  
[3c0b384b] QuantumInformation v0.4.9  
… (+23)

julia\> versioninfo()  
Julia Version 1.7.2  
Commit bf53498635 (2022-02-06 15:21 UTC)  
Platform Info:  
OS: Linux (x86\_64-pc-linux-gnu)  
CPU: Intel(R) Core™ i3-7020U CPU @ 2.30GHz  
WORD\_SIZE: 64  
LIBM: libopenlibm  
LLVM: libLLVM-12.0.1 (ORCJIT, skylake)

Linux fedora 5.15.8-200.fc35.x86\_64 #1 SMP Tue Dec 14 14:26:01 UTC 2021 x86\_64 x86\_64 x86\_64 GNU/Linu

---

<div class="post-metadata">

**Author:** ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)\
**Post date:** [May 9, 2022, 3:42pm UTC](https://discourse.julialang.org/t/krotov-s-method-in-quantum-control/80146/9 "2022-05-09T15:42:42Z")

</div>

The latest version of `QuantumControl` is `0.2.0`. You have `0.0.1`, which was basically a placeholder release and will not be useable for anything.

If you start with a new Project and install `QuantumControl` into that, you should be fine.

You can do this, e.g., with the following, in some (new) project folder:

```julia
julia --project=.
julia> ]add QuantumControl

```

Then you can run all the command from the [Example](https://juliaquantumcontrol.github.io/Krotov.jl/stable/examples/simple_state_to_state/) in the same REPL (you’ll also want to install Plots, and probably skip the “DrWatson” cell, unless you’ve [set up your project with DrWatson](https://juliadynamics.github.io/DrWatson.jl/dev/project/))

I think you can also just run `] update` in your base environment (the REPL you get if you just run `julia`), but I strongly recommend against installing anything into your base environment. For reproducibility, you’ll always want to work with a project folder, or even use something like DrWatson. Maybe start afresh by deleting your `~./julia` folder (make a backup fist: you might have dev-installs are in there, but otherwise that folder doesn’t contain anything that’s not re-generated automatically)

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [May 9, 2022, 4:50pm UTC](https://discourse.julialang.org/t/krotov-s-method-in-quantum-control/80146/11 "2022-05-09T16:50:10Z")

</div>

Interesting, I never used Julia as you showed. Just curious, in QuantumControl you solve the Liouville Equation using Chebyshev derivative matrices?

(krotov) pkg\> st  
Status `~/Documentos/tmp/krotov/Project.toml`  
[b05dcdc7] Krotov v0.2.1  
[8a270532] QuantumControl v0.2.0  
(all)

---

<div class="post-metadata">

**Author:** ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)\
**Post date:** [May 9, 2022, 4:53pm UTC](https://discourse.julialang.org/t/krotov-s-method-in-quantum-control/80146/12 "2022-05-09T16:53:05Z")

</div>

Chebychev polynomials are only defined for real numbers, which means they can only expand the exponential for Hermitian Hamiltonians (real eigenvalues). For open system dynamics, you can use an expansion into Newton polynomials instead (`:newton` prop-method in `QuantumPropagators`). For some reason, my Julia implementation of the Newton expansion is a bit slower than I’d like (about a factor of 10 slower compared to Cheby), but still much better than an ODE solver. Maybe I’ll be able to bring that down in the future.

In any case, the optimization should automatically pick the right propagation method, in most cases, so you probably don’t have to worry about it too much.

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [May 9, 2022, 5:05pm UTC](https://discourse.julialang.org/t/krotov-s-method-in-quantum-control/80146/13 "2022-05-09T17:05:13Z")

</div>

Thank you very much Goerz.
