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

change: Support PRx native gate #12

Merged
merged 2 commits into from
May 9, 2024
Merged
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 .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ jobs:
with:
version: '1'
- uses: julia-actions/cache@v1
- uses: julia-actions/julia-buildpkg@v1 # forces PkgServer refresh
- name: Configure doc environment
shell: julia --project=docs --color=yes {0}
run: |
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ BraketSimulatorPythonExt = "PythonCall"

[compat]
Aqua = "=0.8"
Braket = "=0.8.3"
Braket = "=0.9.0"
Combinatorics = "=1.0.2"
Dates = "1.6"
JSON3 = "=1.14.0"
Expand Down
1 change: 1 addition & 0 deletions ext/BraketSimulatorPythonExt/BraketSimulatorPythonExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function __init__()
PythonCall.pyconvert_add_rule("braket.default_simulator.gate_operations:MS", Instruction, jl_convert_sim_ms)
PythonCall.pyconvert_add_rule("braket.default_simulator.gate_operations:GPi", Instruction, jl_convert_sim_gpi)
PythonCall.pyconvert_add_rule("braket.default_simulator.gate_operations:GPi2", Instruction, jl_convert_sim_gpi2)
PythonCall.pyconvert_add_rule("braket.default_simulator.gate_operations:PRx", Instruction, jl_convert_sim_prx)
PythonCall.pyconvert_add_rule("braket.default_simulator.gate_operations:S", Instruction, jl_convert_sim_s)
PythonCall.pyconvert_add_rule("braket.default_simulator.gate_operations:Si", Instruction, jl_convert_sim_si)
PythonCall.pyconvert_add_rule("braket.default_simulator.gate_operations:V", Instruction, jl_convert_sim_v)
Expand Down
7 changes: 7 additions & 0 deletions ext/BraketSimulatorPythonExt/translation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,13 @@ for (fn, jl_typ) in ((:jl_convert_sim_gpi, :GPi),
end
end
end
function jl_convert_sim_prx(t::Type{Braket.Instruction}, x::Py)
angle = (pyconvert(Union{Float64, FreeParameter}, getproperty(x, :_angle_1)),
pyconvert(Union{Float64, FreeParameter}, getproperty(x, :_angle_2)),
)
jl_operation = _handle_modifiers(PRx(angle), x)
PythonCall.pyconvert_return(t(jl_operation, convert_targets(x.targets)))
end
for (fn, jl_typ, a1, a2, a3) in ((:jl_convert_sim_ms, :MS, "_angle_1", "_angle_2", "_angle_3"),
(:jl_convert_sim_u, :U, "_theta", "_phi", "_lambda"),
)
Expand Down
6 changes: 6 additions & 0 deletions src/gate_kernels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ matrix_rep(g::CZ) = SMatrix{4,4}(complex([1.0 0.0 0.0 0.0; 0.0 1.0 0.0 0.0; 0.0
matrix_rep(g::CV) = SMatrix{4,4}([1.0 0.0 0.0 0.0; 0.0 1.0 0.0 0.0; 0.0 0.0 0.5+0.5im 0.5-0.5im; 0.0 0.0 0.5-0.5im 0.5+0.5im])
matrix_rep(g::CCNot) = SMatrix{8,8}(complex([1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0; 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0; 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0]))
matrix_rep(g::CSwap) = SMatrix{8,8}(complex([1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0; 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0; 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0]))
matrix_rep(g::PRx) = SMatrix{2,2}(
[
cos(g.angle[1] / 2.0) -im*exp(-im*g.angle[2])*sin(g.angle[1]/2.0)
-im*exp(im*g.angle[2])*sin(g.angle[1]/2.0) cos(g.angle[1] / 2.0)
],
)
matrix_rep(g::Rz) =
SMatrix{2,2}([exp(-im * g.angle[1] / 2.0) 0.0; 0.0 exp(im * g.angle[1] / 2.0)])
matrix_rep(g::Rx) = SMatrix{2,2}(
Expand Down
1 change: 1 addition & 0 deletions src/inverted_gates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ for (G, Gi) in ((:V, :Vi), (:S, :Si), (:T, :Ti))
end
end
Base.inv(g::Unitary) = Unitary(inv(g.matrix))
Base.inv(g::PRx) = PRx(-g.angle[1], g.angle[2])
Base.inv(g::GPi2) = GPi2(g.angle[1] + π)
Base.inv(g::MS) = MS(g.angle[1] + π, g.angle[2], g.angle[3])

Expand Down
4 changes: 4 additions & 0 deletions src/properties.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const sv_props_dict = Dict(
"iswap",
"pswap",
"phaseshift",
"prx",
"rx",
"ry",
"rz",
Expand Down Expand Up @@ -112,6 +113,7 @@ const sv_props_dict = Dict(
"ms",
"pswap",
"phaseshift",
"prx",
"rx",
"ry",
"rz",
Expand Down Expand Up @@ -249,6 +251,7 @@ const dm_props_dict = Dict(
"ms",
"pswap",
"phaseshift",
"prx",
"rx",
"ry",
"rz",
Expand Down Expand Up @@ -363,6 +366,7 @@ const dm_props_dict = Dict(
"phase_damping",
"phaseshift",
"pswap",
"prx",
"rx",
"ry",
"rz",
Expand Down
1 change: 1 addition & 0 deletions test/test_dm_simulator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ LARGE_TESTS = get(ENV, "BRAKET_SV_LARGE_TESTS", false)
"ms",
"pswap",
"phaseshift",
"prx",
"rx",
"ry",
"rz",
Expand Down
4 changes: 2 additions & 2 deletions test/test_gate_operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using BraketSimulator: DoubleExcitation, SingleExcitation, matrix_rep, Control
q1_mat = [0 -im; im 0]
q2_mat = [1 0 0 0; 0 1 0 0 ; 0 0 0 -im; 0 0 im 0]
@testset "Inverted gates" begin
@testset "1q Gate $g" for g in [X(), Y(), Z(), H(), GPi(ϕ), GPi2(ϕ), Rx(ϕ), Ry(ϕ), Rz(ϕ), PhaseShift(ϕ), S(), Si(), T(), Ti(), V(), Vi(), U(θ, ϕ, λ), Unitary(q1_mat)]
@testset "1q Gate $g" for g in [X(), Y(), Z(), H(), GPi(ϕ), GPi2(ϕ), Rx(ϕ), Ry(ϕ), Rz(ϕ), PRx(θ, ϕ), PhaseShift(ϕ), S(), Si(), T(), Ti(), V(), Vi(), U(θ, ϕ, λ), Unitary(q1_mat)]
@test inv(matrix_rep(g)) ≈ matrix_rep(inv(g))
@test matrix_rep(inv(g)) * matrix_rep(g) ≈ Diagonal(ones(2))
sim = StateVectorSimulator(nq, 0)
Expand Down Expand Up @@ -43,7 +43,7 @@ using BraketSimulator: DoubleExcitation, SingleExcitation, matrix_rep, Control
end
end
@testset "Exponentiated gates" begin
@testset "1q Gate $g, pow $pow" for g in [X(), Y(), Z(), H(), GPi(ϕ), GPi2(ϕ), Rx(ϕ), Ry(ϕ), Rz(ϕ), PhaseShift(ϕ), S(), Si(), T(), Ti(), V(), Vi(), U(θ, ϕ, λ), Unitary(q1_mat), MultiQubitPhaseShift{1}(ϕ)], pow in (0, 1, 2, 3, 4)
@testset "1q Gate $g, pow $pow" for g in [X(), Y(), Z(), H(), GPi(ϕ), GPi2(ϕ), Rx(ϕ), Ry(ϕ), Rz(ϕ), PRx(θ, ϕ), PhaseShift(ϕ), S(), Si(), T(), Ti(), V(), Vi(), U(θ, ϕ, λ), Unitary(q1_mat), MultiQubitPhaseShift{1}(ϕ)], pow in (0, 1, 2, 3, 4)
sim = StateVectorSimulator(nq, 0)
new_sim = StateVectorSimulator(nq, 0)
instructions = vcat([Instruction(H(), [q]) for q in 0:nq-1], [Instruction(g^pow, [0])])
Expand Down
13 changes: 13 additions & 0 deletions test/test_python_ext.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ using Test, PythonCall, BraketSimulator, Braket
)
@test pyconvert(Braket.Instruction, py_sim_gate) == Braket.Instruction(jl_gate, 0)
end
@testset "2-parameter gate $jl_gate" for (jl_gate, py_sim_gate, targets) in zip(
[
PRx(angle, angle2),
],
[
braket_sim_gates.PRx(targets=pylist([0]), angle_1=angle, angle_2=angle2),
],
[
[0],
],
)
@test pyconvert(Braket.Instruction, py_sim_gate) == Braket.Instruction(jl_gate, targets)
end
@testset "3-parameter gate $jl_gate" for (jl_gate, py_sim_gate, targets) in zip(
[
MS(angle, angle2, angle3),
Expand Down
7 changes: 7 additions & 0 deletions test/test_sv_simulator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ LARGE_TESTS = get(ENV, "BRAKET_SV_LARGE_TESTS", false)
[0.69916673 + 0.10566872im, 0.69916673 + 0.10566872im],
[0.5, 0.5],
),
(
[Instruction(H(), [0]), Instruction(PRx(0.15, 0.4), [0])],
1,
[0.6844863 - 0.04880085im, 0.72575165 - 0.04880085im],
[0.47090303, 0.52909697],
),
(
[Instruction(X(), [0]), Instruction(PSwap(0.15), [0, 1])],
2,
Expand Down Expand Up @@ -381,6 +387,7 @@ LARGE_TESTS = get(ENV, "BRAKET_SV_LARGE_TESTS", false)
"ms",
"pswap",
"phaseshift",
"prx",
"rx",
"ry",
"rz",
Expand Down
Loading