xquad README

项目的 README,来自 github.com/QuipNetwork/xquad。合并为一个 markdown 文件:README.md。

XQuad

Early public release. The instruction set, binary format, and public API may still change before v1.0. Production use is not recommended yet.

XQuad is a hardware-agnostic toolchain for expressing and running quadratic optimization problems (QUBO / Ising / integer) across quantum annealers and classical solvers. It ships as two Rust crates plus one Python distribution, xquad, built around a single virtual-machine specification.

Think of it as LLVM for quadratic models -- a common intermediate representation that lets you write a problem once and retarget it to any supported backend.

Install

Python (from PyPI)

Install XQuad into a project environment first: a virtual environment, or a uv project.

python -m venv .venv && source .venv/bin/activate
pip install xquad
# or, in a uv project:
uv add xquad

For the xquad command alone, outside any project, install it as a uv tool, with the quip extra for the xquad quip commands:

uv tool install xquad            # the xquad command
uv tool install 'xquad[quip]'    # with the xquad quip commands

On macOS, a user install (pip install --user, or pip falling back to one) puts the xquad command in ~/Library/Python/3.x/bin, which is not on PATH; python -m xquad runs the same command without it.

xquad is the only distribution: one install carries the full pipeline, the xquad command, and the xqffi, xqcp and xqsa packages (FFI bindings, DSL, solvers), which import under their own names. Up to 0.5 those three were separate PyPI distributions; from 0.6 they ship only inside xquad. Prebuilt wheels are available for Linux x86_64 and aarch64 (CPython >= 3.13, glibc >= 2.27). On macOS and Windows, pip builds the xqffi Rust extension from source -- this requires a Rust toolchain (>= 1.85).

GPU/QPU support

The base install includes CPU simulated annealing only. To use GPU or quantum hardware solvers, install the corresponding extra:

Solver Install Prerequisite
NVIDIA CUDA GPU (cuda) pip install 'xquad[cuda]' CUDA 12.x driver + NVIDIA GPU
Apple Metal GPU (metal) pip install 'xquad[metal]' Apple Silicon Mac with a Metal device
D-Wave Advantage QPU (dwave) pip install 'xquad[dwave]' D-Wave Leap account + API token
Quip Network solver (quip) pip install 'xquad[quip]' QUIP_RPC_URL + a configured signer

Extras are composable: pip install 'xquad[cuda,dwave]'. See xqsa/README.md for driver prerequisites and per-solver quick-starts.

For a guided walkthrough that installs XQuad, verifies it, and runs a complete problem end to end, see Install XQuad in the book.

Package map

Two Rust crates on crates.io:

Crate Description
xqvm X-Quadratic Virtual Machine interpreter, bytecode codec, opcode table (no_std + alloc)
xqasm .xqasm text-format assembler

One Python distribution on PyPI, xquad, carrying four packages:

Package Description
xqffi PyO3 FFI bindings for xqvm + xqasm (xqffi.vm, xqffi.asm, xqffi.checker)
xqcp Constraint-programming DSL that compiles to .xqasm
xqsa Solver adapters -- CPU/GPU annealers, D-Wave QPU, Quip network
xquad Umbrella package with interactive Program / Session / RunResult API, and the xquad CLI (xquad vm asm / dis / run / check)

The one Rust package without a crates.io artefact is the xqffi pyo3 crate itself -- it ships compiled inside the xquad wheel rather than as a reusable Rust library.

Quick start

A minimal program (CLI)

; add.xqasm -- push two integers and add them
PUSH 10
PUSH 32
ADD
HALT
xquad vm asm add.xqasm -o add.xqb && xquad vm run add.xqb

End-to-end (Python, via the umbrella)

The recommended user-facing surface is xquad.program.Program + Session + RunResult. Load once, run many times with different calldata, inspect outputs by slot:

from xquad.program import Program

program = Program.from_source("""
PUSH 0
INPUT r0
PUSH 1
INPUT r1
LOAD r0
LOAD r1
ADD
STOW r2
PUSH 0
OUTPUT r2
HALT
""")

session = program.session(output_slots=1)
session.set_calldata([40, 2])
result = session.run()
assert dict(result.outputs) == {0: 42}

Program.load(bytes) takes wire-format bytecode; Session.run() returns a RunResult with dict-keyed outputs (unset slots present as None), residual stack, and step count. See Running Programs for the full tour.

Low-level / conformance surface

For conformance harnesses and one-shot execution, the raw xqffi.vm.Vm is still available directly:

from xqffi.asm import assemble_source
from xqffi.vm import Vm

bytecode = assemble_source(src)
v = Vm()
v.set_calldata([40, 2])
v.set_output_slots(1)
v.run(bytecode)
assert v.outputs() == [42]

The xquad.vm.VM class offers a middle tier -- a wrapper around the Rust VM that accepts .xqasm source, keeps calldata, output slots and limits across runs, and hands back the xqffi types unchanged.

End-to-end worked example

examples/tsp/ shows a full Travelling Salesman Problem driven from the xqcp DSL through the VM and xqsa solver:

uv run python examples/tsp/runner.py --seed 42
uv run python examples/maxcut/runner.py --seed 42

# Run every example and check each finds a valid solution:
make example-smoke

Architecture

XQuad is a stack-based interpreter with a 256-slot register file. Registers hold typed values -- integers, integer vectors, QUBO/Ising models (XqmxModel), and candidate solutions (XqmxSample). A dedicated loop stack drives RANGE/ITER iteration.

The opcode table is declared once in xqvm/src/bytecode/types/table.rs via the opcodes! x-macro; xqvm/opcodes.yaml is the machine-readable mirror, compared with the Rust macro at compile time (xqvm/src/bytecode/types/parity.rs), so the two cannot drift.

The binary format (.xqb) opens with a fixed 15-byte XQBC header -- magic, version, calldata/output-slot counts, code length, and a CRC-32 checksum of the payload -- followed by the instruction stream: an opcode byte followed by its operands in big-endian byte order.

See docs/book/src/xqvm/opcodes.md for the opcode reference and spec/xqvm/SPEC.md for the normative spec.

Development

Prerequisites

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh   # stable Rust
make deps                                                          # dev tools

Local setup

make xquad

This installs the root xquad project (xqffi, xqcp, xqsa, xquad) editable into .venv/, builds the xqffi pyo3 extension via maturin, puts the repo root on sys.path so scripts anywhere in the repo can import xqcp / xqsa / xqffi / xquad naturally, and installs the xquad command into .venv/bin/, run with uv run xquad.

Open a REPL with everything ready:

make repl

CI-equivalent locally

make preflight    # what CI enforces, split into preflight-rs / -py / -docs / -policy
make conformance  # the specification vectors under xqvm/tests/vectors/

Contributing

See CONTRIBUTING.md for the development workflow, commit conventions, and DCO sign-off requirements. Agent guidelines for AI coding assistants are in AGENTS.md; cutting a release is documented in RELEASING.md.

License

Licensed under the GNU Affero General Public License v3.0 or later.

xquad (QuipNetwork/xquad)

正在搜索数据库…

这是什么?XQuad 是一套与硬件无关的工具链,用于表达和运行二次优化问题(QUBO / Ising / 离散问题),可在量子退火机和经典求解器上运行。它充当一种通用的中间表示:你只需编写一次,就能切换到任何受支持的后端运行。
这是 Rust 库、Python 包,还是命令行工具?三者都是。它包含三个 Rust crate(xqvm、xqasm、xqcli)和五个 Python 发行包(xqffi、xqvmpy、xqcp、xqsa、xquad),它们都基于同一份虚拟机规范构建。因此你可以把它当作 Rust crate 使用,作为 Python 库调用,或通过 xquad 命令行工具使用。
我该如何让问题在 XQuad 上运行?通过 pip install xquad 安装 Python 总包(或从 crates.io 安装 Rust crate),然后编写或编译一个 .xqasm 程序,再通过 xquad 命令行(asm/dism/run/verify)或 Python 的 Program / Session / RunResult API 运行它。
安装 Python 包需要 Rust 工具链吗?仅限 macOS 和 Windows:Linux x86_64/aarch64 平台提供预编译的 wheel 包(需 CPython >=3.13)。其他平台上,pip 会从源码构建 xqffi 扩展,此时需要 Rust 工具链 >=1.85。
它现在可以用于生产环境了吗?不行。这是一个早期的公开版本,指令集、二进制格式和公共 API 在 v1.0 之前都可能发生变化。
Quip Network 求解器扩展有什么用?pip install xquad[quip] 会添加一个后端,用于将问题直接提交到 Quip Network。使用时需要设置 QUIP_RPC_URL,并配置签名者以完成身份验证。
XQuad 如何保证 Rust 和 Python 虚拟机保持一致?规范中的每个一致性测试向量都会在 CI 中同时运行于 Rust 生产虚拟机(xqvm)和 Python 参考虚拟机(xqvmpy)上,只要两者结果有任何不一致,构建就会失败。
虚拟机的底层架构是什么样的?它是一个基于栈的解释器,拥有 256 个槽位的带类型寄存器文件(整数、整数向量、QUBO/Ising 模型、候选解),并配有专门用于 RANGE/ITER 迭代的循环栈。
有没有完整的示例可以学习?examples/tsp/ 逐步演示了一个用 xqcp DSL 定义的旅行商问题(TSP),编译为 .xqasm 后,通过 Python 或 Rust 虚拟机,借助 xqsa 求解器完整运行。
有哪些类似的项目?hexstellar, burn, turboquant_plus, scalene, jax, fframes, koharu, bend
大小
5.5 MB
星标
5.6K
时长
8个月
提交 · 90 天