Skip to content

ir

IR

Source code in opensquirrel/ir/ir.py
class IR:
    def __init__(self) -> None:
        self.statements: list[Statement] = []

    def accept(self, visitor: IRVisitor) -> None:
        """Accepts visitor and processes the IR nodes."""
        for statement in self.statements:
            statement.accept(visitor)

    def add_asm_declaration(self, asm_declaration: AsmDeclaration) -> None:
        """Adds an assembly declaration to the IR.

        Args:
            asm_declaration (AsmDeclaration): The assembly declaration to add.

        """
        self.statements.append(asm_declaration)

    def add_gate(self, gate: Gate) -> None:
        """Adds a gate to the IR.

        Args:
            gate (Gate): The gate to add.

        """
        self.statements.append(gate)

    def add_non_unitary(self, non_unitary: NonUnitary) -> None:
        """Adds a non-unitary operation to the IR.

        Args:
            non_unitary (NonUnitary): The non-unitary operation to add.

        """
        self.statements.append(non_unitary)

    def add_statement(self, statement: Statement) -> None:
        """Adds a generic statement to the IR.

        Args:
            statement (Statement): The statement to add.

        """
        self.statements.append(statement)

    def reverse(self) -> IR:
        """Reverses the order of statements in the IR."""
        ir = IR()
        for statement in self.statements[::-1]:
            ir.add_statement(statement)
        return ir

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, IR):
            return False
        return self.statements == other.statements

    def __repr__(self) -> str:
        return f"IR: {self.statements}"

accept

accept(visitor: IRVisitor) -> None

Accepts visitor and processes the IR nodes.

Source code in opensquirrel/ir/ir.py
def accept(self, visitor: IRVisitor) -> None:
    """Accepts visitor and processes the IR nodes."""
    for statement in self.statements:
        statement.accept(visitor)

add_asm_declaration

add_asm_declaration(
    asm_declaration: AsmDeclaration,
) -> None

Adds an assembly declaration to the IR.

Parameters:

Name Type Description Default
asm_declaration AsmDeclaration

The assembly declaration to add.

required
Source code in opensquirrel/ir/ir.py
def add_asm_declaration(self, asm_declaration: AsmDeclaration) -> None:
    """Adds an assembly declaration to the IR.

    Args:
        asm_declaration (AsmDeclaration): The assembly declaration to add.

    """
    self.statements.append(asm_declaration)

add_gate

add_gate(gate: Gate) -> None

Adds a gate to the IR.

Parameters:

Name Type Description Default
gate Gate

The gate to add.

required
Source code in opensquirrel/ir/ir.py
def add_gate(self, gate: Gate) -> None:
    """Adds a gate to the IR.

    Args:
        gate (Gate): The gate to add.

    """
    self.statements.append(gate)

add_non_unitary

add_non_unitary(non_unitary: NonUnitary) -> None

Adds a non-unitary operation to the IR.

Parameters:

Name Type Description Default
non_unitary NonUnitary

The non-unitary operation to add.

required
Source code in opensquirrel/ir/ir.py
def add_non_unitary(self, non_unitary: NonUnitary) -> None:
    """Adds a non-unitary operation to the IR.

    Args:
        non_unitary (NonUnitary): The non-unitary operation to add.

    """
    self.statements.append(non_unitary)

add_statement

add_statement(statement: Statement) -> None

Adds a generic statement to the IR.

Parameters:

Name Type Description Default
statement Statement

The statement to add.

required
Source code in opensquirrel/ir/ir.py
def add_statement(self, statement: Statement) -> None:
    """Adds a generic statement to the IR.

    Args:
        statement (Statement): The statement to add.

    """
    self.statements.append(statement)

reverse

reverse() -> IR

Reverses the order of statements in the IR.

Source code in opensquirrel/ir/ir.py
def reverse(self) -> IR:
    """Reverses the order of statements in the IR."""
    ir = IR()
    for statement in self.statements[::-1]:
        ir.add_statement(statement)
    return ir