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.
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.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 ofnqubits, indexedq[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
| Name | Meaning |
|---|---|
qalgora.qubit() | A single qubit |
qalgora.qvector(n) | An owned register of n qubits |
qalgora.qview | A non-owning reference to qubits passed into a sub-kernel |
qalgora.pauli_word | A Pauli string such as "XYZ" |
qalgora.SpinOperator | A weighted sum of Pauli strings |
qalgora.State | A 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
- 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.
- The representation is optimized and lowered (下沉) to the selected target — today the CPU statevector simulator.
- A primitive (
sample/observe) executes it and returns classical results.
什么是 qalgora-Q 内核
import qalgora,且 @qalgora.kernel 装饰器会把门名(h、x、ry、mz 等)引入内核作用域,因此书写时省略 qalgora. 前缀。未给出定义的辅助符号(哈密顿量 图 优化器等)均为占位符。涉及 GPU 云端 真机或未发布库的示例,会在出现处带有"规划中 / 规范接口"的就地标注。set_target("qpp-cpu")——cpu 是 qpp-cpu 的别名。内核是运行在量子后端上的函数,是 qalgora-Q 中量子计算的基本单元。
定义内核
在完整规范中,函数体转换为 qalgora IR 并调度到目标后端;开放参考实现主要将核心门模型内核转换为可由 CPU 态矢量模拟器执行的电路表示。
import qalgora
@qalgora.kernel
def my_kernel(theta: float):
q = qalgora.qubit()
ry(theta, q)
mz(q)
门名 ry、mz 由 @qalgora.kernel 自动注入内核作用域,因此无需 qalgora. 前缀。
分配量子比特
qalgora.qubit()— 单个量子比特。qalgora.qvector(n)— 包含n个量子比特的寄存器,通过q[i]索引访问。qalgora.qview— 在内核间传递的非拥有引用。
量子比特初始处于 |0⟩ 态,内核执行结束后会自动释放。
类型系统
| 名称 | 含义 |
|---|---|
qalgora.qubit() | 单个量子比特 |
qalgora.qvector(n) | 拥有所有权的 n 量子比特寄存器 |
qalgora.qview | 传入子内核的量子比特非拥有引用 |
qalgora.pauli_word | Pauli 串,如 "XYZ" |
qalgora.SpinOperator | Pauli 串的加权求和(自旋算符) |
qalgora.State | 可作为输入复用的模拟态矢量 |
规则与限制
- 内核参数必须带类型注解——每个参数都需标注(
int、float、list[float]、qalgora.qview等)。 - 内核内部只允许使用量子内建指令及受限的经典语法子集(带类型的循环、对测量比特的条件判断、算术运算)。
- 不支持:对量子比特使用推导式、异常、动态类型以及大多数库调用。
- 内核可以调用其他内核,从而实现模块化电路构建。
执行模型
- 在完整规范中,被装饰的函数转换为 qalgora IR(一种 MLIR 方言);开放参考实现则将核心门模型内核转换为可执行的电路表示。
- 该表示经过优化后下沉到所选 目标后端——当前即 CPU 态矢量模拟器。
- 通过原语(
sample/observe)执行并返回经典结果。