Skip to content

Commit

Permalink
Apply new deprecation decorators to circuit folder (#9869) (#10099)
Browse files Browse the repository at this point in the history
* 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 54f6ee8.

* Better fix for the deprecations

* Use QiskitTestCase ignore mechanism

* Review feedback

* Fix lint and test failure

(cherry picked from commit 5323d8f)

Co-authored-by: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com>
  • Loading branch information
mergify[bot] and Eric-Arellano committed May 10, 2023
1 parent 5b53031 commit cc118f3
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 65 deletions.
48 changes: 21 additions & 27 deletions qiskit/circuit/bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -59,53 +59,47 @@ 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
this attribute on bits that were not constructed as "owned" by a register."""
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
this attribute on bits that were not constructed as "owned" by a register."""
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):
Expand Down
13 changes: 7 additions & 6 deletions qiskit/circuit/classicalregister.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
28 changes: 12 additions & 16 deletions qiskit/circuit/instructionset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
*,
Expand All @@ -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
Expand All @@ -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)
)
Expand Down
14 changes: 6 additions & 8 deletions qiskit/circuit/library/arithmetic/polynomial_pauli_rotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
12 changes: 7 additions & 5 deletions qiskit/circuit/quantumregister.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion qiskit/test/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/python/circuit/test_instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cc118f3

Please sign in to comment.