-
Notifications
You must be signed in to change notification settings - Fork 10
Add support for gradients w.r.t. trajectories #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
48dceda
Add support for gradients w.r.t. trajectories
goerz c611f03
Fix incompatibility with Zygote 0.7
goerz 0f889af
Fix normalization, comment typo, and add numerical gradient checks in…
Copilot 62cd8e4
Replace ≈ with norm-based comparison in Zygote gradient tests
Copilot c8cf47a
Fix analytical derivative in test
goerz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| module QuantumControlChainRulesCoreExt | ||
|
|
||
| using ChainRulesCore: ChainRulesCore, NoTangent | ||
| using QuantumControl: Trajectory | ||
|
|
||
|
|
||
| # Allow to differentiate w.r.t. to a trajectory. See `test_traj_zygote.jl` for | ||
| # an example. Evaluating a gradient with Zygote returns a NamedTuple with | ||
| # the fields of the trajectory. Unfortunately, Zygote gets confused about the | ||
| # custom `getproperty` method that is defined for a Trajectory, and we need a | ||
| # special method to differential through `getproperty` | ||
| function ChainRulesCore.rrule(::typeof(getproperty), traj::Trajectory, name::Symbol) | ||
| val = getproperty(traj, name) | ||
| if name in (:initial_state, :generator, :target_state, :weight) | ||
| function field_pullback(Δ) | ||
| dt = ChainRulesCore.Tangent{typeof(traj)}(; (name => Δ,)...) | ||
| return NoTangent(), dt, NoTangent() | ||
| end | ||
| return val, field_pullback | ||
| else | ||
| # kwargs-stored property: route gradient back into the kwargs Dict | ||
| function kwargs_pullback(Δ) | ||
| dkwargs = Dict{Symbol,Any}(name => ChainRulesCore.unthunk(Δ)) | ||
| dt = ChainRulesCore.Tangent{typeof(traj)}(; kwargs = dkwargs) | ||
| return NoTangent(), dt, NoTangent() | ||
| end | ||
| return val, kwargs_pullback | ||
| end | ||
| end | ||
|
|
||
|
|
||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| using Test | ||
| using StableRNGs | ||
| using IOCapture | ||
| using QuantumControl: Trajectory | ||
| using LinearAlgebra: dot, norm | ||
| using Random: rand | ||
| using Zygote | ||
|
|
||
|
|
||
| function J_T(Ψ; Ψtgt, N) | ||
| return 1 - (abs2(dot(Ψ, Ψtgt)) / N) | ||
| end | ||
|
|
||
|
|
||
|
|
||
| @testset "Gradient w.r.t. trajectory.initial_state" begin | ||
|
|
||
| function f(traj; Ψtgt, N) | ||
| return J_T(traj.initial_state; Ψtgt, N) | ||
| end | ||
|
|
||
| rng = StableRNG(3143162815) | ||
| N = 4 | ||
| H = nothing | ||
| Ψ = rand(rng, ComplexF64, N) | ||
| Ψ ./= norm(Ψ) | ||
| Ψtgt = zeros(ComplexF64, N) | ||
| Ψtgt[1] = 1.0 | ||
| traj = Trajectory(Ψ, H) | ||
| @test f(traj; Ψtgt, N) > 0.0 | ||
| grad = Zygote.gradient(traj -> f(traj; Ψtgt, N), traj)[1] | ||
| @test grad isa NamedTuple | ||
| @test grad.initial_state isa Vector | ||
| expected_grad = -2 .* Ψtgt .* conj(dot(Ψ, Ψtgt)) / N | ||
| @test norm(grad.initial_state - expected_grad) < 1e-14 | ||
|
|
||
| end | ||
|
|
||
|
|
||
| @testset "Gradient w.r.t. trajectory.x" begin | ||
|
|
||
| function f(traj; Ψtgt, N) | ||
| return J_T(traj.x; Ψtgt, N) | ||
| end | ||
|
|
||
| rng = StableRNG(3143162816) | ||
| N = 4 | ||
| H = nothing | ||
| Ψ = rand(rng, ComplexF64, N) | ||
| Ψ ./= norm(Ψ) | ||
| Ψtgt = zeros(ComplexF64, N) | ||
| Ψtgt[1] = 1.0 | ||
| x = Ψ | ||
| traj = Trajectory(Ψ, H; x) | ||
| @test f(traj; Ψtgt, N) > 0.0 | ||
| captured = IOCapture.capture(rethrow = Union{}) do | ||
| # Without the custom `rrule` in `QuantumControlChainRulesCoreExt`, this | ||
| # test would show a potentially very confusing error, and throw an | ||
| # `UndefRefError`. See also: https://discourse.julialang.org/t/136704/ | ||
| Zygote.gradient(traj -> f(traj; Ψtgt, N), traj)[1] | ||
| end | ||
| grad = captured.value | ||
| @test grad isa NamedTuple | ||
| if grad isa NamedTuple | ||
| @test grad.initial_state isa Nothing | ||
| @test grad.kwargs[:x] isa Vector | ||
| expected_grad = -2 .* Ψtgt .* conj(dot(Ψ, Ψtgt)) / N | ||
| @test norm(grad.kwargs[:x] - expected_grad) < 1e-14 | ||
| end | ||
|
|
||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.