mqt.qecc.circuit_synthesis.elimination

Methods for performing Gaussian elimination on GF(2) and symplectic matrices.

Classes

EliminationSequence

Class representing a sequence of tableau operations.

EliminationStrategy

Strategy for elimination methods.

CandidateGenerator

Abstract base class for generating candidate operations.

SelectionStrategy

Abstract base class for selecting the best operation from candidates.

GreedySelection

Always select the first candidate.

OperationFilter

Abstract base class for filtering tableau operations.

ParallelFilter

Filter that blocks operations on qubits already used in current layer.

Functions

eliminate(target_tableau, strategy) → tuple[EliminationSequence, mqt.qecc.circuit_synthesis.types.BinaryMatrix]

Perform Gaussian elimination on a binary matrix or stabilizer tableau.

is_identity(tableau) → bool

Check if the given stabilizer tableau is the identity tableau.

get_n(tableau) → int

Get the number of qubits in the stabilizer tableau.

has_k_non_zero_columns(matrix, k) → bool

Check if the given binary matrix has at least k non-zero columns.

Module Contents

class EliminationSequence(operations: Sequence[TableauOperation])[source]

Class representing a sequence of tableau operations.

to_circuit() → stim.Circuit[source]

Convert the elimination sequence to a Stim circuit.

Returns:

The Stim circuit representing the elimination sequence.

Return type:

stim.Circuit

to_circuit_inverse() → stim.Circuit[source]

Convert the inverse of the elimination sequence to a Stim circuit.

Returns:

The Stim circuit representing the inverse elimination sequence.

Return type:

stim.Circuit

num_two_qubit_gates() → int[source]

Count the number of two-qubit gates in the elimination sequence.

Returns:

The number of two-qubit gates.

Return type:

int

num_transvections() → int[source]

Count the number of transvections in the elimination sequence.

Returns: int: The number of transvections.

num_cnots() → int[source]

Count the number of CNOT gates in the elimination sequence.

Returns:

The number of CNOT gates.

Return type:

int

add_operation(op: TableauOperation) → None[source]

Add a tableau operation to the elimination sequence.

Parameters:

op – The tableau operation to add.

apply(tableau: mqt.qecc.circuit_synthesis.types.BinaryMatrix, *, inplace: bool = False) → mqt.qecc.circuit_synthesis.types.BinaryMatrix[source]

Apply the elimination sequence to a stabilizer tableau.

Parameters:
  • tableau – The stabilizer tableau to apply the sequence to.

  • inplace – If True, modifies the tableau in place. If False, returns a new tableau.

Returns:

The resulting stabilizer tableau after applying the sequence.

Return type:

BinaryMatrix

extend(other: EliminationSequence, ignore_depth_impact: bool = False) → None[source]

Extend the elimination sequence with another sequence.

Parameters:
  • other – The other elimination sequence to append.

  • ignore_depth_impact – If True, do not update depth based on the new operations (useful for post-processing).

depth() → int[source]

Estimate the circuit depth of the elimination sequence.

Returns:

The estimated circuit depth.

Return type:

int

copy() → EliminationSequence[source]

Create a copy of the elimination sequence.

Returns:

A new instance with the same operations.

Return type:

EliminationSequence

last_layer_qubits() → set[int][source]

Get the set of qubits involved in the last layer of operations.

class EliminationStrategy[source]

Strategy for elimination methods.

setup() → None[source]

Run setup function if provided.

cleanup() → None[source]

Run cleanup function if provided.

eliminate(target_tableau: mqt.qecc.circuit_synthesis.types.BinaryMatrix, strategy: EliminationStrategy) → tuple[EliminationSequence, mqt.qecc.circuit_synthesis.types.BinaryMatrix][source]

Perform Gaussian elimination on a binary matrix or stabilizer tableau.

This function is the main elimination engine used for synthesizing encoding circuits. It iteratively applies operations (e.g., CNOTs, transvections, Clifford gates) to reduce the given tableau until a termination criterion defined by the provided strategy is satisfied.

The elimination process is fully driven by the EliminationStrategy, which specifies how candidates are generated, selected, filtered, and optionally post-processed.

The function also manages the lifecycle of the strategy by invoking strategy.setup() before the elimination loop and strategy.cleanup() after completion. Cleanup is guaranteed to run even if an exception occurs.

Parameters:
  • target_tableau – The input binary matrix or stabilizer tableau to reduce. This is copied internally, so the input object is not modified.

  • strategy – Strategy object defining the elimination behavior, including: - termination_criterion: Function that determines when elimination is complete - candidate_generator: Generates candidate operations at each step - selection_strategy: Chooses an operation from candidates (optional) - filters: Optional constraints applied during candidate generation - callback: Optional function invoked after each applied operation - post_process_fn: Optional function to finalize the result - setup_fn / cleanup_fn: Optional lifecycle hooks (e.g., for cache management)

Returns:

  • EliminationSequence: The sequence of operations applied during elimination

  • BinaryMatrix: The resulting reduced tableau

Return type:

A tuple containing

Raises:

RuntimeError – If no valid operations are available but the termination criterion has not been met, indicating that elimination cannot proceed.

Notes

  • Early termination may occur if the candidate generator signals it (e.g., in rollout-based strategies). In this case, the best known solution is returned.

  • The behavior of this function depends entirely on the provided strategy; different strategies may yield different circuits for the same input.

Examples

>>> strategy = EliminationStrategy(
...     termination_criterion=lambda tbl: mod2.rank(tbl.matrix) == k,
...     candidate_generator=GreedyCNOTGenerator(),
... )
>>> ops, final_tbl = eliminate(check_matrix, strategy)
>>> # Rollout-based depth optimization
>>> strategy = make_rollout_strategy(config)
>>> ops, final_tbl = eliminate(tableau, strategy)

See also

  • EliminationStrategy: Configuration of elimination behavior

  • CandidateGenerator: Base class for candidate generation

  • RolloutCandidateGenerator: Lookahead-based candidate generation

class CandidateGenerator(filters: Sequence[OperationFilter] | None = None)[source]

Bases: abc.ABC

Abstract base class for generating candidate operations.

abstractmethod get_candidates(tableau: mqt.qecc.circuit_synthesis.types.BinaryMatrix) → Sequence[tuple[TableauOperation, int | tuple[int, ...]]][source]

Generate sorted candidate operations for the current tableau.

Parameters:

tableau – The current binary matrix/tableau

Returns:

A list of (operation, score) tuples, sorted by preference

abstractmethod update(op: TableauOperation, tableau: mqt.qecc.circuit_synthesis.types.BinaryMatrix) → None[source]

Update internal state after an operation is applied.

Parameters:
  • op – The operation that was just applied

  • tableau – The resulting tableau after applying the operation

abstractmethod reset() → None[source]

Reset internal state (useful for lookahead simulations).

should_terminate_early() → bool[source]

Check if elimination should terminate early and use the best solution found.

Returns:

True if elimination should terminate early, False otherwise.

get_best_solution() → EliminationSequence | None[source]

Get the best complete solution found during lookahead exploration.

Returns:

Tuple of (sequence, tableau) if a solution is available, None otherwise.

escape_local_minimum(tableau: mqt.qecc.circuit_synthesis.types.BinaryMatrix) → Sequence[TableauOperation] | None[source]

Generate operations to escape a local minimum when no candidates are available.

Parameters:

tableau – The current tableau that is stuck in a local minimum.

Returns:

A list of operations to apply to escape the local minimum.

class SelectionStrategy[source]

Bases: abc.ABC

Abstract base class for selecting the best operation from candidates.

abstractmethod select(candidates: Sequence[tuple[TableauOperation, int | tuple[int, ...]]]) → TableauOperation[source]

Select the best operation from candidates.

Parameters:

candidates – List of (operation, score) tuples, typically sorted by preference.

Returns:

The selected operation to apply.

class GreedySelection[source]

Bases: SelectionStrategy

Always select the first candidate.

select(candidates: Sequence[tuple[TableauOperation, int | tuple[int, ...]]]) → TableauOperation[source]

Select the first (best-scored) candidate.

Parameters:

candidates – List of (operation, score) tuples.

Returns:

The first candidate in the list.

class OperationFilter[source]

Bases: abc.ABC

Abstract base class for filtering tableau operations.

abstractmethod should_include(op: TableauOperation) → bool[source]

Check if an operation should be included in candidates.

Parameters:

op – The tableau operation to check.

Returns:

True if the operation should be included, False otherwise.

abstractmethod update(op: TableauOperation) → None[source]

Update the filter state with the given operation.

Parameters:

op – The tableau operation to update the filter with.

abstractmethod copy() → OperationFilter[source]

Create a copy of the filter with the same state.

Returns:

A new filter instance with copied state.

abstractmethod reset() → None[source]

Reset the filter to its initial state.

class ParallelFilter(n_qubits: int)[source]

Bases: OperationFilter

Filter that blocks operations on qubits already used in current layer.

should_include(op: TableauOperation) → bool[source]

Check if operation uses any blocked qubits.

Parameters:

op – The operation to check.

Returns:

True if no qubits are blocked, False otherwise.

update(op: TableauOperation) → None[source]

Block qubits involved in the operation.

Parameters:

op – The tableau operation to update the filter with.

block_qubits(qubits: set[int]) → None[source]

Manually block a list of qubits.

Parameters:

qubits – List of qubit indices to block.

has_available_qubits() → bool[source]

Check if there are qubits available for operations.

reset() → None[source]

Unblock all qubits.

copy() → ParallelFilter[source]

Create a copy of the filter with the same state.

Returns:

A new ParallelFilter with copied blocked_qubits state.

is_identity(tableau: PauliTableau) → bool[source]

Check if the given stabilizer tableau is the identity tableau.

Parameters:

tableau (PauliTableau) – The stabilizer tableau to check.

Returns:

True if the tableau is the identity tableau, False otherwise.

Return type:

bool

get_n(tableau: mqt.qecc.circuit_synthesis.types.BinaryMatrix) → int[source]

Get the number of qubits in the stabilizer tableau.

Parameters:

tableau (BinaryMatrix) – The stabilizer tableau.

Returns:

The number of qubits.

Return type:

int

has_k_non_zero_columns(matrix: mqt.qecc.circuit_synthesis.types.BinaryMatrix, k: int) → bool[source]

Check if the given binary matrix has at least k non-zero columns.

Parameters:
  • matrix (BinaryMatrix) – The binary matrix to check.

  • k (int) – The number of non-zero columns to check for.

Returns:

True if the matrix has at least k non-zero columns, False otherwise.

Return type:

bool