s
stella4528/cirdit_multimodal_compile_3to5qubit_v1.1-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

CirDiT 多模态量子电路编译(3-5 qubit)昇腾 NPU 适配版

CirDiT Multimodal Quantum Circuit Compilation:基于多模态扩散模型(CirDiT)的 离散-连续量子电路编译模型,输入目标酉矩阵(unitary)U,输出编译后的量子电路 (离散门 + 连续旋转参数),支持 3-5 qubit、最多 32 门,无需针对特定体系训练。

  • 论文:"Synthesis of discrete-continuous quantum circuits with multimodal diffusion models"(arXiv:2506.01666)
  • 项目主页:https://florianfuerrutter.github.io/genQC/
  • 参数量:约 191M(model.safetensors 765MB + embedder.safetensors + beta_schedule_learned_linear.safetensors)

本仓库为昇腾 Model-Agent 模型适配大赛交付物,已将模型完整适配到 Ascend NPU (Ascend-910 系列)推理,交付 inference.py 推理脚本、readme.md 部署说明与 requirements.txt 依赖清单。

模型信息

项目值
模型Floki00/cirdit_multimodal_compile_3to5qubit_v1.1(CirDiT)
任务离散-连续量子电路编译(unitary compilation,3-5 qubit,≤32 门)
架构多模态扩散 Transformer(Packing/Core/Unpacking + UnitaryCLIP 条件编码 + OpenCLIP 文本编码 + 旋转预设嵌入),DDIM v-prediction 双调度器
参数量约 191M(F32,610 个张量)
输入目标酉矩阵 U [1,2,N,N](实部/虚部两通道)+ 文本条件 + 噪声 latent [1,qubits,32,16]
输出编译后的电路门序列(门名 + 控制/目标 qubit + 连续参数 rad)
许可证Apache-2.0
原始仓库https://huggingface.co/Floki00/cirdit_multimodal_compile_3to5qubit_v1.1

环境依赖

  • Python 3.10/3.11,PyTorch 与 torch_npu 版本匹配(本环境为 PyTorch 2.9.0 + torch_npu 2.9.0)
  • 推理框架:genQC(PyPI 包,含管线/模型/调度器)与 open_clip_torch(文本编码器)
  • 依赖安装(华为云 PyPI 镜像加速):
pip install -r requirements.txt -i https://repo.huaweicloud.com/repository/pypi/simple/

文本编码器使用 OpenCLIP ViT-B-32(datacomp_xl_s13b_b90k)冻结权重,首次运行自动下载 (默认 HF_ENDPOINT=https://hf-mirror.com),约 578MB,缓存于 HF_HOME。

NPU 适配说明

genQC 0.2.5 的推理路径为纯 PyTorch,无 CUDA 硬编码——CUDA 相关代码仅存在于 训练 dataset 数据搬运(.to("cuda"))与 CUDA-Q 后端(circuits_cudaq.py,推理不使用), 因此无需修改源码,只需将 from_config_file(weights_dir, device) 的 device 参数透传 为 npu:0,模型/文本编码器/调度器即全量运行在 NPU。经实测验证无算子兼容性问题。

推理用法

# 默认用例:3-qubit 随机酉矩阵编译,NPU 推理(20 步 DDIM)
python3 inference.py --device npu:0

# 指定权重目录、推理步数与输出文件
python3 inference.py --device npu:0 --weights-dir ./weights --steps 20 --output result.json

# CPU 推理(对比基线)
python3 inference.py --device cpu --steps 10

# 自定义 qubit 数(3-5)与引导强度
python3 inference.py --device npu:0 --qubits 4 --steps 20 --guidance 3.0

权重目录不存在时,脚本会自动从 hf-mirror(或 GitCode 镜像兜底)下载 config.yaml 与三个 safetensors 权重。

实测结果(默认用例:3-qubit 随机酉矩阵,seed=42)

项目CPUNPU (npu:0)
目标酉矩阵8×8 随机酉(QR 分解,seed=42)同左
DDIM 推理步数1020
编译电路门数2928
有限性检查 finite_allTrueTrue
推理耗时17.163s1.213s
输出 latent 形状(1, 3, 32, 16)(1, 3, 32, 16)

注:扩散模型为随机采样,不同步数/设备下生成的电路门序列允许不同;验收比较 以输出张量有限性、电路合法性(门序列可解码、参数在 [-π, π] 量级)与可复现性 (同 seed 同参数下输出一致)为准,不作逐门逐元素一致要求。CPU 与 NPU 输出均在 相同 seed 下可复现。

输入输出说明

  • 输入:目标酉矩阵 U(--qubits 决定维数 N=2^qubits,实部/虚部堆叠为 [1,2,N,N])、 文本条件(默认 "compile the unitary")、随机种子(默认 42)
  • 输出:编译电路门序列 JSON——每门含 gate(h/cx/ccx/swap/rx/ry/rz/cp)、 control/target qubit 索引、params_rad 连续参数(rad,仅参数门)
  • 限制:支持 3-5 qubit、最多 32 门;门池为 h/cx/ccx/swap/rx/ry/rz/cp

目录结构

├── inference.py          # 推理脚本(必选,NPU device 透传)
├── readme.md             # 部署说明文档(必选)
├── requirements.txt      # 环境依赖清单
└── weights/              # 模型权重(config.yaml + 三个 safetensors)

引用

@misc{fuerrutter2025synthesis,
  title  = {Synthesis of discrete-continuous quantum circuits with multimodal diffusion models},
  author = {Fürrutter, Florian and others},
  year   = {2025},
  eprint = {2506.01666},
  archivePrefix = {arXiv}
}