Skip to content

feat: add tan, log, floor, asin, acos, atan math functions to kernel expressions#4747

Closed
kish-00 wants to merge 3 commits into
NVIDIA:mainfrom
kish-00:feat/math-functions-2942
Closed

feat: add tan, log, floor, asin, acos, atan math functions to kernel expressions#4747
kish-00 wants to merge 3 commits into
NVIDIA:mainfrom
kish-00:feat/math-functions-2942

Conversation

@kish-00

@kish-00 kish-00 commented Jun 16, 2026

Copy link
Copy Markdown

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:

  • tan — tangent (real + complex)
  • log — natural logarithm (real + complex)
  • floor — floor (real only; complex raises a clear error)
  • asin — arc sine (real only)
  • acos — arc cosine (real only)
  • atan — arc tangent (real only)

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)

  • Extended __isSupportedNumpyFunction to include tan, log, floor, asin, acos, atan.
  • Added MLIR emission for each new function:
    • tanmath.TanOp / complex.TanOp
    • logmath.LogOp / complex.LogOp
    • floormath.FloorOp (complex raises fatal error, matching ceil)
    • asinmath.AsinOp (real only)
    • acosmath.AcosOp (real only)
    • atanmath.AtanOp (real only)

C++ (cudaq/lib/Frontend/nvqpp/ConvertExpr.cpp)

  • Wired std::tan/std::tanfmath::TanOp in visitMathLibFunc.
  • Wired std::log/std::logfmath::LogOp.
  • Wired std::floor/std::floorfmath::FloorOp.
  • Wired std::asin/std::asinfmath::AsinOp.
  • Wired std::acos/std::acosfmath::AcosOp.
  • Wired std::atan/std::atanfmath::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_trig
  • python/tests/kernel/test_kernel_complex.py: test_complex_new_math_functions, test_complex_inverse_trig

AI 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/ceil handlers.

Closes #2942.

kish-00 and others added 2 commits June 16, 2026 10:03
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>
@copy-pr-bot

copy-pr-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

… 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.
@kish-00

kish-00 commented Jun 17, 2026

Copy link
Copy Markdown
Author

Closing — PRs #4680 (approved by schweitzpgi) and #4697 (CI passing) were submitted earlier and cover overlapping math function support for #2942. This branch also has a contamination issue (includes embeddings files from a parent branch). Will look at other unitaryHACK bounties.

@kish-00 kish-00 closed this Jun 17, 2026
github-actions Bot pushed a commit that referenced this pull request Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mathematical functions support

1 participant