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 and CUDA-Q 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 measurement samples with 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] |
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")
Density-matrix execution supports circuit measurements and multi-shot counts.
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()
cudaq_source = circuit.to_python(framework="cudaq")
Generating CUDA-Q source does not require CUDA-Q to be installed. Running that
source or calling circuit.to_cudaq() requires a separate cudaq installation.
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 和 CUDA-Q 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] |
需要时可显式选择后端:
result = circuit.execute(backend="numpy", device="cpu")
result = circuit.execute(backend="tensornet", device="cpu")
density_result = circuit.execute_density(device="cpu")
密度矩阵执行支持线路测量和多 shots 的 counts 统计。
线路转义分析及量子真机任务提交
高层接口提供线路绘图与分析、线路结构变换、本地优化与转译、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()
cudaq_source = circuit.to_python(framework="cudaq")
生成 CUDA-Q 源码无需安装 CUDA-Q;运行生成的源码或调用 circuit.to_cudaq()
则需要单独安装 cudaq。
真机提交需要真机供应商 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.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
Total release size: 211.0 MB
Release files / unitarylab-1.5.0-cp312-cp312-win_amd64.whl
| Download URL | unitarylab-1.5.0-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 14.4 MB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
9303ace57bcd9948e2ff43c0459279ee3cbcfa2ef8077bab40b5bdc8784cf214
|
|
BLAKE2b-256 checksum How to use checksums |
826594d4ea73099667ca465ee5ce9e9ecd4daf6c5fb3bc2900822f0606d483b6
|
| 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.5.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | unitarylab-1.5.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 20.5 MB |
| Tags | CPython 3.12 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
c515bb6a64958472da23f14b0a519097130362ba96efc076a40f089bc40cea76
|
|
BLAKE2b-256 checksum How to use checksums |
8ad593dfcc203e910685492ef97afa16c2f742870e2c193c88fc64d3b593cd2c
|
| 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.5.0-cp312-cp312-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
| Download URL | unitarylab-1.5.0-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 |
50d014b0dfbcb5acff6763932104d06273d0b26c97400da163f45b428282ca3d
|
|
BLAKE2b-256 checksum How to use checksums |
8159e3c97b63010d8f7d68b789f39ff47cb0e23032bbdef90acbdd618f0b5ed1
|
| 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.5.0-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | unitarylab-1.5.0-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 14.9 MB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
5946fd1499068f81ccf0639989564b697fca5e3f5e053f7a86d912aadacc528a
|
|
BLAKE2b-256 checksum How to use checksums |
bbd9d9b1c764a45120064af6d482b507ac78e906d59d14143b17d8c325fd4e72
|
| 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.5.0-cp311-cp311-win_amd64.whl
| Download URL | unitarylab-1.5.0-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 14.8 MB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
ad14db677df9ae94e947263aa68c345091a877a8ef03f5f7ca5e15993fbc8c95
|
|
BLAKE2b-256 checksum How to use checksums |
07ee70a1cbb3ae6b5c7766b393402db019a97747e8ef043f2cca9edf2dac8d4e
|
| 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.5.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | unitarylab-1.5.0-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 |
eff4f40df3cdbacb3fdfb3e1b71bbfb4371e3a89758b76571944fadda4b06bca
|
|
BLAKE2b-256 checksum How to use checksums |
28642e69d5a287ab7c3dd640b8928bb8da2df55e62d9130493b39575a7820d75
|
| 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.5.0-cp311-cp311-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
| Download URL | unitarylab-1.5.0-cp311-cp311-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 19.9 MB |
| Tags | CPython 3.11 Linux glibc 2.26+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
ea40639e32f8f65819c3bc2c7c56d3c66788e8d540ced59356983369e600b7e8
|
|
BLAKE2b-256 checksum How to use checksums |
0b224017cd5cdbfdcb08ed88ff415079b444088d3bb51723467fbdc466e1eb32
|
| 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.5.0-cp311-cp311-macosx_11_0_arm64.whl
| Download URL | unitarylab-1.5.0-cp311-cp311-macosx_11_0_arm64.whl |
|---|---|
| Size | 15.0 MB |
| Tags | CPython 3.11 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
aed47eabebcc24fd290c796a5eb19f1766085a06b79f86731a293c7b0a376a5d
|
|
BLAKE2b-256 checksum How to use checksums |
ac8cfdb8fa0865cc57c95e8604d72ba0b71dc53fdf0bab2c4790baa9e86bb80e
|
| 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.5.0-cp310-cp310-win_amd64.whl
| Download URL | unitarylab-1.5.0-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 14.9 MB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
3f507995b99fe28eaed7ec3c3322db9b999eb4470fbea491120bf2dc06557c9c
|
|
BLAKE2b-256 checksum How to use checksums |
69fb190508cc9151830506e69e7bbc0d8aa83fd52e33925f8c9c1263c8c79a55
|
| 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.5.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | unitarylab-1.5.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 21.3 MB |
| Tags | CPython 3.10 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
fb4bec7fb701c2a8ab14c27a6f14a625bd2cc3dea522f8b697ec4e0d40688df4
|
|
BLAKE2b-256 checksum How to use checksums |
1c01d3dca8dc4726b6dff4809a97a4d2f0c60e4f5df497db435de5d8cfb04129
|
| 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.5.0-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
| Download URL | unitarylab-1.5.0-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 20.1 MB |
| Tags | CPython 3.10 Linux glibc 2.26+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
61baeae2125b8338df76a3c1ab3e04f77083e8a11a258453965bcd59b74df324
|
|
BLAKE2b-256 checksum How to use checksums |
e0cca93b921a3477158ba1f98fd6ecc92f08f5d540ee32535568e2299381f80a
|
| 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.5.0-cp310-cp310-macosx_11_0_arm64.whl
| Download URL | unitarylab-1.5.0-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 |
fa9cd20995dc32b5a1027a790c1a97dda0443fadf6ad3f436c558c6fd9ec55bd
|
|
BLAKE2b-256 checksum How to use checksums |
9346530ec343475b2dcc5c7059a0f4b8b5ea7c489fda5851e19c8757b7c857bc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|