【代码贡献】优化 QSEncode Walsh 变换与 Top-K 筛选性能 - #52
Open
omegaxyv wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
关联 Issue:#13(2026 本源杯开源创新赛道|代码贡献)
参赛队伍:Basia
简介
QSpare_Code在 Walsh 稀疏编码预处理中存在两个性能热点:sympy.fwht会将纯数值输入带入 SymPy 符号对象流水线,带来较高的 Python 对象与内存分配开销。numpy.argsort,即使只保留少量系数,仍需对所有 N 个幅值排序。本 PR 将两条热路径改为原生 NumPy 数值实现,并增加独立正确性测试和可复现基准。
主要修改
_fast_walsh_hadamard_transform:object数组;O(N log N),辅助空间O(N)。argsort替换为argpartition,平均时间复杂度由O(N log N)降为O(N)。n >= N快路径。正确性与测试
Quantum_Res()端到端概率分布对拍。本地定向测试(新增 30 例 + 原有 1 例):
性能数据
环境:Python 3.11.6,NumPy 2.4.6,SymPy 1.14.0,PyQPanda3 0.3.2。每组重复 3 次取中位数,最小采样时间 0.02 s。结果为单机参考,不作为跨平台保证。
N=4,096 时,
tracemalloc跟踪的 Python 峰值分配由 0.855 MiB 降为 0.080 MiB,约降低 10.7 倍。argsortargpartitionQuantum_Res加速兼容性说明
QSpare_Code公共 API 和方法签名不变。NaN和Inf现在会抛出明确异常。sympy.fwht,不移除整个项目的 SymPy 依赖。复现
PYTHONPATH=pyqpanda-algorithm python -m pytest \ -c test/pytest.ini -o addopts='' -p no:cacheprovider \ test/QAlgBase/Test_QSEncode_numeric_pipeline.py \ test/QAlgBase/Test_class_basic_sparecode_QSpare_Code.py -q python benchmarks/benchmark_qsencode.py \ --repeats 3 \ --minimum-sample-time 0.02变更文件
pyqpanda-algorithm/pyqpanda_alg/QSEncode/QSEncode.pytest/QAlgBase/Test_QSEncode_numeric_pipeline.py