feat: add tan, log, floor, asin, acos, atan math functions to kernel expressions#4747
Closed
kish-00 wants to merge 3 commits into
Closed
feat: add tan, log, floor, asin, acos, atan math functions to kernel expressions#4747kish-00 wants to merge 3 commits into
kish-00 wants to merge 3 commits into
Conversation
Add built-in APIs for common quantum embeddings used in QML: - cudaq.amplitude_encode(data, pad=None): encodes classical feature vectors as normalized quantum state amplitudes, with optional zero-padding to the nearest power of 2. - cudaq.kernels.angular_encode(kernel, qubits, data, rotation='Y'): encodes classical features as single-qubit rotation gates (Rx, Ry, or Rz), following the same builder-API pattern as hwe and uccsd. Closes: NVIDIA#2982 Signed-off-by: kish-00 <kish@example.com>
…expressions Extends the numpy function bridge in ast_bridge.py and the C++ ConvertExpr.cpp to support six additional math functions: - tan, log, floor (real + complex support where applicable) - asin, acos, atan (real-only, matching MLIR Math dialect ops) New tests cover float64, float32, and complex types for all functions. Closes NVIDIA#2942. Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com>
… float tests Complex inverse trig functions are not supported (same as ceil/floor), so remove those test cases. Also remove redundant standalone inverse trig test functions since they are already covered in the main new_math_functions tests.
Author
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.
Summary
Extends the Python AST-to-MLIR bridge and the C++ Clang AST converter to support six additional math functions inside CUDA-Q kernel expressions:
The MLIR Math dialect already provides the corresponding ops (
math::TanOp,math::LogOp,math::FloorOp,math::AsinOp,math::AcosOp,math::AtanOp); this PR wires them into both the Python and C++ lowering paths.Changes
Python (
python/cudaq/kernel/ast_bridge.py)__isSupportedNumpyFunctionto includetan,log,floor,asin,acos,atan.tan→math.TanOp/complex.TanOplog→math.LogOp/complex.LogOpfloor→math.FloorOp(complex raises fatal error, matchingceil)asin→math.AsinOp(real only)acos→math.AcosOp(real only)atan→math.AtanOp(real only)C++ (
cudaq/lib/Frontend/nvqpp/ConvertExpr.cpp)std::tan/std::tanf→math::TanOpinvisitMathLibFunc.std::log/std::logf→math::LogOp.std::floor/std::floorf→math::FloorOp.std::asin/std::asinf→math::AsinOp.std::acos/std::acosf→math::AcosOp.std::atan/std::atanf→math::AtanOp.Tests
python/tests/kernel/test_kernel_float.py:test_float64_new_math_functions,test_float32_new_math_functions,test_float64_inverse_trig,test_float32_inverse_trigpython/tests/kernel/test_kernel_complex.py:test_complex_new_math_functions,test_complex_inverse_trigAI Disclosure
AI tools (Anthropic Claude) were used as a co-pilot for implementation planning and code scaffolding. All code was manually reviewed against the CUDA-Q codebase patterns, follows existing conventions exactly, and the new functions mirror the already-supported
sin/cos/exp/ceilhandlers.Closes #2942.