Skip to content
Closed
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
30 changes: 22 additions & 8 deletions qiskit/transpiler/passmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@

import io
import re

try:
from functools import cached_property

class immutable_cached_property(cached_property): # pylint: disable=invalid-name
"""Immutable cached_property memoization descriptor"""

def __set__(self, instance, value):
raise AttributeError("can't set attribute")

def __delete__(self, instance):
raise AttributeError("can't delete attribute")

except ImportError: # Below python 3.8
immutable_cached_property = property # pylint: disable=invalid-name

from typing import Union, List, Tuple, Callable, Dict, Any, Optional, Iterator, Iterable

import dill
Expand Down Expand Up @@ -379,7 +395,7 @@ def __init__(self, stages: Optional[Iterable[str]] = None, **kwargs) -> None:
stages (Iterable[str]): An optional list of stages to use for this
instance. If this is not specified the default stages list
``['init', 'layout', 'routing', 'translation', 'optimization', 'scheduling']`` is
used. After instantiation, the final list will be immutable and stored as tuple.
used. After instantiation, the final list will be immutable and stored as a tuple.
kwargs: The initial :class:`~.PassManager` values for any stages
defined in ``stages``. If a argument is not defined the
stages will default to ``None`` indicating an empty/undefined
Expand All @@ -398,9 +414,7 @@ def __init__(self, stages: Optional[Iterable[str]] = None, **kwargs) -> None:
"scheduling",
]
self._validate_stages(stages)
# Set through parent class since `__setattr__` requieres `expanded_stages` to be defined
super().__setattr__("_stages", tuple(stages))
super().__setattr__("_expanded_stages", tuple(self._generate_expanded_stages()))
self._stages = tuple(stages)
super().__init__()
self._validate_init_kwargs(kwargs)
for stage in self.expanded_stages:
Expand All @@ -420,19 +434,19 @@ def _validate_stages(self, stages: Iterable[str]) -> None:

def _validate_init_kwargs(self, kwargs: Dict[str, Any]) -> None:
expanded_stages = set(self.expanded_stages)
for stage in kwargs.keys():
for stage in kwargs:
if stage not in expanded_stages:
raise AttributeError(f"{stage} is not a valid stage.")

@property
def stages(self) -> Tuple[str, ...]:
"""Pass manager stages"""
return self._stages # pylint: disable=no-member
return self._stages

@property
@immutable_cached_property
def expanded_stages(self) -> Tuple[str, ...]:
"""Expanded Pass manager stages including ``pre_`` and ``post_`` phases."""
return self._expanded_stages # pylint: disable=no-member
return tuple(self._generate_expanded_stages())

def _generate_expanded_stages(self) -> Iterator[str]:
for stage in self.stages:
Expand Down