Skip to content

Commit

Permalink
Merge pull request #130 from kestrelquantum/dev_testing_and_readme
Browse files Browse the repository at this point in the history
Dev testing and readme
  • Loading branch information
andgoldschmidt authored Jun 24, 2024
2 parents cbb62d4 + 5889cfe commit ce85a34
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 99 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,41 @@ 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
using QuantumCollocation
using NamedTrajectories
using TrajectoryIndexingUtils

using LinearAlgebra
using CairoMakie
```

First, let's install a standard library packages. From the package manager and in the current environment (type `julia`, `]`, and `activate .`), you can type `add LinearAlgebra` to install and precompile _LinearAlgebra_. Same with `CairoMakie`. These are like Numpy and Matplotlib.

Second, let's install _Piccolo_. The first three packages (_QuantumCollocation_, _NamedTrajetories_, _TrajectoryIndexingUtils_) are the core of [Piccolo](https://docs.juliahub.com/General/Piccolo/stable/). We could do `add Piccolo` to get the three as a bundle from the Julia repository, which requires only `using Piccolo`.

As a developer, we want to use the git repositories directly from [our Github page](https://github.com/aarontrowbridge). Clone, then add to the Project file with e.g. `dev ../relative/path/to/repo/QuantumCollocation` to obtain a 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 your 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.

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
95 changes: 95 additions & 0 deletions src/integrators/exponential_integrators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,98 @@ end
return ∂ψ̃ₜℰ, ∂ψ̃ₜ₊₁ℰ, ∂aₜℰ
end
end

# ******************************************************************************* #

@testitem "testing UnitaryExponentialIntegrator" begin
using NamedTrajectories
using ForwardDiff

T = 100
H_drift = GATES[:Z]
H_drives = [GATES[:X], GATES[:Y]]
n_drives = length(H_drives)

system = QuantumSystem(H_drift, H_drives)

U_init = GATES[:I]
U_goal = GATES[:X]

Ũ⃗_init = operator_to_iso_vec(U_init)
Ũ⃗_goal = operator_to_iso_vec(U_goal)

dt = 0.1

Z = NamedTrajectory(
(
Ũ⃗ = unitary_geodesic(U_goal, T),
a = randn(n_drives, T),
da = randn(n_drives, T),
Δt = fill(dt, 1, T),
),
controls=(:da,),
timestep=:Δt,
goal=(Ũ⃗ = Ũ⃗_goal,)
)

= UnitaryExponentialIntegrator(system, :Ũ⃗, :a)


∂Ũ⃗ₜℰ, ∂Ũ⃗ₜ₊₁ℰ, ∂aₜℰ, ∂Δtₜℰ = jacobian(ℰ, Z[1].data, Z[2].data, Z)

∂ℰ_forwarddiff = ForwardDiff.jacobian(
zz -> (zz[1:Z.dim], zz[Z.dim+1:end], Z),
[Z[1].data; Z[2].data]
)

@test ∂Ũ⃗ₜℰ ∂ℰ_forwarddiff[:, 1:.dim]
@test ∂Ũ⃗ₜ₊₁ℰ ∂ℰ_forwarddiff[:, Z.dim .+ (1:.dim)]
@test ∂aₜℰ ∂ℰ_forwarddiff[:, Z.components.a]
@test ∂Δtₜℰ ∂ℰ_forwarddiff[:, Z.components.Δt]
end

@testitem "testing QuantumStateExponentialIntegrator" begin
using NamedTrajectories
using ForwardDiff

T = 100
H_drift = GATES[:Z]
H_drives = [GATES[:X], GATES[:Y]]
n_drives = length(H_drives)

system = QuantumSystem(H_drift, H_drives)

U_init = GATES[:I]
U_goal = GATES[:X]

ψ̃_init = ket_to_iso(quantum_state("g", [2]))
ψ̃_goal = ket_to_iso(quantum_state("e", [2]))

dt = 0.1

Z = NamedTrajectory(
(
ψ̃ = linear_interpolation(ψ̃_init, ψ̃_goal, T),
a = randn(n_drives, T),
da = randn(n_drives, T),
Δt = fill(dt, 1, T),
),
controls=(:da,),
timestep=:Δt,
goal=(ψ̃ = ψ̃_goal,)
)

= QuantumStateExponentialIntegrator(system, :ψ̃, :a)

∂ψ̃ₜℰ, ∂ψ̃ₜ₊₁ℰ, ∂aₜℰ, ∂Δtₜℰ = jacobian(ℰ, Z[1].data, Z[2].data, Z)

∂ℰ_forwarddiff = ForwardDiff.jacobian(
zz -> (zz[1:Z.dim], zz[Z.dim+1:end], Z),
[Z[1].data; Z[2].data]
)

@test ∂ψ̃ₜℰ ∂ℰ_forwarddiff[:, 1:.dim]
@test ∂ψ̃ₜ₊₁ℰ ∂ℰ_forwarddiff[:, Z.dim .+ (1:.dim)]
@test ∂aₜℰ ∂ℰ_forwarddiff[:, Z.components.a]
@test ∂Δtₜℰ ∂ℰ_forwarddiff[:, Z.components.Δt]
end
1 change: 1 addition & 0 deletions src/quantum_system_templates/_quantum_system_templates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ using ..QuantumUtils
using ..QuantumSystems

using LinearAlgebra
using TestItemRunner

include("transmons.jl")
include("rydberg.jl")
Expand Down
20 changes: 19 additions & 1 deletion src/quantum_system_templates/quantum_optics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,22 @@ function QuantumOpticsSystem(
H_drives;
constructor=QuantumOpticsSystem,
)
end
end

# ******************************************************************************* #

@testitem "Quantum Optics System" begin
using QuantumOpticsBase
N = rand(1:5);
T = ComplexF64;
b = FockBasis(N);
a = QuantumOpticsBase.create(T, b);
H = a + a';
sys = QuantumOpticsSystem(H, [H, H]);
@test typeof(sys) == QuantumSystem
@test sys.constructor == QuantumOpticsSystem
@test sys.H_drift == H.data

# creation with non-Hermitian operators
@test_broken QuantumOpticsSystem(a, [a])
end
92 changes: 0 additions & 92 deletions test/integrators_test.jl

This file was deleted.

7 changes: 1 addition & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,5 @@ using NamedTrajectories

include("test_utils.jl")

@testset "QuantumCollocation.jl" begin
# include("objectives_test.jl")
# include("dynamics_test.jl")
include("quantum_system_templates_test.jl")
end

# Run testitem
@run_package_tests

0 comments on commit ce85a34

Please sign in to comment.