qiskit icon indicating copy to clipboard operation
qiskit copied to clipboard

Fix simplify to sum duplicates before applying tolerance (#14194)

Open rahaman-quantum opened this issue 7 months ago • 3 comments

Summary

Details and comments

rahaman-quantum avatar May 21 '25 19:05 rahaman-quantum

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])

rahaman-quantum avatar May 21 '25 19:05 rahaman-quantum

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.

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 Coverage Status
Change from base Build 15742931774: -0.3%
Covered Lines: 81468
Relevant Lines: 92832

💛 - Coveralls

coveralls avatar Jun 10 '25 06:06 coveralls

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

  • @Qiskit/terra-core

qiskit-bot avatar Jun 11 '25 17:06 qiskit-bot

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

aeddins-ibm avatar Jul 18 '25 21:07 aeddins-ibm