同一份電路描述檔(.qc)驅動三個彼此獨立的態向量模擬器:C++17 核心、純 Python 套件、
以及可直接在瀏覽器裡執行的 JavaScript 引擎。三者的振幅與機率必須一致到 1e-12 以內,
一致性由 scripts/check_parity.py 實際比對產生,不是靠人工核對。
線上示範:https://tewei02.github.io/qbridge/
頁面載入預先產生的參考結果 docs/reference.json,在瀏覽器內用 JavaScript 引擎重算同一批
電路,並當場顯示兩邊的偏差;離線後仍可透過 Service Worker 開啟。
已完成並通過測試的部分:
- C++17 態向量核心:複數振幅、單量子位閘、受控閘、
ccx、swap、旋轉閘 - Python 套件:
pyproject.toml可直接建置 sdist / wheel,附qbridge命令列工具; 範例電路隨套件一起安裝(share/qbridge/circuits/),pip 安裝後qbridge --list立即有內容 - 瀏覽器端 JavaScript 引擎:無 CDN、無建置步驟、可離線
- 七個示範電路與跨引擎一致性檢查
- CMake + CTest(C++)、pytest(Python)、ruff(風格)、GitHub Actions CI
尚未實作,屬於規劃中的方向(請不要當成現有能力):
- 雜訊模型與密度矩陣模擬(目前只有理想態向量)
- 真實量子硬體後端;本專案所有數值都是理想模擬結果,沒有跑過任何量子硬體
- GPU 加速、多執行緒
- 電路深度最佳化、參數化電路的變分流程、測量取樣統計(目前輸出的是精確機率)
# Bell pair
qubits 2
h 0
cx 0 1 // entangle
支援的閘:i id h x y z s sdg t tdg p、rx ry rz、cx cz cp、ccx、swap。
角度可寫成數字或圓週率倍數(pi/2、-pi/4);# 與 // 之後的內容視為註解。
C++(需要 CMake 3.16+ 與支援 C++17 的編譯器):
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --parallel
ctest --test-dir build --output-on-failure
./build/qbridge_cli --list
./build/qbridge_cli --circuit bell --json
Python(3.9+,無執行期相依套件):
裝成套件(會一併帶上七個示範電路,裝完在任何目錄都能直接用):
pip install "qbridge @ git+https://github.com/TeWei02/qbridge.git" # 直接從 repo 安裝
pip install . # 或是在 clone 出來的目錄裡
pip install qbridge # PyPI 發佈後可用,見「發佈」
qbridge --list
qbridge --circuit ghz3
qbridge --circuit bell --json
不安裝、直接跑原始碼:
PYTHONPATH=python python3 -m qbridge.cli --list
PYTHONPATH=python python3 -m qbridge.cli --circuit ghz3
python3 -m pytest -q
JavaScript(macOS 內建 JavaScriptCore 即可,不需要 node):
/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Helpers/jsc docs/qbridge.js
在本機開啟示範頁:
python3 -m http.server --directory docs 8000 # http://localhost:8000
python3 scripts/gen_reference.py # 用 Python 引擎重算 docs/reference.json
python3 scripts/check_parity.py # 比對 C++ / Python / JavaScript 三方結果
目前涵蓋的七個電路(量子位/閘數/非零振幅數)以及三方最大振幅偏差 2.776e-17
(容忍度 1e-12):
| 電路 | 量子位 | 閘數 | 非零振幅 |
|---|---|---|---|
bell |
2 | 2 | 2 |
ghz3 |
3 | 3 | 2 |
deutsch_jozsa_balanced |
4 | 9 | 2 |
deutsch_jozsa_constant |
4 | 8 | 2 |
grover2 |
2 | 12 | 1 |
qft3 |
3 | 8 | 8 |
rotations3 |
3 | 13 | 8 |
circuits/ 七個 .qc 電路,三個引擎共用的輸入
include/qbridge/ C++ 標頭(statevector.hpp、circuit.hpp)
src/main.cpp C++ 命令列工具
python/qbridge/ Python 引擎、解析器與 CLI
docs/ 瀏覽器示範(index.html、app.js、qbridge.js、reference.json、PWA 資產)
scripts/ 參考檔產生、跨引擎比對、圖示產生
tests/ C++ 與 Python 測試
python3 -m build # 產生 dist/qbridge-<version>.tar.gz 與 .whl
打上與 pyproject.toml 版本一致的標籤(例如 v1.0.0)並推送,
.github/workflows/release.yml 會自動建置、驗證 wheel 內含示範電路、實測 CLI,
再透過 PyPI Trusted Publishing 發佈。第一次發佈前要先到 PyPI 帳號設定新增一個
trusted publisher,指向這個 repo 的 release.yml(環境名稱 pypi),之後就不需要
任何 API token。
MIT,作者 Te-Wei Ko。