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

Add the missing built-in constant-expression functions (`popcount`, `rotl`, `rotr`, `mod`, `exp`, `log`, `pow`, `ceiling`, `floor`)

Đang mở
#390 0 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

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

Từ ngày 24/8/2026.

  • #407 của @TheGupta2012 — đang mở

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
45/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
python
Lĩnh vực
compilers

Hướng nghiên cứu

Start in src/pyqasm/maps/expressions.py, where FUNCTION_MAP is defined near line 196, and check how existing entries such as sqrt are applied in constant and gate-argument contexts. Reproduce with the pyqasm.loads(...).validate() snippets in the issue. Done means ceiling, floor, exp, log and mod validate in both const declarations and gate arguments, with tests added in tests/qasm3/test_expressions.py. Leave popcount, rotl, rotr and pow for a maintainer to scope, since they depend on a companion issue and an upstream parser problem.

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

Mô tả

enhancement llm-assisted qasm3 qasm3-coverage

Limitation

Table 2 — Built-in mathematical functions
lists the functions every OpenQASM 3 implementation must provide in constant expressions:

arccos, arcsin, arctan, ceiling, cos, exp, floor, log, mod, popcount,
pow, rotl, rotr, sin, sqrt, tan.

pyqasm's FUNCTION_MAP provides only abs, real, imag, sqrt, sin, cos, tan,
arccos, arcsin, arctan. Nine of the spec's functions are missing:
ceiling, floor, exp, log, mod, popcount, pow, rotl, rotr.

Example QASM failure

Every one of these is a valid constant expression per the spec:

import pyqasm

for decl in [
    "const uint c = popcount(37);",
    'const bit[8] c = rotl("00101010", 3);',
    'const bit[8] c = rotr("00101010", 3);',
    "const int c = mod(7, 2);",
    "const float[64] c = exp(1.0);",
    "const float[64] c = log(2.0);",
    "const int c = ceiling(1.2);",
    "const int c = floor(1.8);",
]:
    m = pyqasm.loads("OPENQASM 3.0;\n" + decl)
    m.validate()
ValidationError: Invalid initialization value for constant 'c'

They fail as gate arguments too, where sqrt already works:

OPENQASM 3.0;
include "stdgates.inc";
qubit[1] q;
rx(sqrt(2.0)) q[0];   // OK today
rx(exp(1.0)) q[0];    // ValidationError: Invalid parameter 'exp(1.0)' for gate 'rx'

The spec's own example under Built-in constant expression functions exercises most of them:

const float[64] f2 = 2.0 * exp(f1);
const int[8] i2 = pow(i1, u1);
const bit[8] b2 = rotl(b1, 3);

Change Requested

  1. Add ceiling, floor, exp, log, mod to FUNCTION_MAP with the spec's signatures.
  2. Add popcount, rotl, rotr, which operate on bit[n] and uint[n] and must preserve
    the input width.
  3. Add pow(a, b) as a function. Note the parser caveat below — this one is not purely a
    FUNCTION_MAP entry.
  4. Report an unknown function name and an arity or type mismatch as a ValidationError naming
    the function, rather than the current generic "Invalid initialization value" message,
    which gives the user no indication that the function was the problem.

Implementation Details

  • FUNCTION_MAP is in src/pyqasm/maps/expressions.py:196. Most additions are one line each:
    "exp": np.exp, "log": np.log, "ceiling": np.ceil, "floor": np.floor,
    "mod": np.mod.
  • popcount / rotl / rotr are defined on bit[n] and depend on a width-carrying bit
    representation — see the companion issue on bit[n] operators. Widths must be preserved:
    rotl(a, n) == rotr(a, -n), and the output width equals the input width.
  • pow has a known blocker. const int c = pow(2, 3); fails at parse time in
    openqasm3 1.0.1 because pow is also the gate-modifier keyword. Verify against the
    current upstream release and, if it still reproduces, open an issue on
    openqasm/openqasm and link it here; the pyqasm-side change can land independently for the
    other eight functions.
  • Spec overload resolution matters for the error messages: pow(int, uint) -> int is
    preferred over pow(float, float) -> float, which is preferred over the complex overload.
    exp and sqrt each have float -> float and complex -> complex forms.
  • Tests: tests/qasm3/test_expressions.py — one case per function covering a const
    declaration and a gate argument, plus arity and type-error cases.
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.