Quantum Operations
qalgora-Q provides the standard library of single- and multi-qubit gates.
Single-qubit gates
| Gate | Call | Effect |
|---|---|---|
| Hadamard | h(q) | Superposition; (1/√2)[[1,1],[1,−1]] |
| Pauli-X | x(q) | Bit flip (NOT); [[0,1],[1,0]] |
| Pauli-Y | y(q) | [[0,−i],[i,0]] |
| Pauli-Z | z(q) | Phase flip; [[1,0],[0,−1]] |
| S / T | s(q) / t(q) | π/2 and π/4 phase gates |
| Rotations | rx(θ,q) ry(θ,q) rz(θ,q) | Rotations about the X/Y/Z axes |
| Phase rotation | r1(λ,q) | Phase on |1⟩; [[1,0],[0,eiλ]] |
| General unitary | u3(θ,φ,λ,q) | Arbitrary single-qubit unitary |
Apply to one qubit (h(q[0])) or broadcast across a register (h(q)).
Rotation conventions
All rotation angles are in radians. The axis rotations are
rx(θ,q), ry(θ,q), rz(θ,q) = exp(−i θ P / 2) for P = X, Y, Z.
The phase gate r1(λ,q) applies a relative phase only on |1⟩, i.e.
[[1, 0], [0, eiλ]]; it differs from rz(λ,q) only by a global phase.
The general single-qubit unitary is
u3(θ,φ,λ,q) = [[cos(θ/2), −eiλ·sin(θ/2)], [eiφ·sin(θ/2), ei(φ+λ)·cos(θ/2)]],
which matches the OpenQASM 3 U(θ,φ,λ) gate and Qiskit's U gate (up to a
global phase).
Controlled & multi-qubit gates
@qalgora.kernel
def gates():
q = qalgora.qvector(3)
x.ctrl(q[0], q[1]) # CNOT
x.ctrl([q[0], q[1]], q[2]) # Toffoli (CCX)
swap(q[0], q[2]) # SWAP
rz.ctrl(0.5, q[0], q[1]) # controlled phase rotation
Adjoint of a gate
The .adj modifier produces the inverse (Hermitian conjugate) of any gate —
gate.adj(...) takes the same arguments as the gate itself. For a parameterized gate this
is equivalent to negating the angle, e.g. rz.adj(θ, q) == rz(−θ, q).
@qalgora.kernel
def k():
q = qalgora.qubit()
t(q)
t.adj(q) # inverse T gate
rz.adj(0.5, q) # adjoint of a parameterized gate; same as rz(-0.5, q)
Measurement bases
mz measures in the computational (Z) basis. mx and my measure
in the X and Y bases; on backends without a native X/Y measurement they are implemented as a
basis-change rotation followed by a standard Z-basis measurement (apply h before
mx; apply s.adj then h before my).
Custom operations
Register any unitary from a flat, row-major matrix and use it like a built-in gate, controls included.
import numpy as np
# iSWAP as a 4x4 row-major matrix
qalgora.register_operation("iswap", np.array([
1, 0, 0, 0, 0, 0, 1j, 0, 0, 1j, 0, 0, 0, 0, 0, 1]))
@qalgora.kernel
def use_iswap():
q = qalgora.qvector(2)
iswap(q[0], q[1])qalgora.register_operation is a documented specification interface
(规范接口·暂未实现); the open reference build does not bundle it yet. Use the built-in gate set to
run code today.
Python ↔ C++ gate names
| Python | C++ | Meaning |
|---|---|---|
x.ctrl(c, t) | x<qalgora::ctrl>(c, t) | Controlled-X (CNOT); C++ also exposes the cx(c, t) shorthand |
x.ctrl([c0, c1], t) | x<qalgora::ctrl>(c0, c1, t) | Multi-control — the last qubit is the target, the rest are controls |
gate.adj(...) | gate<qalgora::adj>(...) | Adjoint, e.g. rz.adj(θ, q) ↔ rz<qalgora::adj>(θ, q) |
mz(q) | mz(q) | Z-basis measurement |
.ctrl and .adj work on every gate — built-in or custom — so
rx.ctrl(θ, c, t) and custom_op.adj(q) are both valid.
量子操作
qalgora-Q 提供单比特与多比特门标准库。
单比特门
| 门 | 调用方式 | 效果 |
|---|---|---|
| Hadamard | h(q) | 叠加;(1/√2)[[1,1],[1,−1]] |
| Pauli-X | x(q) | 比特翻转(非门);[[0,1],[1,0]] |
| Pauli-Y | y(q) | [[0,−i],[i,0]] |
| Pauli-Z | z(q) | 相位翻转;[[1,0],[0,−1]] |
| S / T | s(q) / t(q) | π/2 与 π/4 相位门 |
| 旋转门 | rx(θ,q) ry(θ,q) rz(θ,q) | 绕 X/Y/Z 轴的旋转 |
| 相位旋转 | r1(λ,q) | 作用于 |1⟩ 的相位;[[1,0],[0,eiλ]] |
| 通用幺正门 | u3(θ,φ,λ,q) | 任意单比特幺正算符 |
可施加于单个量子比特(h(q[0])),也可广播至整个寄存器(h(q))。
旋转门约定
所有旋转角度均以弧度为单位。轴旋转门为 rx(θ,q)、ry(θ,q)、rz(θ,q) = exp(−i θ P / 2),其中 P = X、Y、Z。相位门 r1(λ,q) 仅在 |1⟩ 上施加相对相位,即 [[1, 0], [0, eiλ]],与 rz(λ,q) 仅相差一个全局相位。通用单比特幺正门为
u3(θ,φ,λ,q) = [[cos(θ/2), −eiλ·sin(θ/2)], [eiφ·sin(θ/2), ei(φ+λ)·cos(θ/2)]],
它与 OpenQASM 3 的 U(θ,φ,λ) 门以及 Qiskit 的 U 门一致(至多相差一个全局相位)。
受控门与多比特门
@qalgora.kernel
def gates():
q = qalgora.qvector(3)
x.ctrl(q[0], q[1]) # CNOT
x.ctrl([q[0], q[1]], q[2]) # Toffoli (CCX)
swap(q[0], q[2]) # SWAP
rz.ctrl(0.5, q[0], q[1]) # controlled phase rotation
门的伴随
.adj 修饰符可生成任意门的逆(厄米共轭)——gate.adj(...) 与门本身接受相同的参数。对带参数的门而言,这等价于对角度取负,例如 rz.adj(θ, q) == rz(−θ, q)。
@qalgora.kernel
def k():
q = qalgora.qubit()
t(q)
t.adj(q) # 逆 T 门
rz.adj(0.5, q) # 带参数门的伴随;等价于 rz(-0.5, q)
测量基
mz 在计算(Z)基下测量。mx 与 my 分别在 X 基与 Y 基下测量;在没有原生 X/Y 测量的后端上,它们通过先做基变换旋转、再进行标准 Z 基测量来实现(mx 前施加 h;my 前先施加 s.adj 再施加 h)。
自定义操作
通过行优先展开的矩阵注册任意幺正算符,之后即可像内建门一样使用,包括受控形式。
import numpy as np
# iSWAP as a 4x4 row-major matrix
qalgora.register_operation("iswap", np.array([
1, 0, 0, 0, 0, 0, 1j, 0, 0, 1j, 0, 0, 0, 0, 0, 1]))
@qalgora.kernel
def use_iswap():
q = qalgora.qvector(2)
iswap(q[0], q[1])qalgora.register_operation 是已写入规范的接口(规范接口·暂未实现);开放参考实现暂未内置。如需立即运行,请使用内建门集合。
Python 与 C++ 门名对应
| Python | C++ | 含义 |
|---|---|---|
x.ctrl(c, t) | x<qalgora::ctrl>(c, t) | 受控 X(CNOT);C++ 另提供 cx(c, t) 简写 |
x.ctrl([c0, c1], t) | x<qalgora::ctrl>(c0, c1, t) | 多控制——最后一个比特为目标位,其余为控制位 |
gate.adj(...) | gate<qalgora::adj>(...) | 伴随,例如 rz.adj(θ, q) ↔ rz<qalgora::adj>(θ, q) |
mz(q) | mz(q) | Z 基测量 |
.ctrl 与 .adj 适用于所有门——无论内建还是自定义——因此
rx.ctrl(θ, c, t) 和 custom_op.adj(q) 均合法。