Skip to content

Commit 68de71b

Browse files
Yi-Te suggestions
1 parent 1fd04c8 commit 68de71b

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

docs/src/index.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ In order to get a better experience and take full advantage of `QuantumToolbox`
9999
- [`SlurmClusterManager.jl`](https://github.com/JuliaParallel/SlurmClusterManager.jl)
100100
- Plotting Libraries:
101101
- [`Makie.jl`](https://github.com/MakieOrg/Makie.jl)
102+
- Automatic Differentiation
103+
- [`SciMLSensitivity.jl`](https://github.com/SciML/SciMLSensitivity.jl)
104+
- [`Zygote.jl`](https://github.com/FluxML/Zygote.jl)
105+
- [`Enzyme.jl`](https://github.com/EnzymeAD/Enzyme.jl)
106+
- [`ForwardDiff.jl`](https://github.com/JuliaDiff/ForwardDiff.jl)
102107
- Packages for other advanced usage:
103108
- [`StaticArrays.jl`](https://github.com/JuliaArrays/StaticArrays.jl)
104109
- [`SciMLOperators.jl`](https://github.com/SciML/SciMLOperators.jl)

docs/src/users_guide/autodiff.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [Differentiate almost anything in QuantumToolbox.jl](@id doc:autodiff)
1+
# [Automatic Differentiation](@id doc:autodiff)
22

33
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.
44

@@ -8,7 +8,7 @@ In `QuantumToolbox.jl`, we have introduced preliminary support for automatic dif
88
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.
99

1010

11-
## [Forward VS Reverse Mode AD](@id doc:autodiff:forward_vs_reverse)
11+
## [Forward versus Reverse Mode AD](@id doc:autodiff:forward-versus-reverse)
1212

1313
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:
1414

@@ -18,7 +18,7 @@ Automatic differentiation can be broadly categorized into two modes: forward mod
1818

1919
Understanding the differences between these two modes can help users choose the most appropriate approach for their specific use case in `QuantumToolbox.jl`.
2020

21-
## [Differentiate the master equation](@id doc:autodiff:master_equation)
21+
## [Differentiate the master equation](@id doc:autodiff:master-equation)
2222

2323
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
2424

@@ -54,7 +54,7 @@ Our goal is to compute the derivative of the expectation value with respect to t
5454
\frac{\partial \langle \hat{O}(\mathbf{p}, t) \rangle}{\partial p_j} = \frac{\partial}{\partial p_j} \text{Tr}[\hat{O} \hat{\rho}(\mathbf{p}, t)] \, ,
5555
```
5656

57-
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).
5858

5959
Let's apply this to a simple example of a driven-dissipative quantum harmonic oscillator. The Hamiltonian in the drive frame is given by
6060

@@ -81,15 +81,15 @@ with the gradient given by
8181
\end{pmatrix} \, .
8282
```
8383

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).
8585

8686
### [Forward Mode AD with ForwardDiff.jl](@id doc:autodiff:forward)
8787

8888
```@setup autodiff
8989
using QuantumToolbox
9090
```
9191

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:
9393

9494
```@example autodiff
9595
using ForwardDiff
@@ -141,7 +141,7 @@ grad_fd = ForwardDiff.gradient(my_f_mesolve_direct, params)
141141
and test if the results match:
142142

143143
```@example autodiff
144-
isapprox(grad_exact, grad_fd; atol = 1e-6)
144+
isapprox(grad_exact, grad_fd; atol = 1e-5)
145145
```
146146

147147
### [Reverse Mode AD with Zygote.jl](@id doc:autodiff:reverse)
@@ -184,12 +184,12 @@ And the gradient can be computed using `Zygote.gradient`:
184184
grad_zygote = Zygote.gradient(my_f_mesolve, params)[1]
185185
```
186186

187-
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):
188188

189189
```@example autodiff
190-
isapprox(grad_fd, grad_zygote; atol = 1e-6)
190+
isapprox(grad_fd, grad_zygote; atol = 1e-5)
191191
```
192192

193193
## [Conclusion](@id doc:autodiff:conclusion)
194194

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

Comments
 (0)