From e67c802ff38058e4cb3cf864fb68dfb0f1abc980 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Wed, 13 Sep 2023 12:56:08 +0100 Subject: [PATCH] Do not emit warnings from `opflow` initialisation Opflow is completely deprecated, and `import qiskit.opflow` should and does raise a warning blaming the importer. However, during the import, several opflow objects are initialised, which also trigger deprecation warnings. These will correctly be blamed on internal Qiskit library code, and so will not be shown to users by default. However, test suites running with all warnings treated as errors will also see these warnings, which will mask the one true warning that should be handled by the downstream code. It would be valid for downstream code to filter out these internal warnings, but in order to make it easier for downstream code to do the correct thing immediately, this opts to silence the internal warnings during initialisation. Downstream code running with all warnings enabled will now _only_ see the warning from `import qiskit.opflow` which is the only actionable warning they should have to deal with anyway. These warnings are only filtered during the initialisation and are targetted specifically at opflow-internal deprecation warnings. In general, deprecated code will call other deprecated code and it is not necessary to filter every single usage. This particular commit is a slight compromise to make it slightly easier for downstream libraries to do the right thing. --- qiskit/opflow/operator_globals.py | 44 +++++++++++++++++++------------ 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/qiskit/opflow/operator_globals.py b/qiskit/opflow/operator_globals.py index a5afcd9cf08f..ac0c624c7287 100644 --- a/qiskit/opflow/operator_globals.py +++ b/qiskit/opflow/operator_globals.py @@ -14,6 +14,8 @@ Operator Globals """ +import warnings + from qiskit.quantum_info import Pauli from qiskit.circuit.library import CXGate, SGate, TGate, HGate, SwapGate, CZGate @@ -48,22 +50,30 @@ def make_immutable(obj): return obj -# 1-Qubit Paulis -X = make_immutable(PauliOp(Pauli("X"))) -Y = make_immutable(PauliOp(Pauli("Y"))) -Z = make_immutable(PauliOp(Pauli("Z"))) -I = make_immutable(PauliOp(Pauli("I"))) +# All the deprecation warnings triggered by these object creations correctly blame `qiskit.opflow` +# and so are not shown to users by default. However, since they are eagerly triggered at `import +# qiskit.opflow`, they obscure the one "true" warning of the import when downstream testing code is +# running with all warnings showing. The true warning that really needs attention becomes easy to +# overlook because there's so many that the downstream code didn't explicitly call. +with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=DeprecationWarning, module=r"qiskit\.opflow\.") + + # 1-Qubit Paulis + X = make_immutable(PauliOp(Pauli("X"))) + Y = make_immutable(PauliOp(Pauli("Y"))) + Z = make_immutable(PauliOp(Pauli("Z"))) + I = make_immutable(PauliOp(Pauli("I"))) -# Clifford+T, and some other common non-parameterized gates -CX = make_immutable(CircuitOp(CXGate())) -S = make_immutable(CircuitOp(SGate())) -H = make_immutable(CircuitOp(HGate())) -T = make_immutable(CircuitOp(TGate())) -Swap = make_immutable(CircuitOp(SwapGate())) -CZ = make_immutable(CircuitOp(CZGate())) + # Clifford+T, and some other common non-parameterized gates + CX = make_immutable(CircuitOp(CXGate())) + S = make_immutable(CircuitOp(SGate())) + H = make_immutable(CircuitOp(HGate())) + T = make_immutable(CircuitOp(TGate())) + Swap = make_immutable(CircuitOp(SwapGate())) + CZ = make_immutable(CircuitOp(CZGate())) -# 1-Qubit states -Zero = make_immutable(DictStateFn("0")) -One = make_immutable(DictStateFn("1")) -Plus = make_immutable(H.compose(Zero)) -Minus = make_immutable(H.compose(X).compose(Zero)) + # 1-Qubit states + Zero = make_immutable(DictStateFn("0")) + One = make_immutable(DictStateFn("1")) + Plus = make_immutable(H.compose(Zero)) + Minus = make_immutable(H.compose(X).compose(Zero))