From 382ce45c2917ea098266e946dc605e110f0d327f Mon Sep 17 00:00:00 2001 From: David Ryan Date: Wed, 1 Jul 2026 21:30:03 +0200 Subject: [PATCH] test: import qiskit eagerly in conftest to avoid patch.dict corruption 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. --- tests/conftest.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index cd0ea2a..80abfb8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1 +1,17 @@ """Pytest configuration for test suite.""" + +# Import qiskit eagerly, once, at collection time — before any test enters a +# ``patch.dict("sys.modules", ...)`` block. qiskit imports its standard-gate +# library lazily; if that first import happens *inside* a patched sys.modules +# context, patch.dict deletes the freshly-added qiskit submodule entries on +# exit, leaving qiskit half-initialized. Subsequent qiskit use then raises +# (e.g. ``TypeError: invalid input: Instruction(name='rcccx_dg', ...)``) when +# tests are run in isolation. Loading qiskit here makes that first import a +# clean no-op everywhere downstream. See issue #47. +try: + import qiskit # noqa: F401 + from qiskit import qasm2, qasm3 # noqa: F401 +except ImportError: + # qiskit is an optional dependency; tests that need it will skip/fail on + # their own terms rather than being blocked by this safeguard. + pass