unitarylab
A Python toolkit for building, simulating, analyzing, and compiling quantum circuits.
面向量子线路构建、模拟、分析与编译的 Python 工具包。
English
UnitaryLab provides a high-level Circuit API for quantum-circuit construction,
simulation, analysis, visualization, interoperability, and hardware compilation.
It supports dense statevectors, matrix-product states, and density matrices.
Website · Simulator User Manual · 中文
Installation
UnitaryLab requires Python 3.10–3.12.
pip install unitarylab
Optional components can be installed as needed:
# CUDA 12 C++ backend
pip install "unitarylab[cuda]"
# Quantum chemistry workflows
pip install "unitarylab[chemistry]"
The main package supports Windows, Linux, and macOS. The CUDA extra supports Linux x86_64, Linux aarch64 server environments, and Windows x86-64, and requires a compatible NVIDIA driver. The chemistry extension currently supports Linux and macOS.
Bell-state quick start
from unitarylab import Circuit
circuit = Circuit(2)
circuit.h(0)
circuit.cx(0, 1)
result = circuit.execute()
print(result.probabilities)
The probability mapping contains every computational basis state:
{"00": 0.5, "10": 0.0, "01": 0.0, "11": 0.5}
Displayed bit strings use little-endian qubit order: qubit 0 is the rightmost bit.
Core capabilities
| Area | Capability |
|---|---|
| Circuit construction | Registers, standard gates, controlled gates, measurements, and reusable circuit blocks |
| Statevector execution | PyTorch, NumPy, native C++ CPU/CUDA backends |
| Tensor Network execution | Matrix-Product-State-based TensorNet backends |
| Mixed-state execution | Density-matrix simulation and built-in noise channels |
| Results | State, probabilities, marginal queries, sampling, measurement, counts, and expectation values |
| Circuit tools | Drawing, analysis, transforms, optimization, and transpilation |
| Interoperability | OpenQASM 2/3 import and export, plus unitarylab native Python source generation |
| Hardware | Local compilation and provider-neutral hardware submission |
| Algorithms | Quantum transforms, simulation, linear solvers, equations, and Schrodingerization |
Common workflow
Create a circuit, add measurements when classical results are needed, and choose the number of independent execution shots:
from unitarylab import Circuit, Register, ClassicalRegister
q = Register("q", 2)
c = ClassicalRegister("c", 2)
circuit = Circuit(q, c)
circuit.h(q[0])
circuit.cx(q[0], q[1])
circuit.measure(q[0:2], c[0:2])
result = circuit.execute(shots=1000, seed=42)
print(result.counts)
print(result.classical_results_map)
print(result.classical_registers)
countsaggregates classical bit strings from all shots.classical_results_mapcontains classical-bit values from the final shot.classical_registersgroups final-shot values by register name.stateis the dense final-shot state;backend_stateexposes the native backend representation.
For state inspection without circuit measurements, execution results also provide targeted probability queries, marginal distributions, non-collapsing sampling, projective measurement, and normalized observable expectation values.
result = Circuit(1).execute()
print(result.probability("0"))
print(result.sample(shots=8, seed=7))
print(result.expectation("Z"))
Pauli-string characters correspond to the supplied qubits in order. This operator order is separate from displayed probability strings, where qubit 0 appears at the right-hand end.
Backend compatibility
| Backend | Device | Notes |
|---|---|---|
torch |
cpu, gpu |
Default backend with broad compatibility; GPU uses a compatible CUDA or Apple MPS environment |
numpy |
cpu |
Dense NumPy statevector and a convenient NumPy-native result |
cpp |
cpu |
Performance-oriented native C++ execution; generally preferred when speed is the priority |
cpp |
gpu |
Performance-oriented CUDA C++ execution; requires unitarylab[cuda] |
tensornet |
cpu |
Tensor network execution for low-entanglement circuits |
execute_density() |
cpu, gpu/cuda |
CUDA devices require unitarylab[cuda] |
Choosing a backend
- Start with the default
torchCPU backend when broad compatibility and a ready-to-use environment matter most. - Prefer
cppwithdevice="cpu"when execution speed is the priority and the native extension is available. - Prefer
cppwithdevice="gpu"for the highest-performance supported CUDA path; installunitarylab[cuda]and use a compatible NVIDIA driver. - Use
numpywhen a NumPy-native dense state or minimal backend integration is more important. - Use
tensornetfor larger, low-entanglement circuits where an MPS representation can avoid a full dense statevector. - Use
execute_density()for mixed states and noise models. Density-matrix execution does not currently provide circuit measurement or shots/counts.
Backend selection is explicit when needed:
result = circuit.execute(backend="numpy", device="cpu")
result = circuit.execute(backend="tensornet", device="cpu")
density_result = circuit.execute_density(device="cpu")
Circuit analysis and quantum computer task submission
The high-level API includes circuit drawing and analysis, structural circuit operations, local optimization and transpilation, OpenQASM 2/3 import and export, compilation, and provider-neutral hardware submission.
circuit.draw()
info = circuit.analyze(show=False)
optimized = circuit.optimize(optimization_level=1)
transpiled = circuit.transpile(basis="default")
qasm = circuit.to_qasm()
Real-hardware submission requires provider API token and may require a machine identifier or vendor SDK:
TOKEN = "your-provider-token"
hardware_result = circuit.submit(
provider="guodun",
machine="gd_qc1", # Guodun "Xiaohong-1"
token=TOKEN,
shots=1024,
)
Confirm the provider, target machine, credentials, and possible quota or charges before submitting a real hardware task. See the online manual for provider-specific requirements.
For complete API signatures, backend behavior, noise semantics, hardware provider requirements, and algorithm examples, see the Simulator User Manual.
Documentation
- Quick start
- API overview
- Core circuit interface
- Execution and tool workflow
- Algorithms and utilities
中文
UnitaryLab 通过高层 Circuit API 提供量子线路构建、模拟、分析、绘图、
互操作和硬件编译能力,支持稠密状态向量、矩阵乘积态与密度矩阵模拟。
安装
UnitaryLab 支持 Python 3.10–3.12。
pip install unitarylab
可按需安装可选组件:
# CUDA 12 C++ 后端
pip install "unitarylab[cuda]"
# 量子化学工作流
pip install "unitarylab[chemistry]"
主包支持 Windows、Linux 和 macOS。CUDA extra 支持 Linux x86_64、Linux aarch64 服务器环境和 Windows x86-64,并要求兼容的 NVIDIA 驱动。量子化学 扩展目前支持 Linux 和 macOS。
Bell 态快速示例
from unitarylab import Circuit
circuit = Circuit(2)
circuit.h(0)
circuit.cx(0, 1)
result = circuit.execute()
print(result.probabilities)
概率映射包含全部计算基态:
{"00": 0.5, "10": 0.0, "01": 0.0, "11": 0.5}
显示的比特字符串采用 little-endian 量子比特顺序,qubit 0 位于最右侧。
核心能力
| 方向 | 能力 |
|---|---|
| 线路构建 | 寄存器、标准门、受控门、测量与可复用线路块 |
| 状态向量执行 | PyTorch、NumPy、原生 C++ CPU/CUDA 后端 |
| 张量网络执行 | 基于矩阵乘积态的 TensorNet 后端 |
| 混态执行 | 密度矩阵模拟与内置噪声通道 |
| 执行结果 | 状态、概率、边缘查询、采样、测量、计数与期望值 |
| 线路工具 | 绘图、分析、线路变换、优化与转译 |
| 互操作 | OpenQASM 2/3 导入导出与原生 unitarylab Python 源码生成 |
| 量子硬件 | 本地编译与统一的真机提交接口 |
| 算法 | 量子变换、模拟、线性求解、方程与薛定谔化 |
常用工作流
创建线路;需要经典结果时添加测量,并通过 shots 指定独立执行次数:
from unitarylab import Circuit, Register, ClassicalRegister
q = Register("q", 2)
c = ClassicalRegister("c", 2)
circuit = Circuit(q, c)
circuit.h(q[0])
circuit.cx(q[0], q[1])
circuit.measure(q[0:2], c[0:2])
result = circuit.execute(shots=1000, seed=42)
print(result.counts)
print(result.classical_results_map)
print(result.classical_registers)
counts汇总全部 shots 的经典比特串。classical_results_map保存最后一个 shot 的经典位值。classical_registers按寄存器名称组织最后一个 shot 的结果。state是最后一个 shot 的稠密状态,backend_state则保留后端原生表示。
未在线路中添加测量时,执行结果还支持单项概率、边缘分布、不坍缩采样、 投影测量和归一化 Observable 期望值查询。
result = Circuit(1).execute()
print(result.probability("0"))
print(result.sample(shots=8, seed=7))
print(result.expectation("Z"))
Pauli 字符串中的字符按传入的 qubits 顺序逐一对应量子比特。该算符顺序与 概率字符串的显示顺序相互独立;概率字符串中 qubit 0 位于最右侧。
后端兼容表
| 后端 | device | 说明 |
|---|---|---|
torch |
cpu、gpu |
兼容性广的默认后端;GPU 使用兼容的 CUDA 或 Apple MPS 环境 |
numpy |
cpu |
稠密 NumPy 状态向量,便于直接获得 NumPy 原生结果 |
cpp |
cpu |
面向性能的原生 C++ 执行 |
cpp |
gpu |
面向高性能的 CUDA C++ 执行;需要 unitarylab[cuda] |
tensornet |
cpu |
面向低纠缠线路的张量网络后端执行 |
execute_density() |
cpu、gpu/cuda |
CUDA device 需要 unitarylab[cuda] |
后端选择建议
- 更看重兼容性和开箱即用体验时,从默认的
torchCPU 后端开始。 - 性能优先且原生扩展可用时,优先选择
cpp+device="cpu"。 - 具备 NVIDIA 环境时,优先选择
cpp+device="gpu"获得更高 性能;使用前需安装unitarylab[cuda]并配置兼容的 NVIDIA 驱动。 - 更看重 NumPy 原生稠密状态或最小化后端集成时选择
numpy。 - 面向较大规模、低纠缠线路时,可使用
tensornet的 MPS 表示避免构造完整 稠密状态向量。 - 模拟混态和噪声模型时使用
execute_density();密度矩阵执行目前不提供 线路测量或 shots/counts。
需要时可显式选择后端:
result = circuit.execute(backend="numpy", device="cpu")
result = circuit.execute(backend="tensornet", device="cpu")
density_result = circuit.execute_density(device="cpu")
线路转义分析及量子真机任务提交
高层接口提供线路绘图与分析、线路结构变换、本地优化与转译、OpenQASM 2/3 导入导出、编译,以及统一的真机提交能力。
circuit.draw()
info = circuit.analyze(show=False)
optimized = circuit.optimize(optimization_level=1)
transpiled = circuit.transpile(basis="default")
qasm = circuit.to_qasm()
真机提交需要真机供应商 API token,并可能要求机器标识或厂商 SDK:
TOKEN = "your-provider-token"
hardware_result = circuit.submit(
provider="guodun",
machine="gd_qc1", # 国盾量子“骁鸿一号”
token=TOKEN,
shots=1024,
)
提交真实硬件任务前,请确认 provider、目标机器、token以及可能产生的额度或费用, 并在在线用户手册中查看各 provider 的具体要求。
完整 API 签名、后端行为、噪声语义、硬件 provider 要求和算法示例请查看 模拟器用户手册。
文档导航
License
License: LicenseRef-UnitaryLab-LICENSE. The Chinese license text is authoritative; the English version is provided for reference only.
Metadata
Release files for unitarylab 1.4.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
Total release size: 259.1 MB
Release files / unitarylab-1.4.4-cp312-cp312-win_amd64.whl
| Download URL | unitarylab-1.4.4-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 14.3 MB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
233c031bbda14997880e9895186ce05e195e32ed2845834d00714df9277ed269
|
|
BLAKE2b-256 checksum How to use checksums |
71ea1ede33bea2768caa338508f66c1836c9786c26139b7b0c6cea62f58e4da3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Release files / unitarylab-1.4.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | unitarylab-1.4.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 20.4 MB |
| Tags | CPython 3.12 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
e3b404aa25700676a1d3ceba7e6e08c31e0cce5bdf2cf9af1c8c83f3ffcda3d2
|
|
BLAKE2b-256 checksum How to use checksums |
b20ea893aeb96689505b900823c132483e690bf9c4c7a8fe0eec60ba9f10e33f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Release files / unitarylab-1.4.4-cp312-cp312-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
| Download URL | unitarylab-1.4.4-cp312-cp312-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 19.0 MB |
| Tags | CPython 3.12 Linux glibc 2.26+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
3fecb50abd2fd1bf5ceb311dffe1aa5d98e7de94a6483861e69dea53d24434eb
|
|
BLAKE2b-256 checksum How to use checksums |
34512435e7731f6ac75bc2055656b3daff8147463b9516fbe53e69dd96185bde
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Release files / unitarylab-1.4.4-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | unitarylab-1.4.4-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 14.8 MB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
45a392dac470b7cc7f70a9a2d671fa9850a55b0a2c7c8c86c309825fdd279c60
|
|
BLAKE2b-256 checksum How to use checksums |
e3d7a6b36a27c9d40d5e6449fa826c959998d9ce9fa140e7baa9625fe41ecae1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Release files / unitarylab-1.4.4-cp312-cp312-macosx_10_13_x86_64.whl
| Download URL | unitarylab-1.4.4-cp312-cp312-macosx_10_13_x86_64.whl |
|---|---|
| Size | 16.3 MB |
| Tags | CPython 3.12 macOS 10.13+ x86-64 |
|
SHA-256 checksum How to use checksums |
8064dbcaea1e1a06f02266748d21ebab873722a48c0f348b1ba9ccb7fb682372
|
|
BLAKE2b-256 checksum How to use checksums |
fffa675052b168d992d56ad0ebd6ea4f2ab238e9450eb6424da096af48c11280
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Release files / unitarylab-1.4.4-cp311-cp311-win_amd64.whl
| Download URL | unitarylab-1.4.4-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 14.7 MB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
626cfed23440cd0b1f0c6413edd202e8a22960ce12c8eccdbbec4cb1320217d1
|
|
BLAKE2b-256 checksum How to use checksums |
341144b13f8e2050dade64c2709ba2b32f969edf150ac5b81e5684b4483d1cb1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Release files / unitarylab-1.4.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | unitarylab-1.4.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 21.1 MB |
| Tags | CPython 3.11 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
3e8ecb6cec46c94849f9fbe80228990ccf1a8bf37db5e6464ab1b5682d60d898
|
|
BLAKE2b-256 checksum How to use checksums |
861dbbd69e4e74ff0fe14ea386808f15d2a6bf77b22045a087502c5b937ef7e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Release files / unitarylab-1.4.4-cp311-cp311-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
| Download URL | unitarylab-1.4.4-cp311-cp311-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 19.8 MB |
| Tags | CPython 3.11 Linux glibc 2.26+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
5ee7ea79f631994ce2df4d30c47002707fd604fe1510a429c33f52e41ff9ff61
|
|
BLAKE2b-256 checksum How to use checksums |
b2f9b9d3babc6e22c559af466cf2476ecc1ae2704bc35a3df62dc3599cf4ff42
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Release files / unitarylab-1.4.4-cp311-cp311-macosx_11_0_arm64.whl
| Download URL | unitarylab-1.4.4-cp311-cp311-macosx_11_0_arm64.whl |
|---|---|
| Size | 14.9 MB |
| Tags | CPython 3.11 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
d4f245a6463596aed190fd915a07fe0bf236c8f90007eb1a135f25ab35f69164
|
|
BLAKE2b-256 checksum How to use checksums |
aedb1ac8f4db45b8addf0eccd596a7e406625c481eabb348d6f6894048db33d2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Release files / unitarylab-1.4.4-cp311-cp311-macosx_10_9_x86_64.whl
| Download URL | unitarylab-1.4.4-cp311-cp311-macosx_10_9_x86_64.whl |
|---|---|
| Size | 16.1 MB |
| Tags | CPython 3.11 macOS 10.9+ x86-64 |
|
SHA-256 checksum How to use checksums |
fa6d54743cb8ff7c30aee273e0a336a7e37de0645b8e1e0e237734aa9ff8d009
|
|
BLAKE2b-256 checksum How to use checksums |
9ea00887cd920a61abdbf0446411265529805024ca5adfdee7bbbf4c300af1a2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Release files / unitarylab-1.4.4-cp310-cp310-win_amd64.whl
| Download URL | unitarylab-1.4.4-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 14.8 MB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
af566d1ada50f3867d284f7cb394c5c6c8f60d7bf791eb01e96f40c6e67d9d30
|
|
BLAKE2b-256 checksum How to use checksums |
2706225fe58f6250cd81df1896148b8fd864c509e2d10c459069c43357a561e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Release files / unitarylab-1.4.4-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | unitarylab-1.4.4-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 21.2 MB |
| Tags | CPython 3.10 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
a91880dbe23926f6975755b53582f656677d2cfc173b2e55bcd58a3afcd5ba53
|
|
BLAKE2b-256 checksum How to use checksums |
cef8512236c62d28d7bd33768e080e961afbb68af816c65847a76a5fd1e3797e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Release files / unitarylab-1.4.4-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
| Download URL | unitarylab-1.4.4-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 20.0 MB |
| Tags | CPython 3.10 Linux glibc 2.26+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
99bae2ede085bb45db4a2b28439751a520f1e7b06c9b05185f8f135116482786
|
|
BLAKE2b-256 checksum How to use checksums |
1c5c15e2d17f493de420ea9d9c2be100e1af2f3a0a4cb776dbfe78dc1e065739
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Release files / unitarylab-1.4.4-cp310-cp310-macosx_11_0_arm64.whl
| Download URL | unitarylab-1.4.4-cp310-cp310-macosx_11_0_arm64.whl |
|---|---|
| Size | 15.2 MB |
| Tags | CPython 3.10 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
74fa75977487118d6198429c6ba7dada9d27cf342a025238a47216cbe5b08ba3
|
|
BLAKE2b-256 checksum How to use checksums |
e6d0b0d3a1d3533c493f385eddb4401769a284091acaa530f5b52b38ec4bf2fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Release files / unitarylab-1.4.4-cp310-cp310-macosx_10_9_x86_64.whl
| Download URL | unitarylab-1.4.4-cp310-cp310-macosx_10_9_x86_64.whl |
|---|---|
| Size | 16.3 MB |
| Tags | CPython 3.10 macOS 10.9+ x86-64 |
|
SHA-256 checksum How to use checksums |
6adbcbf39c50cb5fa05ac5707a0aba95e371c9912476233366a3cbcaad527058
|
|
BLAKE2b-256 checksum How to use checksums |
660b29dfafd3f46a02e2f3a4f5f3d9ac0ca4d0661be4880f308affeaed629db6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|