-
Notifications
You must be signed in to change notification settings - Fork 7
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
feature: Adding support for FreeParameterExpression #85
Changes from all commits
5faf99e
56d342e
076fc37
0ba3d49
4e18330
e0f9c95
e91f943
0499e3b
915b248
24f676c
9234788
c23bfec
b4fdc23
3b17d90
8594d44
c5610c4
ca42bff
ff3c840
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using Braket, Test | ||
|
||
@testset "Free parameter expressions" begin | ||
Fe-r-oz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
α = FreeParameter(:alpha) | ||
θ = FreeParameter(:theta) | ||
gate = FreeParameterExpression("α + 2*θ") | ||
@test copy(gate) === gate | ||
gsub = subs(gate, Dict(:α => 2.0, :θ => 2.0)) | ||
circ = Circuit() | ||
circ = H(circ, 0) | ||
circ = Rx(circ, 1, gsub) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But here
? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That will show the FPE before evaluating it via substitute at the backend wrapped as subs:
|
||
circ = Ry(circ, 0, θ) | ||
circ = Probability(circ) | ||
new_circ = circ(6.0) | ||
non_para_circ = Circuit() |> (ci->H(ci, 0)) |> (ci->Rx(ci, 1, gsub)) |> (ci->Ry(ci, 0, 6.0)) |> Probability | ||
@test new_circ == non_para_circ | ||
ϕ = FreeParameter(:phi) | ||
circ = apply_gate_noise!(circ, BitFlip(ϕ)) | ||
circ = apply_gate_noise!(circ, PhaseFlip(0.1)) | ||
new_circ = circ(theta=2.0, alpha=1.0, phi=0.2) | ||
non_para_circ = Circuit() |> (ci->H(ci, 0)) |> (ci->Rx(ci, 1, gsub)) |> (ci->Ry(ci, 0, 2.0)) |> Probability |> (ci->apply_gate_noise!(ci, BitFlip(0.2))) |> (ci->apply_gate_noise!(ci, PhaseFlip(0.1))) | ||
@test new_circ == non_para_circ | ||
|
||
# creating gates directly | ||
gate = FreeParameterExpression("phi + 2*gamma") | ||
gsub₁ = subs(gate, Dict(:phi => 4.0, :gamma => 4.0)) | ||
@test gsub₁ == 12.0 | ||
circ = Circuit() | ||
circ = H(circ, 0) | ||
circ = Rx(circ, 1, gsub₁) | ||
circ = Ry(circ, 0, θ) | ||
circ = Probability(circ) | ||
new_circ = circ(6.0) | ||
non_para_circ = Circuit() |> (ci->H(ci, 0)) |> (ci->Rx(ci, 1, gsub₁)) |> (ci->Ry(ci, 0, 6.0)) |> Probability | ||
@test new_circ == non_para_circ | ||
|
||
# + operator | ||
fpe1 = FreeParameterExpression("α + θ") | ||
fpe2 = FreeParameterExpression("2 * θ") | ||
result = fpe1 + fpe2 | ||
@test result == FreeParameterExpression("2 * θ + α + θ") | ||
gsub = subs(result, Dict(:α => 1.0, :θ => 1.0)) | ||
@test gsub == 4.0 # α + 3θ == 4.0 | ||
fpe3 = FreeParameterExpression("2 * θ") | ||
# == operator | ||
@test fpe3 == fpe2 | ||
# != operator | ||
@test fpe1 != fpe2 | ||
show(fpe3) | ||
# - operator | ||
result = fpe1 - fpe2 | ||
@test result == FreeParameterExpression("α - θ") | ||
gsub = subs(result, Dict(:α => 1.0, :θ => 1.0)) | ||
@test gsub == 0.0 # α - θ == 0.0 | ||
# * operator | ||
result = fpe1 * fpe2 | ||
@test result == FreeParameterExpression("2(α + θ)*θ") | ||
gsub = subs(result, Dict(:α => 1.0, :θ => 1.0)) | ||
@test gsub == 4.0 # 2(α + θ)*θ == 4.0 | ||
# / operator | ||
result = fpe1 / fpe2 | ||
@test result == FreeParameterExpression("(α + θ) / (2θ)") | ||
gsub = subs(result, Dict(:α => 1.0, :θ => 1.0)) | ||
@test gsub == 1.0 # (α + θ) / (2θ) == 1.0 | ||
# ^ operator | ||
result = fpe1 ^ fpe2 | ||
@test result == FreeParameterExpression("(α + θ)^(2θ)") | ||
gsub = subs(result, Dict(:α => 1.0, :θ => 1.0)) | ||
@test gsub == 4.0 # (α + θ)^(2θ) == 4.0 | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could slim all these down by making them one liners like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was giving error
We can slim it down like this: