Skip to main content

HyQ: hybrid quantum algorithms toolkit with backend-agnostic circuits and solvers

Project description

HyQ

HyQpip install hyq)是用于混合量子算法的 Python 工具包:后端无关的线路抽象、可插拔模拟器,以及 VQE / QITE 系列求解器与常用算法工具。

  • Python:3.10、3.11(见 requires-python
  • 核心依赖(随 pip install hyq 自动安装):NumPy、SciPy、PyTorch、Qiskitqiskit-aerpylatexenc(2.10,示例/线路 LaTeX 相关)
  • 其他后端与化学功能:通过可选 extra 安装(见下表)

安装

pip install hyq

默认可用 Qiskit 后端(线路构建、采样、QASM 等)。若需要可微分或其它模拟器,可加装 extras:

pip install "hyq[tensorcircuit]"              # VQE / VarQITE 等可微分算法
pip install "hyq[tensorcircuit,pennylane]"    # 常用组合
pip install "hyq[all]"                        # 全部可选 Python 依赖(体积较大)

可选依赖(extras)

Extra 用途
pennylane PennyLane 后端
tensorcircuit TensorCircuit 后端(推荐可微分流程)
optional-backends Cirq、Qulacs、QuTiP
chemistry OpenFermion、PySCF 等(分子哈密顿量)
notebooks 运行 notebook 的辅助包
dev pytest、black 等开发工具
all 上述除 dev 外的组合

pip install hyqpip install "hyq[chemistry]" 的区别

安装命令 自动安装的内容 能否用 hyq.chemistry
pip install hyq NumPy、SciPy、PyTorch、Qiskit、qiskit-aer、pylatexenc 不能(缺 OpenFermion / PySCF)
pip install "hyq[chemistry]" 上述 加上 openfermion、openfermionpyscf、openfermionpsi4、pyscf 可以(默认走 PySCF 算分子积分)

只做 VQE、QPE、线路编译等 不需要 [chemistry]。运行 examples/hamiltonian.ipynbfrom hyq.chemistry import Hamiltonian 需要 [chemistry]

量子化学安装(hyq.chemistry

hyq.chemistry 用 OpenFermion 生成分子/qubit 哈密顿量。计算时会优先尝试 Psi4,若未安装 Psi4 程序则 自动改用 PySCF(多数用户只需 PySCF 即可)。

方式一:仅 pip(推荐,无需 Psi4)

Python 版本须为 3.10 或 3.11(与 hyq 一致)。

pip install -U pip
pip install "hyq[chemistry]"

验证:

python -c "import hyq; from hyq.chemistry import Hamiltonian; print('hyq', hyq.__version__)"
python -c "from openfermionpyscf import run_pyscf; import pyscf; print('PySCF ok')"

最小用法:

from hyq.chemistry import Hamiltonian

ham = Hamiltonian(
    symbols=["H", "H"],
    geometry=[[0.0, 0.0, 0.0], [0.0, 0.0, 0.74]],
    basis="sto-3g",
)
terms, n_qubits, n_electrons = ham.get_processed_hamiltonian(
    n_active_electrons=2,
    n_active_orbitals=2,
    mapping="jw",
)

方式二:使用 Psi4(可选,需 Conda)

Psi4 本体不在 PyPI,不能写进 pip install 的依赖里;hyq[chemistry] 里的 openfermionpsi4 只是 Python 接口,还必须单独安装 Psi4 可执行环境

# 1. 用 Conda 安装 Psi4(示例)
conda create -n hyq_chem python=3.11 psi4=1.10 pip -y
conda activate hyq_chem

# 2. 再装 hyq 与化学 extra
pip install "hyq[chemistry]"

验证:

python -c "import psi4; print('psi4', psi4.__version__)"
python -c "from openfermionpsi4 import run_psi4; print('openfermionpsi4 ok')"

可选环境变量(磁盘/权限异常时):

export PSI4_SCRATCH=/tmp
export TMPDIR=/tmp

运行时会使用当前目录下的 .psi4_temp_data/ 存放临时文件。

从源码开发(Conda 一键环境)

克隆仓库后可用 environment.yml(已包含 Psi4 + PySCF + 全部 pip 依赖):

conda env create -f environment.yml
conda activate hyq_alg_lib_env
pip install -e .

详见仓库内 README_HYQ.md

常见报错

若出现:

ImportError: 需要安装 openfermionpyscf/pyscf,或安装 openfermionpsi4 并配置 Psi4。

表示当前环境 未安装 hyq[chemistry] 对应依赖。请先执行 pip install "hyq[chemistry]";若已安装仍报错,请在 同一 Python 环境 中运行上文验证命令。

导入方式

安装后统一从 hyq 命名空间导入(与 pip 包名一致):

import hyq
print(hyq.__version__)

from hyq.backends import get_backend, available_backends, set_backend
from hyq.backends.core import QuantumCircuit
from hyq.backends.Tensorcircuit import TensorCircuitBackend
from hyq.solvers import VQESolver
from hyq.algorithms import exact_diagonalization, build_qpe_circuit
from hyq.ansatz import HEAAnsatz, UCCSD
from hyq.chemistry import Hamiltonian

选择默认后端

export HYQ_BACKEND=tensorcircuit   # 或 qiskit、pennylane 等
from hyq.backends import available_backends, set_backend

print(available_backends())
backend = set_backend("tensorcircuit")

快速示例(VQE)

import torch
from hyq.backends.core import QuantumCircuit
from hyq.backends.Tensorcircuit import TensorCircuitBackend
from hyq.solvers import VQESolver

backend = TensorCircuitBackend()
solver = VQESolver(backend)

def ansatz(params):
    qc = QuantumCircuit(2)
    qc.ry(0, params[0])
    qc.cx(0, 1)
    return qc

hamiltonian = [(-1.0, "ZI"), (-1.0, "IZ"), (0.5, "XX")]
init_params = torch.tensor([0.1], dtype=torch.float64)

energy, params, history = solver.solve(
    ansatz, init_params, hamiltonian, steps=100, lr=0.1
)
print(energy, params)

哈密顿量格式

hamiltonian = [
    (-1.0, "ZI"),
    (-1.0, "IZ"),
    (0.5, "XX"),
]

字符串中第 i 个字符对应第 i 个 qubit,支持 I / X / Y / Z

包结构

hyq/
├── algorithms/   # QPE、Trotter、QSE、VQD、shadow 等
├── ansatz/       # HEA、UCCSD、ADAPT 等
├── backends/     # 线路抽象与各模拟器后端
├── chemistry/    # 分子与哈密顿量(可选)
└── solvers/      # VQE、VarQITE、SA-QITE、SS-QITE、RITE

链接

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hyq-26.1.3.tar.gz (75.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hyq-26.1.3-py3-none-any.whl (94.9 kB view details)

Uploaded Python 3

File details

Details for the file hyq-26.1.3.tar.gz.

File metadata

  • Download URL: hyq-26.1.3.tar.gz
  • Upload date:
  • Size: 75.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for hyq-26.1.3.tar.gz
Algorithm Hash digest
SHA256 3177f0808f7a12b96a28711fb8e83e0a1d6fe46d04ac83478be4c40b9ae9b460
MD5 8bbfc556a85efe0c468284de237e0146
BLAKE2b-256 0c5210c99dcbb570294da1e91c70edc30debb0ed6e9845a7f32ffb18f14a160e

See more details on using hashes here.

File details

Details for the file hyq-26.1.3-py3-none-any.whl.

File metadata

  • Download URL: hyq-26.1.3-py3-none-any.whl
  • Upload date:
  • Size: 94.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for hyq-26.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d2354d37e44d2308c535f5f9909db7a67e96fe3079e4626882829ed22fd2b471
MD5 d090734d864c0c9f0131ab862a57fa96
BLAKE2b-256 db5319d703a0495f9ac1bdff0a2635a438ec4490ba2c54a55cdff1dbf0528df2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page