Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Subroutines that return `bit` / `bit[n]` fail, including `return measure q;`

未关闭
#387 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@TheGupta2012 已经在做这个了。

开始于 2026年8月24日。

  • #410 来自 @TheGupta2012 —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清
技术栈
python
领域
compilers

调研方向

Start by running the def f() -> bit snippet to reproduce the AttributeError, then find the .value dereference on the return path in src/pyqasm/subroutines.py and the call/return visiting in src/pyqasm/visitor.py. The measurement-return case needs a design decision about the caller's classical bit, and the issue says it depends on the bit[n] representation change. Done means the bit, bit[n] and return measure q cases in tests/qasm3/test_function.py either work or raise a ValidationError naming both types.

由索引模型根据 Issue 内容生成。

描述

bug llm-assisted qasm3

Limitation

Subroutines allow any classical scalar return type,
including bit and bit[n], and the spec shows returning a measurement result directly.

pyqasm crashes with an AttributeError for a bit return, and rejects bit[n] returns and
measurement returns with a misleading "Invalid initialization value" error at the call site.

Example QASM failure

Internal error:

OPENQASM 3.0;
def f() -> bit {
    bit b;
    return b;
}
bit x = f();
AttributeError: 'NoneType' object has no attribute 'value'

Register return, rejected:

OPENQASM 3.0;
def f() -> bit[2] {
    bit[2] b;
    return b;
}
bit[2] x = f();
// ValidationError: Invalid initialization value for variable 'x'

Returning a measurement — the idiom the spec uses:

OPENQASM 3.0;
include "stdgates.inc";
def f(qubit q) -> bit {
    return measure q;
}
qubit[1] r;
bit b = f(r[0]);
// ValidationError: Invalid initialization value for variable 'b'

Assigning first does not help:

def f(qubit q) -> bit {
    bit c;
    c = measure q;
    return c;
}
// ValidationError: Invalid initialization value for variable 'b'

For contrast, -> int[8], -> float[64] and -> bool returns all work today, so the gap is
specific to bit-typed returns.

Change Requested

  1. def ... -> bit and def ... -> bit[n] must return a usable value that can initialise or
    be assigned to a matching bit / bit[n] variable.
  2. return measure q; inside a subroutine must be accepted, with the measurement recorded in
    the unrolled program and its result becoming the return value.
  3. A genuine type mismatch between the declared return type and the returned expression must
    raise a ValidationError naming both types — never an AttributeError.

Change note

This is distinct from #311, which concerns qubit index remapping for arguments passed into a
subroutine. This issue is about the return path and bit-typed values.

Implementation Details

  • Subroutine handling is in src/pyqasm/subroutines.py together with the call/return visiting
    in src/pyqasm/visitor.py.
  • The AttributeError: 'NoneType' object has no attribute 'value' indicates the return-value
    path assumes the returned object carries a .value — true for the literal-backed numeric
    types, false for the bit representation. Locating that dereference identifies the fix site
    directly.
  • The measurement case is the harder half: a measurement inside a subroutine body must both
    (a) emit a QuantumMeasurementStatement into the unrolled output against the caller's
    qubit, and (b) bind its classical result to the return value. Decide whether to synthesise a
    temporary classical bit in the caller's scope for this, which keeps the emitted program
    valid OpenQASM.
  • This depends on the bit[n] representation change tracked in the bit-register operators
    issue; sequencing that one first will make this fix smaller.
  • Tests: tests/qasm3/test_function.py — -> bit, -> bit[n], return measure q, assign-
    then-return, a declared/returned type mismatch, and a nested subroutine that forwards a bit
    return.
主要语言
Python
星标
29
派生
33
平均合并
1 天 22 小时
30 天内合并 PR
15

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

qBraid/pyqasm 的其他 Issue

查看 qBraid/pyqasm 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。