Skip to content

Commit

Permalink
fix: Stop using no longer exported methods and qualify LocalSimulator
Browse files Browse the repository at this point in the history
  • Loading branch information
kshyatt-aws committed Apr 25, 2024
1 parent 877faf2 commit e3a09f9
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 38 deletions.
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
16 changes: 8 additions & 8 deletions PyBraket/test/noise.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Test, PyBraket, Braket, Braket.IR, PythonCall
using PythonCall: Py, pyconvert, pyisTrue
using PythonCall: Py, pyconvert

@testset "Noise" begin
circ = CNot(H(Circuit(), 0), 0, 1)
noise = BitFlip(0.1)
circ = Braket.apply_gate_noise!(circ, noise)

device = LocalSimulator("braket_dm")
device = PyBraket.LocalSimulator("braket_dm")
# run the circuit on the local simulator
task = run(device, circ, shots=1000)

Expand Down Expand Up @@ -37,14 +37,14 @@ using PythonCall: Py, pyconvert, pyisTrue
n = noise(0.1)
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_noise, Py(ir_n)) == ir_n
end
@testset "(noise, ir_noise) = (PauliChannel, IR.PauliChannel)" begin
n = PauliChannel(0.1, 0.2, 0.1)
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.PauliChannel, Py(ir_n)) == ir_n
end
@testset "(noise, ir_noise) = (MultiQubitPauliChannel{1}, IR.MultiQubitPauliChannel)" begin
Expand All @@ -56,30 +56,30 @@ using PythonCall: Py, pyconvert, pyisTrue
n = TwoQubitPauliChannel(Dict("XX"=>0.1, "YY"=>0.2))
ir_n = Braket.ir(n, [0, 1])
py_n = Py(n)
@test pyisTrue(py_n.to_ir([0, 1]) == Py(ir_n))
@test pyconvert(Bool, py_n.to_ir([0, 1]) == Py(ir_n))
@test pyconvert(IR.MultiQubitPauliChannel, Py(ir_n)) == ir_n
end
@testset for (noise, ir_noise) in ((TwoQubitDephasing, IR.TwoQubitDephasing),
(TwoQubitDepolarizing, IR.TwoQubitDepolarizing))
n = noise(0.4)
ir_n = Braket.ir(n, [0, 1])
py_n = Py(n)
@test pyisTrue(py_n.to_ir([0, 1]) == Py(ir_n))
@test pyconvert(Bool, py_n.to_ir([0, 1]) == Py(ir_n))
@test pyconvert(ir_noise, Py(ir_n)) == ir_n
end
@testset "(noise, ir_noise) = (GeneralizedAmplitudeDamping, IR.GeneralizedAmplitudeDamping)" begin
n = GeneralizedAmplitudeDamping(0.1, 0.2)
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.GeneralizedAmplitudeDamping, Py(ir_n)) == ir_n
end
@testset "(noise, ir_noise) = (Kraus, IR.Kraus)" begin
mat = complex([0. 1.; 1. 0.])
n = Kraus([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.Kraus, Py(ir_n)) == ir_n
end
Braket.IRType[] = :OpenQASM
Expand Down

0 comments on commit e3a09f9

Please sign in to comment.