-
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 11 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module Braket | ||
|
||
export Circuit, QubitSet, Qubit, Device, AwsDevice, AwsQuantumTask, AwsQuantumTaskBatch | ||
export metadata, status, Observable, Result, FreeParameter, Job, AwsQuantumJob, LocalQuantumJob, LocalSimulator | ||
export metadata, status, Observable, Result, FreeParameter, FreeParameterExpression, Job, AwsQuantumJob, LocalQuantumJob, LocalSimulator, subs | ||
export Tracker, simulator_tasks_cost, qpu_tasks_cost | ||
export arn, cancel, state, result, results, name, download_result, id, ir, isavailable, search_devices, get_devices | ||
export provider_name, properties, type | ||
|
@@ -32,13 +32,18 @@ using DecFP | |
using Graphs | ||
using HTTP | ||
using StaticArrays | ||
using Symbolics | ||
using SymbolicUtils | ||
using JSON3, StructTypes | ||
using LinearAlgebra | ||
using DataStructures | ||
using NamedTupleTools | ||
using OrderedCollections | ||
using Tar | ||
|
||
# Operator overloading for FreeParameterExpression | ||
import Base: +, -, *, /, ^, == | ||
|
||
include("utils.jl") | ||
""" | ||
IRType | ||
|
@@ -134,6 +139,139 @@ end | |
Base.copy(fp::FreeParameter) = fp | ||
Base.show(io::IO, fp::FreeParameter) = print(io, string(fp.name)) | ||
|
||
""" | ||
FreeParameterExpression | ||
FreeParameterExpression(expr::Union{FreeParameterExpression, Number, Symbolics.Num, String}) | ||
Struct representing a Free Parameter expression, which can be used in symbolic computations. | ||
Instances of `FreeParameterExpression` can represent symbolic expressions involving FreeParameters, | ||
such as mathematical expressions with undetermined values. | ||
This type is often used in combination with [`FreeParameter`](@ref), which represents individual FreeParameter. | ||
### Examples | ||
```jldoctest | ||
julia> α = FreeParameter(:alpha) | ||
alpha | ||
julia> θ = FreeParameter(:theta) | ||
theta | ||
julia> gate = FreeParameterExpression("α + 2*θ") | ||
α + 2θ | ||
julia> gsub = subs(gate, Dict(:α => 2.0, :θ => 2.0)) | ||
6.0 | ||
julia> gate + gate | ||
2α + 4θ | ||
julia> gate * gate | ||
(α + 2θ)^2 | ||
julia> gate₁ = FreeParameterExpression("phi + 2*gamma") | ||
2gamma + phi | ||
``` | ||
""" | ||
struct FreeParameterExpression | ||
expression::Symbolics.Num | ||
function FreeParameterExpression(expr::Symbolics.Num) | ||
new(expr) | ||
end | ||
end | ||
|
||
FreeParameterExpression(expr::FreeParameterExpression) = FreeParameterExpression(expr.expression) | ||
FreeParameterExpression(expr::Number) = FreeParameterExpression(Symbolics.Num(expr)) | ||
FreeParameterExpression(expr) = throw(ArgumentError("Unsupported expression type")) | ||
|
||
function FreeParameterExpression(expr::String) | ||
parsed_expr = parse_expr_to_symbolic(Meta.parse(expr), @__MODULE__) | ||
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. This looks very suspect to me... is there a less sketchy way to accomplish this? 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. Generally directly calling 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. I understand your concern. In order to alleviate it, Added a function Tried a bunch of different ways without
|
||
return FreeParameterExpression(parsed_expr) | ||
end | ||
|
||
Base.show(io::IO, fpe::FreeParameterExpression) = print(io, fpe.expression) | ||
Base.copy(fp::FreeParameterExpression) = fp | ||
|
||
function subs(fpe::FreeParameterExpression, parameter_values::Dict{Symbol, <:Number}) | ||
param_values_num = Dict(Symbolics.variable(string(k); T=Real) => v for (k, v) in parameter_values) | ||
subbed_expr = Symbolics.substitute(fpe.expression, param_values_num) | ||
if isempty(Symbolics.get_variables(subbed_expr)) | ||
subbed_expr = Symbolics.value(subbed_expr) | ||
return subbed_expr | ||
else | ||
subbed_expr = Symbolics.value(subbed_expr) | ||
return FreeParameterExpression(subbed_expr) | ||
end | ||
end | ||
|
||
function Base.:+(fpe1::FreeParameterExpression, fpe2::FreeParameterExpression) | ||
return FreeParameterExpression(fpe1.expression + fpe2.expression) | ||
end | ||
|
||
function Base.:+(fpe1::FreeParameterExpression, fpe2::Union{Number, Symbolics.Num}) | ||
return FreeParameterExpression(fpe1.expression + fpe2) | ||
end | ||
|
||
function Base.:+(fpe1::Union{Number, Symbolics.Num}, fpe2::FreeParameterExpression) | ||
return FreeParameterExpression(fpe1 + fpe2.expression) | ||
end | ||
|
||
function Base.:*(fpe1::FreeParameterExpression, fpe2::FreeParameterExpression) | ||
return FreeParameterExpression(fpe1.expression * fpe2.expression) | ||
end | ||
|
||
function Base.:*(fpe1::FreeParameterExpression, fpe2::Union{Number, Symbolics.Num}) | ||
return FreeParameterExpression(fpe1.expression * fpe2) | ||
end | ||
|
||
function Base.:*(fpe1::Union{Number, Symbolics.Num}, fpe2::FreeParameterExpression) | ||
return FreeParameterExpression(fpe1 * fpe2.expression) | ||
end | ||
|
||
function Base.:-(fpe1::FreeParameterExpression, fpe2::FreeParameterExpression) | ||
return FreeParameterExpression(fpe1.expression - fpe2.expression) | ||
end | ||
|
||
function Base.:-(fpe1::FreeParameterExpression, fpe2::Union{Number, Symbolics.Num}) | ||
return FreeParameterExpression(fpe1.expression - fpe2) | ||
end | ||
|
||
function Base.:-(fpe1::Union{Number, Symbolics.Num}, fpe2::FreeParameterExpression) | ||
return FreeParameterExpression(fpe1 - fpe2.expression) | ||
end | ||
|
||
function Base.:/(fpe1::FreeParameterExpression, fpe2::FreeParameterExpression) | ||
return FreeParameterExpression(fpe1.expression / fpe2.expression) | ||
end | ||
|
||
function Base.:/(fpe1::FreeParameterExpression, fpe2::Union{Number, Symbolics.Num}) | ||
return FreeParameterExpression(fpe1.expression / fpe2) | ||
end | ||
|
||
function Base.:/(fpe1::Union{Number, Symbolics.Num}, fpe2::FreeParameterExpression) | ||
return FreeParameterExpression(fpe1 / fpe2.expression) | ||
end | ||
|
||
function Base.:^(fpe1::FreeParameterExpression, fpe2::FreeParameterExpression) | ||
return FreeParameterExpression(fpe1.expression ^ fpe2.expression) | ||
end | ||
|
||
function Base.:^(fpe1::FreeParameterExpression, fpe2::Union{Number, Symbolics.Num}) | ||
return FreeParameterExpression(fpe1.expression ^ fpe2) | ||
end | ||
|
||
function Base.:^(fpe1::Union{Number, Symbolics.Num}, fpe2::FreeParameterExpression) | ||
return FreeParameterExpression(fpe1 ^ fpe2.expression) | ||
end | ||
|
||
function Base.:(==)(fpe1::FreeParameterExpression, fpe2::FreeParameterExpression) | ||
return isequal(fpe1.expression, fpe2.expression) | ||
end | ||
|
||
function Base.:!=(fpe1::FreeParameterExpression, fpe2::FreeParameterExpression) | ||
return !(isequal(fpe1.expression, fpe2.expression)) | ||
end | ||
|
||
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. Could slim all these down by making them one liners like:
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. This was giving error
We can slim it down like this:
|
||
include("compiler_directive.jl") | ||
include("gates.jl") | ||
include("noises.jl") | ||
|
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.
FreeParameter
here should get a doc linkThere 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.
You might need to add this docstring to the relevant
docs/src
file explicitly