Fix simplify to sum duplicates before applying tolerance (#14194)
Summary
Details and comments
Summary
This PR fixes issue #14194 by modifying SparsePauliOp.simplify so that it first sums coefficients of duplicate Pauli terms, and only then applies the tolerance check (atol, rtol).
What Changed
- Moved the coefficient filtering step to occur after summing duplicates.
- This prevents incorrect simplification results when small terms accumulate or cancel.
Example (test case)
from qiskit.quantum_info import SparsePauliOp
op = SparsePauliOp(['X', 'X', 'Y', 'Z', 'Z'], coeffs=[5e-8, 5e-8, 1e-6, 1e-8, -1e-8])
simplified = op.simplify(atol=1e-7)
print(simplified)
# Expected: SparsePauliOp(['X', 'Y'], [1e-7, 1e-6])
Pull Request Test Coverage Report for Build 16405568661
Warning: This coverage report may be inaccurate.
This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
- For more information on this, see Tracking coverage changes with pull request builds.
- To avoid this issue with future PRs, see these Recommended CI Configurations.
- For a quick fix, rebase this PR at GitHub. Your next report should be accurate.
Details
- 7 of 7 (100.0%) changed or added relevant lines in 1 file are covered.
- 2376 unchanged lines in 90 files lost coverage.
- Overall coverage decreased (-0.3%) to 87.759%
| Files with Coverage Reduction | New Missed Lines | % |
|---|---|---|
| crates/accelerate/src/circuit_library/multi_local.rs | 1 | 99.39% |
| crates/accelerate/src/results/marginalization.rs | 1 | 90.45% |
| crates/cext/src/circuit.rs | 1 | 80.45% |
| crates/circuit/src/classical/expr/binary.rs | 1 | 98.68% |
| crates/circuit/src/classical/expr/cast.rs | 1 | 79.63% |
| crates/circuit/src/classical/expr/index.rs | 1 | 98.15% |
| crates/circuit/src/classical/expr/stretch.rs | 1 | 93.65% |
| crates/circuit/src/classical/expr/unary.rs | 1 | 83.82% |
| crates/circuit/src/classical/expr/value.rs | 1 | 97.87% |
| crates/circuit/src/classical/expr/var.rs | 1 | 92.16% |
| <!-- | Total: | 2376 |
| Totals | |
|---|---|
| Change from base Build 15742931774: | -0.3% |
| Covered Lines: | 81468 |
| Relevant Lines: | 92832 |
💛 - Coveralls
One or more of the following people are relevant to this code:
@Qiskit/terra-core
Excited for this bug fix! Just in case it's flying under the radar, is the only thing blocking merging this small typo? @rahaman-quantum