mqt.qmap.ph.unitary_to_phase_compilation

Unitary-to-phase compilation via gradient-based optimization.

Classes

Functions

build_unitary_selected_columns_from_components(...[, ...]) → torch.Tensor

Build selected columns of the chip unitary without constructing the full matrix.

fidelity_loss(effective_unitary, target_unitary) → torch.Tensor

Compute the normalized fidelity loss between two unitaries.

optimize_unitary_subcircuit_parameters(target_unitary, ...) → OptimizationResult

Optimize phase-shifter parameters to approximate a target unitary.

Module Contents

build_unitary_selected_columns_from_components(num_modes: int, beam_splitter_params: torch.Tensor, phase_shifter_params: torch.Tensor, column_indices: list[int] | torch.Tensor, exclude_edge_phase_shifters: bool = False) → torch.Tensor[source]

Build selected columns of the chip unitary without constructing the full matrix.

Propagates only the selected input state vectors through the MZI mesh, so it is faster when len(column_indices) << num_modes and never forms an N x N component matrix. Selecting every column (column_indices=list(range(num_modes))) yields the full chip unitary.

Parameters:
  • num_modes – Number of spatial modes on the chip.

  • beam_splitter_params – 1D tensor of beam-splitter reflectivities ordered MZI-by-MZI as in/out pairs, layer by layer.

  • phase_shifter_params – Phase grid of shape (num_modes, num_modes).

  • column_indices – Indices of the columns to compute.

  • exclude_edge_phase_shifters – If True, corner phase shifters are omitted.

Returns:

Complex tensor of shape (num_modes, len(column_indices)) containing the selected columns of the full chip unitary.

fidelity_loss(effective_unitary: torch.Tensor, target_unitary: torch.Tensor) → torch.Tensor[source]

Compute the normalized fidelity loss between two unitaries.

Loss is defined as \(1 - |\mathrm{Tr}(U_\mathrm{tgt}^\dagger U_\mathrm{eff})|^2 / N^2\), where \(N\) is the number of compared columns. A loss of 0.0 indicates a perfect match (up to global phase).

Parameters:
  • effective_unitary – Selected columns and output rows of the chip unitary.

  • target_unitary – Target unitary with compatible shape.

Returns:

Scalar tensor holding the fidelity loss in [0, 1].

class OptimizationResult[source]

Result of an optimize_unitary_subcircuit_parameters() run.

phase_shifter_params

Best (num_modes_opt, num_modes_opt) parameter grid (mod 2pi), where num_modes_opt is target_dim when movement_mask is None or movement_mask.shape[0] otherwise. Routing constraints are already applied. Excluded corner phase shifters have zero phase.

best_loss

Loss of phase_shifter_params (the minimum over all steps), matching the returned parameters rather than the final step.

optimize_unitary_subcircuit_parameters(target_unitary: torch.Tensor, beam_splitter_reflectivities: torch.Tensor, movement_mask: torch.Tensor | None = None, lr: float = 0.05, threshold: float = 1e-05, active_cols: list[int] | None = None, active_cols_target: list[int] | None = None, verbose: bool = False, max_iterations: int = 10000, output_rows: list[int] | None = None, exclude_edge_phase_shifters: bool = False, optimize_routing_parameters: bool = True, early_stop_patience: int = 50, min_improvement: float = 0.0001) → OptimizationResult[source]

Optimize phase-shifter parameters to approximate a target unitary.

Runs an Adam optimizer with optional learning-rate scheduling.

Parameters:
  • target_unitary – Target unitary tensor of shape (target_dim, target_dim).

  • beam_splitter_reflectivities – 1D tensor of chip beam-splitter reflectivities. Treated as fixed (no gradient).

  • movement_mask – Integer tensor of shape (num_modes, num_modes) encoding routing constraints. When None, the full chip is treated as a computation zone.

  • lr – Initial Adam learning rate.

  • threshold – Loss value below which optimization is considered successful and terminates early.

  • active_cols – Physical input column indices to inject photons into.

  • active_cols_target – Column indices within the computation zone corresponding to active_cols.

  • verbose – If True, log progress (INFO level) every 100 iterations.

  • max_iterations – Exact upper bound on the number of gradient steps. The loop evaluates the initial parameters and the parameters after each step, so 0 performs no step and returns the initial state, 1 performs exactly one step, and so on.

  • output_rows – Output rows to compare. Defaults to all rows.

  • exclude_edge_phase_shifters – If True, the two corner phase shifters are excluded from the parameter set.

  • optimize_routing_parameters – If True, routing cells contribute a single trainable degree of freedom.

  • early_stop_patience – Number of consecutive steps without improvement before optimization is terminated early.

  • min_improvement – Minimum absolute loss decrease required to reset the patience counter.

Returns:

An OptimizationResult with the best parameter grid and its loss.