Skip to content

test: import qiskit eagerly in conftest to fix test_simulation isolation failures#49

Merged
ddri merged 1 commit into
mainfrom
fix/test-qiskit-import-pollution
Jul 2, 2026
Merged

test: import qiskit eagerly in conftest to fix test_simulation isolation failures#49
ddri merged 1 commit into
mainfrom
fix/test-qiskit-import-pollution

Conversation

@ddri

@ddri ddri commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes the qiskit half of #47: tests/test_simulation.py fails 2 tests when the file is run in isolation (e.g. pytest tests/test_simulation.py), while passing in full-suite runs. (The full-suite segfault part of #47 was a separate native-reload bug, already fixed by #46.)

Root cause

TestSimulationExecutor runs execute() inside patch.dict("sys.modules", {"qristal": ..., "qristal.core": ...}). Inside that block, circuit.to_openqasm() triggers qiskit's first import (from qiskit import qasm2, qasm3). qiskit loads its standard-gate library lazily, so a batch of qiskit.* submodules get added to sys.modules during the block — and patch.dict deletes them on exit because they weren't in the entry snapshot. qiskit is left half-initialized, so the next gate-class construction fails:

TypeError: invalid input: Instruction(name='rcccx_dg', num_qubits=4, num_clbits=0, params=[])

In full-suite runs an earlier file imports qiskit cleanly first, so the in-block import is a no-op and the failure is masked — which is why it only shows up in isolation.

Fix

Import qiskit once at collection time in tests/conftest.py, before any test can enter a patched sys.modules context. Guarded with try/except ImportError since qiskit is an optional dependency.

Verification

  • pytest tests/test_simulation.py (isolation): 2 failed → 39 passed
  • pytest tests/ (full suite): 414 passed, 16 skipped, exit 0 (unchanged; no regression)

Closes #47.


Note

Low Risk
Test-only collection-time import with optional-dependency guard; no production or security impact.

Overview
Fixes isolated failures in tests/test_simulation.py (issue #47) by eagerly importing qiskit and qasm2/qasm3 at pytest collection time in tests/conftest.py.

When SimulationExecutor tests run inside patch.dict("sys.modules", ...), a first-time lazy qiskit import can add submodules that patch.dict removes on exit, leaving qiskit half-initialized and causing gate/Instruction errors on later use. Full-suite runs often mask this because another test imports qiskit first.

The new imports are wrapped in try/except ImportError so optional qiskit absence does not break collection.

Reviewed by Cursor Bugbot for commit 382ce45. Bugbot is set up for automated code reviews on this repo. Configure here.

test_simulation.py triggers qiskit's first import inside a
patch.dict("sys.modules", ...) block; on exit patch.dict deletes the
freshly-added qiskit submodule entries, leaving qiskit half-initialized
and failing its own tests when the file is run in isolation. Importing
qiskit once at collection time makes that first import a clean no-op.

Refs #47.
@ddri ddri merged commit 3993aba into main Jul 2, 2026
3 checks passed
@ddri ddri deleted the fix/test-qiskit-import-pollution branch July 2, 2026 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test_simulation.py: order-dependent failures + full-suite segfault from qiskit import inside patch.dict(sys.modules)

1 participant