Repository navigation
Add compiled single-GPU execution with cuQuantum and ixquantum - #179
Open
Shaobo-Zhou wants to merge 7 commits into
Open
Shaobo-Zhou wants to merge 7 commits into
Shaobo-Zhou wants to merge 7 commits into
Conversation
Introduce QubitCircuitGPU with optional first-use native compilation, automatic vendor gate dispatch, terminal readout, and adjoint gradients. Preserve DeepQuantum wire semantics, shared parameters, and fixed gate inverses, with exception-safe resource ownership and explicit limitations. Document CoreX and ixquantum dependencies, provide a CPU compatibility workflow, and validate numerical behavior against DeepQuantum on Iluvatar hardware. Keep the scope to single-GPU execution.
sansiro77
reviewed
Nov 18, 2026
sansiro77
left a comment
Contributor
There was a problem hiding this comment.
- 把“共享存储”误当成“同一个可训练参数”,会错误合并梯度。例如:
initial = torch.tensor(0.37)
rx = dq.Rx(inputs=initial, requires_grad=True)
ry = dq.Ry(inputs=initial, requires_grad=True)| 检查项 | 原有 DeepQuantum | 本 PR |
|---|---|---|
| 参数数量 | 2 | 被合并成 1 |
| Rx 参数梯度 | −0.337144 | −0.674288 |
| Ry 参数梯度 | −0.337144 | 没有梯度 |
- 参数解冻后,编译缓存没有失效。先 requires_grad_(False) 并执行线路,参数会被编译成常量,随后 requires_grad_(True) 再修改参数,会沿用旧的固定值
- GPU 测试需要区分“有 CUDA”和“装了可选后端”。现在 test_circuit_gpu.py 只根据 torch.cuda.is_available() 决定是否跳过。因此普通 NVIDIA 开发环境即使没有安装 cuQuantum,执行完整 pytest 也会进入这些测试,随后因缺 SDK 失败
- 建议文档从 ixquantum.md 改成 gpu_simulation.md,实现目录从 backends/gpu/ 收成一层 custatevec/
- 现在的 Markdown 对已经配好 GPU 开发环境的人够用,对从空环境开始的用户还不够。建议两个 GPU 后端各给一套从全新 conda 环境到跑通 GHZ 示例的步骤
- Matplotlib本来就会带入'packaging',应该不用额外写
- test-gpu-backend.yml 实际检查的是无 GPU、无厂商 SDK 时,打出来的 wheel 能否安装和使用,建议改名 test-cpu-compatibility.yml
- C++/CUDA 格式建议用固定版本的 clang-format 接入现有 pre-commit,并加到dev依赖里。用 20.1.8 检查,_gpu_executor.cpp 有 5 处格式不通过
sansiro77
reviewed
Nov 19, 2026
sansiro77
left a comment
Contributor
There was a problem hiding this comment.
-
调用
init_para()后,线路仍用旧参数计算,梯度也写回旧参数。import deepquantum as dq import torch c = dq.QubitCircuit(1) # 对比时换成 dq.QubitCircuitGPU(1) ry = dq.Ry(inputs=0.37, requires_grad=True) c.add(ry) c.observable(0, 'z') old = ry.theta c() # 第一次用 0.37 执行 c.init_para() # 创建新参数,不是在 old 上改值 new = ry.theta with torch.no_grad(): new.fill_(0.90) c() # 两种实现都重新执行 z = c.expectation().sum() z.backward()
原因是缓存只检查旧参数的
requires_grad有没有变,不检查门里的参数是不是换了对象。init_para()换了参数,CompiledCircuitPlan也应该更新。 -
通过
inputs传入的参数,先冻结再解冻,仍然不能求导。theta = torch.tensor(0.37) c.ry(0, inputs=theta) c.observable(0, 'z') c() # 此时 theta 不求导,被编译成常量 theta.requires_grad_(True) c() c.expectation().requires_grad # 原有 DeepQuantum 是 True,本 PR 是 False
这次
theta没换对象,变的是requires_grad。但通过inputs传入的张量在门里注册为buffer,不在c.parameters()里,所以缓存检查漏掉了它。应该检查门实际使用的theta。 -
把
psi.conj()传给expectation_from_state(),算出来的是原态psi的期望值。c.observable(0, 'y') psi = torch.tensor([1, 1j], device='cuda') / 2**0.5 c.expectation_from_state(psi, c.observables) # +1 c.expectation_from_state(psi.conj(), c.observables) # 应为 -1,当前实现仍按 +1 计算
原因是 PyTorch 的
conj()默认只加一个“取共轭”的标记,不改底层数据。C++ 直接把data_ptr()交给 SDK,两次读到的是同一份数据。传给 SDK 前需要调用resolve_conj()。 -
同一条线路第一次在
inference_mode()里执行,退出后再执行就会报错。(如果是之前说的为了多模拟一个qubit的缓存优化,感觉可以不管)with torch.inference_mode(): c() # 第一次执行,在这个模式下分配状态缓冲区 c() # 普通前向就会报错,不需要调用 backward()
原因是执行器要复用第一次分配的
state_,第二次运行时会先调用state_.zero_()。但 PyTorch 不允许在inference_mode()外原地修改这个模式里创建的张量。跨调用复用的缓冲区应按普通张量分配。 -
文档里的 GHZ 示例,建议直接检查态矢
This branch has not been deployed
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.
Add
QubitCircuitGPU, an optional compiled single-GPU statevector backend supporting NVIDIA cuQuantum and Iluvatar ixquantum through one shared cuStateVec API executor and adjoint kernel implementation.Behavior
backend='auto'|'cuquantum'|'ixquantum'. Auto selection follows the installed PyTorch build; explicit mismatches and missing SDKs raise errors without fallback.diff_methodargument is needed. Inference reads the existing state after a forward pass; explicit parameters trigger a fresh forward.Installation
For NVIDIA, install CUDA-enabled PyTorch and
deepquantum[cuquantum], plus a matching CUDA development toolkit with NVCC and a C++17 compiler. SDK discovery uses the installed cuStateVec wheel orCUQUANTUM_ROOT.For Iluvatar, install CoreX-compatible PyTorch and
deepquantum[ixquantum], and obtain the matching CoreX toolkit and ixquantum SDK from Iluvatar's developer downloads page; contact information is available at the bottom of that page. ConfigureIXQUANTUM_ROOT.See the installation and usage guide. Extensions compile on first GPU use with separate backend caches; normal package import does not require either SDK.
Example
Validation
Current limitations
Single-card pure statevector only; no runtime encoding/reupload, batching, noise, mid-circuit measurements, differentiable initial states, or higher-order gradients. Matrix observables support non-differentiable readout only.