Skip to content

Index maps

The layout of every tensor-typed parameter a transpiled program reads, axis by axis, with the coordinate at each position. qvr transpile --index-map FILE writes it as JSON.

axis_layout

The index map a transpiled program's arrays are laid out by.

Every transpile target reads tensors positionally: axis names stop at the first-order boundary (erase_axes). An array whose type has axes named by finite types is laid out in the canonical order that type fixes (canonical_dimensions), and the index map records that order: for each program and computation parameter of tensor type, its axes in layout order, each with its name, its extent, and its coordinate labels in position order. A caller lays data out for a target by the map, and reads a target's results back by it.

qvr transpile --index-map FILE writes the map as JSON beside the transpiled program.

IndexedAxis didactic-model

IndexedAxis(*, name: str, extent: int | None, coordinates: tuple[str, ...] = (), named: bool = False)

Bases: Model

One axis of a parameter's array, in layout order.

PARAMETER DESCRIPTION
name

The axis's name: the finite type's rendering, or Fin n.

TYPE: str

extent

Its extent, when the type fixes it.

TYPE: int or None

coordinates

The coordinate label at each position, when the extent is fixed.

TYPE: tuple[str, ...] DEFAULT: ()

named

Whether a finite type names the axis.

TYPE: bool DEFAULT: False

IndexedParameter didactic-model

IndexedParameter(*, entry: str, parameter: str, type: str, axes: tuple[IndexedAxis, ...])

Bases: Model

The layout of one tensor-typed parameter.

PARAMETER DESCRIPTION
entry

The program or computation the parameter belongs to.

TYPE: str

parameter

The parameter's name.

TYPE: str

type

Its type, as source spells it.

TYPE: str

axes

Its axes, outermost first.

TYPE: tuple[IndexedAxis, ...]

IndexMap didactic-model

IndexMap(*, parameters: tuple[IndexedParameter, ...] = ())

Bases: Model

The layout of every tensor-typed parameter of a module.

PARAMETER DESCRIPTION
parameters

One record per tensor-typed parameter of each program and computation, in declaration order.

TYPE: tuple[IndexedParameter, ...] DEFAULT: ()

to_json

to_json() -> str

The map as JSON text.

RETURNS DESCRIPTION
str

{"parameters": [{"entry", "parameter", "type", "axes"}, ...]} with every axis as {"name", "extent", "coordinates", "named"}.

Source code in src/quivers/transpile/axis_layout.py
def to_json(self) -> str:
    """The map as JSON text.

    Returns
    -------
    str
        ``{"parameters": [{"entry", "parameter", "type", "axes"}, ...]}``
        with every axis as ``{"name", "extent", "coordinates", "named"}``.
    """
    return json.dumps(
        {
            "parameters": [
                {
                    "entry": item.entry,
                    "parameter": item.parameter,
                    "type": item.type,
                    "axes": [
                        {
                            "name": axis.name,
                            "extent": axis.extent,
                            "coordinates": list(axis.coordinates),
                            "named": axis.named,
                        }
                        for axis in item.axes
                    ],
                }
                for item in self.parameters
            ]
        },
        indent=2,
    )

index_map

index_map(module: QiecModule) -> IndexMap

The index map of a checked module's tensor-typed parameters.

PARAMETER DESCRIPTION
module

The checked module, before the first-order boundary erases its axis names.

TYPE: QiecModule

RETURNS DESCRIPTION
IndexMap

The layout of every tensor-typed program and computation parameter.

Source code in src/quivers/transpile/axis_layout.py
def index_map(module: QiecModule) -> IndexMap:
    """The index map of a checked module's tensor-typed parameters.

    Parameters
    ----------
    module : QiecModule
        The checked module, before the first-order boundary erases its
        axis names.

    Returns
    -------
    IndexMap
        The layout of every tensor-typed program and computation parameter.
    """
    declarations = ModuleDeclarations(module)
    found: list[IndexedParameter] = []
    typed = [
        (entry.name, parameter.name, parameter.type)
        for entry in module.entries
        for parameter in entry.parameters
    ] + [
        (computation.name, parameter.name, parameter.type)
        for computation in module.computations
        if not computation.name.startswith("__")
        for parameter in computation.parameters
    ]
    for entry, name, type_ in typed:
        axes = axis_coordinates(type_, declarations)
        if not axes:
            continue
        found.append(
            IndexedParameter(
                entry=entry,
                parameter=name,
                type=render_static(type_),
                axes=tuple(
                    IndexedAxis(
                        name=axis.name,
                        extent=axis.extent,
                        coordinates=axis.coordinates,
                        named=axis.named,
                    )
                    for axis in axes
                ),
            )
        )
    return IndexMap(parameters=tuple(found))