Skip to content

🐛 Support BQSKit conversion of IQM's native r gate#679

Merged
flowerthrower merged 20 commits into
mainfrom
fix-bqskit-r-gate
May 29, 2026
Merged

🐛 Support BQSKit conversion of IQM's native r gate#679
flowerthrower merged 20 commits into
mainfrom
fix-bqskit-r-gate

Conversation

@flowerthrower
Copy link
Copy Markdown
Collaborator

@flowerthrower flowerthrower commented May 12, 2026

Description

Fixes #547, which reports

ValueError: The 'r' gate of device 'iqm_crystal_20' is not supported in BQSKIT.

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.
  • 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 11 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 🐛 add r gate support 🐛 Support BQSKit conversion of IQM's native r gate May 12, 2026
@flowerthrower flowerthrower marked this pull request as ready for review May 12, 2026 10:59
@flowerthrower
Copy link
Copy Markdown
Collaborator Author

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 12, 2026

✅ Actions performed

Full review triggered.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 12, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds support for IQM's native r gate in BQSKit by mapping it to U1qGate, implementing a new conversion function that translates BQSKit circuits to Qiskit through OpenQASM 2 rewriting, and updating imports and tests across the codebase.

Changes

IQM r-gate support in BQSKit

Layer / File(s) Summary
Type annotations and native gate mapping
src/mqt/predictor/rl/parsing.py
Improved type system introduces BqskitCircuit, TketCircuit, and QuantumCircuit annotations. Parameter type for PreProcessTKETRoutingAfterQiskitLayout.apply narrowed to TketCircuit. Native gate mapping in get_bqskit_native_gates gains entry for "r" gate mapped to gates.U1qGate().
BQSKit to Qiskit conversion function
src/mqt/predictor/rl/parsing.py
New bqskit_to_qiskit function converts BqskitCircuit to QuantumCircuit by encoding to OpenQASM 2, rewriting U1q( gate name to r(, and registering custom Qiskit r instruction. Parameter annotation for final_layout_pytket_to_qiskit updated to TketCircuit.
Test coverage for r-gate support
tests/compilation/test_helper_rl.py
Import Circuit and gates from bqskit.ir. Add test validating IQM crystal device includes U1qGate in BQSKit native gates. Add test verifying bqskit_to_qiskit converts U1qGate to Qiskit r operation with matching parameters.
Import refactoring and integration test updates
src/mqt/predictor/rl/predictorenv.py, tests/compilation/test_integration_further_SDKs.py
Remove bqskit_to_qiskit from bqskit.ext imports and import instead from mqt.predictor.rl.parsing. Update BQSKit synthesis test to use GatesInBasis(basis_gates=device.operation_names) instead of target=device, and remove "iqm" from unsupported device condition.
Changelog documentation
CHANGELOG.md
Document BQSKit conversion support for IQM's native r gate under Unreleased/Changed section. Add PR reference link.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested labels

bug

Poem

🐰 A rabbit hops through quantum gates,
Finding r where U1q waits,
From BQSKIT's home to Qiskit's door,
IQM's crystal shines once more! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding BQSKit conversion support for IQM's native 'r' gate, which directly addresses the issue.
Linked Issues check ✅ Passed The PR successfully implements the core requirement from issue #547: mapping IQM's 'r' gate to BQSKit's U1qGate and handling the QASM naming issue through conversion logic.
Out of Scope Changes check ✅ Passed All changes are directly related to the objective of supporting IQM's 'r' gate conversion: updates to gate mapping, QASM conversion logic, imports, tests, and documentation.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description check ✅ Passed The PR description addresses the core issue (#547), includes AI tool disclosure, and checks all required items, but lacks detailed motivation, context, and dependency information.

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

✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch fix-bqskit-r-gate

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 and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 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/rl/parsing.py`:
- Around line 189-206: Add a Google-style docstring to bqskit_to_qiskit
explaining purpose, Args (circuit: BqskitCircuit), Returns (QuantumCircuit) and
any Raises, and then replace the fragile qasm.replace("U1q(", "r(") with a
robust transformation: obtain the QASM via OPENQASM2Language().encode(circuit),
then perform a targeted substitution that only replaces the U1q gate tokens (not
occurrences inside comments or identifiers) before calling qasm2.loads; update
references to the r gate constructor (r_gate/r_gate_constructor) and the
qasm2.loads custom_instructions usage accordingly so the new replacement
preserves token boundaries and avoids touching comments or gate names.

In `@tests/compilation/test_helper_rl.py`:
- Around line 64-73: The test test_bqskit_to_qiskit_converts_u1q_to_r_gate uses
exact equality to compare floating params; replace the direct equality checks on
qc.data[0].operation.params with an approximate comparison (e.g., use
pytest.approx or math.isclose for each element) so floating-point precision
won't cause flaky failures; update assertions referencing
qc.data[0].operation.params accordingly to compare each parameter with an
approximate tolerance.
🪄 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: 3f19cc14-6cfb-43bc-b9de-c41095c7af81

📥 Commits

Reviewing files that changed from the base of the PR and between 4797e4c and 09563bc.

📒 Files selected for processing (5)
  • CHANGELOG.md
  • src/mqt/predictor/rl/parsing.py
  • src/mqt/predictor/rl/predictorenv.py
  • tests/compilation/test_helper_rl.py
  • tests/compilation/test_integration_further_SDKs.py

Comment thread src/mqt/predictor/rl/parsing.py
Comment thread tests/compilation/test_helper_rl.py
@codecov
Copy link
Copy Markdown

codecov Bot commented May 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

flowerthrower added a commit that referenced this pull request May 12, 2026
@flowerthrower flowerthrower requested a review from burgholzer May 13, 2026 15:47
Copy link
Copy Markdown
Member

@burgholzer burgholzer left a comment

Choose a reason for hiding this comment

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

LGTM 👍🏻
Would be nice to contribute an upstream fix for the conversion to BQSKit at some point.

@flowerthrower flowerthrower merged commit 90b7913 into main May 29, 2026
13 checks passed
@flowerthrower flowerthrower deleted the fix-bqskit-r-gate branch May 29, 2026 13:56
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.

🐛 Cannot train on IQM's crystal device

2 participants