aglais-xqvm-quantum-vm
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAglais XQVM Skill
Aglais XQVM技能
Skill by ara.so — Daily 2026 Skills collection.
Aglais XQVM is a hardware-agnostic virtual machine for quantum computing written in Rust. It provides a unified bytecode intermediate representation for binary optimization problems (QUBO/Ising formulations) targeting quantum annealers — think LLVM for quantum computing. The VM is stack-based with a 256-slot register file, supports for WASM/bare-metal deployment, and ships four crates: bytecode, assembler, disassembler, and interpreter.
no_std + alloc由ara.so提供的技能——2026每日技能合集。
Aglais XQVM是一款基于Rust开发的硬件无关量子计算虚拟机。它为面向量子退火器的二进制优化问题(QUBO/Ising公式表示)提供统一的字节码中间表示——相当于量子计算领域的LLVM。该虚拟机基于栈结构,拥有256槽寄存器文件,支持以部署在WASM/裸金属环境中,包含四个crate:bytecode、assembler、disassembler和interpreter。
no_std + allocInstallation & Setup
安装与设置
Prerequisites
前置条件
sh
undefinedsh
undefinedInstall Rust stable
安装稳定版Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Install dev tools (cargo-nextest, clippy, etc.)
安装开发工具(cargo-nextest、clippy等)
make deps
undefinedmake deps
undefinedBuild from source
从源码构建
sh
git clone https://github.com/QuipNetwork/xq-rs
cd xq-rs
cargo build --releasesh
git clone https://github.com/QuipNetwork/xq-rs
cd xq-rs
cargo build --releaseBinaries: target/release/xqasm, target/release/xqdism, target/release/xqvm
二进制文件路径:target/release/xqasm, target/release/xqdism, target/release/xqvm
undefinedundefinedAdd as a library dependency
添加为库依赖
toml
undefinedtoml
undefinedCargo.toml
Cargo.toml
[dependencies]
aglais-xqvm-bytecode = { path = "crates/bytecode" }
aglais-xqvm-vm = { path = "crates/vm" }
For `no_std` environments (WASM, bare-metal):
```toml
[dependencies]
aglais-xqvm-bytecode = { path = "crates/bytecode", default-features = false, features = ["alloc"] }[dependencies]
aglais-xqvm-bytecode = { path = "crates/bytecode" }
aglais-xqvm-vm = { path = "crates/vm" }
针对`no_std`环境(WASM、裸金属):
```toml
[dependencies]
aglais-xqvm-bytecode = { path = "crates/bytecode", default-features = false, features = ["alloc"] }Workspace Crate Overview
工作区Crate概述
| Crate | Binary | Role |
|---|---|---|
| — | Opcode table, instruction types, builder, binary codec, stream reader |
| | Text assembler: |
| | Bytecode → human-readable listing |
| | Bytecode interpreter: stack, registers, QUBO/Ising execution |
| Crate | 二进制文件 | 作用 |
|---|---|---|
| — | 操作码表、指令类型、构建器、二进制编解码器、流读取器 |
| | 文本汇编器:将 |
| | 字节码反汇编器:将字节码转换为人类可读的清单 |
| | 字节码解释器:栈、寄存器、QUBO/Ising执行 |
CLI Commands
CLI命令
xqasm
— Assembler
xqasmxqasm
— 汇编器
xqasmsh
undefinedsh
undefinedAssemble a source file to bytecode
将源文件汇编为字节码
xqasm program.xqasm -o program.xqbc
xqasm program.xqasm -o program.xqbc
Assemble with verbose output
带详细输出的汇编
xqasm program.xqasm -o program.xqbc --verbose
undefinedxqasm program.xqasm -o program.xqbc --verbose
undefinedxqdism
— Disassembler
xqdismxqdism
— 反汇编器
xqdismsh
undefinedsh
undefinedInspect bytecode encoding as human-readable listing
以人类可读清单形式查看字节码编码
xqdism program.xqbc
xqdism program.xqbc
Pipe to file
输出到文件
xqdism program.xqbc > listing.txt
undefinedxqdism program.xqbc > listing.txt
undefinedxqvm
— Interpreter
xqvmxqvm
— 解释器
xqvmsh
undefinedsh
undefinedExecute bytecode
执行字节码
xqvm program.xqbc
xqvm program.xqbc
Run with debug output (if supported)
带调试输出运行(若支持)
xqvm program.xqbc --debug
undefinedxqvm program.xqbc --debug
undefinedFull pipeline
完整流程
sh
xqasm problem.xqasm -o problem.xqbc && xqdism problem.xqbc && xqvm problem.xqbcsh
xqasm problem.xqasm -o problem.xqbc && xqdism problem.xqbc && xqvm problem.xqbcXQASM Language Reference
XQASM语言参考
The assembler accepts text files. The VM is stack-based; most instructions pop operands from the stack and push results.
.xqasm汇编器接受文本文件。虚拟机基于栈结构;大多数指令从栈中弹出操作数并将结果压入栈中。
.xqasmBasic stack operations
基础栈操作
asm
; push two integers and add them
PUSH 10
PUSH 32
ADD
HALTasm
; 推入两个整数并相加
PUSH 10
PUSH 32
ADD
HALTRegisters (0–255)
寄存器(0–255)
asm
PUSH 42
STORE 0 ; pop stack → register 0
LOAD 0 ; push register 0 → stackasm
PUSH 42
STORE 0 ; 弹出栈内容 → 寄存器0
LOAD 0 ; 将寄存器0内容推入栈Arithmetic
算术运算
asm
PUSH 10
PUSH 3
ADD ; stack: [13]
PUSH 7
SUB ; stack: [6]
PUSH 2
MUL ; stack: [12]
PUSH 4
DIV ; stack: [3]asm
PUSH 10
PUSH 3
ADD ; 栈内容: [13]
PUSH 7
SUB ; 栈内容: [6]
PUSH 2
MUL ; 栈内容: [12]
PUSH 4
DIV ; 栈内容: [3]Vectors / integer arrays
向量/整数数组
asm
; build a 3-element vector [1, 2, 3]
PUSH 1
PUSH 2
PUSH 3
PUSH 3 ; length
VEC ; stack: [Vec([1,2,3])]
STORE 1asm
; 创建一个3元素向量 [1, 2, 3]
PUSH 1
PUSH 2
PUSH 3
PUSH 3 ; 长度
VEC ; 栈内容: [Vec([1,2,3])]
STORE 1QUBO / Ising model construction
QUBO/Ising模型构建
asm
; XQMX_NEW n creates an n-variable QUBO model
PUSH 4
XQMX_NEW ; stack: [XqmxModel(4 vars)]
STORE 2
; set quadratic coupling Q[i][j] = weight
LOAD 2
PUSH 0 ; i
PUSH 1 ; j
PUSH -1 ; weight (integer encoding)
XQMX_SET_Q ; modifies model in reg 2
; set linear bias h[i] = weight
LOAD 2
PUSH 0
PUSH 5
XQMX_SET_H
; evaluate energy of a candidate solution
LOAD 2 ; model
PUSH 0 ; sample register (XqmxSample)
XQMX_EVAL ; pushes energy onto stackasm
; XQMX_NEW n 创建一个n变量的QUBO模型
PUSH 4
XQMX_NEW ; 栈内容: [XqmxModel(4个变量)]
STORE 2
; 设置二次耦合项 Q[i][j] = weight
LOAD 2
PUSH 0 ; i
PUSH 1 ; j
PUSH -1 ; 权重(整数编码)
XQMX_SET_Q ; 修改寄存器2中的模型
; 设置线性偏置 h[i] = weight
LOAD 2
PUSH 0
PUSH 5
XQMX_SET_H
; 评估候选解的能量
LOAD 2 ; 模型
PUSH 0 ; 样本寄存器(XqmxSample)
XQMX_EVAL ; 将能量值推入栈Control flow & iteration
控制流与迭代
asm
; RANGE lo hi → loop stack entry, ITER steps through it
PUSH 0
PUSH 5
RANGE ; loop i in 0..5
ITER ; advance; jumps past matching END_ITER when done
LOAD 0
PUSH 1
ADD
STORE 0
END_ITER
HALTasm
; RANGE lo hi → 推入循环栈条目,ITER遍历该范围
PUSH 0
PUSH 5
RANGE ; 循环i从0到5(不含5)
ITER ; 推进;完成时跳转到匹配的END_ITER之后
LOAD 0
PUSH 1
ADD
STORE 0
END_ITER
HALTLabels and jumps
标签与跳转
asm
PUSH 0
loop:
PUSH 1
ADD
DUP
PUSH 10
LT
JMP_TRUE loop
HALTasm
PUSH 0
loop:
PUSH 1
ADD
DUP
PUSH 10
LT
JMP_TRUE loop
HALTRust API: Bytecode Builder
Rust API:字节码构建器
Use to construct programs programmatically:
aglais-xqvm-bytecoderust
use aglais_xqvm_bytecode::{BytecodeBuilder, Instruction, Opcode};
fn build_add_program() -> Vec<u8> {
let mut builder = BytecodeBuilder::new();
builder.emit(Instruction::Push(10));
builder.emit(Instruction::Push(32));
builder.emit(Instruction::Add);
builder.emit(Instruction::Halt);
builder.finish()
}使用以编程方式构建程序:
aglais-xqvm-bytecoderust
use aglais_xqvm_bytecode::{BytecodeBuilder, Instruction, Opcode};
fn build_add_program() -> Vec<u8> {
let mut builder = BytecodeBuilder::new();
builder.emit(Instruction::Push(10));
builder.emit(Instruction::Push(32));
builder.emit(Instruction::Add);
builder.emit(Instruction::Halt);
builder.finish()
}Decoding bytecode (stream reader)
解码字节码(流读取器)
rust
use aglais_xqvm_bytecode::StreamReader;
fn decode(bytes: &[u8]) {
let mut reader = StreamReader::new(bytes);
while let Some(instr) = reader.next_instruction().unwrap() {
println!("{:?}", instr);
}
}rust
use aglais_xqvm_bytecode::StreamReader;
fn decode(bytes: &[u8]) {
let mut reader = StreamReader::new(bytes);
while let Some(instr) = reader.next_instruction().unwrap() {
println!("{:?}", instr);
}
}Rust API: Running the VM
Rust API:运行虚拟机
rust
use aglais_xqvm_vm::Vm;
fn main() {
// Load bytecode from a file
let bytecode = std::fs::read("program.xqbc").expect("read bytecode");
let mut vm = Vm::new();
vm.load(&bytecode).expect("load");
vm.run().expect("run");
// Inspect top of stack after execution
if let Some(val) = vm.stack_top() {
println!("Result: {:?}", val);
}
}rust
use aglais_xqvm_vm::Vm;
fn main() {
// 从文件加载字节码
let bytecode = std::fs::read("program.xqbc").expect("read bytecode");
let mut vm = Vm::new();
vm.load(&bytecode).expect("load");
vm.run().expect("run");
// 执行后检查栈顶元素
if let Some(val) = vm.stack_top() {
println!("Result: {:?}", val);
}
}Accessing registers after execution
执行后访问寄存器
rust
use aglais_xqvm_vm::{Vm, Value};
fn run_and_inspect(bytecode: &[u8]) -> Value {
let mut vm = Vm::new();
vm.load(bytecode).unwrap();
vm.run().unwrap();
vm.register(0).cloned().unwrap_or(Value::Int(0))
}rust
use aglais_xqvm_vm::{Vm, Value};
fn run_and_inspect(bytecode: &[u8]) -> Value {
let mut vm = Vm::new();
vm.load(bytecode).unwrap();
vm.run().unwrap();
vm.register(0).cloned().unwrap_or(Value::Int(0))
}Real-World Pattern: TSP as QUBO
实际应用模式:以QUBO表示旅行商问题(TSP)
The directory contains a complete Travelling Salesman Problem encoded as a QUBO driven by a Rust harness. The pattern is:
crates/vm/examples/tsp/- Generate coefficients in a Rust harness (problem-specific math).
- Emit files parameterised by those coefficients.
.xqasm - Assemble + run with /
xqasm.xqvm
rust
// crates/vm/examples/tsp/main.rs pattern
use std::process::Command;
fn assemble_and_run(src: &str, out: &str) {
let asm = Command::new("xqasm")
.args([src, "-o", out])
.status()
.expect("xqasm failed");
assert!(asm.success());
let run = Command::new("xqvm")
.arg(out)
.status()
.expect("xqvm failed");
assert!(run.success());
}
fn main() {
assemble_and_run("init.xqasm", "init.xqbc");
assemble_and_run("problem.xqasm", "problem.xqbc");
assemble_and_run("eval.xqasm", "eval.xqbc");
}crates/vm/examples/tsp/- 生成系数:在Rust驱动程序中完成(问题特定的数学计算)。
- 生成文件:使用上述系数参数化生成。
.xqasm - 汇编并运行:使用/
xqasm工具。xqvm
rust
// crates/vm/examples/tsp/main.rs模式
use std::process::Command;
fn assemble_and_run(src: &str, out: &str) {
let asm = Command::new("xqasm")
.args([src, "-o", out])
.status()
.expect("xqasm failed");
assert!(asm.success());
let run = Command::new("xqvm")
.arg(out)
.status()
.expect("xqvm failed");
assert!(run.success());
}
fn main() {
assemble_and_run("init.xqasm", "init.xqbc");
assemble_and_run("problem.xqasm", "problem.xqbc");
assemble_and_run("eval.xqasm", "eval.xqbc");
}Common Patterns
常见模式
Pattern: build a QUBO model in assembly
模式:在汇编中构建QUBO模型
asm
; 2-variable QUBO: minimise x0 - x1 + 2*x0*x1
PUSH 2
XQMX_NEW
STORE 0
LOAD 0
PUSH 0
PUSH -1 ; h[0] = -1 (linear)
XQMX_SET_H
LOAD 0
PUSH 1
PUSH -1 ; h[1] = -1 (linear)
XQMX_SET_H
LOAD 0
PUSH 0
PUSH 1
PUSH 2 ; Q[0][1] = 2 (quadratic)
XQMX_SET_Q
HALTasm
; 2变量QUBO:最小化 x0 - x1 + 2*x0*x1
PUSH 2
XQMX_NEW
STORE 0
LOAD 0
PUSH 0
PUSH -1 ; h[0] = -1 (线性项)
XQMX_SET_H
LOAD 0
PUSH 1
PUSH -1 ; h[1] = -1 (线性项)
XQMX_SET_H
LOAD 0
PUSH 0
PUSH 1
PUSH 2 ; Q[0][1] = 2 (二次项)
XQMX_SET_Q
HALTPattern: iterate over model variables
模式:遍历模型变量
asm
PUSH 4
XQMX_NEW
STORE 0
PUSH 0
PUSH 4
RANGE
ITER
; register 1 holds current loop index after ITER
LOAD 0
LOAD 1 ; index i
LOAD 1 ; index i (diagonal → linear term)
PUSH -1
XQMX_SET_Q
END_ITER
HALTasm
PUSH 4
XQMX_NEW
STORE 0
PUSH 0
PUSH 4
RANGE
ITER
; ITER后寄存器1保存当前循环索引
LOAD 0
LOAD 1 ; 索引i
LOAD 1 ; 索引i(对角线→线性项)
PUSH -1
XQMX_SET_Q
END_ITER
HALTPattern: no_std bytecode decoding (WASM)
模式:no_std字节码解码(WASM)
rust
#![no_std]
extern crate alloc;
use alloc::vec::Vec;
use aglais_xqvm_bytecode::StreamReader;
pub fn decode_instructions(bytes: &[u8]) -> Vec<alloc::string::String> {
let mut reader = StreamReader::new(bytes);
let mut out = Vec::new();
while let Ok(Some(instr)) = reader.next_instruction() {
out.push(alloc::format!("{:?}", instr));
}
out
}rust
#![no_std]
extern crate alloc;
use alloc::vec::Vec;
use aglais_xqvm_bytecode::StreamReader;
pub fn decode_instructions(bytes: &[u8]) -> Vec<alloc::string::String> {
let mut reader = StreamReader::new(bytes);
let mut out = Vec::new();
while let Ok(Some(instr)) = reader.next_instruction() {
out.push(alloc::format!("{:?}", instr));
}
out
}Development Workflow
开发流程
sh
undefinedsh
undefinedRun all lints and tests (mirrors CI)
运行所有检查和测试(与CI一致)
make all
make all
Run only tests
仅运行测试
cargo test --workspace
cargo test --workspace
Run lints
运行代码检查
cargo clippy --workspace --all-targets -- -D warnings
cargo clippy --workspace --all-targets -- -D warnings
Format
格式化代码
cargo fmt --all
cargo fmt --all
Run a specific example
运行特定示例
cargo run --example tsp --manifest-path crates/vm/Cargo.toml
undefinedcargo run --example tsp --manifest-path crates/vm/Cargo.toml
undefinedInstruction Set Quick Reference
指令集快速参考
The opcode table in is the single source of truth for all 76 instructions. Key categories:
crates/bytecode/src/types/table.rs| Category | Instructions |
|---|---|
| Stack | |
| Registers | |
| Arithmetic | |
| Comparison | |
| Control flow | |
| Iteration | |
| Vectors | |
| QUBO/Ising | |
All operands are big-endian. The binary format is a bare instruction stream with no file header.
crates/bytecode/src/types/table.rs| 类别 | 指令 |
|---|---|
| 栈操作 | |
| 寄存器操作 | |
| 算术运算 | |
| 比较运算 | |
| 控制流 | |
| 迭代 | |
| 向量操作 | |
| QUBO/Ising操作 | |
所有操作数均为大端字节序。二进制格式为无文件头的纯指令流。
Troubleshooting
故障排除
xqasm: command not found
xqasm: command not foundxqasm: command not found
xqasm: command not foundEnsure is on or use the full path:
target/release$PATHsh
export PATH="$PWD/target/release:$PATH"确保已加入,或使用完整路径:
target/release$PATHsh
export PATH="$PWD/target/release:$PATH"Stack underflow at runtime
运行时栈下溢
The VM is strictly stack-based. Every instruction that pops values requires them to be present. Check that / precedes every operation, and that loops don't consume values without restoring the stack balance.
PUSHLOAD虚拟机严格基于栈结构。所有弹出值的指令要求栈中存在对应值。检查每个操作前是否有/指令,且循环不会在未恢复栈平衡的情况下消耗值。
PUSHLOADITER
never terminates
ITERITER
永不终止
ITERRANGERANGEEND_ITERlohilohiRANGERANGEEND_ITERlohilohiBuild fails in no_std
environment
no_stdno_std
环境下构建失败
no_stdDisable default features and enable the feature on :
allocaglais-xqvm-bytecodetoml
aglais-xqvm-bytecode = { ..., default-features = false, features = ["alloc"] }The VM crate () requires and is not suitable for bare-metal.
aglais-xqvm-vmstd禁用的默认特性并启用特性:
aglais-xqvm-bytecodealloctoml
aglais-xqvm-bytecode = { ..., default-features = false, features = ["alloc"] }VM crate()依赖,不适合裸金属环境。
aglais-xqvm-vmstdInspecting unexpected bytecode
检查异常字节码
Use to verify the assembler output before running:
xqdismsh
xqasm suspect.xqasm -o suspect.xqbc
xqdism suspect.xqbc # check instruction sequence and operand values
xqvm suspect.xqbc运行前使用验证汇编器输出:
xqdismsh
xqasm suspect.xqasm -o suspect.xqbc
xqdism suspect.xqbc # 检查指令序列和操作数值
xqvm suspect.xqbcLicense
许可证
AGPL-3.0-or-later. Embedding in proprietary network services requires source disclosure under the AGPL.
AGPL-3.0-or-later。将其嵌入专有网络服务需遵循AGPL协议披露源码。