Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bad deprecation of Register.name_format (backport #9494) #9497

Merged
merged 1 commit into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions qiskit/circuit/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,35 @@
"""
import re
import itertools
import warnings
import numpy as np

from qiskit.circuit.exceptions import CircuitError

# Over-specific import to avoid cyclic imports.
from qiskit.utils.deprecation import deprecate_function

class _NameFormat:
REGEX = re.compile("[a-z][a-zA-Z0-9_]*")

def __get__(self, obj, objtype=None):
warnings.warn(
"Register.name_format is deprecated as of Qiskit Terra 0.23, and will be removed in a"
" future release. There is no longer a restriction on the names of registers, so the"
" attribute has no meaning any more.",
DeprecationWarning,
stacklevel=2,
)
return self.REGEX


class Register:
"""Implement a generic register."""

__slots__ = ["_name", "_size", "_bits", "_bit_indices", "_hash", "_repr"]

_name_format = re.compile("[a-z][a-zA-Z0-9_]*")

# In historical version of Terra, registers' name had to conform to the OpenQASM 2 specification
# (see appendix A of https://arxiv.org/pdf/1707.03429v2.pdf), and this regex enforced it. That
# restriction has been relaxed, so this is no longer necessary.
@property
@deprecate_function(
"Register.name_format is deprecated as of Qiskit Terra 0.23, and will be removed in a"
" future release. There is no longer a restriction on the names of registers, so the"
" attribute has no meaning any more."
)
def name_format(self):
# pylint: disable=missing-function-docstring
return self._name_format
name_format = _NameFormat()

# Counter for the number of instances in this class.
instances_counter = itertools.count()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
fixes:
- |
Fixed a bad deprecation of :attr:`.Register.name_format` which had made the class attribute
available only from instances and not the class. When trying to send dynamic-circuits jobs to
hardware backends, this would frequently cause the error::
AttributeError: 'property' object has no attribute 'match'
Fixed `#9493 <https://github.com/Qiskit/qiskit-terra/issues/9493>`__.
12 changes: 12 additions & 0 deletions test/python/circuit/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,15 @@ def test_newstyle_register_eq(self, reg_type):
bits_difftype = [difftype.bit_type() for _ in range(3)]
reg_difftype = difftype(name="foo", bits=bits_difftype)
self.assertNotEqual(reg_difftype, test_reg)

@data(QuantumRegister, ClassicalRegister, AncillaRegister)
def test_register_name_format_deprecation(self, reg_type):
"""Test that the `Register.name_format` class data can be accessed and triggers its
deprecation correctly."""
# From instance:
reg_inst = reg_type(2)
with self.assertWarnsRegex(DeprecationWarning, "Register.name_format is deprecated"):
self.assertTrue(reg_inst.name_format.match("name"))
# From class:
with self.assertWarnsRegex(DeprecationWarning, "Register.name_format is deprecated"):
self.assertTrue(reg_type.name_format.match("name"))