Skip to content

Commit

Permalink
feature: Local simulator support (#72)
Browse files Browse the repository at this point in the history
feature: Initial implementation of pure Julia local statevector simulator
  • Loading branch information
kshyatt-aws authored Apr 25, 2024
1 parent 0cd6f5c commit 368675b
Show file tree
Hide file tree
Showing 28 changed files with 593 additions and 253 deletions.
18 changes: 10 additions & 8 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 <[email protected]>"]
version = "0.8.2"
version = "0.8.3"

[deps]
AWS = "fbe9abb3-538b-5e4e-ba9e-bc94f4f92ebc"
Expand All @@ -26,6 +26,7 @@ Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
NamedTupleTools = "d9ec5142-1e00-5aa0-9d6a-321866360f50"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StructTypes = "856f2bd8-1eba-4b0a-8007-ebc267875bd4"
Tar = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
Expand All @@ -35,19 +36,19 @@ UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
AWS = "=1.90.4"
AWSS3 = "=0.11.2"
Aqua = "=0.8"
Base64 = "1.6"
AxisArrays = "=0.4.7"
CSV = "=0.10.13"
Base64 = "1.6"
CSV = "=0.10.14"
CodeTracking = "=1.3.5"
Compat = "=4.14.0"
DataStructures = "=0.18.18"
DataStructures = "=0.18.20"
Dates = "1.6"
DecFP = "=1.3.2"
Distributions = "=0.25.76"
Distributed = "1.6"
Distributions = "=0.25.76"
Downloads = "1"
Graphs = "=1.9.0"
HTTP = "=1.10.3"
Graphs = "=1.10.0"
HTTP = "=1.10.6"
InteractiveUtils = "1.6"
JLD2 = "=0.4.46"
JSON3 = "=1.14.0"
Expand All @@ -59,8 +60,9 @@ NamedTupleTools = "=0.14.3"
OrderedCollections = "=1.6.3"
Pkg = "1.6"
Random = "1.6"
Statistics = "1.6"
SparseArrays = "1.6"
StaticArrays = "=1.9.3"
Statistics = "1.6"
StructTypes = "=1.10.0"
Tar = "1.9.3"
Test = "1.6"
Expand Down
4 changes: 2 additions & 2 deletions PyBraket/CondaPkg.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[deps]
python = ">=3.7,<=3.9"
python = ">=3"
pydantic = ""
scipy = ""
numpy = ""

[pip.deps]
amazon-braket-sdk = ">=1.35.0"
amazon-braket-sdk = ">=1.70.0"
8 changes: 4 additions & 4 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 <[email protected]>"]
version = "0.8.2"
version = "0.8.3"

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

[compat]
Aqua = "=0.8"
Braket = "=0.8.2"
Braket = "=0.8.3"
CondaPkg = "=0.2.22"
DataStructures = "=0.18.18"
DataStructures = "=0.18.20"
LinearAlgebra = "1.6"
PythonCall = "=0.9.15"
PythonCall = "=0.9.19"
Statistics = "1"
StructTypes = "=1.10.0"
Test = "1.6"
Expand Down
2 changes: 1 addition & 1 deletion PyBraket/src/py_type_conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function jl_convert_attr(n, t, attr)
return pyconvert(t, attr)
end
else
PythonCall.pyisnone(attr) && return nothing
PythonCall.pyis(attr, PythonCall.pybuiltins.None) && return nothing
return union_convert(t, attr)
end
end
Expand Down
2 changes: 1 addition & 1 deletion PyBraket/src/pycircuit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mutable struct PyCircuit
end
(c::PyCircuit)(arg::Number; kwargs...) = PyCircuit(Py(c)(arg; kwargs...))
(c::PyCircuit)(; kwargs...) = PyCircuit(Py(c)(; kwargs...))
Base.:(==)(c1::PyCircuit, c2::PyCircuit) = PythonCall.pyisTrue(Py(c1) == Py(c2))
Base.:(==)(c1::PyCircuit, c2::PyCircuit) = pyconvert(Bool, Py(c1) == Py(c2))
Py(c::PyCircuit) = getfield(c, :o)
Base.getproperty(c::PyCircuit, s::Symbol) = pyconvert(Any, getproperty(Py(c), s))
Base.getproperty(c::PyCircuit, s::AbstractString) = pyconvert(Any, getproperty(Py(c), s))
Expand Down
4 changes: 2 additions & 2 deletions PyBraket/test/ahs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ using Braket: AtomArrangement, AtomArrangementItem, TimeSeries, DrivingField, Aw
drive = DrivingField(Ω, ϕ, Δ)
ahs_program = AnalogHamiltonianSimulation(register, drive)

ahs_local = LocalSimulator("braket_ahs")
ahs_local = PyBraket.LocalSimulator("braket_ahs")
local_result = result(run(ahs_local, ahs_program, shots=1_000))
@test length(local_result.measurements) == 1_000

Expand Down Expand Up @@ -82,7 +82,7 @@ end
drive = DrivingField(Ω, ϕ, Δ)
ahs_program = AnalogHamiltonianSimulation(register, drive)

ahs_local = LocalSimulator("braket_ahs")
ahs_local = PyBraket.LocalSimulator("braket_ahs")
local_result = result(run(ahs_local, ahs_program, shots=1_000))

g_count = 0
Expand Down
30 changes: 15 additions & 15 deletions PyBraket/test/circuits.jl
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
using Braket, PyBraket, Test, LinearAlgebra, PythonCall
using PythonCall: pyconvert, Py, pyisTrue, pyisinstance
using PythonCall: pyconvert, Py, pyisinstance
@testset "PyBraket circuits" begin
@testset for ir_type in (:JAQCD, :OpenQASM)
Braket.IRType[] = ir_type
@testset "Expectation" begin
c = Circuit([(H, 0), (CNot, 0, 1), (Expectation, Braket.Observables.Z(), 0)])
dev = LocalSimulator()
dev = PyBraket.LocalSimulator()
braket_task = run(dev, c, shots=0)
res = result(braket_task)
@test res.values[1] 0.0 atol=1e-12
end

@testset "Variance" begin
c = Circuit() |> (ci->H(ci, 0)) |> (ci->CNot(ci, 0, 1)) |> (ci->Variance(ci, Braket.Observables.Z(), 0))
dev = LocalSimulator()
dev = PyBraket.LocalSimulator()
braket_task = run(dev, c, shots=0)
res = result(braket_task)
@test res.values[1] 1.0 atol=1e-12
end

@testset "Probability" begin
c = Circuit() |> (ci->H(ci, 0)) |> (ci->CNot(ci, 0, 1)) |> (ci->Probability(ci))
dev = LocalSimulator()
dev = PyBraket.LocalSimulator()
braket_task = run(dev, c, shots=0)
res = result(braket_task)
@test res.values[1] [0.5, 0.0, 0.0, 0.5] atol=1e-12
end

@testset "DensityMatrix" begin
c = Circuit() |> (ci->H(ci, 0)) |> (ci->CNot(ci, 0, 1)) |> (ci->DensityMatrix(ci))
dev = LocalSimulator()
dev = PyBraket.LocalSimulator()
braket_task = run(dev, c, shots=0)
res = result(braket_task)
ρ = zeros(ComplexF64, 4, 4)
Expand All @@ -44,7 +44,7 @@ using PythonCall: pyconvert, Py, pyisTrue, pyisinstance

@testset "Results type" begin
c = Circuit() |> (ci->H(ci, 0)) |> (ci->CNot(ci, 0, 1)) |> (ci->Expectation(ci, Braket.Observables.Z(), 0))
dev = LocalSimulator()
dev = PyBraket.LocalSimulator()
braket_task = run(dev, c, shots=0)
res = result(braket_task)
@test res.values[1] 0.0 atol=1e-12
Expand All @@ -53,7 +53,7 @@ using PythonCall: pyconvert, Py, pyisTrue, pyisinstance

@testset "Observable on all qubits" begin
c = Circuit() |> (ci->H(ci, 0)) |> (ci->CNot(ci, 0, 1)) |> (ci->Expectation(ci, Braket.Observables.Z()))
dev = LocalSimulator()
dev = PyBraket.LocalSimulator()
res = result(run(dev, c, shots=0))
@test pyconvert(Vector{Float64}, res.values[1]) [0.0, 0.0] atol=1e-12
end
Expand All @@ -66,7 +66,7 @@ using PythonCall: pyconvert, Py, pyisTrue, pyisinstance
Unitary(c, [0, 1, 2], ccz_mat)
H(c, [0, 1, 2])
StateVector(c)
dev = LocalSimulator()
dev = PyBraket.LocalSimulator()
braket_task = run(dev, c, shots=0)
res = result(braket_task)
@test res.values[1] ComplexF64[0.75, 0.25, 0.25, -0.25, 0.25, -0.25, -0.25, 0.25] atol=1e-12
Expand All @@ -93,35 +93,35 @@ using PythonCall: pyconvert, Py, pyisTrue, pyisinstance
obs = rt(o, [0])
py_obs = Py(obs)
ir_obs = ir(obs)
@test pyisTrue(py_obs.to_ir() == Py(ir_obs))
@test pyconvert(Bool, py_obs.to_ir() == Py(ir_obs))
@test pyconvert(ir_rt, Py(ir_obs)) == ir_obs
end
@testset for (rt, ir_rt) in ((Braket.Probability, Braket.IR.Probability),
(Braket.DensityMatrix, Braket.IR.DensityMatrix))
obs = rt([0])
py_obs = Py(obs)
ir_obs = ir(obs)
@test pyisTrue(py_obs.to_ir() == Py(ir_obs))
@test pyconvert(Bool, py_obs.to_ir() == Py(ir_obs))
@test pyconvert(ir_rt, Py(ir_obs)) == ir_obs

obs = rt()
py_obs = Py(obs)
ir_obs = ir(obs)
@test pyisTrue(py_obs.to_ir() == Py(ir_obs))
@test pyconvert(Bool, py_obs.to_ir() == Py(ir_obs))
@test pyconvert(ir_rt, Py(ir_obs)) == ir_obs
end
@testset "(rt, ir_rt) = (Amplitude, IR.Amplitude)" begin
obs = Braket.Amplitude(["0000"])
py_obs = Py(obs)
ir_obs = ir(obs)
@test pyisTrue(py_obs.to_ir() == Py(ir_obs))
@test pyconvert(Bool, py_obs.to_ir() == Py(ir_obs))
@test pyconvert(Braket.IR.Amplitude, Py(ir_obs)) == ir_obs
end
@testset "(rt, ir_rt) = (StateVector, IR.StateVector)" begin
obs = Braket.StateVector()
py_obs = Py(obs)
ir_obs = ir(obs)
@test pyisTrue(py_obs.to_ir() == Py(ir_obs))
@test pyconvert(Bool, py_obs.to_ir() == Py(ir_obs))
@test pyconvert(Braket.IR.StateVector, Py(ir_obs)) == ir_obs
end
@testset "HermitianObservable and TensorProduct" begin
Expand All @@ -136,7 +136,7 @@ using PythonCall: pyconvert, Py, pyisTrue, pyisinstance
o = 3.0 * Braket.Observables.Z()
py_obs = Py(o)
@test pyisinstance(py_obs, PyBraket.braketobs.Z)
@test pyisTrue(py_obs.coefficient == 3.0)
@test pyconvert(Bool, py_obs.coefficient == 3.0)
end
@testset "Sum" begin
m = [1. -im; im -1.]
Expand Down Expand Up @@ -168,7 +168,7 @@ using PythonCall: pyconvert, Py, pyisTrue, pyisinstance
py_c2 = PyCircuit(non_para_circ)
@test py_c2 == py_c1(1.0)
@testset "running with inputs" begin
dev = LocalSimulator()
dev = PyBraket.LocalSimulator()
oq3_circ = ir(circ, Val(:OpenQASM))
braket_task = run(dev, oq3_circ, shots=0, inputs=Dict(string(α)=>1.0, string(θ)=>2.0))
res = result(braket_task)
Expand Down
14 changes: 7 additions & 7 deletions PyBraket/test/gates.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Test, PyBraket, Braket, Braket.IR, PythonCall
using PythonCall: Py, pyconvert, pyisTrue
using PythonCall: Py, pyconvert

using Braket: I

Expand All @@ -20,7 +20,7 @@ using Braket: I
g = gate()
ir_g = Braket.ir(g, 0)
py_g = Py(g)
@test pyisTrue(py_g.to_ir([0]) == Py(ir_g))
@test pyconvert(Bool, py_g.to_ir([0]) == Py(ir_g))
@test pyconvert(ir_gate, Py(ir_g)) == ir_g
end
@testset for (gate, ir_gate) in ((PhaseShift, IR.PhaseShift),
Expand All @@ -32,7 +32,7 @@ using Braket: I
g = gate(angle)
ir_g = Braket.ir(g, 0)
py_g = Py(g)
@test pyisTrue(py_g.to_ir([0]) == Py(ir_g))
@test pyconvert(Bool, py_g.to_ir([0]) == Py(ir_g))
@test pyconvert(ir_gate, Py(ir_g)) == ir_g
end
@testset for (gate, ir_gate) in ((CNot, IR.CNot),
Expand All @@ -46,7 +46,7 @@ using Braket: I
g = gate()
ir_g = Braket.ir(g, [0, 1])
py_g = Py(g)
@test pyisTrue(py_g.to_ir([0, 1]) == Py(ir_g))
@test pyconvert(Bool, py_g.to_ir([0, 1]) == Py(ir_g))
@test pyconvert(ir_gate, Py(ir_g)) == ir_g
end
@testset for (gate, ir_gate) in ((CPhaseShift, IR.CPhaseShift),
Expand All @@ -62,22 +62,22 @@ using Braket: I
g = gate(angle)
ir_g = Braket.ir(g, [0, 1])
py_g = Py(g)
@test pyisTrue(py_g.to_ir([0, 1]) == Py(ir_g))
@test pyconvert(Bool, py_g.to_ir([0, 1]) == Py(ir_g))
@test pyconvert(ir_gate, Py(ir_g)) == ir_g
end
@testset for (gate, ir_gate) in ((CCNot, IR.CCNot), (CSwap, IR.CSwap))
g = gate()
ir_g = Braket.ir(g, [0, 1, 2])
py_g = Py(g)
@test pyisTrue(py_g.to_ir([0, 1, 2]) == Py(ir_g))
@test pyconvert(Bool, py_g.to_ir([0, 1, 2]) == Py(ir_g))
@test pyconvert(ir_gate, Py(ir_g)) == ir_g
end
@testset "(gate, ir_gate) = (Unitary, IR.Unitary)" begin
mat = complex([0. 1.; 1. 0.])
n = Unitary(mat)
ir_n = Braket.ir(n, 0)
py_n = Py(n)
@test pyisTrue(py_n.to_ir([0]) == Py(ir_n))
@test pyconvert(Bool, py_n.to_ir([0]) == Py(ir_n))
@test pyconvert(IR.Unitary, Py(ir_n)) == ir_n
end
Braket.IRType[] = :OpenQASM
Expand Down
8 changes: 4 additions & 4 deletions PyBraket/test/integ_tests/local_braket_simulator.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Braket, Braket.Observables, LinearAlgebra, PyBraket, Statistics, Test
using Braket: I

PURE_DEVICE = LocalSimulator()
NOISE_DEVICE = LocalSimulator("braket_dm")
PURE_DEVICE = PyBraket.LocalSimulator()
NOISE_DEVICE = PyBraket.LocalSimulator("braket_dm")
SHOTS = 8000

get_tol(shots::Int) = return (shots > 0 ? Dict("atol"=> 0.1, "rtol"=>0.15) : Dict("atol"=>0.01, "rtol"=>0))
Expand All @@ -29,7 +29,7 @@ end
@testset for (backend, device_name) in [("default", "StateVectorSimulator"),
("braket_sv", "StateVectorSimulator"),
("braket_dm", "DensityMatrixSimulator")]
local_simulator_device = LocalSimulator(backend)
local_simulator_device = PyBraket.LocalSimulator(backend)
@test name(local_simulator_device) == device_name
end
@testset for DEVICE in (PURE_DEVICE, NOISE_DEVICE)
Expand Down Expand Up @@ -357,4 +357,4 @@ end
end
end
end
end
end
Loading

4 comments on commit 368675b

@kshyatt-aws
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/105612

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.3 -m "<description of version>" 368675b8937f63feebd9ff04a51a72db7de19a24
git push origin v0.8.3

@kshyatt-aws
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register subdir=PyBraket

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/105613

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a PyBraket-v0.8.3 -m "<description of version>" 368675b8937f63feebd9ff04a51a72db7de19a24
git push origin PyBraket-v0.8.3

Please sign in to comment.