Source code for pyqasm.modules.qasm3

# Copyright 2025 qBraid
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Defines a module for handling OpenQASM 3.0 programs.
"""

from openqasm3.ast import Program
from openqasm3.printer import dumps

from pyqasm.modules.base import QasmModule


[docs] class Qasm3Module(QasmModule): """ A module representing an openqasm3 quantum program. Args: name (str): Name of the module. program (Program): The original openqasm3 program. statements (list[Statement]): list of openqasm3 Statements. """
[docs] def __init__(self, name: str, program: Program): super().__init__(name, program) self._unrolled_ast = Program(statements=[], version="3.0")
def _qasm_ast_to_str(self, qasm_ast): """Convert the qasm AST to a string""" # set the version to 3.0 qasm_ast.version = "3.0" return dumps(qasm_ast) def accept(self, visitor): """Accept a visitor for the module Args: visitor (QasmVisitor): The visitor to accept """ unrolled_stmt_list = visitor.visit_basic_block(self._statements) final_stmt_list = visitor.finalize(unrolled_stmt_list) self._unrolled_ast.statements = final_stmt_list