From 4a239a9df4a554d1e186685f9e733cc957ad0eb6 Mon Sep 17 00:00:00 2001 From: Eric Arellano Date: Wed, 29 Mar 2023 08:51:03 -0600 Subject: [PATCH 1/8] Apply new deprecation decorators to circuit folder --- qiskit/circuit/bit.py | 42 ++++++++----------- qiskit/circuit/classicalregister.py | 13 +++--- qiskit/circuit/instructionset.py | 26 +++++------- .../arithmetic/polynomial_pauli_rotations.py | 14 +++---- qiskit/circuit/quantumregister.py | 12 +++--- 5 files changed, 48 insertions(+), 59 deletions(-) diff --git a/qiskit/circuit/bit.py b/qiskit/circuit/bit.py index 9d8cdbd0af20..e05277ea28cf 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,17 @@ def __init__(self, register=None, index=None): self._repr = f"{self.__class__.__name__}({self._register}, {self._index})" @property + @deprecate_func( + is_property=True, + since="0.17", + additional_msg=( + "Instead, use the method ``QuantumCircuit.find_bit`` to find all the containing " + "registers within a circuit and the index of the bit within the circuit." + ), + ) def register(self): """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 +77,20 @@ 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 + @deprecate_func( + is_property=True, + since="0.17", + additional_msg=( + "Instead, use the method ``QuantumCircuit.find_bit`` to find all the containing " + "registers within a circuit and the index of the bit within the circuit." + ), + ) def index(self): """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 +98,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..10c0653caf01 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,6 +95,16 @@ class InstructionSet: __slots__ = ("_instructions", "_requester") + @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__( 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 42011e0c0b59..d2b8759259a4 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 @@ -214,15 +214,13 @@ def degree(self) -> int: return 0 @property + @deprecate_func( + is_property=True, + since="0.16.0", + additional_msg="Instead, use the property ``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): From 928f73933cba75efdc2612331c26ad476d10d596 Mon Sep 17 00:00:00 2001 From: Eric Arellano Date: Thu, 30 Mar 2023 07:09:58 -0600 Subject: [PATCH 2/8] Fix what I can (blocked by production issues) --- qiskit/circuit/instructionset.py | 2 +- test/python/circuit/test_instructions.py | 16 +++++----- .../visualization/timeline/test_generators.py | 30 +++++++++++-------- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/qiskit/circuit/instructionset.py b/qiskit/circuit/instructionset.py index 10c0653caf01..d4bfefa595dc 100644 --- a/qiskit/circuit/instructionset.py +++ b/qiskit/circuit/instructionset.py @@ -105,7 +105,7 @@ class InstructionSet: "indices to be resolved incorrectly if any registers overlap.)" ), ) - def __init__( + def __init__( # pylint: disable=bad-docstring-quotes self, circuit_cregs: list[ClassicalRegister] | None = None, *, diff --git a/test/python/circuit/test_instructions.py b/test/python/circuit/test_instructions.py index 95466917d9f1..d1170f7c9d8d 100644 --- a/test/python/circuit/test_instructions.py +++ b/test/python/circuit/test_instructions.py @@ -568,7 +568,7 @@ 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.""" @@ -583,14 +583,14 @@ def dummy_requester(specifier): with self.subTest("classical register"): instruction = HGate() with self.assertWarnsRegex(DeprecationWarning, deprecated_regex): - instructions = InstructionSet(registers) + instructions = InstructionSet(circuit_cregs=registers) instructions.add(instruction, [Qubit()], []) instructions.c_if(registers[0], 0) self.assertIs(instruction.condition[0], registers[0]) with self.subTest("classical bit"): instruction = HGate() with self.assertWarnsRegex(DeprecationWarning, deprecated_regex): - instructions = InstructionSet(registers) + instructions = InstructionSet(circuit_cregs=registers) instructions.add(instruction, [Qubit()], []) instructions.c_if(registers[0][1], 0) self.assertIs(instruction.condition[0], registers[0][1]) @@ -598,7 +598,7 @@ def dummy_requester(specifier): with self.subTest("bit index", index=i): instruction = HGate() with self.assertWarnsRegex(DeprecationWarning, deprecated_regex): - instructions = InstructionSet(registers) + instructions = InstructionSet(circuit_cregs=registers) instructions.add(instruction, [Qubit()], []) instructions.c_if(i, 0) self.assertIs(instruction.condition[0], bit) @@ -606,7 +606,7 @@ def dummy_requester(specifier): with self.subTest("raises on bad register"): instruction = HGate() with self.assertWarnsRegex(DeprecationWarning, deprecated_regex): - instructions = InstructionSet(registers) + instructions = InstructionSet(circuit_cregs=registers) instructions.add(instruction, [Qubit()], []) with self.assertRaisesRegex( CircuitError, r"Condition register .* is not one of the registers known here: .*" @@ -615,7 +615,7 @@ def dummy_requester(specifier): with self.subTest("raises on bad bit"): instruction = HGate() with self.assertWarnsRegex(DeprecationWarning, deprecated_regex): - instructions = InstructionSet(registers) + instructions = InstructionSet(circuit_cregs=registers) instructions.add(instruction, [Qubit()], []) with self.assertRaisesRegex( CircuitError, "Condition bit .* is not in the registers known here: .*" @@ -624,14 +624,14 @@ def dummy_requester(specifier): with self.subTest("raises on bad index"): instruction = HGate() with self.assertWarnsRegex(DeprecationWarning, deprecated_regex): - instructions = InstructionSet(registers) + instructions = InstructionSet(circuit_cregs=registers) instructions.add(instruction, [Qubit()], []) with self.assertRaisesRegex(CircuitError, r"Bit index .* is out-of-range\."): instructions.c_if(len(bits), 0) with self.subTest("raises on bad type"): instruction = HGate() with self.assertWarnsRegex(DeprecationWarning, deprecated_regex): - instructions = InstructionSet(registers) + instructions = InstructionSet(circuit_cregs=registers) instructions.add(instruction, [Qubit()], []) with self.assertRaisesRegex(CircuitError, r"Invalid classical condition\. .*"): instructions.c_if([0], 0) diff --git a/test/python/visualization/timeline/test_generators.py b/test/python/visualization/timeline/test_generators.py index 7089bbc618d1..a8b976107759 100644 --- a/test/python/visualization/timeline/test_generators.py +++ b/test/python/visualization/timeline/test_generators.py @@ -55,15 +55,17 @@ def test_gen_sched_gate_with_finite_duration(self): [-0.5 * self.formatter["box_height.gate"], 0.5 * self.formatter["box_height.gate"]], ) self.assertListEqual(drawing_obj.bits, [self.qubit]) - ref_meta = { - "name": "u3", - "label": "n/a", - "bits": str(self.qubit.register.name), - "t0": 100, - "duration": 20, - "unitary": "[[1.+0.j 0.-0.j]\n [0.+0.j 1.+0.j]]", - "parameters": "0, 0, 0", - } + # TODO: rewrite this to not use the deprecated `register` property. + with self.assertWarns(DeprecationWarning): + ref_meta = { + "name": "u3", + "label": "n/a", + "bits": str(self.qubit.register.name), + "t0": 100, + "duration": 20, + "unitary": "[[1.+0.j 0.-0.j]\n [0.+0.j 1.+0.j]]", + "parameters": "0, 0, 0", + } self.assertDictEqual(ref_meta, drawing_obj.meta) ref_styles = { @@ -244,10 +246,12 @@ def test_gen_bit_name(self): self.assertListEqual(list(drawing_obj.xvals), [types.AbstractCoordinate.LEFT]) self.assertListEqual(list(drawing_obj.yvals), [0]) self.assertListEqual(drawing_obj.bits, [self.qubit]) - self.assertEqual(drawing_obj.text, str(self.qubit.register.name)) - ref_latex = r"{{\rm {register}}}_{{{index}}}".format( - register=self.qubit.register.prefix, index=self.qubit.index - ) + # TODO: rewrite this to not use the deprecated `register` property. + with self.assertWarns(DeprecationWarning): + self.assertEqual(drawing_obj.text, str(self.qubit.register.name)) + ref_latex = r"{{\rm {register}}}_{{{index}}}".format( + register=self.qubit.register.prefix, index=self.qubit.index + ) self.assertEqual(drawing_obj.latex, ref_latex) ref_styles = { From 54f6ee81e524ba42ce127da72adc41bf9c72aa48 Mon Sep 17 00:00:00 2001 From: Eric Arellano Date: Thu, 30 Mar 2023 09:42:48 -0600 Subject: [PATCH 3/8] Work around tests using bad production code --- .../visualization/test_circuit_latex.py | 22 ++++++++++++++----- .../visualization/timeline/test_layouts.py | 5 ++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/test/python/visualization/test_circuit_latex.py b/test/python/visualization/test_circuit_latex.py index ed95ab64d515..ae4455090713 100644 --- a/test/python/visualization/test_circuit_latex.py +++ b/test/python/visualization/test_circuit_latex.py @@ -141,7 +141,10 @@ def test_teleport(self): circuit.x(qr[2]).c_if(cr, 2) circuit.measure(qr[2], cr[2]) - circuit_drawer(circuit, filename=filename, output="latex_source") + # TODO(#9880): This is a hack to work-around production code incorrectly using a + # deprecated property. Remove the assertWarns once fixed. + with self.assertWarns(DeprecationWarning): + circuit_drawer(circuit, filename=filename, output="latex_source") self.assertEqualToReference(filename) @@ -197,7 +200,10 @@ def test_conditional(self): circuit.measure(qr, cr) circuit.h(qr[0]).c_if(cr, 2) - circuit_drawer(circuit, filename=filename, output="latex_source") + # TODO(#9880): This is a hack to work-around production code incorrectly using a + # deprecated property. Remove the assertWarns once fixed. + with self.assertWarns(DeprecationWarning): + circuit_drawer(circuit, filename=filename, output="latex_source") self.assertEqualToReference(filename) @@ -557,7 +563,10 @@ def test_meas_condition(self): circuit.h(qr[0]) circuit.measure(qr[0], cr[0]) circuit.h(qr[1]).c_if(cr, 1) - circuit_drawer(circuit, filename=filename, output="latex_source") + # TODO(#9880): This is a hack to work-around production code incorrectly using a + # deprecated property. Remove the assertWarns once fixed. + with self.assertWarns(DeprecationWarning): + circuit_drawer(circuit, filename=filename, output="latex_source") self.assertEqualToReference(filename) @@ -627,8 +636,11 @@ def test_measures_with_conditions(self): circuit.measure(0, cr1[1]) circuit.measure(1, cr2[0]).c_if(cr1, 1) circuit.h(0).c_if(cr2, 3) - circuit_drawer(circuit, cregbundle=False, filename=filename1, output="latex_source") - circuit_drawer(circuit, cregbundle=True, filename=filename2, output="latex_source") + # TODO(#9880): This is a hack to work-around production code incorrectly using a + # deprecated property. Remove the assertWarns once fixed. + with self.assertWarns(DeprecationWarning): + circuit_drawer(circuit, cregbundle=False, filename=filename1, output="latex_source") + circuit_drawer(circuit, cregbundle=True, filename=filename2, output="latex_source") self.assertEqualToReference(filename1) self.assertEqualToReference(filename2) diff --git a/test/python/visualization/timeline/test_layouts.py b/test/python/visualization/timeline/test_layouts.py index eb49619eb357..6416878289f4 100644 --- a/test/python/visualization/timeline/test_layouts.py +++ b/test/python/visualization/timeline/test_layouts.py @@ -45,7 +45,10 @@ def test_qreg_creg_ascending(self): def test_qreg_creg_descending(self): """Test qreg_creg_descending layout function.""" - sorted_regs = layouts.qreg_creg_descending(self.regs) + # TODO(#9880): This is a hack to work-around production code incorrectly using a + # deprecated property. Remove the assertWarns once fixed. + with self.assertWarns(DeprecationWarning): + sorted_regs = layouts.qreg_creg_descending(self.regs) ref_regs = [ self.regs[2], self.regs[1], From de64cde8efca4baa8db2ce556d88bc5b20a76425 Mon Sep 17 00:00:00 2001 From: Eric Arellano Date: Thu, 30 Mar 2023 15:22:58 -0600 Subject: [PATCH 4/8] Revert "Work around tests using bad production code" This reverts commit 54f6ee81e524ba42ce127da72adc41bf9c72aa48. --- .../visualization/test_circuit_latex.py | 22 +++++-------------- .../visualization/timeline/test_layouts.py | 5 +---- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/test/python/visualization/test_circuit_latex.py b/test/python/visualization/test_circuit_latex.py index ae4455090713..ed95ab64d515 100644 --- a/test/python/visualization/test_circuit_latex.py +++ b/test/python/visualization/test_circuit_latex.py @@ -141,10 +141,7 @@ def test_teleport(self): circuit.x(qr[2]).c_if(cr, 2) circuit.measure(qr[2], cr[2]) - # TODO(#9880): This is a hack to work-around production code incorrectly using a - # deprecated property. Remove the assertWarns once fixed. - with self.assertWarns(DeprecationWarning): - circuit_drawer(circuit, filename=filename, output="latex_source") + circuit_drawer(circuit, filename=filename, output="latex_source") self.assertEqualToReference(filename) @@ -200,10 +197,7 @@ def test_conditional(self): circuit.measure(qr, cr) circuit.h(qr[0]).c_if(cr, 2) - # TODO(#9880): This is a hack to work-around production code incorrectly using a - # deprecated property. Remove the assertWarns once fixed. - with self.assertWarns(DeprecationWarning): - circuit_drawer(circuit, filename=filename, output="latex_source") + circuit_drawer(circuit, filename=filename, output="latex_source") self.assertEqualToReference(filename) @@ -563,10 +557,7 @@ def test_meas_condition(self): circuit.h(qr[0]) circuit.measure(qr[0], cr[0]) circuit.h(qr[1]).c_if(cr, 1) - # TODO(#9880): This is a hack to work-around production code incorrectly using a - # deprecated property. Remove the assertWarns once fixed. - with self.assertWarns(DeprecationWarning): - circuit_drawer(circuit, filename=filename, output="latex_source") + circuit_drawer(circuit, filename=filename, output="latex_source") self.assertEqualToReference(filename) @@ -636,11 +627,8 @@ def test_measures_with_conditions(self): circuit.measure(0, cr1[1]) circuit.measure(1, cr2[0]).c_if(cr1, 1) circuit.h(0).c_if(cr2, 3) - # TODO(#9880): This is a hack to work-around production code incorrectly using a - # deprecated property. Remove the assertWarns once fixed. - with self.assertWarns(DeprecationWarning): - circuit_drawer(circuit, cregbundle=False, filename=filename1, output="latex_source") - circuit_drawer(circuit, cregbundle=True, filename=filename2, output="latex_source") + circuit_drawer(circuit, cregbundle=False, filename=filename1, output="latex_source") + circuit_drawer(circuit, cregbundle=True, filename=filename2, output="latex_source") self.assertEqualToReference(filename1) self.assertEqualToReference(filename2) diff --git a/test/python/visualization/timeline/test_layouts.py b/test/python/visualization/timeline/test_layouts.py index 6416878289f4..eb49619eb357 100644 --- a/test/python/visualization/timeline/test_layouts.py +++ b/test/python/visualization/timeline/test_layouts.py @@ -45,10 +45,7 @@ def test_qreg_creg_ascending(self): def test_qreg_creg_descending(self): """Test qreg_creg_descending layout function.""" - # TODO(#9880): This is a hack to work-around production code incorrectly using a - # deprecated property. Remove the assertWarns once fixed. - with self.assertWarns(DeprecationWarning): - sorted_regs = layouts.qreg_creg_descending(self.regs) + sorted_regs = layouts.qreg_creg_descending(self.regs) ref_regs = [ self.regs[2], self.regs[1], From 1122627b465bc37b3cf94edf8a5b356a6672726b Mon Sep 17 00:00:00 2001 From: Eric Arellano Date: Thu, 30 Mar 2023 15:27:12 -0600 Subject: [PATCH 5/8] Better fix for the deprecations --- qiskit/test/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit/test/base.py b/qiskit/test/base.py index ec7379613669..1decdcdbb08d 100644 --- a/qiskit/test/base.py +++ b/qiskit/test/base.py @@ -211,7 +211,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 From 3988b3327ff6ff1b051a69b9d77176ae361fbed8 Mon Sep 17 00:00:00 2001 From: Eric Arellano Date: Fri, 7 Apr 2023 06:30:17 -0600 Subject: [PATCH 6/8] Use QiskitTestCase ignore mechanism --- .../visualization/timeline/test_generators.py | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/test/python/visualization/timeline/test_generators.py b/test/python/visualization/timeline/test_generators.py index a8b976107759..7089bbc618d1 100644 --- a/test/python/visualization/timeline/test_generators.py +++ b/test/python/visualization/timeline/test_generators.py @@ -55,17 +55,15 @@ def test_gen_sched_gate_with_finite_duration(self): [-0.5 * self.formatter["box_height.gate"], 0.5 * self.formatter["box_height.gate"]], ) self.assertListEqual(drawing_obj.bits, [self.qubit]) - # TODO: rewrite this to not use the deprecated `register` property. - with self.assertWarns(DeprecationWarning): - ref_meta = { - "name": "u3", - "label": "n/a", - "bits": str(self.qubit.register.name), - "t0": 100, - "duration": 20, - "unitary": "[[1.+0.j 0.-0.j]\n [0.+0.j 1.+0.j]]", - "parameters": "0, 0, 0", - } + ref_meta = { + "name": "u3", + "label": "n/a", + "bits": str(self.qubit.register.name), + "t0": 100, + "duration": 20, + "unitary": "[[1.+0.j 0.-0.j]\n [0.+0.j 1.+0.j]]", + "parameters": "0, 0, 0", + } self.assertDictEqual(ref_meta, drawing_obj.meta) ref_styles = { @@ -246,12 +244,10 @@ def test_gen_bit_name(self): self.assertListEqual(list(drawing_obj.xvals), [types.AbstractCoordinate.LEFT]) self.assertListEqual(list(drawing_obj.yvals), [0]) self.assertListEqual(drawing_obj.bits, [self.qubit]) - # TODO: rewrite this to not use the deprecated `register` property. - with self.assertWarns(DeprecationWarning): - self.assertEqual(drawing_obj.text, str(self.qubit.register.name)) - ref_latex = r"{{\rm {register}}}_{{{index}}}".format( - register=self.qubit.register.prefix, index=self.qubit.index - ) + self.assertEqual(drawing_obj.text, str(self.qubit.register.name)) + ref_latex = r"{{\rm {register}}}_{{{index}}}".format( + register=self.qubit.register.prefix, index=self.qubit.index + ) self.assertEqual(drawing_obj.latex, ref_latex) ref_styles = { From 23c19f19c99d0a26fdaa704cb615b969c12f6407 Mon Sep 17 00:00:00 2001 From: Eric Arellano Date: Tue, 2 May 2023 11:24:23 -0600 Subject: [PATCH 7/8] Review feedback --- qiskit/circuit/bit.py | 10 ++++++---- qiskit/circuit/instructionset.py | 4 ++-- .../arithmetic/polynomial_pauli_rotations.py | 2 +- test/python/circuit/test_instructions.py | 14 +++++++------- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/qiskit/circuit/bit.py b/qiskit/circuit/bit.py index e05277ea28cf..0635741c9d7e 100644 --- a/qiskit/circuit/bit.py +++ b/qiskit/circuit/bit.py @@ -63,8 +63,9 @@ def __init__(self, register=None, index=None): is_property=True, since="0.17", additional_msg=( - "Instead, use the method ``QuantumCircuit.find_bit`` to find all the containing " - "registers within a circuit and the index of the bit within the circuit." + "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): @@ -84,8 +85,9 @@ def register(self): is_property=True, since="0.17", additional_msg=( - "Instead, use the method ``QuantumCircuit.find_bit`` to find all the containing " - "registers within a circuit and the index of the bit within the circuit." + "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): diff --git a/qiskit/circuit/instructionset.py b/qiskit/circuit/instructionset.py index d4bfefa595dc..b1a06c386078 100644 --- a/qiskit/circuit/instructionset.py +++ b/qiskit/circuit/instructionset.py @@ -100,9 +100,9 @@ class InstructionSet: 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 " + "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.)" + "indices to be resolved incorrectly if any registers overlap." ), ) def __init__( # pylint: disable=bad-docstring-quotes diff --git a/qiskit/circuit/library/arithmetic/polynomial_pauli_rotations.py b/qiskit/circuit/library/arithmetic/polynomial_pauli_rotations.py index d2b8759259a4..ac2c6545a938 100644 --- a/qiskit/circuit/library/arithmetic/polynomial_pauli_rotations.py +++ b/qiskit/circuit/library/arithmetic/polynomial_pauli_rotations.py @@ -217,7 +217,7 @@ def degree(self) -> int: @deprecate_func( is_property=True, since="0.16.0", - additional_msg="Instead, use the property ``num_ancillas``.", + additional_msg="Instead, use the property :attr:`~num_ancillas`.", ) def num_ancilla_qubits(self): """Deprecated. Use num_ancillas instead.""" diff --git a/test/python/circuit/test_instructions.py b/test/python/circuit/test_instructions.py index d1170f7c9d8d..f8145e01c272 100644 --- a/test/python/circuit/test_instructions.py +++ b/test/python/circuit/test_instructions.py @@ -583,14 +583,14 @@ def dummy_requester(specifier): with self.subTest("classical register"): instruction = HGate() with self.assertWarnsRegex(DeprecationWarning, deprecated_regex): - instructions = InstructionSet(circuit_cregs=registers) + instructions = InstructionSet(registers) instructions.add(instruction, [Qubit()], []) instructions.c_if(registers[0], 0) self.assertIs(instruction.condition[0], registers[0]) with self.subTest("classical bit"): instruction = HGate() with self.assertWarnsRegex(DeprecationWarning, deprecated_regex): - instructions = InstructionSet(circuit_cregs=registers) + instructions = InstructionSet(registers) instructions.add(instruction, [Qubit()], []) instructions.c_if(registers[0][1], 0) self.assertIs(instruction.condition[0], registers[0][1]) @@ -598,7 +598,7 @@ def dummy_requester(specifier): with self.subTest("bit index", index=i): instruction = HGate() with self.assertWarnsRegex(DeprecationWarning, deprecated_regex): - instructions = InstructionSet(circuit_cregs=registers) + instructions = InstructionSet(registers) instructions.add(instruction, [Qubit()], []) instructions.c_if(i, 0) self.assertIs(instruction.condition[0], bit) @@ -606,7 +606,7 @@ def dummy_requester(specifier): with self.subTest("raises on bad register"): instruction = HGate() with self.assertWarnsRegex(DeprecationWarning, deprecated_regex): - instructions = InstructionSet(circuit_cregs=registers) + instructions = InstructionSet(registers) instructions.add(instruction, [Qubit()], []) with self.assertRaisesRegex( CircuitError, r"Condition register .* is not one of the registers known here: .*" @@ -615,7 +615,7 @@ def dummy_requester(specifier): with self.subTest("raises on bad bit"): instruction = HGate() with self.assertWarnsRegex(DeprecationWarning, deprecated_regex): - instructions = InstructionSet(circuit_cregs=registers) + instructions = InstructionSet(registers) instructions.add(instruction, [Qubit()], []) with self.assertRaisesRegex( CircuitError, "Condition bit .* is not in the registers known here: .*" @@ -624,14 +624,14 @@ def dummy_requester(specifier): with self.subTest("raises on bad index"): instruction = HGate() with self.assertWarnsRegex(DeprecationWarning, deprecated_regex): - instructions = InstructionSet(circuit_cregs=registers) + instructions = InstructionSet(registers) instructions.add(instruction, [Qubit()], []) with self.assertRaisesRegex(CircuitError, r"Bit index .* is out-of-range\."): instructions.c_if(len(bits), 0) with self.subTest("raises on bad type"): instruction = HGate() with self.assertWarnsRegex(DeprecationWarning, deprecated_regex): - instructions = InstructionSet(circuit_cregs=registers) + instructions = InstructionSet(registers) instructions.add(instruction, [Qubit()], []) with self.assertRaisesRegex(CircuitError, r"Invalid classical condition\. .*"): instructions.c_if([0], 0) From 5400750708f423733f14f996541076f9bd7d0f9a Mon Sep 17 00:00:00 2001 From: Eric Arellano Date: Tue, 2 May 2023 15:05:34 -0600 Subject: [PATCH 8/8] Fix lint and test failure --- qiskit/circuit/bit.py | 4 ++-- test/python/circuit/test_instructions.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/qiskit/circuit/bit.py b/qiskit/circuit/bit.py index 0635741c9d7e..c6c8db35ea65 100644 --- a/qiskit/circuit/bit.py +++ b/qiskit/circuit/bit.py @@ -68,7 +68,7 @@ def __init__(self, register=None, index=None): "circuit." ), ) - def register(self): + def register(self): # pylint: disable=bad-docstring-quotes """Get the register of an old-style bit. In modern Qiskit Terra (version 0.17+), bits are the fundamental object and registers are @@ -90,7 +90,7 @@ def register(self): "circuit." ), ) - def index(self): + def index(self): # pylint: disable=bad-docstring-quotes """Get the index of an old-style bit in the register that owns it. In modern Qiskit Terra (version 0.17+), bits are the fundamental object and registers are diff --git a/test/python/circuit/test_instructions.py b/test/python/circuit/test_instructions.py index f8145e01c272..bb4b957d66dc 100644 --- a/test/python/circuit/test_instructions.py +++ b/test/python/circuit/test_instructions.py @@ -575,7 +575,7 @@ def dummy_requester(specifier): 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)