Skip to content

[FEATURE] Introduce a ScopeManager in pyqasm #230

@TheGupta2012

Description

@TheGupta2012

Feature Description

  • With the oncoming addition of the PulseVisitor, we will require scope checks across both of the original qasm visitor and the new pulse visitor. To avoid referencing the base qasm visitor from inside the pulse visitor for scope related checks, it will be good to refactor the scope related attributes and methods to a ScopeManager.
  • This manager will be initialized for every new module and referenced inside the base and pulse visitors both.

Implementation (Optional)

Scope Methods -

def _push_scope(self, scope: dict) -> None:
    ...

def _pop_scope(self) -> None:
    ...

def _get_parent_scope(self) -> dict:
    ...

def _get_curr_scope(self) -> dict:
    ...

def _get_global_scope(self) -> dict:
    ...

def _push_context(self, context: Context) -> None:
    ...

def _restore_context(self) -> None:
    ...

def _get_curr_context(self) -> Context:
    ...

def _in_global_scope(self) -> bool:
    ...

def _in_function_scope(self) -> bool:
    ...

def _in_gate_scope(self) -> bool:
    ...

def _in_block_scope(self) -> bool:
    ...

def _check_in_scope(self, var_name: str) -> bool:
    ...

def _get_from_visible_scope(self, var_name: str) -> Variable | None:
    ...

def _add_var_in_scope(self, variable: Variable) -> None:
    ...

def _update_var_in_scope(self, variable: Variable) -> None:
    ...

def _check_if_name_in_scope(self, name: str, operation: Any) -> None:
    ...

Attributes -

self._scope: deque = deque([{}])
self._context: deque = deque([Context.GLOBAL])
self._curr_scope: int = 0
self._label_scope_level: dict[int, set] = {self._curr_scope: set()}
  • Create a new ScopeManager class in a dedicated module (e.g., scope_manager.py).
  • Move the above attributes and methods from QasmVisitor into ScopeManager.
  • Update QasmVisitor and PulseVisitor to accept a ScopeManager instance upon initialization and delegate all scope-related operations to it.
  • Ensure all scope checks, variable lookups, and context management are handled through the manager.
  • Add unit tests for ScopeManager to verify correct behavior in isolation.
  • Refactor existing code to use the manager, ensuring backwards compatibility and no regression in functionality.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestllm-assistedUsed LLMs to fine tune issue description.no-qc-knowledge-reqdDoes not require knowledge of QC or QIS

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions