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

Remove _legacy_format_cache slot from CircuitInstruction #10417

Merged
Merged
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
18 changes: 7 additions & 11 deletions qiskit/circuit/quantumcircuitdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class CircuitInstruction:
of distinct items, with no duplicates.
"""

__slots__ = ("operation", "qubits", "clbits", "_legacy_format_cache")
__slots__ = ("operation", "qubits", "clbits")

operation: Operation
"""The logical operation that this instruction represents an execution of."""
Expand All @@ -76,7 +76,6 @@ def __init__(
self.operation = operation
self.qubits = tuple(qubits)
self.clbits = tuple(clbits)
self._legacy_format_cache = None

def copy(self) -> "CircuitInstruction":
"""Return a shallow copy of the :class:`CircuitInstruction`."""
Expand Down Expand Up @@ -117,7 +116,7 @@ def __eq__(self, other):
and self.operation == other.operation
)
if isinstance(other, tuple):
return self._legacy_format == other
return self._legacy_format() == other
return NotImplemented

# Legacy tuple-like interface support.
Expand All @@ -127,19 +126,16 @@ def __eq__(self, other):
# like that via unpacking or similar. That means that the `parameters` field is completely
# absent, and the qubits and clbits must be converted to lists.

@property
def _legacy_format(self):
if self._legacy_format_cache is None:
# The qubits and clbits were generally stored as lists in the old format, and various
# places assume that they will certainly be lists.
self._legacy_format_cache = (self.operation, list(self.qubits), list(self.clbits))
return self._legacy_format_cache
# The qubits and clbits were generally stored as lists in the old format, and various
# places assume that they will certainly be lists.
return (self.operation, list(self.qubits), list(self.clbits))

def __getitem__(self, key):
return self._legacy_format[key]
return self._legacy_format()[key]

def __iter__(self):
return iter(self._legacy_format)
return iter(self._legacy_format())

def __len__(self):
return 3
Expand Down