Skip to content

Commit

Permalink
Make shifting Fields or local Detunings optional. Add tests. (#24)
Browse files Browse the repository at this point in the history
* Make shifting Fields or local Detunings optional. Add tests.

* Respond to PR comments
  • Loading branch information
ykharkov authored Aug 13, 2024
1 parent f06924c commit a9f942d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
28 changes: 22 additions & 6 deletions src/mps_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,26 @@ function parse_protocol(ahs_program, τ::Float64, n_τ_steps::Int)

time_points_Ω = ahs_program["hamiltonian"]["drivingFields"][1]["amplitude"]["time_series"]["times"]
values_Ω = ahs_program["hamiltonian"]["drivingFields"][1]["amplitude"]["time_series"]["values"]

n_atoms = length(ahs_program["setup"]["ahs_register"]["sites"])

pattern = ahs_program["hamiltonian"]["shiftingFields"][1]["magnitude"]["pattern"]
time_points_shift = ahs_program["hamiltonian"]["shiftingFields"][1]["magnitude"]["time_series"]["times"]
values_shift = ahs_program["hamiltonian"]["shiftingFields"][1]["magnitude"]["time_series"]["values"]
# Supporting both localDetuning and shiftingFields naming conventions
loc_detuning_key = if "shiftingFields" keys(ahs_program["hamiltonian"])
"shiftingFields"
elseif "localDetuning" keys(ahs_program["hamiltonian"])
"localDetuning"
end

# Check if local detuning or shifting fields were found, otherwise assign zeros
if !(loc_detuning_key === nothing)
values_local_detuning = ahs_program["hamiltonian"][loc_detuning_key][1]["magnitude"]["time_series"]["values"]
time_points_loc_detuning = ahs_program["hamiltonian"][loc_detuning_key][1]["magnitude"]["time_series"]["times"]
pattern = ahs_program["hamiltonian"][loc_detuning_key][1]["magnitude"]["pattern"]
else
time_points_loc_detuning = ["0", "4e-6"]
values_local_detuning = ["0", "0"]
pattern = ["0" for _ in 1:n_atoms]
end

# Convert strings to floats [parsing Braket AHS program]
time_points_Δ = parse.(Float64, time_points_Δ)
Expand All @@ -67,15 +83,15 @@ function parse_protocol(ahs_program, τ::Float64, n_τ_steps::Int)
values_Ω = parse.(Float64, values_Ω)

pattern = parse.(Float64, pattern)
time_points_shift = parse.(Float64, time_points_shift)
values_shift = parse.(Float64, values_shift)
time_points_loc_detuning = parse.(Float64, time_points_loc_detuning)
values_local_detuning = parse.(Float64, values_local_detuning)

# Define piecewise protocols
t_vals = [i/n_τ_steps*total_time for i in 1:n_τ_steps]
# Global detuning time series
Δ_glob_ts = [piecewise_protocol(t, time_points_Δ, values_Δ) for t in t_vals]
# Local detuning time series
Δ_loc_ts = [piecewise_protocol(t, time_points_shift, values_shift) for t in t_vals]
Δ_loc_ts = [piecewise_protocol(t, time_points_loc_detuning, values_local_detuning) for t in t_vals]
# Rabi driving field
Ω_ts = [piecewise_protocol(t, time_points_Ω, values_Ω) for t in t_vals]

Expand Down
33 changes: 29 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ using BraketAHS: run, save_results
using Test
using JSON3

@testset "BraketAHS.jl" begin
# Write your tests here.

@testset "BraketAHS.jl: Non-empty local detunings" begin
json_str = """{"setup":{"ahs_register":{"sites":[["0.0","0.0"],["5.499999999999999856993733449161965e-6","0.0"],["0.0","5.499999999999999856993733449161965e-6"],["5.499999999999999856993733449161965e-6","5.499999999999999856993733449161965e-6"]],"filling":[1,1,1,1]}},"braketSchemaHeader":{"name":"braket.ir.ahs.program","version":"1"},"hamiltonian":{"shiftingFields":[{"magnitude":{"pattern":["0.0","0.0","0.0","0.0"],"time_series":{"values":["0.0","0.0"],"times":["0.0","3.999999999999999818992447303545035e-6"]}}}],"drivingFields":[{"phase":{"pattern":"uniform","time_series":{"values":["0.0","0.0"],"times":["0.0","3.999999999999999818992447303545035e-6"]}},"detuning":{"pattern":"uniform","time_series":{"values":["-3.15e7","-3.15e7","3.15e7","3.15e7"],"times":["0.0","9.999999999999999547481118258862587e-8","3.899999999999999929396754527743951e-6","3.999999999999999818992447303545035e-6"]}},"amplitude":{"pattern":"uniform","time_series":{"values":["0.0","6.3e6","6.3e6","0.0"],"times":["0.0","9.999999999999999547481118258862587e-8","3.899999999999999929396754527743951e-6","3.999999999999999818992447303545035e-6"]}}}]}}
"""
ahs_json = JSON3.read(json_str)
Expand All @@ -30,5 +28,32 @@ using JSON3
mps_density = result["density"][:, end]
expected_density = [0.49558400861051327,0.4955839949350386,0.49558370707943783,0.49558323080865946]
@test isapprox(mps_density, expected_density; atol=1e-7)
# add apprx cmoparison isapprox(array1, array2; atol=tolerance)
end
@testset "BraketAHS.jl: Empty local detunings" begin
json_str = """{"setup":{"ahs_register":{"sites":[["0.0","0.0"],["5.499999999999999856993733449161965e-6","0.0"],["0.0","5.499999999999999856993733449161965e-6"],["5.499999999999999856993733449161965e-6","5.499999999999999856993733449161965e-6"]],"filling":[1,1,1,1]}},"braketSchemaHeader":{"name":"braket.ir.ahs.program","version":"1"},"hamiltonian":{"drivingFields":[{"phase":{"pattern":"uniform","time_series":{"values":["0.0","0.0"],"times":["0.0","3.999999999999999818992447303545035e-6"]}},"detuning":{"pattern":"uniform","time_series":{"values":["-3.15e7","-3.15e7","3.15e7","3.15e7"],"times":["0.0","9.999999999999999547481118258862587e-8","3.899999999999999929396754527743951e-6","3.999999999999999818992447303545035e-6"]}},"amplitude":{"pattern":"uniform","time_series":{"values":["0.0","6.3e6","6.3e6","0.0"],"times":["0.0","9.999999999999999547481118258862587e-8","3.899999999999999929396754527743951e-6","3.999999999999999818992447303545035e-6"]}}}]}}
"""
ahs_json = JSON3.read(json_str)
args = Dict(
"experiment-path" => "test/",
"program-path" => "",
"interaction-radius" => 7e-6,
"cutoff" => 1e-7,
"shots" => 1000,
"max-bond-dim" => 16,
"compute-truncation-error" => false,
"tau" => 0.01e-6,
"n-tau-steps" => 400,
"C6" => 5.42e-24,
"compute-correlators" => false,
"compute-energies" => false,
"generate-plots" => false
)
result = run(ahs_json, args)
mps_density = result["density"][:, end]
expected_density = [0.49558400861051327,0.4955839949350386,0.49558370707943783,0.49558323080865946]
@test isapprox(mps_density, expected_density; atol=1e-7)
end

0 comments on commit a9f942d

Please sign in to comment.