feat: add math function support to FreeParameterExpression#1263
Open
x-haolun wants to merge 1 commit into
Open
feat: add math function support to FreeParameterExpression#1263x-haolun wants to merge 1 commit into
x-haolun wants to merge 1 commit into
Conversation
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
There was a problem hiding this comment.
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) toFreeParameterExpression. - Introduce
OQASM_FUNCTION_MAPand extend_to_oqpy_expression()to map SymPy functions /Modinto 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Add 12 math function methods to
FreeParameterExpressionso users can write expressions likesin(theta),cos(phi + 0.5), andexp(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 symbolstest/unit_tests/braket/parametric/test_math_functions.py— 47 tests covering all 12 functions, chaining, composition, substitution, OQASM mappingCHANGELOG.md— UpdatedTesting
Related Issue
Closes #1252