Skip to content

✨ Add RL truncation#697

Open
flowerthrower wants to merge 82 commits into
mainfrom
678-add-rl-truncation
Open

✨ Add RL truncation#697
flowerthrower wants to merge 82 commits into
mainfrom
678-add-rl-truncation

Conversation

@flowerthrower

@flowerthrower flowerthrower commented Jun 2, 2026

Copy link
Copy Markdown
Collaborator

Description

This PR adds a maximum steps (per episode) argument for Predictor/PredictorEnv initialization. As opposed to tracer_path this is not supposed to be changed after training. It defaults to None, keeping previous implementation behavior consistent.

I decided against implementing the pass timeout (for now), since I did not find an issue with overly long passes during training (might change with additional passes in the future), and this often raises issues when running multiple worker instances.

Fixes #678

Assisted-by: GPT-5.5 via Codex

Checklist

  • The pull request only contains commits that are focused and relevant to this change.
  • I have added appropriate tests that cover the new/changed functionality.
  • I have updated the documentation to reflect these changes.
  • I have added entries to the changelog for any noteworthy additions, changes, fixes, or removals.
  • I have added migration instructions to the upgrade guide (if needed).
  • The changes follow the project's style guidelines and introduce no new warnings.
  • The changes are fully tested and pass the CI checks.
  • I have reviewed my own code changes.

If PR contains AI-assisted content:

  • I have disclosed the use of AI tools in the PR description as per our AI Usage Guidelines.
  • AI-assisted commits include an Assisted-by: [Model Name] via [Tool Name] footer.
  • I confirm that I have personally reviewed and understood all AI-generated content, and accept full responsibility for it.

flowerthrower and others added 29 commits March 11, 2026 12:24
## Description

This PR addresses critical bugs in the RL training process with the
following key changes:

**Structure Improvements:**
- **Redesigned action validation logic** (`predictorenv.py`): Rewrote
`determine_valid_actions_for_state()` with a more structured (but
equivalent) state machine that explicitly tracks three circuit states
(synthesized, laid_out, routed) and handles 6 different state
combinations.
- Added helper methods `is_circuit_laid_out()` and `is_circuit_routed()`
to replace the buggy `CheckMap` pass with more reliable state checking.
The new logic supports both the original restricted MDP and a flexible
general MDP mode.

- **Fixed type annotation** (`actions.py`): Corrected `do_while`
parameter type from `dict[str, Circuit]` to `PropertySet` and added
missing import for Qiskit's `PropertySet`.

- **Added reproducibility** (`predictor.py`): Set random seed for
non-test training runs to ensure reproducible results.

- **Improved VF2Layout error handling** (`predictorenv.py`): Replaced
assertion failures with warning logs when VF2Layout doesn't find a
solution, preventing crashes during training.

**Test Updates:**
- Suppressed deprecation warnings in tket routing test

---------

Signed-off-by: Patrick Hopf <81010725+flowerthrower@users.noreply.github.com>
Co-authored-by: flowerthrower <flowerthrower@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@flowerthrower flowerthrower changed the title 678 add rl truncation ✨ Add RL truncation Jun 2, 2026
flowerthrower and others added 21 commits June 19, 2026 17:20
Co-authored-by: Lukas Burgholzer <burgholzer@me.com>
Signed-off-by: Patrick Hopf <81010725+flowerthrower@users.noreply.github.com>
@flowerthrower flowerthrower marked this pull request as ready for review July 8, 2026 18:25
@codecov

codecov Bot commented Jul 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@flowerthrower

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR introduces a configurable max_steps parameter to Predictor and PredictorEnv, enabling episode truncation once a step limit is reached. PredictorEnv.step now returns truncated=True with a truncation_reason when the limit is hit. A test and changelog entry accompany the change.

Changes

Add RL truncation

Layer / File(s) Summary
PredictorEnv max_steps truncation logic
src/mqt/predictor/rl/predictorenv.py
PredictorEnv.__init__ accepts and stores max_steps; step checks whether num_steps reaches max_steps, traces the terminal step, and returns terminated=False, truncated=True with info["truncation_reason"] = "max_steps_exceeded".
Predictor wiring, test, and changelog
src/mqt/predictor/rl/predictor.py, tests/compilation/test_predictor_rl.py, CHANGELOG.md
Predictor.__init__ gains a documented max_steps parameter forwarded to PredictorEnv; a new test verifies truncation at max_steps=1; CHANGELOG.md records the "Add RL truncation" entry with PR #697 link.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Predictor
  participant PredictorEnv
  participant Tracer

  Predictor->>PredictorEnv: __init__(max_steps)
  Predictor->>PredictorEnv: step(action)
  PredictorEnv->>PredictorEnv: increment num_steps
  alt num_steps >= max_steps
    PredictorEnv->>Tracer: trace terminal step
    PredictorEnv-->>Predictor: truncated=True, info.truncation_reason
  else
    PredictorEnv->>Tracer: trace normal step
    PredictorEnv-->>Predictor: normal terminated/truncated result
  end
Loading

Possibly related PRs

Suggested labels: enhancement

Suggested reviewers: burgholzer

Poem

A rabbit hops with steps to spare,
But now a limit's set with care.
When max_steps runs its course so fast,
Truncation saves the day at last! 🐇
No more loops that never end—
Training speeds up, my furry friend!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR adds max-step truncation, but issue #678 also asks for failed/timeout pass truncation, which is not implemented. Extend the implementation to handle failed/timeout pass truncation as requested in #678, or clarify that this behavior is intentionally deferred.
✅ Passed checks (4 passed)
Check name Status Explanation
Out of Scope Changes check ✅ Passed The changes stay focused on RL truncation, with matching tests, docs, and changelog updates.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title is concise and accurately highlights the main change: RL truncation support.
Description check ✅ Passed The description includes the summary, motivation, linked issue, and checklist items required by the template.
✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch 678-add-rl-truncation

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@flowerthrower flowerthrower requested a review from burgholzer July 9, 2026 06:59
@flowerthrower flowerthrower added the enhancement New feature or request label Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

✨ Add RL truncation

2 participants