From cc118f3a8ffffbc3d78896e53fa1f5ccfae1afe4 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 10 May 2023 22:59:25 +0000 Subject: [PATCH] Apply new deprecation decorators to circuit folder (#9869) (#10099) * Apply new deprecation decorators to circuit folder * Fix what I can (blocked by production issues) * Work around tests using bad production code * Revert "Work around tests using bad production code" This reverts commit 54f6ee81e524ba42ce127da72adc41bf9c72aa48. * Better fix for the deprecations * Use QiskitTestCase ignore mechanism * Review feedback * Fix lint and test failure (cherry picked from commit 5323d8f460ed5f1e7b4040feeedd48950de818d6) Co-authored-by: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> --- qiskit/circuit/bit.py | 48 ++++++++----------- qiskit/circuit/classicalregister.py | 13 ++--- qiskit/circuit/instructionset.py | 28 +++++------ .../arithmetic/polynomial_pauli_rotations.py | 14 +++--- qiskit/circuit/quantumregister.py | 12 +++-- qiskit/test/base.py | 2 +- test/python/circuit/test_instructions.py | 4 +- 7 files changed, 56 insertions(+), 65 deletions(-) diff --git a/qiskit/circuit/bit.py b/qiskit/circuit/bit.py index 9d8cdbd0af20..c6c8db35ea65 100644 --- a/qiskit/circuit/bit.py +++ b/qiskit/circuit/bit.py @@ -13,9 +13,9 @@ """ Quantum bit and Classical bit objects. """ -import warnings from qiskit.circuit.exceptions import CircuitError +from qiskit.utils.deprecation import deprecate_func class Bit: @@ -59,12 +59,18 @@ def __init__(self, register=None, index=None): self._repr = f"{self.__class__.__name__}({self._register}, {self._index})" @property - def register(self): + @deprecate_func( + is_property=True, + since="0.17", + additional_msg=( + "Instead, use :meth:`~qiskit.circuit.quantumcircuit.QuantumCircuit.find_bit` to find " + "all the containing registers within a circuit and the index of the bit within the " + "circuit." + ), + ) + def register(self): # pylint: disable=bad-docstring-quotes """Get the register of an old-style bit. - .. deprecated:: 0.17 - Use :meth:`.QuantumCircuit.find_bit` instead. - In modern Qiskit Terra (version 0.17+), bits are the fundamental object and registers are aliases to collections of bits. A bit can be in many registers depending on the circuit, so a single containing register is no longer a property of a bit. It is an error to access @@ -72,24 +78,21 @@ def register(self): if (self._register, self._index) == (None, None): raise CircuitError("Attempt to query register of a new-style Bit.") - warnings.warn( - "'Bit.register' is deprecated since Qiskit Terra 0.17 and will be removed " - "in a future release. Bits may be in more than one register. " - "Use 'QuantumCircuit.find_bit' to find all the containing registers within a circuit, " - "and the index of the bit within the circuit.", - DeprecationWarning, - stacklevel=2, - ) - return self._register @property - def index(self): + @deprecate_func( + is_property=True, + since="0.17", + additional_msg=( + "Instead, use :meth:`~qiskit.circuit.quantumcircuit.QuantumCircuit.find_bit` to find " + "all the containing registers within a circuit and the index of the bit within the " + "circuit." + ), + ) + def index(self): # pylint: disable=bad-docstring-quotes """Get the index of an old-style bit in the register that owns it. - .. deprecated:: 0.17 - Use :meth:`.QuantumCircuit.find_bit` instead. - In modern Qiskit Terra (version 0.17+), bits are the fundamental object and registers are aliases to collections of bits. A bit can be in many registers depending on the circuit, so a single containing register is no longer a property of a bit. It is an error to access @@ -97,15 +100,6 @@ def index(self): if (self._register, self._index) == (None, None): raise CircuitError("Attempt to query index of a new-style Bit.") - warnings.warn( - "'Bit.index' is deprecated since Qiskit Terra 0.17 and will be removed " - "in a future release. Bits may be in more than one register. " - "Use 'QuantumCircuit.find_bit' to find all the containing registers within a circuit, " - "and the index of the bit within the circuit.", - DeprecationWarning, - stacklevel=2, - ) - return self._index def __repr__(self): diff --git a/qiskit/circuit/classicalregister.py b/qiskit/circuit/classicalregister.py index 19aeda7aeac1..644705ffee34 100644 --- a/qiskit/circuit/classicalregister.py +++ b/qiskit/circuit/classicalregister.py @@ -19,8 +19,7 @@ from qiskit.circuit.exceptions import CircuitError -# Over-specific import to avoid cyclic imports. -from qiskit.utils.deprecation import deprecate_function +from qiskit.utils.deprecation import deprecate_func from .register import Register from .bit import Bit @@ -58,10 +57,12 @@ class ClassicalRegister(Register): prefix = "c" bit_type = Clbit - @deprecate_function( - "Register.qasm() is deprecated since Terra 0.23, as correct exporting to OpenQASM 2 is " - "the responsibility of a larger exporter; it cannot safely be done on an object-by-object " - "basis without context. No replacement will be provided, because the premise is wrong.", + @deprecate_func( + additional_msg=( + "Correct exporting to OpenQASM 2 is the responsibility of a larger exporter; it cannot " + "safely be done on an object-by-object basis without context. No replacement will be " + "provided, because the premise is wrong." + ), since="0.23.0", ) def qasm(self): diff --git a/qiskit/circuit/instructionset.py b/qiskit/circuit/instructionset.py index 24a5ef9d4279..b1a06c386078 100644 --- a/qiskit/circuit/instructionset.py +++ b/qiskit/circuit/instructionset.py @@ -16,10 +16,10 @@ from __future__ import annotations import functools -import warnings from typing import Callable from qiskit.circuit.exceptions import CircuitError +from qiskit.utils.deprecation import deprecate_arg from .classicalregister import Clbit, ClassicalRegister from .operation import Operation from .quantumcircuitdata import CircuitInstruction @@ -95,7 +95,17 @@ class InstructionSet: __slots__ = ("_instructions", "_requester") - def __init__( + @deprecate_arg( + "circuit_cregs", + since="0.19.0", + additional_msg=( + "Instead, pass a complete resource requester with the 'resource_requester' argument. " + "The classical registers are insufficient to access all classical resources in a " + "circuit, as there may be loose classical bits as well. It can also cause integer " + "indices to be resolved incorrectly if any registers overlap." + ), + ) + def __init__( # pylint: disable=bad-docstring-quotes self, circuit_cregs: list[ClassicalRegister] | None = None, *, @@ -109,13 +119,6 @@ def __init__( Args: circuit_cregs (list[ClassicalRegister]): Optional. List of ``cregs`` of the circuit to which the instruction is added. Default: `None`. - - .. deprecated:: 0.19 - The classical registers are insufficient to access all classical resources in a - circuit, as there may be loose classical bits as well. It can also cause - integer indices to be resolved incorrectly if any registers overlap. Instead, - pass a complete requester to the ``resource_requester`` argument. - resource_requester: A callable that takes in the classical resource used in the condition, verifies that it is present in the attached circuit, resolves any indices into concrete :obj:`.Clbit` instances, and returns the concrete resource. If this @@ -136,13 +139,6 @@ def __init__( if circuit_cregs is not None: if resource_requester is not None: raise CircuitError("Cannot pass both 'circuit_cregs' and 'resource_requester'.") - warnings.warn( - "The 'circuit_cregs' argument to 'InstructionSet' is deprecated as of" - " qiskit-terra 0.19, and will be removed no sooner than 3 months after its release." - " Pass a complete resource requester as the 'resource_requester' instead.", - DeprecationWarning, - stacklevel=2, - ) self._requester: Callable[..., ClassicalRegister | Clbit] = _requester_from_cregs( tuple(circuit_cregs) ) diff --git a/qiskit/circuit/library/arithmetic/polynomial_pauli_rotations.py b/qiskit/circuit/library/arithmetic/polynomial_pauli_rotations.py index dfd0191b2447..c1194a9555c5 100644 --- a/qiskit/circuit/library/arithmetic/polynomial_pauli_rotations.py +++ b/qiskit/circuit/library/arithmetic/polynomial_pauli_rotations.py @@ -14,12 +14,12 @@ """Polynomially controlled Pauli-rotations.""" from __future__ import annotations -import warnings from itertools import product from qiskit.circuit import QuantumRegister, QuantumCircuit from qiskit.circuit.exceptions import CircuitError +from qiskit.utils.deprecation import deprecate_func from .functional_pauli_rotations import FunctionalPauliRotations @@ -225,15 +225,13 @@ def degree(self) -> int: return 0 @property + @deprecate_func( + is_property=True, + since="0.16.0", + additional_msg="Instead, use the property :attr:`~num_ancillas`.", + ) def num_ancilla_qubits(self): """Deprecated. Use num_ancillas instead.""" - warnings.warn( - "The PolynomialPauliRotations.num_ancilla_qubits property is deprecated " - "as of 0.16.0. It will be removed no earlier than 3 months after the release " - "date. You should use the num_ancillas property instead.", - DeprecationWarning, - stacklevel=2, - ) return self.num_ancillas def _reset_registers(self, num_state_qubits): diff --git a/qiskit/circuit/quantumregister.py b/qiskit/circuit/quantumregister.py index 77e681528fd0..de87f73a79a3 100644 --- a/qiskit/circuit/quantumregister.py +++ b/qiskit/circuit/quantumregister.py @@ -20,7 +20,7 @@ from qiskit.circuit.exceptions import CircuitError # Over-specific import to avoid cyclic imports. -from qiskit.utils.deprecation import deprecate_function +from qiskit.utils.deprecation import deprecate_func from .register import Register from .bit import Bit @@ -58,10 +58,12 @@ class QuantumRegister(Register): prefix = "q" bit_type = Qubit - @deprecate_function( - "Register.qasm() is deprecated since Terra 0.23, as correct exporting to OpenQASM 2 is " - "the responsibility of a larger exporter; it cannot safely be done on an object-by-object " - "basis without context. No replacement will be provided, because the premise is wrong.", + @deprecate_func( + additional_msg=( + "Correct exporting to OpenQASM 2 is the responsibility of a larger exporter; it cannot " + "safely be done on an object-by-object basis without context. No replacement will be " + "provided, because the premise is wrong." + ), since="0.23.0", ) def qasm(self): diff --git a/qiskit/test/base.py b/qiskit/test/base.py index ac9da7a02a2d..870d17cfb535 100644 --- a/qiskit/test/base.py +++ b/qiskit/test/base.py @@ -227,7 +227,7 @@ def setUpClass(cls): r"elementwise comparison failed.*", r"The jsonschema validation included in qiskit-terra.*", r"The DerivativeBase.parameter_expression_grad method.*", - r"'Bit\.(register|index)' is deprecated.*", + r"The property ``qiskit\.circuit\.bit\.Bit\.(register|index)`` is deprecated.*", r"The CXDirection pass has been deprecated", r"The pauli_basis function with PauliTable.*", # Caused by internal scikit-learn scipy usage diff --git a/test/python/circuit/test_instructions.py b/test/python/circuit/test_instructions.py index 95466917d9f1..bb4b957d66dc 100644 --- a/test/python/circuit/test_instructions.py +++ b/test/python/circuit/test_instructions.py @@ -568,14 +568,14 @@ def test_instructionset_c_if_deprecated_resolution(self): registers = [ClassicalRegister(2), ClassicalRegister(3), ClassicalRegister(1)] bits = [bit for register in registers for bit in register] - deprecated_regex = r"The 'circuit_cregs' argument to 'InstructionSet' is deprecated .*" + deprecated_regex = r".* argument ``circuit_cregs`` is deprecated .*" def dummy_requester(specifier): """A dummy requester that technically fulfills the spec.""" raise CircuitError with self.subTest("cannot pass both registers and requester"): - with self.assertRaisesRegex( + with self.assertWarns(DeprecationWarning), self.assertRaisesRegex( CircuitError, r"Cannot pass both 'circuit_cregs' and 'resource_requester'\." ): InstructionSet(registers, resource_requester=dummy_requester)