SparQSim / pysparq
pysparq —— 稀疏态量子电路模拟器的全功能 Python 框架(Register Level Programming、原生 QRAM、动态算子、RIR 解释器)
仓库分工
依赖方向:SparQSim → QRAM-Simulator。SparQSim 发布
pysparq 包并承载 SparQ C++ 框架
(SparQ/ 稀疏态模拟器 + SparQ_Algorithm/ 算法库 + 全部 Python 绑定与算法类实验);
QRAM 基座(Common + QRAM + ThirdParty)在
QRAM-Simulator 仓库独立发版,
本仓库以 git submodule(相对 URL ../QRAM-Simulator.git)引用并编译。
| 仓库 | 内容 | PyPI 包 |
|---|---|---|
| SparQSim(本仓库) | SparQ C++ 框架 + pysparq 全功能 Python 框架(core.cpp 富绑定 + 纯 Python 算法层) | pysparq |
| QRAM-Simulator | 纯 C++ QRAM 基座(Common + QRAM 电路核心 + QRAM 论文实验 + pybind11 薄绑定) | qram-simulator |
安装
pip install pysparq
要求:Python 3.10 – 3.13,NumPy。
需要 QRAM 电路级 Python API(QRAMCircuitQubit/QRAMCircuitQutrit 噪声
仿真工作流)时,另行安装 QRAM-Simulator
仓库发布的独立包:
pip install qram-simulator
GPU 支持:CUDA/GPU 后端当前在 CMake 中临时屏蔽,默认只构建 CPU 路径。
从源码构建
git clone --recurse-submodules <this-repo-url>
cd SparQSim
pip install .
5 分钟上手示例
import pysparq as ps
# 1. 创建稀疏态
state = ps.SparseState()
# 2. 定义寄存器(Register Level Programming 的核心)
addr_id = ps.AddRegister("addr", ps.UnsignedInteger, 4)(state)
data_id = ps.AddRegister("data", ps.UnsignedInteger, 8)(state)
# 3. 初始化叠加态(所有地址等概率)
ps.Hadamard_Int("addr", 4)(state)
# 4. 创建 QRAM 并加载数据
memory = [i * 2 for i in range(16)]
qram = ps.QRAMCircuit_qutrit(addr_size=4, data_size=8, memory=memory)
# 5. 执行 QRAM 加载:|addr⟩|0⟩ → |addr⟩|memory[addr]⟩
ps.QRAMLoad(qram, "addr", "data")(state)
# 6. 直接进行算术操作(无需编译成门!)
ps.Add_ConstUInt_InPlace("data", 5)(state) # data = data + 5
# 7. 测量 / 概率查询
ps.set_seed(0)
outcome, prob = ps.MeasureZ("data")(state)
dist = ps.Probability.distribution(state, "addr")
Register Level 特性的关键 API
# 量子算术 - 直接寄存器操作(Register Level Programming 核心)
ps.Add_UInt_UInt(in1, in2, out) # out = in1 + in2
ps.Add_UInt_ConstUInt(reg, const) # reg = reg + const
ps.Mult_UInt_ConstUInt(in, c, out) # out = in * c
ps.ShiftLeft(reg, n) # 左移 n 位
ps.ShiftRight(reg, n) # 右移 n 位
# 基础量子门
ps.Hadamard_Int(reg, n_digits) # 对整数寄存器应用 Hadamard
ps.X_Bool(reg, pos) # X 门(特定比特位)
ps.Z_Bool(reg, pos) # Z 门
# QRAM 操作
ps.QRAMLoad(qram, addr_reg, data_reg) # QRAM 加载
ps.QRAMLoadFast(qram, addr_reg, data_reg) # 快速版本
# 可播种的测量 / 复位 / 概率查询(面向动态执行器:mid-circuit MEASURE/RESET/QIF)
ps.set_seed(seed) # 播种全局随机数引擎,使采样结果可复现
outcome, prob = ps.MeasureZ(reg)(state) # 投影式 Z 基测量:坍缩 + 重新归一化
measured = ps.Reset(reg, target=0)(state) # 测量 + 经典条件翻转,强制复位到 target
p = ps.Probability(reg, value)(state) # 只读 Born 概率查询,不改变状态
dist = ps.Probability.distribution(state, reg) # 单寄存器完整结果分布(只读)
高层算法库(pysparq.algorithms)
纯 Python 实现的算法层,组合公开原语(Grover、Shor、QDA、CKS、态制备、块编码等), 与 C++ 实验代码的逐一对应关系见 docs/algorithm-implementation.md。
项目结构
SparQSim/
├── SparQ/ # SparQ C++ 稀疏态模拟器(伞形目标 SparQ 在根 CMake 定义)
├── SparQ_Algorithm/ # 高层算法 C++ 库(态制备、块编码、哈密顿模拟、QDA 等)
├── Experiments/ # 量子算法 C++ 实验(QDA/Grover/QFT/Shor/QCNN/CKS/GHZ 等)
├── test/ # C++ 测试(SparQ 单测 + CommonTest 完整版;SPARQ_BUILD_TESTS 门控)
├── extern/qram-simulator/ # QRAM 基座 submodule(Common + QRAM + ThirdParty,相对 URL ../QRAM-Simulator.git)
├── PySparQ/
│ ├── core.cpp # pybind11 富绑定(_core 模块)
│ ├── pysparq/ # Python 包(operators/ algorithms/ rir conformance dynamic_operator)
│ └── test/ # pytest 套件
├── docs/ # Sphinx 文档 + 算法转译指南
├── examples/ # C++ 与 Python 示例(SPARQ_BUILD_EXAMPLES 门控 C++ 部分)
└── pyproject.toml # pysparq 包(scikit-build-core + setuptools-scm)
发版流程
- 在 Gitea(开发主仓)合并变更到 main;
- 需要新版核心时,先等 QRAM-Simulator
发布对应 tag,然后
git submodule update --remote(或 checkout 到该 tag)提交 pin; - 同步到 GitHub 上游
IAI-USTC-Quantum/SparQSim; - 更新
CHANGELOG.md,打 tagvX.Y.Z(延续 pysparq 版本系列,下一版 v0.2.0); - push tag 或创建 GitHub Release →
pypi-publish工作流自动构建 cp310–313 × (manylinux / win_amd64) wheel + sdist(sdist 内嵌核心源码,自包含) 并发布到 PyPI(Trusted Publishing / OIDC)。
外部消费者(qecc_lang、quantum_cfd QFVM)的 API 依赖冻结见
PySparQ/consumer_runtime_inventory.json,由 PySparQ/test/test_consumer_runtime_contract.py 强制。
论文与引用
About Us
本项目由 IAI-USTC Quantum 开发(合肥综合性国家科学中心人工智能研究院量子人工智能团队)。
许可证
Apache-2.0 License
Release files for pysparq 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pysparq-0.2.0.tar.gz | 5.5 MB | Details |
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| pysparq-0.2.0-cp313-cp313-win_amd64.whl | CPython 3.13 | CPython 3.13 | Windows x86-64 | Details |
| pysparq-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.13 | CPython 3.13 | Linux glibc 2.17+ x86-64 | Details |
| pysparq-0.2.0-cp312-cp312-win_amd64.whl | CPython 3.12 | CPython 3.12 | Windows x86-64 | Details |
| pysparq-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.12 | CPython 3.12 | Linux glibc 2.17+ x86-64 | Details |
| pysparq-0.2.0-cp311-cp311-win_amd64.whl | CPython 3.11 | CPython 3.11 | Windows x86-64 | Details |
| pysparq-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.11 | CPython 3.11 | Linux glibc 2.17+ x86-64 | Details |
| pysparq-0.2.0-cp310-cp310-win_amd64.whl | CPython 3.10 | CPython 3.10 | Windows x86-64 | Details |
| pysparq-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.10 | CPython 3.10 | Linux glibc 2.17+ x86-64 | Details |
Total release size: 57.2 MB
Release files / pysparq-0.2.0.tar.gz
| Download URL | pysparq-0.2.0.tar.gz |
|---|---|
| Size | 5.5 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
794abf45a529dd8e5559dc4050236b2c6496bbef6e5428bf07b0b04e9e53c322
|
|
BLAKE2b-256 checksum How to use checksums |
1d1793828e2e754db1e891bc1a05832067a9e0140586f92247ebd79273ea628f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.
Transparency logRelease files / pysparq-0.2.0-cp313-cp313-win_amd64.whl
| Download URL | pysparq-0.2.0-cp313-cp313-win_amd64.whl |
|---|---|
| Size | 9.4 MB |
| Tags | CPython 3.13 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
c69cd2df86b312ae3e2910b1fb2b23bc570fa56ff4524f175b3233e57f7c3e49
|
|
BLAKE2b-256 checksum How to use checksums |
3e2c3bdfbea0d47ab43b788d06a4a1a682d8ac5fa1c0157031fb83dda458db08
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.
Transparency logRelease files / pysparq-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | pysparq-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 3.5 MB |
| Tags | CPython 3.13 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
40e9989a6a633baf2ee92a420f3b45a8c6636f3ecc37d0ab32cf1bf2d1bd5334
|
|
BLAKE2b-256 checksum How to use checksums |
a48831d91245cd222044bb34feca8b55ac587bd84b7c5032d31ddc8f3cddf85c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.
Transparency logRelease files / pysparq-0.2.0-cp312-cp312-win_amd64.whl
| Download URL | pysparq-0.2.0-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 9.4 MB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
3ab089d79f31b3e4aebeb28fe01b9ca9c3aa5bd60c45ced51d4d847ca359b61b
|
|
BLAKE2b-256 checksum How to use checksums |
366bde8526ae9a3dd2973649bed3a0cfc85543a256141acdc07ea24c7abb9b0b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.
Transparency logRelease files / pysparq-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | pysparq-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 3.5 MB |
| Tags | CPython 3.12 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
993a8e8afd92afe877ddcd57ed912af1ce8921b108731f4584e0d7e3af5626ff
|
|
BLAKE2b-256 checksum How to use checksums |
4d3c254ccce8bbb5411ada1223f0fb8b0bd8d3dce46ce2b5e06bc109fb821c28
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.
Transparency logRelease files / pysparq-0.2.0-cp311-cp311-win_amd64.whl
| Download URL | pysparq-0.2.0-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 9.4 MB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
8935896a7eb51df17a7a9f6ee0e8976ac3c2af0ce399d9361872c2142aeec120
|
|
BLAKE2b-256 checksum How to use checksums |
6945c7d2e3ad20afe168052408ebf676979345dbe48297212d370bf167f934ec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.
Transparency logRelease files / pysparq-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | pysparq-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 3.5 MB |
| Tags | CPython 3.11 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
f4dd7bec9d422acfcb9f48bebe743c38f2278d95fc554cf472076e2005fa1f94
|
|
BLAKE2b-256 checksum How to use checksums |
8aa819924a4331ff90dccb7519b230353bf6540eff4c9100e3a727937ed10f2d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.
Transparency logRelease files / pysparq-0.2.0-cp310-cp310-win_amd64.whl
| Download URL | pysparq-0.2.0-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 9.4 MB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
2ce501d6c743dc7a60d8319690e74209b02107f2569e7ab0cd62d0cc7297eeb8
|
|
BLAKE2b-256 checksum How to use checksums |
1a1407f6bbf4d4fc0bf2736da966b5777ef036925fb9e1537aa68c770be43cdd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.
Transparency logRelease files / pysparq-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | pysparq-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 3.5 MB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
d0f81977712f1640b9b79a9a7c96f8124f4187e2417fda69972dbbffa9139805
|
|
BLAKE2b-256 checksum How to use checksums |
4a816c957ade78286e3acd093f6bf5be3661816daeb710dee8dc0877871ec496
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.
Transparency log