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

Fix/ld runtime #19

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ The output .json file will be saved in `examples/ahs_program.json`.
1.B Generate .json using Python Braket SDK from the AHS program object:
```
ahs_ir = ahs_program.to_ir()
json_object = json.loads(ahs_ir.json())
json_data = json.loads(ahs_ir.json())
json_string = json.dumps(json_data, indent=4)
filename = "ahs_program.json"
with open(filename, "w") as json_file:
Expand Down
8 changes: 4 additions & 4 deletions src/mps_runner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ function parse_commandline()
"--compute-truncation-error"
help = "whether to compute the error induced by truncation at each step (computationally expensive)"
action = :store_true # default without this flag is false
"--tau"
help = "time evolution step size in seconds"
arg_type = Float64
default = 0.01e-6
# "--tau"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If tau is not used, should it instead be "supported" here but print that it is deprecated?

# help = "time evolution step size in seconds"
# arg_type = Float64
# default = 0.01e-6
"--n-tau-steps"
help = "number of time evolution steps to simulate"
arg_type = Int
Expand Down
42 changes: 26 additions & 16 deletions src/mps_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,45 @@ function piecewise_protocol(x, points, values)
return y
end

function parse_protocol(ahs_program, τ::Float64, n_τ_steps::Int)
function parse_protocol(ahs_program, n_τ_steps::Int)
# Define piecewise functions (protocols)
time_steps = collect(0:(n_τ_steps-1)) ./ n_τ_steps
total_time = n_τ_steps * τ
time_points_Δ = ahs_program["hamiltonian"]["drivingFields"][1]["detuning"]["time_series"]["times"]
values_Δ = ahs_program["hamiltonian"]["drivingFields"][1]["detuning"]["time_series"]["values"]

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

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"]
if "localDetuning" ∉ keys(ahs_program["hamiltonian"]) || length(ahs_program["hamiltonian"]["localDetuning"]) == 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will be pre-empted by @ykharkov's new PR

# Define an empty local detuning with zero pattern and zero values
filling = ahs_json["setup"]["ahs_register"]["filling"]
pattern = ["0" for _ in filling]
time_points_local_detuning = ["0.0", time_points_Ω[end]]
values_local_detuning = ["0.0", "0.0"]
else
pattern = ahs_program["hamiltonian"]["localDetuning"][1]["magnitude"]["pattern"]
time_points_local_detuning = ahs_program["hamiltonian"]["localDetuning"][1]["magnitude"]["time_series"]["times"]
values_local_detuning = ahs_program["hamiltonian"]["localDetuning"][1]["magnitude"]["time_series"]["values"]
end

# Convert strings to floats [parsing Braket AHS program]
time_points_Δ = parse.(Float64, time_points_Δ)
values_Δ = parse.(Float64, values_Δ)
time_points_Ω = parse.(Float64, time_points_Ω)
values_Ω = parse.(Float64, values_Ω)


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

# Define piecewise protocols
total_time = time_points_Δ[end]
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_local_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 All @@ -88,7 +97,9 @@ function parse_protocol(ahs_program, τ::Float64, n_τ_steps::Int)
rabi_driving=Ω_ts,
global_detuning=Δ_glob_ts,
local_detuning=Δ_loc_ts,
pattern=pattern)
pattern=pattern,
τ=total_time/n_τ_steps
)
end

function compute_energies(samples, Vij::Matrix{Float64}, Δ_glob_ts, Δ_loc_ts, pattern)
Expand All @@ -107,14 +118,15 @@ function compute_energies(samples, Vij::Matrix{Float64}, Δ_glob_ts, Δ_loc_ts,
end

"""
get_trotterized_circuit_2d(sites, τ, n_steps, N, Vij::Matrix{Float64})
get_trotterized_circuit_2d(sites, n_steps, N, Vij::Matrix{Float64})

Second order Trotterization circuit for a time-dependent Hamiltonian,
for a time step `τ` and `n_steps` total time steps, on `N` total atoms.
for a time step `n_steps` total time steps, on `N` total atoms.
`sites` defines the site indices in the MPS used to build the circuit.
Returns a `Vector{Vector{iTensor}}` list of gates at each time step.
"""
function get_trotterized_circuit_2d(sites, τ::Float64, n_steps::Int, N::Int, Vij::Matrix{Float64}, protocol)
function get_trotterized_circuit_2d(sites, n_steps::Int, N::Int, Vij::Matrix{Float64}, protocol)
τ = protocol[:τ]
circuit = Vector{Vector{ITensor}}(undef, n_steps)
for i_τ in 1:n_steps
two_site_gates = ITensor[]
Expand Down Expand Up @@ -172,7 +184,6 @@ Returns prepared experiment protocol `protocol` as a `NamedTuple` with keys
function parse_ahs_program(ahs_json, args::Dict{String, Any})
program_path = args["program-path"]
experiment_path = args["experiment-path"]
τ = args["tau"]
n_τ_steps = args["n-tau-steps"]
interaction_R = args["interaction-radius"]
C6 = args["C6"]
Expand Down Expand Up @@ -202,7 +213,7 @@ function parse_ahs_program(ahs_json, args::Dict{String, Any})
@debug "Atoms in atom array: $atom_coordinates"

Vij = get_Vij(atom_coordinates, N, interaction_R, C6)
protocol = parse_protocol(ahs_json, τ, n_τ_steps)
protocol = parse_protocol(ahs_json, n_τ_steps)
return Vij, protocol, N
end

Expand Down Expand Up @@ -255,7 +266,6 @@ end
function run(ahs_json, args)

experiment_path = args["experiment-path"]
τ = args["tau"]
n_τ_steps = args["n-tau-steps"]
C6 = args["C6"]
interaction_R = args["interaction-radius"]
Expand All @@ -268,7 +278,7 @@ function run(ahs_json, args)
# Initialize ψ to be a product state: Down state (Ground state)
ψ = MPS(s, n -> "Dn")
@info "Generating Trotterized circuit"
circuit = get_trotterized_circuit_2d(s, τ, n_τ_steps, N, Vij, protocol)
circuit = get_trotterized_circuit_2d(s, n_τ_steps, N, Vij, protocol)

max_bond_dim = args["max-bond-dim"]
cutoff = args["cutoff"]
Expand Down
3 changes: 1 addition & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using JSON3
@testset "BraketAHS.jl" begin
# Write your tests here.

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"]}}}]}}
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":{"localDetuning":[{"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 @@ -17,7 +17,6 @@ using JSON3
"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,
Expand Down
Loading