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

Problem template options refactor #160

Merged
merged 36 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
acbf575
refactor: template options into PiccoloOptions (in progress)
aarontrowbridge Aug 21, 2024
6a90a95
in-place apply piccolo options
andgoldschmidt Aug 23, 2024
cfb8433
optional operator kwarg
andgoldschmidt Aug 23, 2024
080a15c
add method for FinalUnitaryFreePhaseFidelityConstraint
andgoldschmidt Aug 27, 2024
7f69132
cleaning and formatting
aarontrowbridge Sep 3, 2024
cd40614
patch bump for compat bumps
aarontrowbridge Sep 3, 2024
9164914
feat: data interpolations
andgoldschmidt Aug 23, 2024
6def0e1
update project patch version
andgoldschmidt Aug 26, 2024
2817a55
patch bump for compat bumps
aarontrowbridge Sep 3, 2024
24a9e52
moved phase infidelity params to options
aarontrowbridge Sep 3, 2024
d502af8
docs updates
aarontrowbridge Sep 9, 2024
4dbf74c
docs final touches
aarontrowbridge Sep 9, 2024
91afdb3
compat bump
aarontrowbridge Sep 9, 2024
23ce875
patch: repo name
aarontrowbridge Sep 10, 2024
6e6cd38
doc string addition
aarontrowbridge Sep 16, 2024
2e768db
docs: two qubit gates
aarontrowbridge Sep 17, 2024
0f5f7e2
cleaning and formatting
aarontrowbridge Sep 3, 2024
537f3c0
mid-refactor save
aarontrowbridge Sep 30, 2024
be7d0f4
rafactor to initialization code
aarontrowbridge Oct 4, 2024
4ccab1a
finishing touches on initialization
aarontrowbridge Oct 7, 2024
91b0f81
renamed symbol to name
aarontrowbridge Oct 7, 2024
e9bade2
docs updates
aarontrowbridge Oct 7, 2024
f59f011
adding manifest to .gitignore
aarontrowbridge Oct 7, 2024
8324909
version bump
aarontrowbridge Oct 7, 2024
896b048
manifest cleaning
aarontrowbridge Sep 3, 2024
c7c40b0
feat: data interpolations
andgoldschmidt Aug 23, 2024
2e29f0c
compat bump
aarontrowbridge Sep 9, 2024
362baf0
refactor: template options into PiccoloOptions (in progress)
aarontrowbridge Aug 21, 2024
b88e07e
cleaning and formatting
aarontrowbridge Sep 3, 2024
ef2599d
patch bump for compat bumps
aarontrowbridge Sep 3, 2024
6502001
feat: data interpolations
andgoldschmidt Aug 23, 2024
a278baa
update project patch version
andgoldschmidt Aug 26, 2024
5e2d067
compat bump
aarontrowbridge Sep 9, 2024
001c8ad
rebase fix
aarontrowbridge Oct 7, 2024
ed8bff7
reverting back to old .gitignore
aarontrowbridge Oct 7, 2024
d540a8a
Merge remote-tracking branch 'origin/main' into problem-template-opti…
aarontrowbridge Oct 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 78 additions & 74 deletions Manifest.toml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "QuantumCollocation"
uuid = "0dc23a59-5ffb-49af-b6bd-932a8ae77adf"
authors = ["Aaron Trowbridge <aaron.j.trowbridge@gmail.com> and contributors"]
version = "0.2.2"
version = "0.3.0"

[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Expand Down
76 changes: 73 additions & 3 deletions docs/src/lib.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
# Library

## QuantumUtils
## Problem Templates
```@autodocs
Modules = [QuantumCollocation.QuantumUtils]
Modules = [QuantumCollocation.ProblemTemplates]
```

## QuantumSystems
## Direct Sums
```@autodocs
Modules = [QuantumCollocation.DirectSums]
```

## Quantum Object Utils
```@autodocs
Modules = [QuantumCollocation.QuantumObjectUtils]
```

## Quantum Systems
```@autodocs
Modules = [QuantumCollocation.QuantumSystems]
```
Expand All @@ -14,3 +24,63 @@ Modules = [QuantumCollocation.QuantumSystems]
```@autodocs
Modules = [QuantumCollocation.Integrators]
```

## Objectives
```@autodocs
Modules = [QuantumCollocation.Objectives]
```

## Losses
```@autodocs
Modules = [QuantumCollocation.Losses]
```

## Embedded Operators
```@autodocs
Modules = [QuantumCollocation.EmbeddedOperators]
```

## Isomorphisms
```@autodocs
Modules = [QuantumCollocation.Isomorphisms]
```

## Options
```@autodocs
Modules = [QuantumCollocation.Options]
```

## Plotting
```@autodocs
Modules = [QuantumCollocation.Plotting]
```

## Problem Solvers
```@autodocs
Modules = [QuantumCollocation.ProblemSolvers]
```

## Rollouts
```@autodocs
Modules = [QuantumCollocation.Rollouts]
```

## Saving and Loading
```@autodocs
Modules = [QuantumCollocation.SaveLoadUtils]
```

## Structure Utils
```@autodocs
Modules = [QuantumCollocation.StructureUtils]
```

## Trajectory Initialization
```@autodocs
Modules = [QuantumCollocation.TrajectoryInitialization]
```

## Trajectory Interpolations
```@autodocs
Modules = [QuantumCollocation.TrajectoryInterpolations]
```
6 changes: 3 additions & 3 deletions src/QuantumCollocation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module QuantumCollocation
using Reexport


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

include("isomorphisms.jl")
@reexport using .Isomorphisms

Expand Down Expand Up @@ -42,9 +45,6 @@ include("dynamics.jl")
include("evaluators.jl")
@reexport using .Evaluators

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

include("problems.jl")
@reexport using .Problems

Expand Down
4 changes: 4 additions & 0 deletions src/constraints/_constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Constraints
export AbstractConstraint

export LinearConstraint

export constrain!

export NonlinearConstraint
Expand All @@ -12,6 +13,7 @@ export NonlinearInequalityConstraint
using ..Losses
using ..Isomorphisms
using ..StructureUtils
using ..Options

using TrajectoryIndexingUtils
using NamedTrajectories
Expand Down Expand Up @@ -126,4 +128,6 @@ struct NonlinearInequalityConstraint <: NonlinearConstraint
params::Dict{Symbol, Any}
end



end
28 changes: 14 additions & 14 deletions src/constraints/complex_modulus_constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ function ComplexModulusContraint(;
R::Union{Float64, Nothing}=nothing,
comps::Union{AbstractVector{Int}, Nothing}=nothing,
times::Union{AbstractVector{Int}, Nothing}=nothing,
dim::Union{Int, Nothing}=nothing,
zdim::Union{Int, Nothing}=nothing,
T::Union{Int, Nothing}=nothing,
)
@assert !isnothing(R) "must provide a value R, s.t. |z| <= R"
@assert !isnothing(comps) "must provide components of the complex number"
@assert !isnothing(times) "must provide times"
@assert !isnothing(dim) "must provide a trajectory dimension"
@assert !isnothing(zdim) "must provide a trajectory dimension"
@assert !isnothing(T) "must provide a T"

@assert length(comps) == 2 "component must represent a complex number and have dimension 2"
Expand All @@ -37,7 +37,7 @@ function ComplexModulusContraint(;
params[:R] = R
params[:comps] = comps
params[:times] = times
params[:dim] = dim
params[:zdim] = zdim
params[:T] = T

gₜ(xₜ, yₜ) = [R^2 - xₜ^2 - yₜ^2]
Expand All @@ -47,7 +47,7 @@ function ComplexModulusContraint(;
@views function g(Z⃗)
r = zeros(length(times))
for (i, t) ∈ enumerate(times)
zₜ = Z⃗[slice(t, comps, dim)]
zₜ = Z⃗[slice(t, comps, zdim)]
xₜ = zₜ[1]
yₜ = zₜ[2]
r[i] = gₜ(xₜ, yₜ)[1]
Expand All @@ -58,17 +58,17 @@ function ComplexModulusContraint(;
∂g_structure = []

for (i, t) ∈ enumerate(times)
push!(∂g_structure, (i, index(t, comps[1], dim)))
push!(∂g_structure, (i, index(t, comps[2], dim)))
push!(∂g_structure, (i, index(t, comps[1], zdim)))
push!(∂g_structure, (i, index(t, comps[2], zdim)))
end

@views function ∂g(Z⃗; ipopt=true)
∂ = spzeros(length(times), length(Z⃗))
for (i, t) ∈ enumerate(times)
zₜ = Z⃗[slice(t, comps, dim)]
zₜ = Z⃗[slice(t, comps, zdim)]
xₜ = zₜ[1]
yₜ = zₜ[2]
∂[i, slice(t, comps, dim)] = ∂gₜ(xₜ, yₜ)
∂[i, slice(t, comps, zdim)] = ∂gₜ(xₜ, yₜ)
end
if ipopt
return [∂[i, j] for (i, j) in ∂g_structure]
Expand All @@ -83,15 +83,15 @@ function ComplexModulusContraint(;
push!(
μ∂²g_structure,
(
index(t, comps[1], dim),
index(t, comps[1], dim)
index(t, comps[1], zdim),
index(t, comps[1], zdim)
)
)
push!(
μ∂²g_structure,
(
index(t, comps[2], dim),
index(t, comps[2], dim)
index(t, comps[2], zdim),
index(t, comps[2], zdim)
)
)
end
Expand All @@ -100,7 +100,7 @@ function ComplexModulusContraint(;
n = length(Z⃗)
μ∂² = spzeros(n, n)
for (i, t) ∈ enumerate(times)
t_slice = slice(t, comps, dim)
t_slice = slice(t, comps, zdim)
μ∂²[t_slice, t_slice] = μₜ∂²gₜ(μ[i])
end
if ipopt
Expand Down Expand Up @@ -143,4 +143,4 @@ function ComplexModulusContraint(
zdim=traj.dim,
T=traj.T
)
end
end
24 changes: 22 additions & 2 deletions src/constraints/fidelity_constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ function FinalUnitaryFidelityConstraint(
subspace::Union{AbstractVector{<:Integer}, Nothing}=nothing,
eval_hessian::Bool=true
)
@assert statesymb ∈ traj.names
return FinalFidelityConstraint(;
fidelity_function=iso_vec_unitary_fidelity,
value=val,
Expand Down Expand Up @@ -221,7 +220,7 @@ function FinalUnitaryFreePhaseFidelityConstraint(;
state_slice::Union{AbstractVector{Int},Nothing}=nothing,
phase_slice::Union{AbstractVector{Int},Nothing}=nothing,
goal::Union{AbstractVector{Float64},Nothing}=nothing,
phase_operators::Union{AbstractVector{<:AbstractMatrix{<:Complex{Float64}}},Nothing}=nothing,
phase_operators::Union{AbstractVector{<:AbstractMatrix{<:Complex}},Nothing}=nothing,
zdim::Union{Int,Nothing}=nothing,
subspace::Union{AbstractVector{<:Integer}, Nothing}=nothing,
eval_hessian::Bool=false
Expand Down Expand Up @@ -284,4 +283,25 @@ function FinalUnitaryFreePhaseFidelityConstraint(;
1,
params
)
end

function FinalUnitaryFreePhaseFidelityConstraint(
state_symbol::Symbol,
global_symbol::Symbol,
phase_operators::AbstractVector{<:AbstractMatrix{<:Complex}},
val::Float64,
traj::NamedTrajectory;
subspace::Union{AbstractVector{<:Integer}, Nothing}=nothing,
eval_hessian::Bool=false
)
return FinalUnitaryFreePhaseFidelityConstraint(;
value=val,
state_slice=slice(traj.T, traj.components[state_symbol], traj.dim),
phase_slice=trajectory.global_components[global_symbol],
goal=traj.goal[state_symbol],
phase_operators=phase_operators,
zdim=length(traj),
subspace=subspace,
eval_hessian=eval_hessian
)
end
22 changes: 11 additions & 11 deletions src/direct_sums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,21 @@ to the `reduce` function.
- `traj1::NamedTrajectory`: The first `NamedTrajectory` object.
- `traj2::NamedTrajectory`: The second `NamedTrajectory` object.
- `free_time::Bool=false`: Whether to construct a free time problem.
- `timestep_symbol::Symbol=:Δt`: The timestep symbol to use for free time problems.
- `timestep_name::Symbol=:Δt`: The timestep symbol to use for free time problems.
"""
function direct_sum(
traj1::NamedTrajectory,
traj2::NamedTrajectory;
free_time::Bool=false,
timestep_symbol::Symbol=:Δt,
timestep_name::Symbol=:Δt,
)
return direct_sum([traj1, traj2]; free_time=free_time, timestep_symbol=timestep_symbol)
return direct_sum([traj1, traj2]; free_time=free_time, timestep_name=timestep_name)
end

function direct_sum(
trajs::AbstractVector{<:NamedTrajectory};
free_time::Bool=false,
timestep_symbol::Symbol=:Δt,
timestep_name::Symbol=:Δt,
)
if length(trajs) < 2
throw(ArgumentError("At least two trajectories must be provided"))
Expand All @@ -122,13 +122,13 @@ function direct_sum(

# add timestep to components
if free_time
components = merge_outer(components, NamedTuple{(timestep_symbol,)}([get_timesteps(trajs[1])]))
components = merge_outer(components, NamedTuple{(timestep_name,)}([get_timesteps(trajs[1])]))
end

return NamedTrajectory(
components,
controls=merge_outer([traj.control_names for traj in trajs]),
timestep=free_time ? timestep_symbol : timestep,
timestep=free_time ? timestep_name : timestep,
bounds=merge_outer([traj.bounds for traj in trajs]),
initial=merge_outer([traj.initial for traj in trajs]),
final=merge_outer([traj.final for traj in trajs]),
Expand Down Expand Up @@ -670,21 +670,21 @@ end
pi_false_ops = PiccoloOptions(verbose=false, free_time=false)
pi_true_ops = PiccoloOptions(verbose=false, free_time=true)
suffix = "_new"
timestep_symbol = :Δt
timestep_name = :Δt

prob1 = UnitarySmoothPulseProblem(sys, GATES[:Y], T, Δt, piccolo_options=pi_false_ops, ipopt_options=ops)
traj1 = direct_sum(prob1.trajectory, add_suffix(prob1.trajectory, suffix), free_time=true)

# Direct sum (shared timestep name)
@test get_suffix(traj1, suffix).timestep == timestep_symbol
@test get_suffix(traj1, suffix, remove=true).timestep == timestep_symbol
@test get_suffix(traj1, suffix).timestep == timestep_name
@test get_suffix(traj1, suffix, remove=true).timestep == timestep_name

prob2 = UnitarySmoothPulseProblem(sys, GATES[:Y], T, Δt, ipopt_options=ops, piccolo_options=pi_true_ops)
traj2 = add_suffix(prob2.trajectory, suffix)

# Trajectory (unique timestep name)
@test get_suffix(traj2, suffix).timestep == add_suffix(timestep_symbol, suffix)
@test get_suffix(traj2, suffix, remove=true).timestep == timestep_symbol
@test get_suffix(traj2, suffix).timestep == add_suffix(timestep_name, suffix)
@test get_suffix(traj2, suffix, remove=true).timestep == timestep_name
end

end # module
Loading
Loading