Skip to content

Commit

Permalink
Merge pull request #10 from kestrelquantum/feat_piccolo_options
Browse files Browse the repository at this point in the history
Adding piccolo options to quantum collocation
  • Loading branch information
aarontrowbridge authored Jun 25, 2024
2 parents b9139b2 + 667a3e7 commit 990eb22
Show file tree
Hide file tree
Showing 41 changed files with 1,554 additions and 1,106 deletions.
6 changes: 3 additions & 3 deletions Manifest.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is machine-generated - editing it directly is not advised

julia_version = "1.10.3"
julia_version = "1.10.0"
manifest_format = "2.0"
project_hash = "0af13b9126caf21a7349df842fd536f87be35b2f"

Expand Down Expand Up @@ -338,7 +338,7 @@ weakdeps = ["Dates", "LinearAlgebra"]
[[deps.CompilerSupportLibraries_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"
version = "1.1.1+0"
version = "1.0.5+1"

[[deps.CompositeTypes]]
git-tree-sha1 = "bce26c3dab336582805503bed209faab1c279768"
Expand Down Expand Up @@ -1542,7 +1542,7 @@ version = "0.3.24+0"
[[deps.OpenBLAS_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"]
uuid = "4536629a-c528-5b80-bd46-f80d51c5b363"
version = "0.3.23+4"
version = "0.3.23+2"

[[deps.OpenEXR]]
deps = ["Colors", "FileIO", "OpenEXR_jll"]
Expand Down
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,43 @@ See the example script [examples/scripts/single_qubit_gate.jl](examples/scripts/

![Single Qubit X-Gate](images/T_100_Q_1000_iter_1000_00004_fidelity_0.9999999999994745.png)

## Quickstart developers guide

__Install Julia__ [Juliaup](https://github.com/JuliaLang/juliaup) is an installer and version manager. This is one useful way to manage Julia versions and keep up with the latest changes. After installing, run `julia` to obtain the Julia _REPL_.

__Julia environments__
[(Documentation)](https://pkgdocs.julialang.org/v1/environments/#Using-someone-else's-project) Your project's environment is stored in _Project.toml_. You can interactively add packages to an environment by using the Julia command line _REPL_ and _package manager_. Start Julia in the project folder. Type `]` to enter the package manager. Type `activate .` to activate or create an environment specified by _Project.toml_ located in the current folder. Separately, you generate a manifest (solving the versions to create a valid environment) by running `instantiate`; instantiate will check that the environment is correct after you add all the packages you want.

__Adding packages__
[(Documentation)](https://pkgdocs.julialang.org/v1/managing-packages/#Adding-packages) The initial cell for a Piccolo notebook might look something like the following:
```Julia
# Standard packages
using LinearAlgebra
using CairoMakie

# Piccolo packages
using QuantumCollocation
using NamedTrajectories
using TrajectoryIndexingUtils
```

First, let's install some standard packages (these are like Numpy and Matplotlib). Open the package manager in the current environment (type `julia`, `]`, and `activate .`), type `add LinearAlgebra` to install and precompile _LinearAlgebra_. Same with `CairoMakie`.

Second, let's install _Piccolo_. There are three packages (_QuantumCollocation_, _NamedTrajetories_, _TrajectoryIndexingUtils_) inside [Piccolo](https://docs.juliahub.com/General/Piccolo/stable/). We could do `add Piccolo` to get the three as a bundle from the Julia repository. Instead of individually calling `using ...` for each, this approach only requires `using Piccolo` at the start of a file or notebook.

As a developer, we want to use the git repositories directly from [the Kestrel Quantum Github page](https://github.com/kestrelquantum). Clone, then add the local packages to the Project file with e.g. `dev ../relative/path/to/repo/QuantumCollocation`. This command installs the development version of _QuantumCollocation_ pointing to the local Github code instead of the package repository. You can repeat this for the others, also.

__Developing__
[Revise.jl](https://timholy.github.io/Revise.jl/stable/) will let you edit source code, update packages, and reload the changes in a notebook---automatically! This is a great tool for development. `add Revise` from the REPL and then include it before any packages you intend to edit:
```Julia
using Revise
using QuantumCollocation
```

### Tips for Visual Studio Code
__Julia extension__ You can run Julia notebooks and much more with [the Julia extension](https://code.visualstudio.com/docs/languages/julia). Upon opening your project folder in VS code and attempting to run an `.ipynb`, you will see that VS Code finds the interpreters managed by juliaup and defaults to using the environment based on the _Project.toml_ in the project directory.

__Fonts__ VS Code will not display all characters allowed by Julia. You can change the editor font family in the settings to `'JuliaMono'` to get full support. If you don't want to mix and mash, you can create a new VS Code settings profile for working in Julia at _File>Preferences>Profile_.

__Tests__ Tests should automatically populate in VS Code when working with a Piccolo package. For example, just by adding the `QuantumCollocation.jl` folder to your workspace, you should see tests appear if you click on the _Testing_ sidebar icon. If you run one of these tests, a new Julia kernel is spawned for the test. You can find the kernel if you click on the _Julia_ sidebar icon (after installing the Julia extensions). Sometimes, for the tests to recognize new changes, you may need to manually kill this kernel to see your changes reflected.

2 changes: 1 addition & 1 deletion docs/src/contribution_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Tests are implemented using the [`TestItemRunner.jl`](https://github.com/julia-v

prob = UnitarySmoothPulseProblem(
H_drift, H_drives, U_goal, T, Δt,
ipopt_options=Options(print_level=1)
ipopt_options=IpoptOptions(print_level=1)
)

solve!(prob, max_iter=100)
Expand Down
4 changes: 2 additions & 2 deletions src/QuantumCollocation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ include("dynamics.jl")
include("evaluators.jl")
@reexport using .Evaluators

include("ipopt_options.jl")
@reexport using .IpoptOptions
include("options.jl")
@reexport using .Options

include("problems.jl")
@reexport using .Problems
Expand Down
29 changes: 8 additions & 21 deletions src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function FinalFidelityConstraint(;
zdim::Union{Int,Nothing}=nothing,
T::Union{Int,Nothing}=nothing,
subspace::Union{AbstractVector{<:Integer}, Nothing}=nothing,
hessian::Bool=true
eval_hessian::Bool=true
)
@assert !isnothing(fidelity_function) "must provide a fidelity function"
@assert !isnothing(value) "must provide a fidelity value"
Expand Down Expand Up @@ -194,7 +194,7 @@ function FinalFidelityConstraint(;
end
end

if hessian
if eval_hessian
∂²ℱ(x) = ForwardDiff.hessian(fid, x)

∂²ℱ_structure = hessian_of_lagrangian_structure(∂²ℱ, statedim, 1)
Expand Down Expand Up @@ -242,7 +242,7 @@ function FinalUnitaryFidelityConstraint(
val::Float64,
traj::NamedTrajectory;
subspace::Union{AbstractVector{<:Integer}, Nothing}=nothing,
hessian::Bool=true
eval_hessian::Bool=true
)
@assert statesymb traj.names
return FinalFidelityConstraint(;
Expand All @@ -254,7 +254,7 @@ function FinalUnitaryFidelityConstraint(
zdim=traj.dim,
T=traj.T,
subspace=subspace,
hessian=hessian
eval_hessian=eval_hessian
)
end

Expand All @@ -268,7 +268,8 @@ is the NamedTrajectory symbol representing the unitary.
function FinalQuantumStateFidelityConstraint(
statesymb::Symbol,
val::Float64,
traj::NamedTrajectory,
traj::NamedTrajectory;
kwargs...
)
@assert statesymb traj.names
return FinalFidelityConstraint(;
Expand All @@ -278,26 +279,12 @@ function FinalQuantumStateFidelityConstraint(
goal=traj.goal[statesymb],
statedim=traj.dims[statesymb],
zdim=traj.dim,
T=traj.T
T=traj.T,
kwargs...
)
end



# function FinalStateFidelityConstraint(
# val::Float64,
# statesymb::Symbol,
# statedim::Int;
# fidelity_function::Function=fidelity
# )
# return FinalFidelityConstraint(;
# fidelity_function=fidelity_function,
# value=val,
# statesymb=statesymb,
# statedim=statedim
# )
# end

"""
ComplexModulusContraint(<keyword arguments>)
Expand Down
29 changes: 15 additions & 14 deletions src/dynamics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ end
function QuantumDynamics(
integrators::Vector{<:AbstractIntegrator},
traj::NamedTrajectory;
hessian_approximation=false,
eval_hessian=true,
jacobian_structure=true,
verbose=false
)
Expand Down Expand Up @@ -235,10 +235,10 @@ function QuantumDynamics(

∂f = dynamics_jacobian(integrators, traj)

if hessian_approximation
μ∂²f = nothing
else
if eval_hessian
μ∂²f = dynamics_hessian_of_lagrangian(integrators, traj)
else
μ∂²f = nothing
end

if verbose
Expand All @@ -247,13 +247,7 @@ function QuantumDynamics(

dynamics_dim = dim(integrators)

if hessian_approximation
∂f_structure, ∂F_structure = dynamics_structure(∂f, traj, dynamics_dim;
verbose=verbose,
jacobian=jacobian_structure,
)
μ∂²F_structure = nothing
else
if eval_hessian
∂f_structure, ∂F_structure, μ∂²f_structure, μ∂²F_structure =
dynamics_structure(∂f, μ∂²f, traj, dynamics_dim;
verbose=verbose,
Expand All @@ -263,6 +257,12 @@ function QuantumDynamics(
)
)
μ∂²f_nnz = length(μ∂²f_structure)
else
∂f_structure, ∂F_structure = dynamics_structure(∂f, traj, dynamics_dim;
verbose=verbose,
jacobian=jacobian_structure,
)
μ∂²F_structure = nothing
end

∂f_nnz = length(∂f_structure)
Expand Down Expand Up @@ -294,9 +294,7 @@ function QuantumDynamics(
return ∂s
end

if hessian_approximation
μ∂²F = nothing
else
if eval_hessian
@views μ∂²F = (Z⃗::AbstractVector{<:Real}, μ⃗::AbstractVector{<:Real}) -> begin
μ∂²s = Vector{eltype(Z⃗)}(undef, length(μ∂²F_structure))
Threads.@threads for t = 1:traj.T-1
Expand All @@ -310,7 +308,10 @@ function QuantumDynamics(
end
return μ∂²s
end
else
μ∂²F = nothing
end

return QuantumDynamics(
integrators,
F,
Expand Down
8 changes: 8 additions & 0 deletions src/embedded_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,18 @@ end
EmbeddedOperator(op::Matrix{<:Number}, subspace_indices::AbstractVector{Int}, levels::Int) =
EmbeddedOperator(op, subspace_indices, [levels])

function embed(matrix::Matrix{ComplexF64}, op::EmbeddedOperator)
return embed(matrix, op.subspace_indices, prod(op.subsystem_levels))
end

function unembed(op::EmbeddedOperator)::Matrix{ComplexF64}
return op.operator[op.subspace_indices, op.subspace_indices]
end

function unembed(matrix::AbstractMatrix, op::EmbeddedOperator)
return matrix[op.subspace_indices, op.subspace_indices]
end

Base.size(op::EmbeddedOperator) = size(op.operator)
Base.size(op::EmbeddedOperator, dim::Union{Int, Nothing}) = size(op.operator, dim)

Expand Down
4 changes: 2 additions & 2 deletions src/evaluators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ mutable struct PicoEvaluator <: MOI.AbstractNLPEvaluator
trajectory::NamedTrajectory,
objective::Objective,
dynamics::QuantumDynamics,
nonlinear_constraints::Vector{<:NonlinearConstraint},
eval_hessian::Bool
nonlinear_constraints::Vector{<:NonlinearConstraint};
eval_hessian::Bool=true
)
n_dynamics_constraints = dynamics.dim * (trajectory.T - 1)
n_nonlinear_constraints = sum(con.dim for con nonlinear_constraints; init=0)
Expand Down
4 changes: 2 additions & 2 deletions src/integrators/_integrator_utils.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# G(a) helper function
function G(
# G_bilinear(a) helper function
function G_bilinear(
a::AbstractVector,
G_drift::AbstractMatrix,
G_drives::AbstractVector{<:AbstractMatrix}
Expand Down
1 change: 1 addition & 0 deletions src/integrators/_integrators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ using TrajectoryIndexingUtils
using LinearAlgebra
using SparseArrays
using ForwardDiff
using TestItemRunner

abstract type AbstractIntegrator end

Expand Down
Loading

0 comments on commit 990eb22

Please sign in to comment.