Skip to content
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

[Feature]: Automated gradient testing #144

Open
1 of 14 tasks
andgoldschmidt opened this issue Aug 14, 2024 · 0 comments
Open
1 of 14 tasks

[Feature]: Automated gradient testing #144

andgoldschmidt opened this issue Aug 14, 2024 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers qnumerics Issue for qnumerics hackathon

Comments

@andgoldschmidt
Copy link
Member

andgoldschmidt commented Aug 14, 2024

Feature Description

Background

A big part of the code is writing gradients for integrators, objectives, and constraints for custom objects.

ForwardDiff.jl allows us to calculate analytic gradients, so we should have a function that automates testing the accuracy of our gradients and Jacobians. The function should dispatch over generic Integrators, Objectives, or Constraints, separately. The function should make informative error reporting about where and how the gradients or Jacobians differ.

A useful starting point is the undocumented function show_diffs in test/test_utils.jl, which starts this process of informative error reporting. An updated version of that function is included here:

function show_diffs(A::AbstractMatrix, B::AbstractMatrix; return_indices=false, atol=0, components=nothing)
    @assert size(A) == size(B)
    matrix_is_square = size(A, 1) == size(A, 2)
    indices = []
    for (i, (a, b)) in enumerate(zip(A, B))
        inds = Tuple(CartesianIndices(A)[i])
        if matrix_is_square
            if !isapprox(a, b; atol=atol) && inds[1]  inds[2]
                println((a, b), " @ ", inds)
            end
        else
            if !isapprox(a, b; atol=atol)
                println((a, b), " @ ", inds)
                if return_indices
                    push!(indices, inds)
                end
            end
        end
    end
    return return_indices ? indices : nothing
end

These functions can be added to the test_utils.jl file, and should use a few kinds of NamedTrajectory's to be robust to different cases when checking derivatives (e.g. named_trajectory_type_1(...) in test_utils.jl).

Suggested checklist

  • Write an automated gradient tester and test on Objectives
  • Extend the automated gradient tester to Jacobians, and test on Constraints
  • Extend the Jacobian tester to Integratators
  • Check that the coverage of named trajectory test objects is sufficient
  • Add tests items for current objectives, constraints, and integrators.

Importance

1 (lowest)

What does this feature affect?

  • quantum system construction
  • problem setup
  • problem solution
  • problem performance
  • solution analysis
  • plotting
  • documentation
  • tests
  • other (please specify below)

Other information

No response

@andgoldschmidt andgoldschmidt added enhancement New feature or request good first issue Good for newcomers qnumerics Issue for qnumerics hackathon labels Aug 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers qnumerics Issue for qnumerics hackathon
Projects
None yet
Development

No branches or pull requests

1 participant