Skip to content

Commit

Permalink
Merge branch 'amazon-braket:main' into LocalDetuning
Browse files Browse the repository at this point in the history
  • Loading branch information
Fe-r-oz committed Jun 21, 2024
2 parents c215b53 + a389d80 commit eba9e13
Show file tree
Hide file tree
Showing 31 changed files with 156 additions and 1,426 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
# don't run on draft PRs
if: ${{ github.event_name == 'push' || !github.event.pull_request.draft }}
# allow windows to fail
continue-on-error: ${{ matrix.os == 'windows-latest' }}
continue-on-error: ${{ matrix.os == 'windows-latest' || matrix.group == 'PyBraket-unit' }}
strategy:
fail-fast: true
max-parallel: 2
Expand Down Expand Up @@ -61,7 +61,7 @@ jobs:
# don't run on draft PRs
if: ${{ github.event_name == 'push' || !github.event.pull_request.draft }}
# allow failures on nightly or beta Julia
continue-on-error: ${{ matrix.version == 'nightly'}}
continue-on-error: ${{ matrix.version == 'nightly' || matrix.group == 'PyBraket-unit' }}
strategy:
fail-fast: true
max-parallel: 2
Expand Down
10 changes: 5 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Braket"
uuid = "19504a0f-b47d-4348-9127-acc6cc69ef67"
authors = ["Katharine Hyatt <hyatkath@amazon.com>"]
version = "0.9.0"
version = "0.9.1"

[deps]
AWS = "fbe9abb3-538b-5e4e-ba9e-bc94f4f92ebc"
Expand Down Expand Up @@ -47,10 +47,10 @@ DecFP = "=1.3.2"
Distributed = "1.6"
Distributions = "=0.25.76"
Downloads = "1"
Graphs = "=1.11.0"
HTTP = "=1.10.6"
Graphs = "=1.11.1"
HTTP = "=1.10.8"
InteractiveUtils = "1.6"
JLD2 = "=0.4.46"
JLD2 = "=0.4.48"
JSON3 = "=1.14.0"
LinearAlgebra = "1.6"
Logging = "1.6"
Expand All @@ -61,7 +61,7 @@ OrderedCollections = "=1.6.3"
Pkg = "1.6"
Random = "1.6"
SparseArrays = "1.6"
StaticArrays = "=1.9.3"
StaticArrays = "=1.9.5"
Statistics = "1.6"
StructTypes = "=1.10.0"
Tar = "1.9.3"
Expand Down
7 changes: 5 additions & 2 deletions PyBraket/CondaPkg.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
[deps]
python = ">=3"
python = ">=3.9,<=3.11"
pydantic = ""
scipy = ""
numpy = ""

[pip.deps]
amazon-braket-sdk = ">=1.70.0"
amazon-braket-sdk = ">=1.81.0"
amazon-braket-default-simulator = ">=1.10.0"
urllib3 = "<2"
botocore = ">=1.34"
4 changes: 2 additions & 2 deletions PyBraket/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PyBraket"
uuid = "e85266a6-1825-490b-a80e-9b9469c53660"
authors = ["Katharine Hyatt <hyatkath@amazon.com>"]
version = "0.9.0"
version = "0.9.1"

[deps]
Braket = "19504a0f-b47d-4348-9127-acc6cc69ef67"
Expand All @@ -14,7 +14,7 @@ StructTypes = "856f2bd8-1eba-4b0a-8007-ebc267875bd4"

[compat]
Aqua = "=0.8"
Braket = "=0.9.0"
Braket = "=0.9.1"
CondaPkg = "=0.2.22"
DataStructures = "=0.18.20"
LinearAlgebra = "1.6"
Expand Down
7 changes: 3 additions & 4 deletions PyBraket/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

**PyBraket.jl is not an officially supported AWS product.**

This package provides Julia-Python interoperability between `Braket.jl` and Python features of the Amazon Braket SDK, such as the Amazon Braket [Local Simulators](https://docs.aws.amazon.com/braket/latest/developerguide/braket-send-to-local-simulator.html) and
[Local Jobs](https://docs.aws.amazon.com/braket/latest/developerguide/braket-jobs-local-mode.html).
This package provides Julia-Python interoperability between `Braket.jl` and Python features of the Amazon Braket SDK, such as the Amazon Braket [Local Simulators](https://docs.aws.amazon.com/braket/latest/developerguide/braket-send-to-local-simulator.html) and support for unpickling the [results of Braket Jobs](https://docs.aws.amazon.com/braket/latest/developerguide/braket-jobs-save-results.html).

This is *experimental* software, and support may be discontinued in the future. For a fully supported SDK, please use
the [Python SDK](https://github.com/aws/amazon-braket-sdk-python). We may change, remove, or deprecate parts of the API when making new releases.
Please review the [CHANGELOG](CHANGELOG.md) for information about changes in each release.

## Installation & Prerequisites

You'll need [`PythonCall.jl`](https://cjdoris.github.io/PythonCall.jl).
You'll need [`PythonCall.jl`](https://cjdoris.github.io/PythonCall.jl) installed to use this package.

### Use `CondaPkg.jl`

`PyBraket.jl` now supports [`CondaPkg.jl`](https://github.com/cjdoris/CondaPkg.jl)! The package and its tests come with `CondaPkg.toml` files ready to go.
`CondaPkg` will install all necessary dependencies for you.
`CondaPkg.jl` will install all necessary dependencies for you.

### Use native Python installation

Expand Down
31 changes: 4 additions & 27 deletions PyBraket/src/PyBraket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,28 @@ module PyBraket
import Braket: Instruction
using Braket.IR
import Braket.IR: TimeSeries, AtomArrangement, DrivingField, PhysicalField, ShiftingField, Setup, Hamiltonian, AHSProgram
export LocalSimulator, PyCircuit
export PyLocalSimulator
import PythonCall: Py

const awsbraket = PythonCall.pynew()
const braketobs = PythonCall.pynew()
const local_sim = PythonCall.pynew()
const default_sim = PythonCall.pynew()
const tasks = PythonCall.pynew()
const circuit = PythonCall.pynew()
const pyjaqcd = PythonCall.pynew()
const pyahs = PythonCall.pynew()
const pyopenqasm = PythonCall.pynew()
const pygates = PythonCall.pynew()
const pynoises = PythonCall.pynew()
const collections = PythonCall.pynew()

function __init__()
# must set these when this code is actually loaded
PythonCall.pycopy!(awsbraket, pyimport("braket.aws"))
PythonCall.pycopy!(braketobs, pyimport("braket.circuits.observables"))
PythonCall.pycopy!(local_sim, pyimport("braket.devices.local_simulator"))
PythonCall.pycopy!(circuit, pyimport("braket.circuits"))
PythonCall.pycopy!(pygates, pyimport("braket.circuits.gates"))
PythonCall.pycopy!(pynoises, pyimport("braket.circuits.noises"))
PythonCall.pycopy!(pyjaqcd, pyimport("braket.ir.jaqcd"))
PythonCall.pycopy!(default_sim, pyimport("braket.default_simulator"))
PythonCall.pycopy!(pyopenqasm, pyimport("braket.ir.openqasm"))
PythonCall.pycopy!(pyahs, pyimport("braket.ir.ahs"))
PythonCall.pycopy!(tasks, pyimport("braket.tasks"))
PythonCall.pycopy!(collections, pyimport("collections"))
PythonCall.pyconvert_add_rule("collections:Counter", Accumulator, counter_to_acc)
Braket._simulator_devices[]["braket_ahs"] = PyLocalSimulator("braket_ahs")
end
function counter_to_acc(::Type{Accumulator}, x::Py)
return counter((pyconvert(Any, el) for el in x.elements()))
Expand Down Expand Up @@ -64,24 +57,8 @@ module PyBraket
end
return args
end

for (irT, pyT) in ((:(Braket.IR.Expectation), :(pyjaqcd.Expectation)),
(:(Braket.IR.Variance), :(pyjaqcd.Variance)),
(:(Braket.IR.Sample), :(pyjaqcd.Sample)),
(:(Braket.IR.Amplitude), :(pyjaqcd.Amplitude)),
(:(Braket.IR.StateVector), :(pyjaqcd.StateVector)),
(:(Braket.IR.Probability), :(pyjaqcd.Probability)),
(:(Braket.IR.DensityMatrix), :(pyjaqcd.DensityMatrix)))
@eval begin
Py(o::$irT) = $pyT(;arg_gen(o, fieldnames($irT))...)
end
end

include("pyahs.jl")
include("pygates.jl")
include("pynoises.jl")
include("pyschema.jl")
using .PySchema
include("pycircuit.jl")
include("local_simulator.jl")
end
78 changes: 34 additions & 44 deletions PyBraket/src/local_simulator.jl
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)
49 changes: 0 additions & 49 deletions PyBraket/src/pycircuit.jl

This file was deleted.

84 changes: 0 additions & 84 deletions PyBraket/src/pygates.jl

This file was deleted.

Loading

0 comments on commit eba9e13

Please sign in to comment.