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

Deprecate subclassing of Register and Bit #13841

Merged
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
12 changes: 12 additions & 0 deletions qiskit/circuit/bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Quantum bit and Classical bit objects.
"""
import copy
import warnings

from qiskit.circuit.exceptions import CircuitError

Expand Down Expand Up @@ -57,6 +58,17 @@ def __init__(self, register=None, index=None):
self._hash = hash((self._register, self._index))
self._repr = f"{self.__class__.__name__}({self._register}, {self._index})"

def __init_subclass__(cls):
# In Qiskit 2.0, `Bit` and `Register` will move to Rust space, and the allowable types of
# them will be fixed, similar to if the classes had been marked as `final`.
if cls.__module__.split(".", 2)[0] != __name__.split(".", 2)[0]:
warnings.warn(
"subclassing 'Bit' is not supported, and may not be possible in Qiskit 2.0",
category=DeprecationWarning,
stacklevel=2,
)
return cls

def __repr__(self):
"""Return the official string representing the bit."""
if (self._register, self._index) == (None, None):
Expand Down
13 changes: 13 additions & 0 deletions qiskit/circuit/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"""

from __future__ import annotations

import itertools
import warnings
import numpy as np

from qiskit.circuit.exceptions import CircuitError
Expand Down Expand Up @@ -125,6 +127,17 @@ def __init__(self, size: int | None = None, name: str | None = None, bits=None):
# until first access.
self._bit_indices = None

def __init_subclass__(cls):
# In Qiskit 2.0, `Bit` and `Register` will move to Rust space, and the allowable types of
# them will be fixed, similar to if the classes had been marked as `final`.
if cls.__module__.split(".", 2)[0] != __name__.split(".", 2)[0]:
warnings.warn(
"subclassing 'Register' is not supported, and may not be possible in Qiskit 2.0",
category=DeprecationWarning,
stacklevel=2,
)
return cls

@property
def name(self):
"""Get the register name."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
deprecations_circuits:
- |
It is now deprecated to subclass :class:`.Register` or :class:`.Bit`, or any subclass of them
(for example, :class:`.QuantumRegister` or :class:`~.circuit.Qubit`). Subclassing these types
was never explicitly supported by Qiskit, and it is not clear precisely what the meaning would
be. In Qiskit 2.0, the subclassing may become impossible due to technical limitations, and will
certainly not be stored in a circuit. This is because of the move of the data model to Rust
space to improve performance.
14 changes: 13 additions & 1 deletion test/python/circuit/test_bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import copy
from unittest import mock

from qiskit.circuit import bit
from qiskit.circuit import bit, Qubit
from test import QiskitTestCase # pylint: disable=wrong-import-order


Expand Down Expand Up @@ -76,6 +76,18 @@ def test_old_style_bit_copy(self):

self.assertIs(bit1, bit2)

def test_deprecation_of_subclass(self):
"""It's not permitted to subclass the objects."""
with self.assertWarnsRegex(DeprecationWarning, "subclassing 'Bit' is not supported"):

class _MyBit(bit.Bit):
"""Direct subclass."""

with self.assertWarnsRegex(DeprecationWarning, "subclassing 'Bit' is not supported"):

class _MyQubit(Qubit):
"""Indirect subclass."""


class TestNewStyleBit(QiskitTestCase):
"""Test behavior of new-style bits."""
Expand Down
17 changes: 13 additions & 4 deletions test/python/circuit/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

from ddt import data, ddt

from qiskit.circuit import bit
from qiskit.circuit import QuantumRegister
from qiskit.circuit import AncillaRegister
from qiskit.circuit import ClassicalRegister
from qiskit.circuit import bit, QuantumRegister, Register, AncillaRegister, ClassicalRegister
from qiskit.circuit.exceptions import CircuitError
from test import QiskitTestCase # pylint: disable=wrong-import-order

Expand Down Expand Up @@ -119,3 +116,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)

def test_deprecation_of_subclass(self):
"""It's not permitted to subclass the objects."""
with self.assertWarnsRegex(DeprecationWarning, "subclassing 'Register' is not supported"):

class _MyRegister(Register):
"""Direct subclass."""

with self.assertWarnsRegex(DeprecationWarning, "subclassing 'Register' is not supported"):

class _MyQuantumRegister(QuantumRegister):
"""Indirect subclass."""
Loading