Skip to content

added MPO zip-up as default long-range gate application method#449

Merged
aaronleesander merged 8 commits into
mainfrom
digital-statevector
Jun 4, 2026
Merged

added MPO zip-up as default long-range gate application method#449
aaronleesander merged 8 commits into
mainfrom
digital-statevector

Conversation

@aaronleesander
Copy link
Copy Markdown
Member

Description

This PR adds the MPO zip-up method (direct MPO-MPS multiplication) as the default long-range circuit simulation method.
This also adds several helpers such as MPO.multiply(MPS | MPO) which are now used throughout the code.

Additionally, the long-range gate settings have been changed from "tebd" to "swaps", "hybrid" to "tdvp" and "tdvp" to "full-tdvp" (all gates including nearest-neighbor gates).

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.

@aaronleesander aaronleesander added the feature New feature or request label Jun 3, 2026
@aaronleesander
Copy link
Copy Markdown
Member Author

@CodeRabbit review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 3, 2026

✅ 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
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 3, 2026

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Added MPO (Matrix Product Operator) as the default long-range gate application method
    • Introduced new gate mode options: "mpo", "full-tdvp", and "swaps"
    • Enhanced two-qubit gate application capabilities with MPO-based methods
  • Documentation

    • Updated simulation parameters documentation to reflect new default gate mode behavior
  • Tests

    • Added comprehensive test coverage for MPO gate application and utilities

Walkthrough

This PR establishes MPO zip-up as the default long-range two-qubit gate application method. It introduces low-level tensor utilities for MPO/MPS contraction, extends the MPO and MPS classes with new operations, refactors the digital gate dispatcher, and provides comprehensive test validation across all scenarios.

Changes

MPO-based Long-Range Two-Qubit Gate Application

Layer / File(s) Summary
Gate mode type definition and validation
src/mqt/yaqs/core/data_structures/simulation_parameters.py, docs/examples/simulation_parameters.md
GateMode type updated from hybrid/tebd/tdvp to mpo/swaps/tdvp/full-tdvp; default changed to "mpo" in both StrongSimParams and WeakSimParams; documentation clarifies behavior for each mode.
MPO/MPS tensor utilities
src/mqt/yaqs/core/data_structures/mpo_utils.py, tests/core/data_structures/test_mpo_utils.py
New module providing six low-level helpers: contract_mpo_site_with_mps_site, contract_mpo_site_with_mpo_site, identity_mpo_site, gate_tensor_lr_order, gate_support_mpo_tensors, and decompose_theta for tensor contraction and MPO construction from gates; tested for site-order invariance and correct identity tensor shape.
MPO.from_gate and MPO.multiply methods
src/mqt/yaqs/core/data_structures/mpo.py, tests/core/data_structures/test_mpo.py
MPO class extended with from_gate classmethod to construct gate-support MPO tensors and a multiply method supporting both MPS and MPO targets with optional compression and conjugation; identity preservation and dense product matching verified.
MPS.compress in-place truncation
src/mqt/yaqs/core/data_structures/mps.py
New MPS.compress method performs two-site SVD sweeps using threshold/max-bond-dim/trunc-mode parameters to truncate bond dimensions in place.
Digital gate dispatcher refactoring
src/mqt/yaqs/digital/digital_tjm.py
Removes local _gate_tensor_left_right_order helper and uses shared gate_tensor_lr_order; adds apply_long_range_gate_mpo for non-nearest-neighbor MPO-based application; dispatcher routes between TEBD (nearest-neighbor), TDVP (full-site), and MPO (long-range) based on mode and gate separation; default mode is "mpo".
Contraction utilities and equivalence checker integration
src/mqt/yaqs/digital/utils/contraction_utils.py, src/mqt/yaqs/equivalence_checker.py, tests/digital/utils/test_contraction_utils.py
Consolidates tensor helpers by importing decompose_theta and contract_mpo_site_with_mpo_site from mpo_utils; refactors apply_long_range_layer to use MPO.from_gate and the shared contraction helper; updates equivalence_checker to import iterate from contraction_utils; test imports refactored accordingly.
Integration tests across scenarios
tests/digital/test_digital_tjm.py, tests/digital/test_mps_utils.py, tests/core/data_structures/test_simulation_parameters.py
Digital TJM tests updated to use new gate modes throughout; helper _run_strong_noiseless defaults to "tdvp"; added test_apply_long_range_gate_mpo_direct_cx_long_range for long-range MPO path; new test_mps_utils.py suite with 14 tests covering identity preservation, nearest-neighbor matching, long-range validation, wide gates, and tensor caching; parameter tests verify new defaults and mode validation.
Documentation and changelog
CHANGELOG.md
Unreleased section notes "MPO zip-up" as the new default long-range gate application method; simulation parameters documentation clarifies all four gate modes and their nearest-neighbor vs long-range behavior.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • munich-quantum-toolkit/yaqs#441: Both PRs modify the gate-mode dispatch in digital_tjm.py and related configuration/docs/tests—added tebd/hybrid/tdvp options for circuit simulation #441 introduced the initial tebd/hybrid/tdvp framework, while this PR replaces those with "mpo"/"swaps"/"full-tdvp" and adds long-range MPO application.
  • munich-quantum-toolkit/yaqs#430: Both PRs touch the equivalence-checking backend wiring in equivalence_checker.py (this PR changes the iterate helper source to the consolidated utilities module).
  • munich-quantum-toolkit/yaqs#429: This PR's new MPS.compress() method depends on the SVD-truncation API and TruncMode from the utilities introduced in PR #429, establishing a direct dependency on that truncation infrastructure.

Poem

🐰 Zip it up, the MPO way!
Long-range gates need not delay—
Tensors contract, bonds compress tight,
From nearest neighbor to far-site light.
A rabbit's leap through quantum states,
MPO chains now seal our fates! 🎯

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the primary change: making MPO zip-up the default long-range gate application method, which is the main focus of the PR.
Description check ✅ Passed The PR description covers the main changes, includes a completed checklist with all items checked, and discloses AI-assisted content use as required by the template.
Docstring Coverage ✅ Passed Docstring coverage is 93.48% 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.

✏️ 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 digital-statevector

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.

coderabbitai[bot]
coderabbitai Bot previously requested changes Jun 3, 2026
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 `@docs/examples/simulation_parameters.md`:
- Around line 178-184: Reword the three consecutive sentences that start with
"Set `gate_mode=...` to avoid repetition: instead of repeating "Set", vary
phrasing by using alternatives like "Use `gate_mode=\"swaps\"` to ...", "Choose
`gate_mode=\"tdvp\"` for ...", or restructure into a single sentence that lists
the options and behaviors (e.g., "For long-range gates use `gate_mode=\"mpo\"`
(generic MPO–MPS), `gate_mode=\"swaps\"` inserts SWAPs, `gate_mode=\"tdvp\"`
mixes local TEBD with generator MPO + two-site TDVP, and
`gate_mode=\"full-tdvp\"` applies TDVP to every two-qubit gate"). Ensure the
descriptions for `gate_mode="mpo"`, `gate_mode="swaps"`, `gate_mode="tdvp"`, and
`gate_mode="full-tdvp"` remain accurate and preserve mentions of TEBD/SVD, MPO
contraction/compression (`svd_threshold`, `max_bond_dim`), and swap insertion
semantics.

In `@src/mqt/yaqs/core/data_structures/mps.py`:
- Around line 641-696: The compress() and truncate() functions duplicate the
same two-sweep SVD logic; remove the copy in truncate by delegating to
compress(): update truncate() to call self.compress(threshold,
trunc_mode="discarded_weight", min_bond_dim=2, max_bond_dim=...) (passing
through max_bond_dim/threshold args as appropriate) and remove the duplicated
sweep code from truncate; ensure symbols referenced are truncate and compress
and that truncate no longer directly indexes self.check_canonical_form()[0] so
it inherits compress()'s canonical fallback behavior.
🪄 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: 1ff68710-0cf1-4061-927d-1923a56692cb

📥 Commits

Reviewing files that changed from the base of the PR and between 80836e9 and a756c2f.

📒 Files selected for processing (16)
  • CHANGELOG.md
  • docs/examples/simulation_parameters.md
  • src/mqt/yaqs/core/data_structures/mpo.py
  • src/mqt/yaqs/core/data_structures/mpo_utils.py
  • src/mqt/yaqs/core/data_structures/mps.py
  • src/mqt/yaqs/core/data_structures/simulation_parameters.py
  • src/mqt/yaqs/digital/digital_tjm.py
  • src/mqt/yaqs/digital/utils/contraction_utils.py
  • src/mqt/yaqs/equivalence_checker.py
  • tests/core/data_structures/test_mpo.py
  • tests/core/data_structures/test_mpo_utils.py
  • tests/core/data_structures/test_simulation_parameters.py
  • tests/digital/test_digital_tjm.py
  • tests/digital/test_mps_utils.py
  • tests/digital/utils/test_contraction_utils.py
  • tests/test_equivalence_checker.py

Comment thread docs/examples/simulation_parameters.md Outdated
Comment thread src/mqt/yaqs/core/data_structures/mps.py
@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 3, 2026

Codecov Report

❌ Patch coverage is 98.80952% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/mqt/yaqs/core/data_structures/mpo.py 96.9% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@aaronleesander aaronleesander merged commit c7305db into main Jun 4, 2026
14 checks passed
@aaronleesander aaronleesander deleted the digital-statevector branch June 4, 2026 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant