mqt.qecc.circuit_synthesis.rollout¶
Rollout-based candidate generation for circuit synthesis.
Classes¶
Generates candidates using rollout simulation. |
|
Protocol for rollout cache key generation. |
|
Cache policy for additive objectives. |
|
Cache policy for depth-like non-additive objectives. |
|
Weighted least-recently-used cache for rollout continuations. |
Functions¶
|
Open a rollout cache session. |
|
Close a rollout cache session. |
Module Contents¶
- class RolloutCandidateGenerator(base_strategy: EliminationStrategy, rollout: int, num_rollout_candidates: int | list[int], score_fn: Callable[[EliminationSequence], tuple[int, ...]], *, enable_early_termination: bool = True, current_sequence: EliminationSequence | None = None, cache_policy: CachePolicy | None = None, num_cached_subsequences: int = 10)[source]¶
Bases:
mqt.qecc.circuit_synthesis.elimination.CandidateGeneratorGenerates candidates using rollout simulation.
This generator tracks the best complete solution found during rollout exploration to ensure that greedy local choices lead to globally good solutions.
- get_base_candidates(tableau: mqt.qecc.circuit_synthesis.types.BinaryMatrix) Sequence[TableauOperation][source]¶
Get base candidates from the underlying strategy without scoring.
- Parameters:
tableau – The current binary matrix.
- Returns:
List of candidate operations from the base strategy.
- score_rollout_candidates(tableau: mqt.qecc.circuit_synthesis.types.BinaryMatrix, base_candidates: Sequence[TableauOperation]) list[ScoredCandidate][source]¶
Score base candidates using rollout simulation.
- Parameters:
tableau – The current binary matrix.
base_candidates – List of candidate operations from the base strategy.
- Returns:
List of (operation, score) tuples sorted by score.
- get_candidates(tableau: mqt.qecc.circuit_synthesis.types.BinaryMatrix) Sequence[tuple[TableauOperation, int | tuple[int, ...]]][source]¶
Generate candidates using rollout simulation.
- Parameters:
tableau – The current binary matrix or tableau.
- Returns:
List of operations sorted by rollout score.
- should_terminate_early() bool[source]¶
Check if elimination should terminate early.
- Returns:
True if early termination is requested, False otherwise.
- get_best_solution() EliminationSequence | None[source]¶
Get the best complete solution found during rollout exploration.
- Returns:
The best EliminationSequence found during rollout, or None if no improving candidate was scored.
- update(op: TableauOperation, tableau: mqt.qecc.circuit_synthesis.types.BinaryMatrix) None[source]¶
Update internal state based on the operation applied.
- Parameters:
op – The operation that was applied to the tableau.
tableau – The resulting tableau after applying the operation.
- escape_local_minimum(tableau: mqt.qecc.circuit_synthesis.types.BinaryMatrix) Sequence[TableauOperation] | None[source]¶
Escape local minimum by applying a random operation.
- Parameters:
tableau – The current binary matrix.
- Returns:
A sequence of operations to escape the local minimum, or an empty sequence if no escape is possible.
- class CachePolicy[source]¶
Bases:
ProtocolProtocol for rollout cache key generation.
Implementations define which parts of the current synthesis state are relevant for cache reuse. For additive objectives, the key may depend only on the tableau and remaining rollout depth. For non-additive objectives, the key may also depend on a compact summary of the current synthesis context.
- key(tableau: mqt.qecc.circuit_synthesis.types.BinaryMatrix, rollout: int, current_sequence: EliminationSequence) Hashable | None[source]¶
Return a cache key for the given rollout subproblem.
- Parameters:
tableau – The current tableau or binary matrix describing the remaining elimination problem.
rollout – The remaining rollout depth for the subproblem.
current_sequence – The elimination sequence built so far. Depending on the cache policy, this may be ignored or used to derive a compact synthesis context.
- Returns:
A hashable cache key, or
Noneif caching should be disabled for this subproblem.
- class AdditiveCachePolicy[source]¶
Cache policy for additive objectives.
This policy assumes that the best continuation depends only on the current tableau and the remaining rollout depth, and not on the synthesis history. This is appropriate for additive or prefix-independent objectives.
- static key(tableau: mqt.qecc.circuit_synthesis.types.BinaryMatrix, rollout: int, current_sequence: EliminationSequence) Hashable[source]¶
Return a cache key independent of the current sequence.
- Parameters:
tableau – The current tableau or binary matrix.
rollout – The remaining rollout depth.
current_sequence – The elimination sequence built so far. It is ignored by this policy.
- Returns:
A hashable key based only on the tableau and remaining rollout depth.
- class NonAdditiveCachePolicy[source]¶
Cache policy for depth-like non-additive objectives.
This policy includes a compact summary of the current synthesis context in the cache key. Here, the context is represented by the set of qubits used in the current last layer, which is sufficient for the current depth-guided tail policy.
- static key(tableau: mqt.qecc.circuit_synthesis.types.BinaryMatrix, rollout: int, current_sequence: EliminationSequence) Hashable[source]¶
Return a cache key that includes the last-layer qubit context.
- Parameters:
tableau – The current tableau or binary matrix.
rollout – The remaining rollout depth.
current_sequence – The elimination sequence built so far. The cache key uses only the qubits occupied in its current last layer.
- Returns:
A hashable key based on the tableau, rollout depth, and the current last-layer qubit occupancy.
- class RolloutCache(max_weight: int = 200000)[source]¶
Weighted least-recently-used cache for rollout continuations.
Cache entries are evicted according to an LRU policy once the configured maximum total weight is exceeded. The weight of an entry is defined as the number of operations stored in its cached continuation.
This design is useful for rollout synthesis because cached continuations can vary significantly in length. A weighted cache controls memory usage more effectively than a cache limited only by the number of entries.
- configure(max_weight: int) None[source]¶
Configure the cache parameters.
- Parameters:
max_weight – Maximum allowed total cache weight. The weight of one cached entry is the number of operations in its stored continuation.
- get(key: Hashable | None) EliminationSequence | None[source]¶
Retrieve a cached continuation for the given key.
Accessing an entry marks it as recently used.
- Parameters:
key – The cache key to look up. If
None, caching is treated as disabled for this lookup.- Returns:
A copy of the cached continuation if present, otherwise
None.
- set(key: Hashable | None, value: EliminationSequence) None[source]¶
Store a continuation under the given key.
If inserting the new value exceeds the configured cache budget, the least recently used entries are evicted until the cache is within the allowed weight bound again.
- Parameters:
key – The cache key under which the continuation should be stored. If
None, the value is not cached.value – The continuation sequence to store.