Skip to content

The environment module

The environment module defines an Environment type that plays a similar role as a context (or typing environment) for type checking systems. Here, it is a record of information for the qubit register and compilation directives.

Environment

An environment to hold register information and compiler directives.

add_grid_options(options)

Add grid options to the Environment.

Source code in qadence2_expressions/core/environment.py
def add_grid_options(options: dict[str, Any]) -> None:
    """Add grid options to the Environment."""
    current = Environment.grid_options or {}
    Environment.grid_options = {**current, **options}

add_qpu_directives(directives)

Add QPU directives to the Environment.

Source code in qadence2_expressions/core/environment.py
def add_qpu_directives(directives: dict[str, Any]) -> None:
    """Add QPU directives to the Environment."""
    current = Environment.directives or {}
    Environment.directives = {**current, **directives}

add_settings(settings)

Add compilation settings to the Environment.

Source code in qadence2_expressions/core/environment.py
def add_settings(settings: dict[str, Any]) -> None:
    """Add compilation settings to the Environment."""
    current = Environment.settings or {}
    Environment.settings = {**current, **settings}

get_grid_options()

Get grid options from the Environment.

Source code in qadence2_expressions/core/environment.py
def get_grid_options() -> dict[str, Any] | None:
    """Get grid options from the Environment."""
    return Environment.grid_options

get_grid_scale()

Get grid scale in the Environment.

Source code in qadence2_expressions/core/environment.py
def get_grid_scale() -> float:
    """Get grid scale in the Environment."""
    return Environment.grid_scale

get_grid_type()

Get grid type from the Environment.

Source code in qadence2_expressions/core/environment.py
def get_grid_type() -> Literal["linear", "square", "triangular"] | None:
    """Get grid type from the Environment."""
    return Environment.grid_type

get_number_qubits()

Get the number of qubits from the Environment.

Source code in qadence2_expressions/core/environment.py
def get_number_qubits() -> int:
    """Get the number of qubits from the Environment."""
    return Environment.num_qubits

get_qpu_directives()

Get QPU directives from the Environment.

Source code in qadence2_expressions/core/environment.py
def get_qpu_directives() -> dict[str, Any] | None:
    """Get QPU directives from the Environment."""
    return Environment.directives

get_qubits_positions()

Get qubit positions from the Environment.

Source code in qadence2_expressions/core/environment.py
def get_qubits_positions() -> list[tuple[int, int]] | list[int] | None:
    """Get qubit positions from the Environment."""
    return Environment.qubit_positions

get_settings()

Get compilation settings from the Environment.

Source code in qadence2_expressions/core/environment.py
def get_settings() -> dict[str, Any] | None:
    """Get compilation settings from the Environment."""
    return Environment.settings

reset_ir_options()

Reset Environment.

Source code in qadence2_expressions/core/environment.py
def reset_ir_options() -> None:
    """Reset Environment."""
    Environment.qubit_positions = None
    Environment.grid_type = None
    Environment.grid_scale = 1.0
    Environment.num_qubits = 0
    Environment.grid_options = None
    Environment.directives = None
    Environment.settings = None

set_grid_scale(s)

Set grid scale in the Environment.

Source code in qadence2_expressions/core/environment.py
def set_grid_scale(s: float) -> None:
    """Set grid scale in the Environment."""
    Environment.grid_scale = s

set_grid_type(grid)

Set grid type in the Environment.

Source code in qadence2_expressions/core/environment.py
def set_grid_type(grid: Literal["linear", "square", "triangular"]) -> None:
    """Set grid type in the Environment."""
    Environment.grid_type = grid

set_number_qubits(n)

Set the number of qubits in the Environment if not defined in the register.

Source code in qadence2_expressions/core/environment.py
def set_number_qubits(n: int) -> None:
    """Set the number of qubits in the Environment if not defined in the register."""
    if Environment.qubit_positions and n != len(Environment.qubit_positions):
        raise ValueError("Number of qubits already defined by the register.")
    Environment.num_qubits = n

set_qubits_positions(pos)

Set qubits positions in the Environment.

Source code in qadence2_expressions/core/environment.py
def set_qubits_positions(pos: list[tuple[int, int]] | list[int]) -> None:
    """Set qubits positions in the Environment."""
    Environment.qubit_positions = pos
    set_number_qubits(len(pos))