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"
job = circuit.submit(
provider="guodun",
machine="gd_qc1", # Guodun "Xiaohong-1"
token=TOKEN,
shots=1024,
)
hardware_result = job.result() # Query once; call again later if unfinished.
An unfinished result has empty distributions and a message. Submission does
not wait for execution, and each job.result() call queries once.
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"
job = circuit.submit(
provider="guodun",
machine="gd_qc1", # 国盾量子“骁鸿一号”
token=TOKEN,
shots=1024,
)
hardware_result = job.result() # 单次查询,未完成时可稍后再次调用。
未完成的结果包含空分布和提示信息。提交不等待任务执行完成,每次
job.result() 只查询一次。
提交真实硬件任务前,请确认 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.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
Total release size: 233.0 MB
Release files / unitarylab-1.5.2-cp312-cp312-win_amd64.whl
| Download URL | unitarylab-1.5.2-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 15.8 MB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
85c051f42069fb25c09979462a045f6ffa6f95d2817717931ec6c2c28f43f4cb
|
|
BLAKE2b-256 checksum How to use checksums |
d951b750bc86da20908a452ea95c848276e8830f3be75a586e65226c0f12fc1a
|
| 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.2-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | unitarylab-1.5.2-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 22.7 MB |
| Tags | CPython 3.12 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
4c9328eaac791505f1e2f20d8900b8c9b75c13505227f0caa359eaafec6f5001
|
|
BLAKE2b-256 checksum How to use checksums |
82cbd2f50bd15b71fa6167315b0d9a84d19f32235c8208b28e6a67c3f1e918bf
|
| 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.2-cp312-cp312-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
| Download URL | unitarylab-1.5.2-cp312-cp312-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 21.0 MB |
| Tags | CPython 3.12 Linux glibc 2.26+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
6f21f5542de2ce8c7de3489dba7173e40589836d1cb9d16770b0dea1a708936a
|
|
BLAKE2b-256 checksum How to use checksums |
d19b34fc16015257935132bcb7d8312c7a2130df2573766244de1becdc0458f1
|
| 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.2-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | unitarylab-1.5.2-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 16.4 MB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
38cda36af615583df6884687d94ac75fc997ab54893965e10cf68180d5988086
|
|
BLAKE2b-256 checksum How to use checksums |
682ca053404deef4337a680577207e1bf5d88e7f827bc542d086718fad017994
|
| 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.2-cp311-cp311-win_amd64.whl
| Download URL | unitarylab-1.5.2-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 16.3 MB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
dac29f60d58b714253d6d1e2d911901c07b28cc156c95939aeea71542e05faa4
|
|
BLAKE2b-256 checksum How to use checksums |
53d19d48fa76ac803dd3e9eb168ba53a7a1b77abcef77a7e339babd2498c6597
|
| 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.2-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | unitarylab-1.5.2-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 23.4 MB |
| Tags | CPython 3.11 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
bd3f674a2b901d3d66ecc6a12b81e1f91b224487f1a9da907edcb674577faf6e
|
|
BLAKE2b-256 checksum How to use checksums |
466b63fce621b6c150b7f6a520d801992c56d530cbaa1e8170a544c404bba4c7
|
| 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.2-cp311-cp311-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
| Download URL | unitarylab-1.5.2-cp311-cp311-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 22.0 MB |
| Tags | CPython 3.11 Linux glibc 2.26+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
19a0014482c84a92b304a9ec213a922cb07a6fa99cc2d3528c030e20ed13b797
|
|
BLAKE2b-256 checksum How to use checksums |
96b7e0fcb3f1cee73c4dc64aca079c55f7b001df9ef6d60b8d8c1957be5a8c3a
|
| 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.2-cp311-cp311-macosx_11_0_arm64.whl
| Download URL | unitarylab-1.5.2-cp311-cp311-macosx_11_0_arm64.whl |
|---|---|
| Size | 16.4 MB |
| Tags | CPython 3.11 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
b0dd16a2cb80b144129bbd5c96d3a336e067c347acb8e92d1cebc75c6f2a70df
|
|
BLAKE2b-256 checksum How to use checksums |
f85f65d1c6d3190926df3520a4644c0d1ce8f84e9a4806eac9d2cff783284126
|
| 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.2-cp310-cp310-win_amd64.whl
| Download URL | unitarylab-1.5.2-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 16.4 MB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
d8f183852e0e79ca20cf38bdd32e95289987d7443c1902512466feb0a76b3e8f
|
|
BLAKE2b-256 checksum How to use checksums |
e6a4e35a50ecf7380adf22b594026d032eec7ab77aeebd1af6e6a6df8adcdfac
|
| 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.2-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | unitarylab-1.5.2-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 23.6 MB |
| Tags | CPython 3.10 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
2e238a7efeb792a57dd0058266e94f8b20744be6fdadda5149156ab7e4e1cd93
|
|
BLAKE2b-256 checksum How to use checksums |
70d09fc0ce5e82aec0fd2a7cd3eeaa7e57caa75bdd675582b61ecb70d190dda6
|
| 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.2-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
| Download URL | unitarylab-1.5.2-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 22.2 MB |
| Tags | CPython 3.10 Linux glibc 2.26+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
e95cd34065cb9323bd3d0b14f45ce5ac81595a56852401c1ca03ad2772da81ec
|
|
BLAKE2b-256 checksum How to use checksums |
d6a17f58b29f8cc6ca98ed8dedae7fd65b49eb9b7d8b7d3be2145a09b0001b84
|
| 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.2-cp310-cp310-macosx_11_0_arm64.whl
| Download URL | unitarylab-1.5.2-cp310-cp310-macosx_11_0_arm64.whl |
|---|---|
| Size | 16.7 MB |
| Tags | CPython 3.10 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
611cf04e8feda823eca86c2d242ef60d7d6a039f3fb2d0636afa6a778b8402da
|
|
BLAKE2b-256 checksum How to use checksums |
9513f37ed356323b5adc460adcfd38a81fe47e176cc96d47efbf7d76d82fa101
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Security for unitarylab 1.5.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.