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.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
Total release size: 334.7 MB
Release files / unitarylab-1.4.3-cp312-cp312-win_amd64.whl
| Download URL | unitarylab-1.4.3-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 14.0 MB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
5adb29159cfdc23f4b946ae36ff34c62e891f76c92f017b41a8ffeee9b415fb4
|
|
BLAKE2b-256 checksum How to use checksums |
1912564d62aa07c3175e342fb49ecd66a97ff9fe58acbc054e29e66d6c55af73
|
| 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.3-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | unitarylab-1.4.3-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 20.0 MB |
| Tags | CPython 3.12 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
f8e3b6245aeada5dc7a4f0bd388beb19941cdc21ddad0ba01985200a363b374a
|
|
BLAKE2b-256 checksum How to use checksums |
9445a97572c8ae8803d4e3634c7e0df1bfd8cf215d0fe89a2956374f4929b27c
|
| 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.3-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
| Download URL | unitarylab-1.4.3-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 18.6 MB |
| Tags | CPython 3.12 Linux glibc 2.24+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
99829d7102bbc714b32180f8d30d54ae3e54c19774314b0062c7e324592dceb9
|
|
BLAKE2b-256 checksum How to use checksums |
c51ffc662c624ae2b9d145190dbc378347a59ef0343728456b4f6147df994e56
|
| 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.3-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | unitarylab-1.4.3-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 14.5 MB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
c82d367dc8d8177411ef0d40ddd93e36bf114652b3e0b06b662019125d1521fc
|
|
BLAKE2b-256 checksum How to use checksums |
977046ecf3d581eec284992582922588c666aa83bf261401bb1880b9fdda64e3
|
| 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.3-cp312-cp312-macosx_10_13_x86_64.whl
| Download URL | unitarylab-1.4.3-cp312-cp312-macosx_10_13_x86_64.whl |
|---|---|
| Size | 15.9 MB |
| Tags | CPython 3.12 macOS 10.13+ x86-64 |
|
SHA-256 checksum How to use checksums |
53f9aa9ad36dee1e7b8be1b1098afce64fefc45e2f4058a4a2b5acdb8da0a419
|
|
BLAKE2b-256 checksum How to use checksums |
35d482c6f8eb6b3481e2e51c9ca2225d35a7f6d5f0ab7879490f853ed740bd92
|
| 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.3-cp311-cp311-win_amd64.whl
| Download URL | unitarylab-1.4.3-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 14.4 MB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
e9d3f0e56dabd313ac1780b3e605f67ba255021ac8ec6c124a228ce65f5c5241
|
|
BLAKE2b-256 checksum How to use checksums |
eae68f92d3d5fbfa2ae6786d191ac80bbab50b7416dadb87634742c13d6e0136
|
| 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.3-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | unitarylab-1.4.3-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 20.6 MB |
| Tags | CPython 3.11 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
01ac77c341e038b549f9a26875071794ded7e55b723da066324e2b3893ebbf3e
|
|
BLAKE2b-256 checksum How to use checksums |
f8659f972d098464140349c33d949f402d9760fb87e1c10dc2aa8c040e4f4032
|
| 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.3-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
| Download URL | unitarylab-1.4.3-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 19.4 MB |
| Tags | CPython 3.11 Linux glibc 2.24+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
2ff3c54b3d403ee3e935ef480fb40a4287da437373974365a0a90d069689dc72
|
|
BLAKE2b-256 checksum How to use checksums |
61d50db6aebbcd17d9f3ab54f182b81b679fe538bd33b812ec4520748d511437
|
| 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.3-cp311-cp311-macosx_11_0_arm64.whl
| Download URL | unitarylab-1.4.3-cp311-cp311-macosx_11_0_arm64.whl |
|---|---|
| Size | 14.5 MB |
| Tags | CPython 3.11 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
af2a6712f443f73302085bc75ed0dc2b8917b2dbca76efe20e72f7e943a48688
|
|
BLAKE2b-256 checksum How to use checksums |
4373444ffed5aa618367544ea61a6bacfbcd16b63506eef0a80fd3ccef9ccb1b
|
| 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.3-cp311-cp311-macosx_10_9_x86_64.whl
| Download URL | unitarylab-1.4.3-cp311-cp311-macosx_10_9_x86_64.whl |
|---|---|
| Size | 15.7 MB |
| Tags | CPython 3.11 macOS 10.9+ x86-64 |
|
SHA-256 checksum How to use checksums |
75e17d9f6165b82d517815155834a5789f92fb59279116e64010bc9ee6da2f37
|
|
BLAKE2b-256 checksum How to use checksums |
e453e6453c30dd533b02d95c87682f7216bcc5e87e9156e28479f35f70e6d714
|
| 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.3-cp310-cp310-win_amd64.whl
| Download URL | unitarylab-1.4.3-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 14.5 MB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
5c887b707f071ec9e49251ee9a4f56d891a4339fb2cee97002a94e8b3e39c3a6
|
|
BLAKE2b-256 checksum How to use checksums |
6b555d89a088ba3e4090bd8472b421c8327231141bae208ce6538d8efb6e2c3c
|
| 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.3-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | unitarylab-1.4.3-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 20.7 MB |
| Tags | CPython 3.10 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
42aa21bb8c19dd228e712f6ae2843ecae669f901c5d31a088a0dd2e7294fa913
|
|
BLAKE2b-256 checksum How to use checksums |
b8531ce078c4690af78a1a7ec35c2869ea3c2bcebb518d63db339f3d77c4a62c
|
| 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.3-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
| Download URL | unitarylab-1.4.3-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 101.3 MB |
| Tags | CPython 3.10 Linux glibc 2.24+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
6677cee476b278f51ad983fc3e3df021b3bb4f56ffca80ade60077b148c8c42c
|
|
BLAKE2b-256 checksum How to use checksums |
ff9638085602a4cc7c2ab5bbf2d1ac7e3c252ef59ee118264bd95de92a2440d2
|
| 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.3-cp310-cp310-macosx_11_0_arm64.whl
| Download URL | unitarylab-1.4.3-cp310-cp310-macosx_11_0_arm64.whl |
|---|---|
| Size | 14.8 MB |
| Tags | CPython 3.10 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
0deadfb830adb2760fa1b56523fa55ee3ab4ea99bf4a8d4645a06658d8a546ba
|
|
BLAKE2b-256 checksum How to use checksums |
bea5700564a29756947b1f70f8f0024e6bf1beb2bb383be3497d4e22323ea23e
|
| 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.3-cp310-cp310-macosx_10_9_x86_64.whl
| Download URL | unitarylab-1.4.3-cp310-cp310-macosx_10_9_x86_64.whl |
|---|---|
| Size | 15.9 MB |
| Tags | CPython 3.10 macOS 10.9+ x86-64 |
|
SHA-256 checksum How to use checksums |
c7c0756cc0859d918a722b8499ed66918c56d3416d5270b9b3fd1c1fa8db29f1
|
|
BLAKE2b-256 checksum How to use checksums |
6528fb8394f11c10dbaec9688401c66850b22b93e6695b8e20c66c5f77bc03b8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|