WebUI 与持久化¶
UnifiedQuantum 自带一个 FastAPI 网关 + 前端 SPA,可以用浏览器查看 backend 列表、 芯片可视化、本地任务历史,并运行本地量子实验。
启动方式¶
# 发行包自带界面,安装后即可启动
uniqc gateway start --host 127.0.0.1 --port 8000
# 然后在浏览器打开 http://127.0.0.1:8000/
打开 /demo 的 Local quantum lab,选择 GHZ、W 态或 Ramsey 干涉,设置
2–8 个量子比特后运行。页面展示原生 statevector 的精确概率、独立解析对照误差、
实际线路和 JSON 导出;右侧最低位对应 q[0]。更改参数后旧结果失效。
这是本地模拟演示,耗时是本机软件运行时间。
同一演示也可从 CLI 运行:
uniqc demo --case ghz --qubits 4 --output ghz.json
uniqc demo --case ramsey --qubits 3 --phase 0.73
开发前端:
cd frontend
npm ci
npm run dev # vite dev server,会代理到 uniqc gateway 后端
后端的 ASGI app 由 uniqc.gateway.server.create_app() 构造;可以直接用 uvicorn /
gunicorn / 第三方 reverse-proxy 部署。
从源码开发构建发行包时,需要 Node/npm;PEP517 构建后端按已提交的
package-lock.json 构建界面,并保存前端源文件、构建器和实际资产的 SHA256。
正式源码包自带这些资产,构建 wheel 无需 Node 或 npm 网络下载。
前端生产依赖的原始许可与来源随包保留。使用 frontend/dist 的开发回退仍可用。
编程访问¶
01 — Gateway / WebUI: programmatic launch and persistence¶
Source: examples/5_webui/01_gateway_demo.py
Status: pass
uniqc gateway start 启动一个 FastAPI 服务(uniqc.gateway.server:app),既给前端 SPA
当后端,又对外提供 REST + WebSocket。本例不真的把进程留住,只演示如何在 Python 里
拿到 ASGI app、读出网关配置(host/port)以及触发一次任务,让后台任务库里有数据
可看。
安装发行包后直接用 uniqc gateway start --host 127.0.0.1 --port 8000 启动,
界面随包提供。前端源码开发使用 cd frontend && npm ci && npm run dev。
Source code
"""01 — Gateway / WebUI: programmatic launch and persistence
[doc-require: ]
[doc-output-include: stdout, source]
``uniqc gateway start`` 启动一个 FastAPI 服务(``uniqc.gateway.server:app``),既给前端 SPA
当后端,又对外提供 REST + WebSocket。本例不真的把进程留住,只演示如何在 Python 里
拿到 ASGI app、读出网关配置(host/port)以及触发一次任务,让后台任务库里有数据
可看。
安装发行包后直接用 ``uniqc gateway start --host 127.0.0.1 --port 8000`` 启动,
界面随包提供。前端源码开发使用 ``cd frontend && npm ci && npm run dev``。
"""
from __future__ import annotations
from uniqc import Circuit, submit_task
from uniqc.gateway.config import load_gateway_config
from uniqc.gateway.server import create_app
def main() -> None:
app = create_app()
print("== Gateway ASGI app ==")
print(type(app).__name__, "with", len(list(app.router.routes)), "routes")
print()
print("== Configured host/port ==")
cfg = load_gateway_config()
print("host:", cfg["host"])
print("port:", cfg["port"])
print()
print("== Triggering one dummy task so the UI has something to show ==")
c = Circuit()
c.h(0)
c.measure(0)
task_id = submit_task(c, backend="dummy:local:simulator", shots=64)
print("task_id:", task_id)
print()
print("Launch the UI with:")
print(f" uniqc gateway start --host {cfg['host']} --port {cfg['port']}")
print(f" # then open http://{cfg['host']}:{cfg['port']}/")
if __name__ == "__main__":
main()
Stdout
== Gateway ASGI app ==
FastAPI with 9 routes
== Configured host/port ==
host: 127.0.0.1
port: 18765
== Triggering one dummy task so the UI has something to show ==
task_id: uqt_<task-id>
Launch the UI with:
uniqc gateway start --host 127.0.0.1 --port 18765
# then open http://127.0.0.1:18765/
02 — Independent local quantum lab¶
Source: examples/5_webui/02_local_quantum_lab.py
Status: pass
The CLI and browser use these same local presets and the existing native simulation path. Only exact probabilities and analytic errors are printed; wall time is deliberately omitted from this deterministic example.
Source code
"""02 — Independent local quantum lab
[doc-require: cpp]
[doc-output-include: stdout, source]
The CLI and browser use these same local presets and the existing native
simulation path. Only exact probabilities and analytic errors are printed;
wall time is deliberately omitted from this deterministic example.
"""
from uniqc.gateway.demo import DemoRequest, run_demo
def main():
for case in ("ghz", "w", "ramsey"):
result = run_demo(DemoRequest(case=case, qubits=3, phase=0.73))
assert result["max_probability_error"] < 1e-10
print(case, "probability sum:", f"{result['probability_sum']:.10f}")
print("analytic error < 1e-10:", result["max_probability_error"] < 1e-10)
if __name__ == "__main__":
main()
Stdout
ghz probability sum: 1.0000000000
analytic error < 1e-10: True
w probability sum: 1.0000000000
analytic error < 1e-10: True
ramsey probability sum: 1.0000000000
analytic error < 1e-10: True
持久化层¶
路径 |
内容 |
|---|---|
|
平台 token、proxy、profile、gateway host/port |
|
本地任务历史(SQLite,由 |
|
后端发现缓存 |
|
chip characterization 缓存 |
|
用户自定义含噪量子虚拟机(YAML) |
|
XEB / readout 校准结果(带 ISO-8601 时间戳) |
|
gateway / cli 运行日志(如果启用) |
任务库是 TaskStore 抽象(uniqc.backend_adapter.task.store)的默认 SQLite 实现,
schema 由 uniqc.backend_adapter.database_migration 管理,可以平滑升级到新版本。
迁移在每次 submit_task / query_task 第一次调用时自动触发。
相关页面¶
CLI gateway 命令 ——
start/stop/status与配置项任务管理器 —— 任务库的上层 API
自定义含噪量子虚拟机 ——
~/.uniqc/backend/virtual/的 YAML 格式芯片校准与 QEM ——
~/.uniqc/calibration_cache/的读写方CLI config 命令 ——
~/.uniqc/config.yaml的字段与 profile