Skip to content

Missing input validation on SynthesizeRZRotations, negative synthesis_error panics #16384

Description

@lmaltsis

Environment

  • Qiskit version: 49eac27
  • Python version: Python 3.12.3
  • Operating system: Ubuntu 24.04.4 LTS

What is happening?

$ python /tmp/bug.py

thread '<unnamed>' panicked at /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rsgridsynth-0.2.0/src/config.rs:118:14:
attempt to subtract with overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Traceback (most recent call last):
  File "/tmp/bug.py", line 7, in <module>
    SynthesizeRZRotations(synthesis_error=-10, cache_error=0.5)(qc)
  File "/home/user/software/qiskit-venv/slot-a/src/qiskit/transpiler/basepasses.py", line 144, in __call__
    out = pm.run(circuit, property_set=property_set)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/software/qiskit-venv/slot-a/src/qiskit/transpiler/passmanager.py", line 456, in wrapper
    return meth(*meth_args, **meth_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/software/qiskit-venv/slot-a/src/qiskit/transpiler/passmanager.py", line 196, in run
    return super().run(
           ^^^^^^^^^^^^
  File "/home/user/software/qiskit-venv/slot-a/src/qiskit/passmanager/passmanager.py", line 238, in run
    _run_workflow(
  File "/home/user/software/qiskit-venv/slot-a/src/qiskit/passmanager/passmanager.py", line 313, in _run_workflow
    passmanager_ir, final_state = flow_controller.execute(
                                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/software/qiskit-venv/slot-a/src/qiskit/passmanager/base_tasks.py", line 215, in execute
    passmanager_ir, state = next_task.execute(
                            ^^^^^^^^^^^^^^^^^^
  File "/home/user/software/qiskit-venv/slot-a/src/qiskit/transpiler/basepasses.py", line 167, in execute
    new_dag, state = super().execute(
                     ^^^^^^^^^^^^^^^^
  File "/home/user/software/qiskit-venv/slot-a/src/qiskit/passmanager/base_tasks.py", line 97, in execute
    ret = self.run(passmanager_ir)
          ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/software/qiskit-venv/slot-a/src/qiskit/transpiler/passes/synthesis/synthesize_rz_rotations.py", line 91, in run
    new_dag = synthesize_rz_rotations(
              ^^^^^^^^^^^^^^^^^^^^^^^^
pyo3_runtime.PanicException: attempt to subtract with overflow

How can we reproduce the issue?

from qiskit import QuantumCircuit
from qiskit.transpiler.passes.synthesis import SynthesizeRZRotations

qc = QuantumCircuit(1)
qc.rz(0.1, 0)

SynthesizeRZRotations(synthesis_error=-10, cache_error=0.5)(qc)

Negative synthesis error of -9 and above will instead get a "root is complex" panic.

What should happen?

Input validation on negative synthesis error, this is invalid code.

Any suggestions?

No response

Activity

  1. pdd23001 commented on Jun 6, 2026

    @pdd23001
    Contributor

    Please could I be assigned to this? Thank you!

  2. Cryoris commented on Jun 7, 2026

    @Cryoris
    Contributor

    Instead of validating every possible input value, I would much prefer to clarify in the documentation that this error should be in [0, 1]. Checking every possible input in functions decreases runtime performance and, while worthwhile in complex cases, I'd argue it's pretty clear that an error cannot be negative and that this is clearly invalid input. Nevertheless it would be good to clarify this in the docs.

  3. alexanderivrii commented on Jun 7, 2026

    @alexanderivrii
    Member

    Parth Danve (@pdd23001), I have assigned you. Please see the comment by Julien Gacon (@Cryoris) directly above for what you should do. Thanks for the interest to contribute.

  4. pdd23001 commented on Jun 7, 2026

    @pdd23001
    Contributor

    Julien Gacon (@Cryoris) check the latest commit on the PR. I think so I have done what you requested for.

  5. jakelishman commented on Jul 1, 2026

    @jakelishman
    Member

    Julien: we can document against this, but the root bug report is that we're panicking in Rust where we should be returning a Python-space exception. We still seem to be panicking on 2.5.0rc1, so I re-opened it if you can check?

  6. Cryoris commented on Jul 1, 2026

    @Cryoris
    Contributor

    Hmm if we want to return an exception we have to explicitly check the values, don't we? I was hoping to avoid that for the overhead it comes with 🤔

  7. jakelishman commented on Jul 1, 2026

    @jakelishman
    Member

    If you prefer to do the check in Rust space where it's cheaper, that's fine too, but PanicException is a sort of last resort from PyO3 and isn't supposedly to be handle-able - we should be emitting the error properly somehow to Python.

  8. pdd23001 commented on Jul 6, 2026

    @pdd23001
    Contributor

    Thanks Jake Lishman (@jakelishman), that makes sense. I'll move the fix into Rust so we return a proper Python exception instead of the PanicException.

    I characterised where it actually panics so we validate the right range:

    synthesis_error <= 0 panics (logarithm is not defined for 0 at exactly 0; attempt to subtract with overflow / root is complex when negative)
    synthesis_error > 1 (e.g. 1.5, 2.0) does not panic
    cache_error = 0, negative, or > 1 does not panic
    So the only value that actually crashes is synthesis_error <= 0.

    Two options for the validation-

    Minimal one: guard only synthesis_error <= 0 (fixes the crash, leaves currently working inputs alone).

    Strict one: enforce the documented [0, 1] range for both params, raising ValueError for anything outside it. This would also reject synthesis_error > 1 and out-of-range cache_error, which don't currently panic.

    Which would you prefer?

  9. Cryoris commented on Jul 7, 2026

    @Cryoris
    Contributor

    I think this needs more clarification before we can jump to action. The ranges we documented for synthesis_error don't seem to match gridsynth's internals, where the accuracy is measured in spectral norm difference, which can be in (0, 2] instead of (0, 1].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    theme: documentationSomething is not clear or an error documentationtheme: fault tolerancerelated to fault tolerance compilation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions