mqt.qmap.ph.routing_to_phases

Utilities for converting routing masks to phase-shifter parameter grids.

Classes

RoutingTransform

Static routing constraints for a phase grid.

Functions

precompute_routing_transform(movement_mask[, ...]) → RoutingTransform

Precompute phase sources and offsets from the mesh and routing states.

apply_routing_transform(raw_params, transform) → torch.Tensor

Apply routing constraints while preserving gradients to their raw sources.

reshape_flattened_params_to_grid(params_1d, num_modes) → torch.Tensor

Inflate a 1D parameter vector into a 2D phase-shifter grid.

Module Contents

class RoutingTransform[source]

Static routing constraints for a phase grid.

source_mode

Raw phase source for each cell, including paired modes.

offset

Fixed phase offset, either zero or pi.

use_raw

Whether the effective phase includes a raw phase.

precompute_routing_transform(movement_mask: torch.Tensor, optimize_routing_parameters: bool = False) → RoutingTransform[source]

Precompute phase sources and offsets from the mesh and routing states.

A bar pair has phases differing by pi; a cross pair has equal phases. Routing pairs share one trainable phase when optimization is enabled. Virtual phase shifters use the raw phase of their active mode. Compute cells remain independent. Mixed pairs take the higher MaskState code.

Parameters:
  • movement_mask – Integer grid of routing states, indexed by mode and layer.

  • optimize_routing_parameters – Whether routing pairs have a trainable phase.

Returns:

Phase sources, offsets, and selectors to reuse during optimization.

apply_routing_transform(raw_params: torch.Tensor, transform: RoutingTransform) → torch.Tensor[source]

Apply routing constraints while preserving gradients to their raw sources.

Parameters:
  • raw_params – Current phase grid.

  • transform – Precomputed routing constraints.

Returns:

Effective phases. Autograd freezes constants and accumulates gradients from derived phases into their sources.

reshape_flattened_params_to_grid(params_1d: torch.Tensor, num_modes: int, exclude_edge_phase_shifters: bool = False) → torch.Tensor[source]

Inflate a 1D parameter vector into a 2D phase-shifter grid.

When exclude_edge_phase_shifters is True, the top-right and bottom-right corner positions are absent from params_1d and are padded with zero in the output grid.

Parameters:
  • params_1d – Flat parameter tensor of size num_modes**2 (or num_modes**2 - 2 when edge phase shifters are excluded).

  • num_modes – Number of spatial modes on the chip.

  • exclude_edge_phase_shifters – If True, the two corner entries are absent from params_1d.

Returns:

Float tensor of shape (num_modes, num_modes) with parameters placed at valid grid positions and zeros at excluded corners.

Raises:

ValueError – If the size of params_1d does not match the expected count for the given num_modes and exclude_edge_phase_shifters setting.