Skip to content

⚡ Optimize arg_alias and fix string alias bug#80

Merged
refraction-ray merged 2 commits intomasterfrom
perf-fix-arg-alias-18006180529273122311
Feb 26, 2026
Merged

⚡ Optimize arg_alias and fix string alias bug#80
refraction-ray merged 2 commits intomasterfrom
perf-fix-arg-alias-18006180529273122311

Conversation

@refraction-ray
Copy link
Member

This PR optimizes the arg_alias decorator in tensorcircuit/utils.py and fixes a bug handling single string aliases.

Optimization:
The original implementation iterated over alias_dict.items() inside the wrapper function, performing isinstance checks on every call.
The new implementation pre-processes alias_dict into a flat list of (key, alias) tuples at decoration time. The wrapper then iterates over this flat list, removing the overhead from the hot path.
Benchmarks indicate a ~40% speedup (from ~2.0s to ~1.2s for 1,000,000 calls).

Bug Fix:
The original implementation had logic if isinstance(vs, str): vs = [], which effectively discarded single string aliases.
The new implementation correctly normalizes single string aliases into a list [vs].
This also fixes a crash in docstring generation where iterating over a single string (alias name) caused re.error when characters like digits were encountered in re.sub.

Verification:

  • Verified with reproduction scripts for both performance and bug fix.
  • Added a new test case in tests/test_miscs.py covering the single string alias scenario.
  • Existing tests pass.

PR created automatically by Jules for task 18006180529273122311 started by @refraction-ray

- Optimized `arg_alias` decorator by pre-processing the alias dictionary once during decoration, removing iteration and type checks from the function call path.
- Fixed a bug where single string aliases were ignored (runtime) and caused `re.error` (docstring generation).
- Added a regression test case in `tests/test_miscs.py`.

Benchmarks show ~40% performance improvement (1.2s vs 2.0s for 1M calls).

Co-authored-by: refraction-ray <35157286+refraction-ray@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

"""
return theta

assert g(alpha=1.0) == 1.0
Copy link
Member Author

Choose a reason for hiding this comment

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

Do not perform equality checks with floating point values.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch! I've updated the test to use np.testing.assert_allclose for the floating point comparison.

Updated the new test case in `tests/test_miscs.py` to use `np.testing.assert_allclose` instead of equality check for floating point values, as requested in code review.

Co-authored-by: refraction-ray <35157286+refraction-ray@users.noreply.github.com>
@sonarqubecloud
Copy link

@codecov
Copy link

codecov bot commented Feb 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
tensorcircuit/utils.py 93.57% <100.00%> (+1.34%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@refraction-ray refraction-ray merged commit 3b8030a into master Feb 26, 2026
7 checks passed
@refraction-ray refraction-ray deleted the perf-fix-arg-alias-18006180529273122311 branch February 26, 2026 06:28
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.

1 participant