Skip to content

Integrate tracer functionality to support MQT FlowViz#714

Draft
linus-hologram wants to merge 16 commits into
munich-quantum-toolkit:668-improve-action-pass-imports-and-wrappersfrom
linus-hologram:thesis-integration
Draft

Integrate tracer functionality to support MQT FlowViz#714
linus-hologram wants to merge 16 commits into
munich-quantum-toolkit:668-improve-action-pass-imports-and-wrappersfrom
linus-hologram:thesis-integration

Conversation

@linus-hologram

@linus-hologram linus-hologram commented Jun 24, 2026

Copy link
Copy Markdown

Description

This PR implements the Predictor Tracer backend which collects information about the RL compilation process of the MQT Predictor. If active, the tracer outputs the collected information to a json file, which is used by the MQT FlowViz frontend to visualize the compilation.

AI Usage

The idea, logical structure and functionality of the code are entirely my own work; Google Gemini Pro 3.1 was solely used to assist with writing syntactically valid Python code. All code that was produced by Gemini was carefully reviewed and tested by myself.

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.
  • I confirm that I have personally reviewed and understood all AI-generated content, and accept full responsibility for it.

@linus-hologram

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 24, 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 Jun 24, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds CompilationTracer, a new tracing component for RL-based quantum compilation. It introduces dataclasses for device metadata, figure-of-merit metrics, and per-step episode snapshots, integrates step timing and trace recording into PredictorEnv, threads a tracer_output_path parameter through Predictor and qcompile, and validates the pipeline with a new integration test.

Changes

CompilationTracer Feature

Layer / File(s) Summary
Tracer dataclasses and JSON export
src/mqt/predictor/rl/tracer.py
Defines TopologyEdge, GateCalibration, DeviceMetadata, FOMMetric, FigureOfMeritMetrics, CompilationStep, and CompilationTracer; implements record_step, save_to_json, _extract_device_metadata, and _extract_float.
PredictorEnv tracing integration
src/mqt/predictor/rl/predictorenv.py
Adds tracer_output_path parameter and tracer state fields to PredictorEnv.__init__; introduces _collect_tracer_data; updates step to time execution and capture action metadata for tracing on both success and exception paths; updates reset to initialize CompilationTracer and record a baseline step.
Predictor and qcompile API wiring
src/mqt/predictor/rl/predictor.py, src/mqt/predictor/qcompile.py
Threads tracer_output_path through Predictor.__init__ into PredictorEnv, adds the parameter to rl_compile with singleton path-override/restore via finally, and exposes tracer_output_path on the public qcompile function.
Integration test and changelog
tests/compilation/test_tracer.py, CHANGELOG.md
Adds test_compilation_tracer_generates_valid_json covering file creation, JSON schema, step accounting, baseline/terminal step properties, figures-of-merit fields, and dataclass-level semantic validation; updates CHANGELOG with the new entry, PR link, and contributor link.

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant qcompile
  participant rl_compile
  participant Predictor
  participant PredictorEnv
  participant CompilationTracer

  User->>qcompile: qcompile(qc, tracer_output_path=path)
  qcompile->>rl_compile: rl_compile(qc, device, fom, tracer_output_path=path)
  rl_compile->>Predictor: Predictor(tracer_output_path=path)
  Predictor->>PredictorEnv: PredictorEnv(tracer_output_path=path)
  rl_compile->>Predictor: compile_as_predicted(qc)
  Predictor->>PredictorEnv: reset(qc) — init CompilationTracer, record baseline step
  loop Each RL step
    Predictor->>PredictorEnv: step(action) — time, trace, collect FOM
    PredictorEnv->>CompilationTracer: record_step(index, action_name, action_type, duration, reward, qc, fom)
  end
  PredictorEnv->>CompilationTracer: save_to_json(tracer_output_path/circuit_name.json)
  rl_compile-->>User: compiled circuit
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Possibly related PRs

Suggested labels

enhancement

Suggested reviewers

  • burgholzer

🐇 A new tracer hops into the scene,
Recording each gate, each step, each dream,
In JSON it writes what the circuit has done,
Every fidelity, depth, action — each one!
Now watch the qubits compile with flair,
The rabbit takes notes with the greatest of care! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: adding tracer functionality for MQT FlowViz support.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The PR description includes a clear summary, context, AI usage disclosure, and checklist, with only minor template details like dependencies left implicit.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
CHANGELOG.md (1)

22-30: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Move this feature entry to Unreleased.

Line 22 is the already released 2.3.0 section dated July 29, 2025. Adding a new feature there rewrites historical release notes instead of documenting the next release.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@CHANGELOG.md` around lines 22 - 30, Move the new feature entry out of the
released 2.3.0 section and into the Unreleased changelog section instead. Update
the CHANGELOG.md entry near the existing Added list so it is documented for the
next release, keeping the released 2.3.0 history unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/mqt/predictor/qcompile.py`:
- Around line 31-39: The public API docstring in the qcompile function uses a
nonstandard parameter section label, so update the docstring to Google-style
formatting. In the docstring for the function that compiles the quantum circuit,
replace the “Arguments:” section with an “Args:” section and keep the parameter
descriptions aligned with the existing parameters like qc, figure_of_merit, and
tracer_output_path.

In `@src/mqt/predictor/rl/predictor.py`:
- Around line 193-200: The rl_compile path is mutating shared singleton state
via predictor_singleton.env.tracer_output_path around
Predictor.compile_as_predicted, which can race across overlapping calls. Update
the rl/predictor.py flow so the tracer path is passed as call-local state into
compile_as_predicted (or another non-shared context) instead of rewriting
predictor.env.tracer_output_path, or otherwise serialize access to
predictor_singleton to prevent interleaving restores.

In `@src/mqt/predictor/rl/predictorenv.py`:
- Around line 232-235: The filename construction in PredictorEnv’s tracer output
path uses current_circuit_name directly, which can contain invalid or
path-separating characters. Sanitize or normalize current_circuit_name before
interpolating it into trace_{...}.json in the out_path logic, and ensure the
fallback name from the current_circuit_name assignment path cannot produce
invalid Windows filenames or unintended subpaths. Keep the fix localized to the
tracing/export flow in PredictorEnv so all uses of tracer_output_path remain
safe.
- Around line 195-207: The estimated metric fields in predictorenv are being
serialized with the wrong kind, since both estimated_success_probability and
estimated_hellinger_distance are approximation-based rather than exact. Update
the metric creation in the predictor environment logic (the esp_metric and
hd_metric handling in the state/hellinger path) so these FOMMetric instances use
kind="approx" instead of kind="exact", while keeping the unavailable fallback
unchanged.

In `@tests/compilation/test_tracer.py`:
- Around line 109-116: The semantic validation in the tracer test is only
checking top-level dataclass construction and still allows nested dict/list
payloads through. Update the validation around DeviceMetadata and
CompilationStep to explicitly construct or validate the nested dataclasses and
objects for figures_of_merit, topology, and calibration_data so malformed nested
payloads are actually caught. Use the existing DeviceMetadata, CompilationStep,
and related nested type names from the test to locate and strengthen the
validation block.

---

Outside diff comments:
In `@CHANGELOG.md`:
- Around line 22-30: Move the new feature entry out of the released 2.3.0
section and into the Unreleased changelog section instead. Update the
CHANGELOG.md entry near the existing Added list so it is documented for the next
release, keeping the released 2.3.0 history unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 0952b2c0-4b77-4fde-ba05-e2f7d3977567

📥 Commits

Reviewing files that changed from the base of the PR and between 2ecf4ad and ff6ab85.

📒 Files selected for processing (6)
  • CHANGELOG.md
  • src/mqt/predictor/qcompile.py
  • src/mqt/predictor/rl/predictor.py
  • src/mqt/predictor/rl/predictorenv.py
  • src/mqt/predictor/rl/tracer.py
  • tests/compilation/test_tracer.py

Comment thread src/mqt/predictor/qcompile.py
Comment thread src/mqt/predictor/rl/predictor.py
Comment thread src/mqt/predictor/rl/predictorenv.py
Comment thread src/mqt/predictor/rl/predictorenv.py
Comment thread tests/compilation/test_tracer.py Outdated
@linus-hologram linus-hologram marked this pull request as ready for review June 24, 2026 18:51
@linus-hologram linus-hologram marked this pull request as draft June 24, 2026 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant