-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change: Use
BraketSimulator.jl
in examples, strip out unnecessary `…
…PyBraket.jl` components (#84)
- Loading branch information
1 parent
1d05da0
commit f357046
Showing
27 changed files
with
133 additions
and
1,388 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ numpy = "" | |
|
||
[pip.deps] | ||
amazon-braket-sdk = ">=1.70.0" | ||
amazon-braket-default-simulator = ">=1.10.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,43 @@ | ||
struct LocalQuantumTask | ||
id::String | ||
result::Braket.AbstractQuantumTaskResult | ||
end | ||
Braket.state(b::LocalQuantumTask) = "COMPLETED" | ||
Braket.id(b::LocalQuantumTask) = b.id | ||
Braket.result(b::LocalQuantumTask) = b.result | ||
|
||
|
||
struct LocalSimulator <: Braket.Device | ||
struct PyLocalSimulator <: Braket.AbstractBraketSimulator | ||
o::Py | ||
LocalSimulator() = new(local_sim.LocalSimulator()) | ||
LocalSimulator(backend::String) = new(local_sim.LocalSimulator(backend)) | ||
PyLocalSimulator() = new(local_sim.LocalSimulator()) | ||
PyLocalSimulator(backend::String) = new(local_sim.LocalSimulator(backend)) | ||
end | ||
Braket.name(ls::LocalSimulator) = pyconvert(String, ls.name) | ||
|
||
function Base.run(d::LocalSimulator, task_spec::Braket.IR.AHSProgram; shots::Int=0, kwargs...) | ||
(ls::PyLocalSimulator)(nq::Int, shots::Int) = ls | ||
Braket.name(ls::PyLocalSimulator) = pyconvert(String, ls.name) | ||
Braket.device_id(ls::PyLocalSimulator) = pyconvert(String, ls._delegate.DEVICE_ID) | ||
Braket.properties(ls::PyLocalSimulator) = ls.properties | ||
function Braket.simulate(d::PyLocalSimulator, task_spec::Braket.IR.AHSProgram; shots::Int=0, kwargs...) | ||
py_ir = Py(task_spec) | ||
py_raw_result = d._delegate.run(py_ir, shots; kwargs...) | ||
jl_raw_result = pyconvert(Braket.AnalogHamiltonianSimulationTaskResult, py_raw_result) | ||
res = Braket.format_result(jl_raw_result) | ||
id = res.task_metadata.id | ||
LocalQuantumTask(id, res) | ||
end | ||
Base.run(d::LocalSimulator, task_spec::Braket.AnalogHamiltonianSimulation; shots::Int=0, kwargs...) = run(d, ir(task_spec); shots=shots, kwargs...) | ||
|
||
function Base.run(d::LocalSimulator, task_spec::Braket.OpenQasmProgram; shots::Int=0, inputs::Dict{String, Float64}=Dict{String,Float64}(), kwargs...) | ||
py_inputs = pydict(pystr(k)=>v for (k,v) in inputs) | ||
py_ts = pyopenqasm.Program(source=pystr(task_spec.source), inputs=py_inputs) | ||
py_raw_result = d._delegate.run_openqasm(py_ts, shots, kwargs...) | ||
jl_result = pyconvert(Braket.GateModelTaskResult, py_raw_result) | ||
t_id = jl_result.taskMetadata.id | ||
res = Braket.format_result(jl_result) | ||
LocalQuantumTask(t_id, res) | ||
return pyconvert(Braket.AnalogHamiltonianSimulationTaskResult, py_raw_result) | ||
end | ||
Braket.simulate(d::PyLocalSimulator, task_spec::Braket.AnalogHamiltonianSimulation; shots::Int=0, kwargs...) = simulate(d, ir(task_spec); shots=shots, kwargs...) | ||
|
||
function Base.run(d::LocalSimulator, task_spec::PyCircuit; shots::Int=0, inputs::Dict{String, Float64}=Dict{String,Float64}(), kwargs...) | ||
jaqcd_ir = task_spec.to_ir(ir_type=circuit.serialization.IRType.JAQCD) | ||
py_raw_result = d._delegate.run(jaqcd_ir, task_spec.qubit_count, shots, kwargs...) | ||
jl_result = pyconvert(Braket.GateModelTaskResult, py_raw_result) | ||
t_id = jl_result.taskMetadata.id | ||
res = Braket.format_result(jl_result) | ||
LocalQuantumTask(t_id, res) | ||
function Braket._run_internal(simulator::PyLocalSimulator, task_spec::AnalogHamiltonianSimulation, args...; kwargs...) | ||
raw_py_result = simulator._run_internal(Py(ir(task_spec)), args...; kwargs...) | ||
jl_task_metadata = pyconvert(Braket.TaskMetadata, raw_py_result.task_metadata) | ||
jl_measurements = map(raw_py_result.measurements) do m | ||
jl_status = pyconvert(String, pystr(m.status)) | ||
status = if jl_status == "Success" | ||
Braket.success | ||
elseif jl_status == "Partial_success" | ||
Braket.partial_success | ||
else | ||
Braket.failure | ||
end | ||
Braket.ShotResult(status, | ||
pyconvert(Any, m.pre_sequence), | ||
pyconvert(Any, m.post_sequence) | ||
) | ||
end | ||
return Braket.AnalogHamiltonianSimulationQuantumTaskResult(jl_task_metadata, jl_measurements) | ||
end | ||
Base.run(d::LocalSimulator, task_spec::Circuit; kwargs...) = run(d, PyCircuit(task_spec); kwargs...) | ||
|
||
(ls::LocalSimulator)(task_spec; kwargs...) = run(ls, task_spec; kwargs...) | ||
(ls::PyLocalSimulator)(task_spec; kwargs...) = simulate(ls, task_spec; kwargs...) | ||
|
||
Py(d::LocalSimulator) = getfield(d, :o) | ||
Base.getproperty(d::LocalSimulator, s::Symbol) = getproperty(Py(d), s) | ||
Base.getproperty(d::LocalSimulator, s::AbstractString) = getproperty(Py(d), s) | ||
Base.setproperty!(d::LocalSimulator, s::Symbol, x) = setproperty!(Py(d), s, x) | ||
Base.setproperty!(d::LocalSimulator, s::AbstractString, x) = setproperty!(Py(d), s, x) | ||
Py(d::PyLocalSimulator) = getfield(d, :o) | ||
Base.getproperty(d::PyLocalSimulator, s::Symbol) = getproperty(Py(d), s) | ||
Base.getproperty(d::PyLocalSimulator, s::AbstractString) = getproperty(Py(d), s) | ||
Base.setproperty!(d::PyLocalSimulator, s::Symbol, x) = setproperty!(Py(d), s, x) | ||
Base.setproperty!(d::PyLocalSimulator, s::AbstractString, x) = setproperty!(Py(d), s, x) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.