Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Move the QASM 2 printer and its branch pre-processing out of qasm2.py

Đang mở
#447 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@danielgaskins đang làm issue này rồi.

Từ ngày 14/10/2026.

  • #454 của @danielgaskins — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
22/100
Loại issue
Tái cấu trúc
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python

Hướng nghiên cứu

Start in src/pyqasm/modules/qasm2.py, where Qasm2Printer and the helpers _flatten_branch, _collapse_broadcast_measurement and _filter_branch sit, then read tests/qasm2/test_branching.py. Done means the printer lives in its own module, _filter_branch and the printer share one definition of what QASM 2 can express, and the suite passes without changing expected output. Pull request #454 is already open and the issue depends on #338, so check those before starting.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

enhancement qasm2
Feature Description

Follow-up to #338. That PR fixes QASM 2 branch output with a Qasm2Printer in src/pyqasm/modules/qasm2.py. The fix works, but it grows qasm2.py from 216 to 564 lines. Most of the growth is module-level helpers that pre-process the unrolled AST before printing:

  • _parse_branch_condition, _add_chain_constraint, _flatten_branch — walk the unroller's per-bit if chain back into one if (creg == int) comparison.
  • _collapse_broadcast_measurement, _single_index, _writes_to_register — rebuild measure q -> c from per-bit measurements and guard multi-statement bodies.
  • _creg_sizes, _qreg_sizes, _qasm3_repr, _unsupported_condition_message — supporting lookups and error text.

These helpers mix three concerns in one file: the Qasm2Module class, QASM 2 validation, and QASM 2 serialization. Qasm2Module._filter_branch also calls _flatten_branch and discards the result, only to reuse its exceptions.

This issue moves the helpers and Qasm2Printer out of qasm2.py and simplifies their design. Behavior must not change.

Acceptance criteria

  • Qasm2Printer and the branch pre-processing helpers live in a separate module. qasm2.py keeps only Qasm2Module and imports what it needs.
  • Validation (_filter_branch) and printing still share one definition of what QASM 2 can express, so validate() is never stricter or looser than the printer.
  • Helpers with one caller fold into it, or get a clear name and docstring if they stay separate.
  • tests/qasm2/test_branching.py and the rest of the suite pass without changes to expected output or error messages.

Out of scope

Ideally, qasm2.py should not need this pre-processing at all. The unroller ravels if (c == 2) into a nested per-bit chain, and the printer then undoes that work. A cleaner fix is an unroller that keeps QASM 2 branches in their original form. That change touches the visitor and is larger, so this issue keeps the pre-processing and only relocates and simplifies it.

Implementation (Optional)
  • src/pyqasm/printer.py already holds circuit drawing, so the new code needs a different name. One option is src/pyqasm/modules/qasm2_printer.py.
  • _flatten_branch could return a small dataclass (register, value, body) in place of a tuple, and expose a separate check function for _filter_branch.

Depends on #338 merging first.

Ngôn ngữ chính
Python
Star
29
Fork
33
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
15

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của qBraid/pyqasm

Tất cả issue của qBraid/pyqasm

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.