Skip to content

🐛 Fix illegal number of qubits input#812

Merged
burgholzer merged 4 commits intomainfrom
fix-qpe-min-qubits
Feb 12, 2026
Merged

🐛 Fix illegal number of qubits input#812
burgholzer merged 4 commits intomainfrom
fix-qpe-min-qubits

Conversation

@flowerthrower
Copy link
Copy Markdown
Collaborator

@flowerthrower flowerthrower commented Jan 27, 2026

Description

This PR fixes (#811) illegal number of qubits in qpeexact and harmonizes docstrings that specify num_qubits.

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.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@flowerthrower flowerthrower marked this pull request as ready for review February 12, 2026 12:27
Copilot AI review requested due to automatic review settings February 12, 2026 12:27
@flowerthrower flowerthrower added the bug Something isn't working label Feb 12, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Feb 12, 2026

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Improved input validation across quantum benchmarks with explicit error messages replacing runtime assertions.
    • Invalid qubit counts now raise clear, descriptive errors before circuit construction.
  • Documentation

    • Updated parameter documentation to specify exact qubit count requirements for each benchmark (e.g., minimum values, divisibility constraints).
    • Enhanced clarity on input constraints for better usability.

Walkthrough

Added explicit input validation and clarified docstrings across multiple benchmark circuit creators; several assertions were replaced with ValueError raises to prevent invalid circuit construction. Tests were updated to cover the new invalid-input behavior and one dedicated AE test was removed.

Changes

Cohort / File(s) Summary
Validation + Early Errors
src/mqt/bench/benchmarks/ae.py, src/mqt/bench/benchmarks/bmw_quark_copula.py, src/mqt/bench/benchmarks/hhl.py, src/mqt/bench/benchmarks/qpeexact.py
Replaced assertions with explicit ValueError raises or added input checks enforcing minimum qubits/divisibility before circuit construction (AE: ≥2, BMW quark copula: divisible by 2, HHL: ≥3, QPE exact: ≥2). Docstrings updated to reflect requirements.
Docstring clarifications (no runtime changes)
src/mqt/bench/benchmarks/bv.py, src/mqt/bench/benchmarks/cdkm_ripple_carry_adder.py, src/mqt/bench/benchmarks/draper_qft_adder.py, src/mqt/bench/benchmarks/full_adder.py, src/mqt/bench/benchmarks/half_adder.py, src/mqt/bench/benchmarks/hrs_cumulative_multiplier.py, src/mqt/bench/benchmarks/modular_adder.py, src/mqt/bench/benchmarks/multiplier.py, src/mqt/bench/benchmarks/rg_qft_multiplier.py, src/mqt/bench/benchmarks/shor.py
Reworded parameter docs to clarify qubit count constraints (minimums, parity, divisibility, and length requirements). No changes to logic, signatures, or error handling.
Tests
tests/test_bench.py
Expanded invalid-input cases in test_wrong_circuit_size for hhl, qpeexact, bmw_quark_copula, and ae with updated expected error messages. Removed test_create_ae_circuit_with_invalid_qubit_number.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I nibble at docs and tidy the gate,
I hop in to guard an invalid state,
With ValueErrors firm and docstrings bright,
Circuits now sleep safe through day and night. 🥕✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Fix illegal number of qubits input' directly describes the main purpose of the PR: fixing invalid qubit count validation across multiple benchmark files.
Description check ✅ Passed The PR description includes a summary of changes (fixes #811, harmonizes docstrings), references the specific issue, and includes all checklist items marked as completed.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-qpe-min-qubits

No actionable comments were generated in the recent review. 🎉


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

@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: 4

🤖 Fix all issues with AI agents
In `@src/mqt/bench/benchmarks/full_adder.py`:
- Around line 24-25: The docstring for the function that documents the parameter
num_qubits is inconsistent with the runtime guard (the guard checks `num_qubits
< 4` and allows `num_qubits == 4`), so update the docstring sentence that
currently says "bigger than 4" to "at least 4" or "≥ 4" to match the validation
for the parameter `num_qubits`; locate the docstring referencing `num_qubits` in
the full adder function (the parameter named `num_qubits`) and change the
wording accordingly so it aligns with the existing guard `num_qubits < 4`.

In `@src/mqt/bench/benchmarks/half_adder.py`:
- Around line 24-25: The docstring for the function that builds the half-adder
circuit describes num_qubits as "bigger than 3" but the code validation allows
num_qubits == 3 (guard checks num_qubits < 3 and error text says "≥ 3"); update
the docstring wording for the num_qubits parameter to "at least 3" (or "≥ 3") to
match the validation and error message for the num_qubits parameter so the
documentation and guard (num_qubits) are consistent.

In `@src/mqt/bench/benchmarks/hhl.py`:
- Line 28: Update the docstring for the parameter num_qubits in hhl.py to state
it is the total number of qubits (including 1 system qubit, num_qpe_qubits =
num_qubits - 2 phase-estimation qubits, and 1 ancilla), not just the
phase-estimation register size; clarify the minimum requirement (at least 3) and
the relationship with num_qpe_qubits so callers understand how num_qubits maps
to phase-estimation precision.

In `@src/mqt/bench/benchmarks/modular_adder.py`:
- Around line 24-25: The docstring for the parameter num_qubits incorrectly says
"bigger than 2" while the validation allows num_qubits == 2; update the
docstring in modular_adder (the num_qubits parameter description) to say "at
least 2" or "≥ 2" to match the guard that checks num_qubits < 2 and the error
message.

Comment thread src/mqt/bench/benchmarks/full_adder.py Outdated
Comment thread src/mqt/bench/benchmarks/half_adder.py Outdated
Comment thread src/mqt/bench/benchmarks/hhl.py Outdated
Comment thread src/mqt/bench/benchmarks/modular_adder.py Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes input validation issues across multiple quantum benchmark circuits by replacing assertions with proper ValueError exceptions and adding comprehensive test coverage.

Changes:

  • Replaced assert statements with proper ValueError exceptions in hhl.py and bmw_quark_copula.py
  • Added input validation to qpeexact.py (previously had no validation)
  • Consolidated validation tests into the existing parameterized test_wrong_circuit_size test and removed redundant standalone test
  • Updated docstrings across 13 benchmark files to clarify input requirements

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/test_bench.py Added test cases for hhl, qpeexact, bmw_quark_copula, and ae validation; removed redundant standalone ae test
src/mqt/bench/benchmarks/hhl.py Replaced assertion with ValueError for num_qubits < 3 validation; updated docstring
src/mqt/bench/benchmarks/qpeexact.py Added new validation for num_qubits <= 1; updated docstring
src/mqt/bench/benchmarks/bmw_quark_copula.py Replaced assertion with ValueError for odd num_qubits validation; updated docstring
src/mqt/bench/benchmarks/ae.py Updated docstring to clarify minimum qubit requirement
src/mqt/bench/benchmarks/shor.py Minor docstring formatting improvement
src/mqt/bench/benchmarks/rg_qft_multiplier.py Enhanced docstring with specific validation requirements
src/mqt/bench/benchmarks/multiplier.py Enhanced docstring with specific validation requirements
src/mqt/bench/benchmarks/modular_adder.py Minor docstring formatting improvement
src/mqt/bench/benchmarks/hrs_cumulative_multiplier.py Enhanced docstring with specific validation requirements
src/mqt/bench/benchmarks/half_adder.py Minor docstring formatting improvement
src/mqt/bench/benchmarks/full_adder.py Minor docstring formatting improvement
src/mqt/bench/benchmarks/draper_qft_adder.py Enhanced docstring with specific validation requirements
src/mqt/bench/benchmarks/cdkm_ripple_carry_adder.py Enhanced docstring with specific validation requirements
src/mqt/bench/benchmarks/bv.py Enhanced docstring to clarify hidden_string length requirement

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/mqt/bench/benchmarks/half_adder.py Outdated
Comment thread src/mqt/bench/benchmarks/full_adder.py Outdated
Comment thread src/mqt/bench/benchmarks/modular_adder.py Outdated
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.

Nice. Great to have more consistency across the documentation and better error messages.
Looking forward to getting a release out with these changes. Probably after the dynamic circuits have been merged.

@burgholzer burgholzer merged commit 903ee7a into main Feb 12, 2026
18 checks passed
@burgholzer burgholzer deleted the fix-qpe-min-qubits branch February 12, 2026 21:57
@github-project-automation github-project-automation Bot moved this from In Progress to Done in MQT Applications Feb 12, 2026
@burgholzer
Copy link
Copy Markdown
Member

Ah, a changelog entry could have been nice.. maybe if you remember in your next PR, you could add one for this PR as well 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants