Skip to content

Optimize1qGatesDecomposition deadlocks on Python-defined gates #16591

Description

@splch

Environment

  • Qiskit version: 2.5.0
  • Python version: 3.10-3.14
  • Operating system: macOS, Linux, Windows

What is happening?

Since #15567, run_optimize_1q_gates_decomposition enters rayon while holding the GIL (L457). Workers fetching a Python-defined gate's matrix (L404) block in PyGILState_Ensure while the main thread blocks on the rayon latch -> deadlock (0 % CPU, immune to Ctrl-C).

So transpile() hangs at optimization_level >= 1 for any target with Python-defined 1q gates - e.g. qiskit-ionq and qiskit-braket-provider native backends (see qiskit-community/qiskit-ionq#266).

How can we reproduce the issue?

import numpy as np
from qiskit import QuantumCircuit, generate_preset_pass_manager
from qiskit.circuit import Gate, Measure, Parameter
from qiskit.transpiler import Target

class PhasedX(Gate):
    def __init__(self, phi):
        super().__init__("phased_x", 1, [phi])
    def __array__(self, dtype=None, copy=None):
        phi = float(self.params[0])
        return np.array([[0, np.exp(-1j * phi)], [np.exp(1j * phi), 0]], dtype=complex)

target = Target(num_qubits=2)
target.add_instruction(PhasedX(Parameter("phi")))
target.add_instruction(Measure())

qc = QuantumCircuit(2)
qc.append(PhasedX(0.1), [0])
qc.append(PhasedX(0.2), [1])
qc.measure_all()

pm = generate_preset_pass_manager(optimization_level=2, target=target)
print(pm.run(qc))  # hangs

Completes on 2.4.2, or with QISKIT_IN_PARALLEL=TRUE (serial path).

What should happen?

Transpilation should complete. The pass may skip Python-defined gates, but must not deadlock.

Any suggestions?

Release the GIL around the par_iter, prefetch Python matrices before it, or fall back to serial when a run contains a Python-defined op.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions