You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/users_guide/autodiff.md
+10-10Lines changed: 10 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# [Differentiate almost anything in QuantumToolbox.jl](@id doc:autodiff)
1
+
# [Automatic Differentiation](@id doc:autodiff)
2
2
3
3
Automatic differentiation (AD) has emerged as a key technique in computational science, enabling exact and efficient computation of derivatives for functions defined by code. Unlike symbolic differentiation, which may produce complex and inefficient expressions, or finite-difference methods, which suffer from numerical instability and poor scalability, AD leverages the chain rule at the level of elementary operations to provide machine-precision gradients with minimal overhead.
4
4
@@ -8,7 +8,7 @@ In `QuantumToolbox.jl`, we have introduced preliminary support for automatic dif
8
8
At present, this functionality is considered experimental and not all parts of the library are AD-compatible. Here we provide a brief overview of the current state of AD support in `QuantumToolbox.jl` and how to use it.
9
9
10
10
11
-
## [Forward VS Reverse Mode AD](@id doc:autodiff:forward_vs_reverse)
11
+
## [Forward versus Reverse Mode AD](@id doc:autodiff:forward-versus-reverse)
12
12
13
13
Automatic differentiation can be broadly categorized into two modes: forward mode and reverse mode. The choice between these modes depends on the nature of the function being differentiated and the number of inputs and outputs:
14
14
@@ -18,7 +18,7 @@ Automatic differentiation can be broadly categorized into two modes: forward mod
18
18
19
19
Understanding the differences between these two modes can help users choose the most appropriate approach for their specific use case in `QuantumToolbox.jl`.
20
20
21
-
## [Differentiate the master equation](@id doc:autodiff:master_equation)
21
+
## [Differentiate the master equation](@id doc:autodiff:master-equation)
22
22
23
23
One of the primary use cases for automatic differentiation in `QuantumToolbox.jl` is the differentiation of the master equation. The master equation describes the time evolution of a quantum system's density matrix under the influence of non-unitary dynamics, such as dissipation and decoherence. Let's consider a set of parameters $\mathbf{p} = (p_1, p_2, \ldots, p_n)$ that influence the system's dynamics. The Hamiltonian and the dissipators will depend on these parameters
24
24
@@ -54,7 +54,7 @@ Our goal is to compute the derivative of the expectation value with respect to t
and to achieve this, we can use an AD engine like `ForwardDiff.jl` (forward mode) or `Zygote.jl` (reverse mode).
57
+
and to achieve this, we can use an AD engine like [`ForwardDiff.jl`](https://github.com/JuliaDiff/ForwardDiff.jl) (forward mode) or [`Zygote.jl`](https://github.com/FluxML/Zygote.jl) (reverse mode).
58
58
59
59
Let's apply this to a simple example of a driven-dissipative quantum harmonic oscillator. The Hamiltonian in the drive frame is given by
60
60
@@ -81,15 +81,15 @@ with the gradient given by
81
81
\end{pmatrix} \, .
82
82
```
83
83
84
-
Although `QuantumToolbox.jl` has the [`steadystate`](@ref) function to directly compute the steady state without explicitly solving the master equation, here we use the [`mesolve`](@ref) function to integrate up to a long time $t_\mathrm{max}$, and then compute the expectation value of the number operator. We will demonstrate how to compute the gradient using both `ForwardDiff.jl` and `Zygote.jl`.
84
+
Although `QuantumToolbox.jl` has the [`steadystate`](@ref) function to directly compute the steady state without explicitly solving the master equation, here we use the [`mesolve`](@ref) function to integrate up to a long time $t_\mathrm{max}$, and then compute the expectation value of the number operator. We will demonstrate how to compute the gradient using both [`ForwardDiff.jl`](https://github.com/JuliaDiff/ForwardDiff.jl) and [`Zygote.jl`](https://github.com/FluxML/Zygote.jl).
85
85
86
86
### [Forward Mode AD with ForwardDiff.jl](@id doc:autodiff:forward)
87
87
88
88
```@setup autodiff
89
89
using QuantumToolbox
90
90
```
91
91
92
-
We start by importing `ForwardDiff.jl` and defining the parameters and operators:
92
+
We start by importing [`ForwardDiff.jl`](https://github.com/JuliaDiff/ForwardDiff.jl) and defining the parameters and operators:
Finally, we can compare the results from `ForwardDiff.jl` and `Zygote.jl`:
187
+
Finally, we can compare the results from [`ForwardDiff.jl`](https://github.com/JuliaDiff/ForwardDiff.jl) and [`Zygote.jl`](https://github.com/FluxML/Zygote.jl):
188
188
189
189
```@example autodiff
190
-
isapprox(grad_fd, grad_zygote; atol = 1e-6)
190
+
isapprox(grad_fd, grad_zygote; atol = 1e-5)
191
191
```
192
192
193
193
## [Conclusion](@id doc:autodiff:conclusion)
194
194
195
-
In this section, we have explored the integration of automatic differentiation into `QuantumToolbox.jl`, enabling users to compute gradients of observables and cost functionals involving the time evolution of open quantum systems. We demonstrated how to differentiate the master equation using both forward mode with `ForwardDiff.jl` and reverse mode with `Zygote.jl`, showcasing the flexibility and power of automatic differentiation in quantum computing applications. AD can be applied to other functions in `QuantumToolbox.jl`, although the support is still experimental and not all functions are guaranteed to be compatible. We encourage users to experiment with AD in their quantum simulations and contribute to the ongoing development of this feature.
195
+
In this section, we have explored the integration of automatic differentiation into `QuantumToolbox.jl`, enabling users to compute gradients of observables and cost functionals involving the time evolution of open quantum systems. We demonstrated how to differentiate the master equation using both forward mode with [`ForwardDiff.jl`](https://github.com/JuliaDiff/ForwardDiff.jl) and reverse mode with [`Zygote.jl`](https://github.com/FluxML/Zygote.jl), showcasing the flexibility and power of automatic differentiation in quantum computing applications. AD can be applied to other functions in `QuantumToolbox.jl`, although the support is still experimental and not all functions are guaranteed to be compatible. We encourage users to experiment with AD in their quantum simulations and contribute to the ongoing development of this feature.
0 commit comments