Skip to content

[Parser] Do not split numbers in scientific notation at the exponent's sign - #233

Merged
xumingkuan merged 2 commits into
quantum-compiler:masterfrom
liupengy19:fix-parser-exponent
Nov 1, 2026
Merged

xumingkuan merged 2 commits into
quantum-compiler:masterfrom
liupengy19:fix-parser-exponent

Conversation

@liupengy19

Copy link
Copy Markdown
Collaborator

ParamParser::parse_expr splits an expression into terms at the last +/-, skipping only a sign right after * (as in pi*-0.25). A literal in scientific notation is therefore split at the sign of its exponent: rz(9.58737992428526e-05) is parsed as 9.58737992428526e minus 05, i.e. about 4.59 rad, without a warning. Likewise -4.79e-05 becomes about -9.79 and 3.451511e-09 about -5.55.

Such literals are common in generated circuits. For example, 15 circuits of the GUOQ benchmark suite (TFIM_step_*_4, TFIM_step_100_{8,16,32}, qft_20, vqe_4; 766 literals) contain them, so Quartz optimized a different circuit than the input.

Fix: also skip a +/- that follows e/E preceded by a digit or ..

Test: src/test/test_parser_exponent.cpp parses rz angles in scientific notation and the same angles written as plain decimals, and checks that the two circuits have the same matrix. On master 6 of the 7 cases fail; with the fix all pass. The changed files are clean under clang-format 21.1.0 (the pre-commit version).

Not changed here: with USE_RATIONAL (the default), string_to_param_without_pi stores a decimal angle as std::to_string(angle / pi), i.e. with 6 decimals, so small angles are still rounded (e.g. 9.587e-05 rad becomes 3.1e-05·π ≈ 9.74e-05 rad); the existing warning covers this.

🤖 Generated with Claude Code

https://claude.ai/code/session_01YFXDzXihmK8D9bNxsBNotC

…s sign

ParamParser::parse_expr splits an expression into terms at the last + or -,
skipping only a sign right after '*'. A literal such as 9.58737992428526e-05
was therefore split into "9.58737992428526e" and "-05" and parsed as about
4.59 rad, without a warning. Also skip a sign that follows 'e'/'E' preceded
by a digit or '.'.

test_parser_exponent checks that angles in scientific notation parse to the
same circuit as the same angles written as plain decimals.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFXDzXihmK8D9bNxsBNotC
Copilot AI balanced review requested due to automatic review settings November 1, 2026 05:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block approval of the scientific-notation parsing change.

No open issues 🎉

What changed in this PR

This PR fixes QASM parameter parsing so signs in scientific-notation exponents are not mistaken for addition or subtraction, preventing incorrect rotation angles in parsed circuits.

Changes:

  • Preserve exponent signs following e or E when preceded by a digit or decimal point.
  • Add seven matrix-equivalence regression cases covering scientific notation, subtraction, and existing negative-factor handling.
  • Add a CMake executable target for the new regression test.
File Description
src/​test/​test_parser_exponent.cpp Compares circuits parsed from scientific-notation angles and equivalent expressions.
src/​test/​CMakeLists.txt Adds the test_parser_exponent executable target.
src/​quartz/​parser/​qasm_parser.cpp Skips exponent signs when splitting expressions into terms.

Review effort: Balanced

@xumingkuan xumingkuan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, thx

@xumingkuan
xumingkuan merged commit 2a5892e into quantum-compiler:master Nov 1, 2026
2 checks passed
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.

3 participants