qalgora-Q Docs Hub量子文档 ✦ Ask AI✦ 问问文档

What is a qalgora-Q Kernel?

A kernel is a function that runs on a quantum backend. It is the unit of quantum computation in qalgora-Q.

Reading the code examples
Snippets across these docs assume import qalgora and that the @qalgora.kernel decorator brings the gate names (h, x, ry, mz, …) into the kernel's scope, so they appear without a qalgora. prefix. Helper symbols (Hamiltonians, graphs, optimizers) shown without a definition are placeholders. Examples targeting GPU, cloud, real QPUs, or unreleased libraries carry an in-line "planned / specification API" note where they appear.
Backend status
Today only a local CPU-only NumPy statevector simulator is supported; real QPUs and other backends are still planned. The runnable default target is set_target("qpp-cpu")cpu is an alias of qpp-cpu.

Defining a kernel

In the full specification, the function body is converted to qalgora IR and dispatched to the target backend; the open reference build mainly converts the core gate-model kernel into a circuit representation executable by the CPU statevector simulator.

import qalgora

@qalgora.kernel
def my_kernel(theta: float):
    q = qalgora.qubit()
    ry(theta, q)
    mz(q)

The gate names ry and mz are auto-injected into the kernel scope by @qalgora.kernel, which is why they appear without a qalgora. prefix.

Allocating qubits

  • qalgora.qubit() — a single qubit.
  • qalgora.qvector(n) — a register of n qubits, indexed q[i].
  • qalgora.qview — a non-owning reference passed between kernels.

Qubits start in the |0⟩ state and are automatically released at the end of the kernel.

The type system

NameMeaning
qalgora.qubit()A single qubit
qalgora.qvector(n)An owned register of n qubits
qalgora.qviewA non-owning reference to qubits passed into a sub-kernel
qalgora.pauli_wordA Pauli string such as "XYZ"
qalgora.SpinOperatorA weighted sum of Pauli strings
qalgora.StateA simulated statevector, reusable as input

Rules & restrictions

  • Kernel arguments must be typed — annotate every one (int, float, list[float], qalgora.qview…).
  • Only quantum intrinsics and a restricted classical subset (typed loops, conditionals on measured bits, arithmetic) are allowed inside a kernel.
  • Unsupported: Python comprehensions over qubits, exceptions, dynamic typing, most library calls.
  • Kernels can call other kernels, enabling modular circuit construction.

Execution model

  1. In the full specification, the decorated function is converted to qalgora IR (an MLIR dialect); the open reference build converts the core gate-model kernel to an executable circuit representation.
  2. The representation is optimized and lowered (下沉) to the selected target — today the CPU statevector simulator.
  3. A primitive (sample/observe) executes it and returns classical results.
Tip
Keep classical pre/post-processing outside the kernel. The kernel should express only the quantum program; orchestration lives in ordinary Python or C++.

什么是 qalgora-Q 内核

如何阅读代码示例
本文档中的代码片段默认已 import qalgora,且 @qalgora.kernel 装饰器会把门名(hxrymz 等)引入内核作用域,因此书写时省略 qalgora. 前缀。未给出定义的辅助符号(哈密顿量 图 优化器等)均为占位符。涉及 GPU 云端 真机或未发布库的示例,会在出现处带有"规划中 / 规范接口"的就地标注。
后端现状
当前仅支持本地 CPU-only NumPy 态矢量模拟器,真实 QPU 及其他后端仍在规划中。默认可运行目标为 set_target("qpp-cpu")——cpuqpp-cpu 的别名。

内核是运行在量子后端上的函数,是 qalgora-Q 中量子计算的基本单元。

定义内核

在完整规范中,函数体转换为 qalgora IR 并调度到目标后端;开放参考实现主要将核心门模型内核转换为可由 CPU 态矢量模拟器执行的电路表示。

import qalgora

@qalgora.kernel
def my_kernel(theta: float):
    q = qalgora.qubit()
    ry(theta, q)
    mz(q)

门名 rymz@qalgora.kernel 自动注入内核作用域,因此无需 qalgora. 前缀。

分配量子比特

  • qalgora.qubit() — 单个量子比特。
  • qalgora.qvector(n) — 包含 n 个量子比特的寄存器,通过 q[i] 索引访问。
  • qalgora.qview — 在内核间传递的非拥有引用。

量子比特初始处于 |0⟩ 态,内核执行结束后会自动释放。

类型系统

名称含义
qalgora.qubit()单个量子比特
qalgora.qvector(n)拥有所有权的 n 量子比特寄存器
qalgora.qview传入子内核的量子比特非拥有引用
qalgora.pauli_wordPauli 串,如 "XYZ"
qalgora.SpinOperatorPauli 串的加权求和(自旋算符)
qalgora.State可作为输入复用的模拟态矢量

规则与限制

  • 内核参数必须带类型注解——每个参数都需标注(intfloatlist[float]qalgora.qview 等)。
  • 内核内部只允许使用量子内建指令及受限的经典语法子集(带类型的循环、对测量比特的条件判断、算术运算)。
  • 不支持:对量子比特使用推导式、异常、动态类型以及大多数库调用。
  • 内核可以调用其他内核,从而实现模块化电路构建。

执行模型

  1. 在完整规范中,被装饰的函数转换为 qalgora IR(一种 MLIR 方言);开放参考实现则将核心门模型内核转换为可执行的电路表示。
  2. 该表示经过优化后下沉到所选 目标后端——当前即 CPU 态矢量模拟器。
  3. 通过原语(sample/observe)执行并返回经典结果。
小贴士
将经典的前处理与后处理逻辑放在内核之外。内核只应表达量子程序本身,编排逻辑留给普通的 Python 或 C++ 代码处理。