Skip to content

Fix add/sub optimizations including the same symbols (backport #16752) - #16759

Merged
Julien Gacon (Cryoris) merged 2 commits into
stable/2.5from
mergify/bp/stable/2.5/pr-16752
Aug 13, 2026
Merged

Julien Gacon (Cryoris) merged 2 commits into
stable/2.5from
mergify/bp/stable/2.5/pr-16752

Conversation

@mergify

@mergify mergify Bot commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Previously add/sub optimizations for binary expressions with that contain the same symbol and a value in the other slot, where the values evaluate to zero, would generically be set to 0.

This was caused by the same faulty optimization in add_opt and sub_opt, where the addition/subtraction of two Binary { op, lhs, rhs } components checked if the lhs parts summed to 0, without considering what was in the rhs part.

For example:

>>> from qiskit.circuit import Parameter
>>> x = Parameter("x")
>>> expr1 = 1 + x
>>> expr2 = -1 + x
>>> print(expr1 + expr2)  # should be 2x
0

or even in general additions

>>> print((2**x) + (1**x))
3**x

AI/LLM disclosure

  • I didn't use LLM tooling, or only used it privately.
  • I used the following tool to help write this PR description:
  • I used the following tool to generate or modify code:

This is an automatic backport of pull request #16752 done by [Mergify](https://mergify.com).

* Fix add and sub optimizations for same symbols

Previously add/sub optimizations for binary expressions with that contain the same symbol and a value in the other slot, where the values evaluate to zero, would generically be set to 0.

* Fix more cases

- there's no valid (Pow, Pow) optimization
- added missing cases for mul/div optimizations

* Rm trailing prints

* Simplify match statements

---------

Co-authored-by: Alexander Ivrii <alexi@il.ibm.com>
(cherry picked from commit 4b0b834)

# Conflicts:
#	test/python/circuit/test_parameter_expression.py
@mergify mergify Bot added the conflicts used by mergify when there are conflicts in a port label Aug 13, 2026
@mergify
mergify Bot requested a review from a team as a code owner August 13, 2026 12:55
@mergify

mergify Bot commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

Cherry-pick of 4b0b834 has failed:

On branch mergify/bp/stable/2.5/pr-16752
Your branch is up to date with 'origin/stable/2.5'.

You are currently cherry-picking commit 4b0b834dc.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   crates/circuit/src/parameter/symbol_expr.rs
	new file:   releasenotes/notes/fix-addsub-symbol-opt-1bc79d0b13d6788e.yaml

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   test/python/circuit/test_parameter_expression.py

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

@mergify mergify Bot added the conflicts used by mergify when there are conflicts in a port label Aug 13, 2026
@qiskit-bot

Copy link
Copy Markdown
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

@github-actions github-actions Bot added the Changelog: Fixed Add a "Fixed" entry in the GitHub Release changelog. label Aug 13, 2026
@github-actions github-actions Bot added this to the 2.5.2 milestone Aug 13, 2026
@ShellyGarion

Copy link
Copy Markdown
Member

Julien Gacon (@Cryoris) - could you please fix the conflict here?

@Cryoris Julien Gacon (Cryoris) removed the conflicts used by mergify when there are conflicts in a port label Aug 13, 2026
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 31705481781

Coverage increased (+0.01%) to 87.759%

Details

  • Coverage increased (+0.01%) from the base build.
  • Patch coverage: 6 uncovered changes across 1 file (30 of 36 lines covered, 83.33%).
  • 12 coverage regressions across 2 files.

Uncovered Changes

File Changed Covered %
crates/circuit/src/parameter/symbol_expr.rs 36 30 83.33%

Coverage Regressions

12 previously-covered lines in 2 files lost coverage.

File Lines Losing Coverage Coverage
crates/qasm2/src/lex.rs 6 92.03%
crates/qasm2/src/parse.rs 6 97.63%

Coverage Stats

Coverage Status
Relevant Lines: 128816
Covered Lines: 113047
Line Coverage: 87.76%
Coverage Strength: 996822.25 hits per line

πŸ’› - Coveralls

Merged via the queue into stable/2.5 with commit 58c5bc6 Aug 13, 2026
31 checks passed
@Cryoris
Julien Gacon (Cryoris) deleted the mergify/bp/stable/2.5/pr-16752 branch August 13, 2026 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: Fixed Add a "Fixed" entry in the GitHub Release changelog.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants