Skip to content

feat: add math function support to FreeParameterExpression#1263

Open
x-haolun wants to merge 1 commit into
amazon-braket:mainfrom
x-haolun:feature/math-functions
Open

feat: add math function support to FreeParameterExpression#1263
x-haolun wants to merge 1 commit into
amazon-braket:mainfrom
x-haolun:feature/math-functions

Conversation

@x-haolun
Copy link
Copy Markdown

@x-haolun x-haolun commented Jun 1, 2026

Description

Add 12 math function methods to FreeParameterExpression so users can write expressions like sin(theta), cos(phi + 0.5), and exp(gamma) directly on free parameters.

Changes

  • src/braket/parametric/free_parameter_expression.py — Added 12 methods: sin(), cos(), tan(), arcsin(), arccos(), arctan(), exp(), log(), sqrt(), mod(other), ceiling(), floor(). Each wraps the corresponding sympy function.
  • src/braket/parametric/__init__.py — Exports for new symbols
  • test/unit_tests/braket/parametric/test_math_functions.py — 47 tests covering all 12 functions, chaining, composition, substitution, OQASM mapping
  • CHANGELOG.md — Updated

Testing

  • 47 new tests: all pass
  • 38 existing parametric tests: all pass (0 regressions)
  • 4 xfailed tests: unchanged

Related Issue

Closes #1252

Add 12 math function methods to FreeParameterExpression:
sin, cos, tan, arcsin, arccos, arctan, exp, log, sqrt,
mod, ceiling, floor

- Each wraps the corresponding sympy function
- OQASM printer maps function names correctly (asin→arcsin, etc.)
- All functions exported from braket.parametric
- 47 new tests pass, 38 existing tests pass, 0 regressions

Closes amazon-braket#1252
@x-haolun x-haolun requested a review from a team as a code owner June 1, 2026 21:37
Copilot AI review requested due to automatic review settings June 1, 2026 21:37
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds callable math-function helpers to FreeParameterExpression and updates OQASM conversion to emit function calls for supported SymPy functions, alongside new unit tests and a changelog entry.

Changes:

  • Add math function methods (e.g., sin, cos, exp, mod, ceiling, floor) to FreeParameterExpression.
  • Introduce OQASM_FUNCTION_MAP and extend _to_oqpy_expression() to map SymPy functions / Mod into OQASM function calls.
  • Add unit tests covering expression construction/substitution and function-map presence; update changelog.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.

File Description
test/unit_tests/braket/parametric/test_math_functions.py Adds unit tests for new math function helpers and map exposure.
src/braket/parametric/free_parameter_expression.py Adds math methods, OQASM function map, and function/mod handling in OQPy conversion.
src/braket/parametric/init.py Re-exports OQASM_FUNCTION_MAP and subs_if_free_parameter.
CHANGELOG.md Documents new math-function support in Unreleased features.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +27 to +39
OQASM_FUNCTION_MAP = {
sympy.sin: "sin",
sympy.cos: "cos",
sympy.tan: "tan",
sympy.asin: "arcsin",
sympy.acos: "arccos",
sympy.atan: "arctan",
sympy.exp: "exp",
sympy.log: "log",
sympy.sqrt: "sqrt",
sympy.ceiling: "ceiling",
sympy.floor: "floor",
}
Comment on lines +312 to +318
if isinstance(self.expression, sympy.Function) and type(self.expression) in OQASM_FUNCTION_MAP:
func_name = OQASM_FUNCTION_MAP[type(self.expression)]
args = [
FreeParameterExpression(arg)._to_oqpy_expression()
for arg in self.expression.args
]
return OQFunctionCall(identifier=func_name, args=args, return_type=None)
Comment on lines +312 to +324
if isinstance(self.expression, sympy.Function) and type(self.expression) in OQASM_FUNCTION_MAP:
func_name = OQASM_FUNCTION_MAP[type(self.expression)]
args = [
FreeParameterExpression(arg)._to_oqpy_expression()
for arg in self.expression.args
]
return OQFunctionCall(identifier=func_name, args=args, return_type=None)
if isinstance(self.expression, sympy.Mod):
args = [
FreeParameterExpression(arg)._to_oqpy_expression()
for arg in self.expression.args
]
return OQFunctionCall(identifier="mod", args=args, return_type=None)
Comment on lines +32 to +36
def _to_float(value):
"""Convert a subs result (sympy numeric or FreeParameterExpression) to float."""
if isinstance(value, FreeParameterExpression):
return float(value.expression)
return float(value)
Comment on lines +98 to +101
def test_arcsin_subs_one(self, theta):
result = theta.arcsin().subs({"theta": 1})
assert isinstance(result, FreeParameterExpression)
assert abs(float(result.expression.evalf()) - math.pi / 2) < 1e-10
Comment on lines +125 to +128
def test_arctan_subs_one(self, theta):
result = theta.arctan().subs({"theta": 1})
assert isinstance(result, FreeParameterExpression)
assert abs(float(result.expression.evalf()) - math.pi / 4) < 1e-10
Comment on lines +141 to +144
def test_exp_subs_one(self, theta):
result = theta.exp().subs({"theta": 1})
assert isinstance(result, FreeParameterExpression)
assert abs(float(result.expression.evalf()) - math.e) < 1e-10
@github-actions github-actions Bot added documentation Improvements or additions to documentation circuits labels Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

circuits documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support math functions (sin, cos, exp, …) in FreeParameterExpression

2 participants