f
fbybl/genqc-cirdit
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

genQC CirDiT — 量子电路编译模型 (Ascend NPU 适配版)

原模型: Floki00/cirdit_multimodal_compile_3to5qubit_v1.1 论文: Synthesis of discrete-continuous quantum circuits with multimodal diffusion models (arXiv:2506.01666) 项目页: https://florianfuerrutter.github.io/genQC/ License: Apache-2.0

本仓库是 genQC 多模态扩散电路编译模型 CirDiT 在昇腾 NPU 上的服务化适配(vLLM-Ascend pooling runner)。将自然语言电路描述 + 目标酉矩阵编译为离散-连续量子门序列(3–5 qubit,最多 32 门)。


1. 模型简介

1.1 它解决什么问题

传统量子电路编译(合成)需要将任意目标酉矩阵 U 分解为量子门序列。genQC 用多模态扩散模型统一处理两类门:

  • 离散门:h、cx、ccx、swap
  • 连续参数门:rx(θ)、ry(θ)、rz(θ)、cp(θ)

扩散模型直接生成"离散门类别 + 连续旋转角参数"的联合表示,从而把离散-连续混合合成问题变成一个去噪生成问题。

1.2 关键能力

属性值
参数量~150M(训练 ~800k steps)
支持 qubit 数3–5
最大门数32(min 4)
门池h cx ccx swap rx ry rz cp
调度器双 DDIM(主 + 宽通道联合去噪)
预测类型v-type(速度预测)
训练classifier-free guidance(enable_guidance_train,guidance_train_p=0.1)

2. 模型架构

                    ┌────────────────────────────────────────────┐
 prompt (37 tokens) │   Frozen OpenCLIP (ViT-B-32, penultimate)  │ → c_emb [1, 37, 512]
                    └────────────────────────────────────────────┘
                    ┌────────────────────────────────────────────┐
 target unitary     │   UnitaryTextEncoder (unitary encoding)    │ → U [1, 2, 32, 32]
  (2×32×32)         └────────────────────────────────────────────┘
                    ┌────────────────────────────────────────────┐
                    │   Rotational Multimodial Embedder Tiny     │
                    │   12 class values + 3 param dims           │
                    └────────────────────────────────────────────┘
                                    ↓
        ┌───────────────────────────────────────────────────────────┐
        │            CirDiT 主干 (UnitaryCLIPPartialNoise)          │
        │  ┌─────────────────────────────────────────────────────┐  │
        │  │  Packing branch  ch=256 depth=6 heads=8             │  │
        │  └─────────────────────────────────────────────────────┘  │
        │  ┌─────────────────────────────────────────────────────┐  │
        │  │  Core branch     ch=768 depth=12 heads=16 (RoPE)    │  │
        │  └─────────────────────────────────────────────────────┘  │
        └───────────────────────────────────────────────────────────┘
                                    ↓
        双 DDIM 去噪(主通道 + 宽通道,classifier-free guidance, g=1.0)
                                    ↓
             门 token [5, 32] + 连续参数 [1, 32] → 192 float

2.1 主干配置(config.yaml 实测)

模块参数值
Core DiTch_core / depth_core / num_heads_core768 / 12 / 16
Packing DiTch_packing / depth_packing / num_heads_packing256 / 6 / 8
RoPEp_rope / base_rope0.9 / 1000.0
嵌入c_emb_size / t_emb_size640 / 512
Color/CLRclr_dim16(离散 13 + 参数 3)
文本编码OpenCLIP ViT-B-32 (datacomp_xl_s13b_b90k)max_length=37, penultimate layer, frozen
Unitary 编码text_encoding_ch 128 / unitary_encoding_ch 40downscale 4, depth 8

2.2 双调度器(joint mode)

调度器β 范围调度用途
scheduler (主)0.001 → 0.012path:beta_schedule_learned_linear离散门通道
scheduler_w (宽)0.0002 → 0.024cos_alpha4连续参数通道

两者均 prediction_type=v-type、1000 timesteps。推理时各用 30 步,双通道联合去噪,classifier-free guidance g=1.0。


3. Ascend NPU 适配方案

原模型是 PyTorch UnitaryCLIPPartialNoiseCompilationCirDiT(扩散模型),不是自回归 LLM,因此通过 vLLM-Ascend --runner pooling --convert embed 服务化,经 OpenAI 兼容的 /v1/embeddings 端点访问。

3.1 适配核心(cirdit_ascend/ OOT 插件)

  1. forward 内跑完整 DDIM 去噪:每次请求在 NPU 上完成整个 30 步联合去噪(含 classifier-free guidance、双通道),不需要 KV cache。
  2. 全部设备原生:forward 内禁 .item()/.cpu() 同步,解码、量化反算、RNG 均在设备上完成。
  3. IsAttentionFree / attn_type="attention_free":满足 vLLM HybridKVCacheCoordinator 断言。
  4. 自定义 Pooler:返回固定长度 [192] 行(门 token + 参数)。
  5. aux 权重预加载:CLIP 文本编码器、Rotational Embedder、β schedule 从 cirdit_compile_aux 装载,避免首请求设备迁移。

3.2 关键启动参数(serve.sh)

export ASCEND_RT_VISIBLE_DEVICES=1          # 逻辑芯片 1
export VLLM_ALLOW_LONG_MAX_MODEL_LEN=1
python3 -m vllm.entrypoints.openai.api_server \
  --model  /opt/atomgit/models/Floki00/cirdit_compile_servable \
  --tokenizer /opt/atomgit/adapt/cirdit_tokenizer \
  --load-format safetensors \
  --dtype float32 \
  --max-model-len 2094 \
  --max-num-seqs 2 \
  --port 8068 \
  --enforce-eager \
  --no-enable-chunked-prefill \
  --no-enable-prefix-caching \
  --runner pooling --convert embed \
  --served-model-name cirdit-compile

⚠️ --no-enable-chunked-prefill 必须:CirDiT 每请求固定 2094 tokens,切块会导致 EngineCore 崩溃。


4. 推理协议

4.1 请求格式(固定 2094 tokens)

[0]          magic = 77
[1..8]       header: system_size, num_qubits, max_gates, num_steps,
             g*1000, seed, prompt_len, flags
[9..45]      prompt tokens (37, CLIP tokenizer, 0-padded)
[46..2093]   2×32×32 quantized unitary(real + imag,16-bit 定点)
  • 2094 = 9 (header) + 37 (prompt) + 2048 (2×32×32 unitary)
  • unitary 每个元素:quantize = round(val × 32767) + 1_000_000(U_SCALE/U_OFFSET)
  • 小于 32×32 的酉按对角线重复填充(identity-block pad,tensor_prod_pad 约定)
  • 服务固定 5-qubit / 32-gate / 30-step 配置

4.2 输出格式(固定 192 float)

[0..159]    gate tokens,flattened [5, 32](行优先)
[160..191]  连续参数 [-1, 1](nP=1 × 32)

192 = 5(qubit) × 32(max_gates) + 1(param) × 32

4.3 门词汇表

token12345678
门hcxccxswaprxryrzcp
参数––––θθθθ

参数化门 token = {5,6,7,8},pad token = 9。


5. 使用说明

5.1 启动服务

cd /opt/atomgit/adapt/cirdit_ascend/run
bash serve.sh

5.2 推理(HTTP)

curl -X POST http://127.0.0.1:8068/v1/embeddings \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "cirdit-compile",
    "input": [ [77, 5, 5, 32, 30, 1000, 0, 37, 0, ...] ]
  }'

5.3 推理(Python)

from cirdit_ascend.protocol import encode_request, decode_output
import numpy as np, requests

# 1. CLIP tokenizer 编码 prompt
pids = clip_tokenizer(prompt)["input_ids"][:37]
pids = pids + [0] * (37 - len(pids))

# 2. 编码目标酉([2, N, N],real+imag)
row = encode_request(np.array(pids), unitary, system_size=5,
                     num_qubits=5, max_gates=32, num_steps=30, g=1.0)

# 3. 请求
r = requests.post("http://127.0.0.1:8068/v1/embeddings",
                  json={"model": "cirdit-compile", "input": [row]}, timeout=300)
emb = np.array(r.json()["data"][0]["embedding"])

# 4. 解码门序列
gates = emb[:160].reshape(5, 32).astype(int)   # [5, 32] gate tokens
params = emb[160:]                              # [32] 连续参数

5.4 模型目录

/opt/atomgit/models/Floki00/
├── cirdit_multimodal_compile_3to5qubit_v1.1/   # 真实权重 (765MB model.safetensors)
│   ├── model.safetensors
│   ├── config.yaml / config.json / README.md
│   ├── embedder.safetensors / beta_schedule_learned_linear.safetensors
├── cirdit_compile_servable/                    # serve 入口(model.safetensors 软链)
└── cirdit_compile_aux/                         # aux 权重
    ├── config.yaml / embedder.safetensors / beta_schedule...
    └── clip/clip_text.safetensors + tokenizer

6. 验证结果(Ascend NPU)

指标值
冷请求(首次 TBE 编译)20.4s
热请求2.96s
NPU1 AICore 峰值19%
HBM 占用4192 MB
语义区分度(不同 prompt,L1)453
输出 embedding有限(finite),维度 192

文件清单

README.md              ← 本文档
__init__.py            ← vLLM pooling adapter(架构注册 + forward)
genqc_model.py         ← CirDiT 主干 1:1 重建(PyTorch)
embedder.py            ← Rotational Multimodial Embedder
schedulers.py          ← 双 DDIM 调度器(v-type)
clip_text.py           ← Frozen OpenCLIP ViT-B-32 编码器
protocol.py            ← 2094-token 请求 / 192-float 输出协议
inference.py           ← Python 推理示例
serve.sh               ← vLLM-Ascend 启动脚本
tests_cirdit_e2e.yaml  ← e2e 验证配置
cirdit-model.md        ← 适配文档
assets/                ← 图片目录

7. 引用

@misc{fuerrutter2025synthesis,
  title={Synthesis of discrete-continuous quantum circuits with multimodal diffusion models},
  author={Fürrutter, Florian and Muñoz-Gil, Gorka and Garcia-Beni, Alejandro and Bermejo, Pablo and Murta, Bruno and Roman-Roche, Pablo and Zini, Marçal and Gasull, Adrià and del Campo, Andrea and Aspuru-Guzik, Alán},
  year={2025},
  eprint={2506.01666},
  archivePrefix={arXiv},
  primaryClass={quant-ph}
}