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

Quantum Edge Detection

◐ Design-level API
This page documents qalgora-Q API design, architecture, or adaptation workflows. Code examples illustrate intended usage and are not guaranteed to run in the current reference implementation.

Encode an image into quantum amplitudes and detect edges with a single Hadamard sweep — the Quantum Hadamard Edge Detection (QHED) algorithm. It is often paraded as a showcase of quantum speedup, but its speedup claim comes with important fine print.

The problem it solves

Edge detection is a cornerstone of image processing: edges appear wherever the intensity of neighbouring pixels changes abruptly. The classical approach slides a detection kernel across the whole image, comparing adjacent pixels one by one — a cost that grows linearly in the number of pixels. For an image of 2n pixels, a classical scan needs O(2n) operations. Quantum edge detection asks a tantalising question: could a single operation acting on the entire quantum state compute all neighbour-differences at once?

Encoding an image — amplitude encoding (QPIE)

The first step is to load the image into a quantum state. Quantum Probability Image Encoding (QPIE) normalizes each pixel's intensity and writes it into the amplitudes of an n-qubit state: the normalized intensity of the k-th pixel becomes the amplitude (or part of it) of the basis state |k⟩. A measurement's probability is then tied to the square of the intensity rather than equal to it linearly, and when the pixel count is not a power of 2 the data must be padded or resampled to fit. In this way 2n pixels are carried by just n qubits — and this is precisely where the "exponential compression," and the allure, comes from. A megapixel image would in principle need only about 20 qubits.

But keep one thing in mind: these pixel values now live in the amplitudes, and you cannot read them out directly. Any single measurement collapses to one pixel with probability proportional to |amplitude|², so the vast majority of the information is lost the moment you read it out. This fact will haunt every trade-off discussed below.

The mechanism — one Hadamard is one neighbour-difference

The crucial insight is the algebraic effect of a Hadamard gate when it acts on amplitudes. Arrange the pixels along one coordinate axis as a row; a Hadamard applied to the lowest-order qubit, under that particular pairing, produces the sum and difference of adjacent amplitudes — for the pairs (0,1), (2,3), … And the difference between adjacent pixels is exactly the discrete gradient — which is where edges live.

So in flat regions (where neighbours are equal) the subtraction yields zero and the amplitudes cancel; at an edge (where neighbours jump) a non-zero amplitude survives. This is the kernel of QHED's speedup claim. But note: one Hadamard does not hand you a complete edge map. It only differences the pixels inside each fixed pair; recovering the full one-dimensional adjacency relation — or two-dimensional horizontal/vertical edges — generally still needs shifts, an ancilla qubit, a re-ordered encoding, or the two directions handled separately.

An honest look at that speedup claim

  • State preparation can swallow the whole gain. Loading an arbitrary image into the amplitudes (i.e. the amplitudes step in QPIE) generally takes O(2n) gates — exactly the same order as the classical pixel-by-pixel scan. Unless the image has special structure that can be prepared efficiently, just "getting the data in" erases the advantage that one Hadamard bought you.
  • Read-out is just as expensive. The gradients hide in the amplitudes, but a measurement only ever gives you a probability distribution: locations with a larger difference amplitude simply appear more often — a single measurement never returns the whole edge map. Reconstructing it requires an enormous number of samples — a count that again grows with the number of pixels, so an O(2n)-scale cost reappears at the output end.
  • This is a textbook "data in / data out" bottleneck. The middle of the circuit (that one Hadamard) really is exponentially fast, but loading and reading out pinch the advantage to death at both ends. Many advertised quantum machine-learning and image-processing speedups stumble at exactly the same spot.
  • Where it genuinely pays off. When the image already exists as a quantum state (say, as the output of some upstream quantum process), so that no classical state-preparation cost need be paid, and when we only care about some aggregate statistic of the edges rather than a pixel-by-pixel reconstruction — only then can QHED hold a real advantage.
Read the ends before the claim
When judging any "exponential" image algorithm, always ask first about the cost at both ends: how does the data get in, and how does the result come out? QHED's Hadamard in the middle is genuinely elegant, but whether the speedup is delivered depends on whether state preparation and read-out are kept below the circuit's own cost. The intermediate quantum transform can be very shallow, yet the end-to-end complexity is usually still dominated by state preparation and read-out.

The algorithm, step by step

  1. Normalize the image's pixel intensities into a unit-norm state vector.
  2. Use QPIE to prepare those amplitudes on an n-qubit state (2n pixels packed into n qubits).
  3. Apply a Hadamard to the relevant qubit along the target axis; neighbouring amplitudes thereby become their sum and difference.
  4. Measure; post-process the samples, reading the edges out of the differences between neighbouring amplitudes.

Seeing it in code — image encoding (QPIE)

Read this QPIE encoding against the theory above: the normalized pixel intensities amps are prepared into a state via qalgora.amplitudes(q, amplitudes) — and this is exactly the state-preparation step that can be expensive enough to swallow the speedup.

import qalgora
import numpy as np

# normalize pixel intensities into a statevector
img = load_image_gray()                 # shape (2^n,)
amps = img / np.linalg.norm(img)

@qalgora.kernel
def encode(amplitudes: list[complex]):
    q = qalgora.qvector(int(np.log2(len(amplitudes))))
    qalgora.amplitudes(q, amplitudes)   # state preparation
Specification API — not in the open reference build yet
This example shows a qalgora-Q specification API (or a third-party library) that the open reference build does not bundle today. It documents the intended interface; to run code now, use the reference build’s supported core API.

Seeing it in code — Hadamard edge detection

This kernel likewise prepares the amplitudes first, then applies a single h(q[0]) to q[0] — that one Hadamard which computes the gradient along one axis and turns neighbouring amplitudes into their sum and difference; the trailing comment spells out the post-processing: after measurement, the edges are read out of the differences between neighbouring amplitudes.

@qalgora.kernel
def qhed(amplitudes: list[complex]):
    q = qalgora.qvector(int(np.log2(len(amplitudes))))
    qalgora.amplitudes(q, amplitudes)
    h(q[0])                              # gradient along one axis
    mz(q)
# post-process: repeated sampling builds the probability distribution, then
# classically reconstruct the edge map from neighbouring amplitude differences
Specification API — not in the open reference build yet
This example shows a qalgora-Q specification API (or a third-party library) that the open reference build does not bundle today. It documents the intended interface; to run code now, use the reference build’s supported core API.
Try it yourself
Encode and run QHED on an image with large flat regions: you will see the measurement probability concentrate on the bitstrings near the edges, while the flat areas almost never appear. Then recall the cost at both the state-preparation and read-out ends, and you will feel why a "fast middle" is not the same as a "fast whole." The state preparation page discusses the real cost of loading data into amplitudes.

量子边缘检测

◐ 设计接口
本页描述的是 qalgora-Q 的接口设计、架构设计或适配工作流。相关代码用于说明预期用法,当前参考实现不保证可以直接运行。

把整幅图像编码进量子振幅,再用一次 Hadamard 扫描检出边缘——即量子 Hadamard 边缘检测(QHED)算法。它常被当作量子加速的招牌示例,但其加速断言带着重要的前提条件。

它解决的问题

边缘检测是图像处理的基石:边缘出现在相邻像素强度发生骤变之处。经典做法是把检测核滑过整幅图像,逐个比较邻近像素——其代价随像素数线性增长。对一幅含 2n 个像素的图像,经典扫描需要 O(2n) 次操作。量子边缘检测想问的是:能否用一次作用于整个量子态的操作,"一举"算出所有邻像差?

编码图像 振幅编码(QPIE)

第一步是把图像装进量子态。量子概率图像编码(QPIE)把每个像素的强度归一化后,写入一个 n 比特态的振幅里:第 k 个像素强度归一化后成为 |k⟩ 的振幅(或一部分),测量概率与强度平方相关而非线性相等;像素数非 2ⁿ 时需 padding/重采样。如此一来,2n 个像素只需 n 个量子比特即可承载——这正是"指数级压缩"的来源,也是诱人之处。一幅百万像素的图像,原则上只需约 20 个比特。

但请记住:这些像素值此刻活在振幅之中,你无法直接读出它们。任何一次测量只能按 |振幅|² 的概率塌缩到某一个像素,绝大部分信息在读出时即告丢失。这一点将贯穿后文的全部权衡。

机制 一次 Hadamard 即一次相邻求差

关键的洞见在于 Hadamard 门作用于振幅时的代数效果。把像素按某一坐标轴排成一行,对最低位 qubit 施加 Hadamard 在特定配对方式下产生相邻振幅的和与差(如 (0,1),(2,3),…)。而相邻像素之差正是离散梯度——边缘所在之处。

于是,平坦区域(邻像相等)相减得零、振幅相消;边缘处(邻像突变)则留下非零振幅。这便是 QHED 加速断言的核心。但要注意:一道 Hadamard 并不能直接给出完整边缘图,它只对每个固定配对内部的像素求差;完整一维相邻关系或二维水平/垂直边缘通常还需移位、辅助 qubit、重排编码或分别处理方向。

诚实地看待那处加速断言

  • 态制备可能吞掉全部收益。把一幅任意图像载入振幅(即 QPIE 的 amplitudes 这一步)一般需要 O(2n) 个门——恰好与经典逐像素扫描同阶。除非图像具有可被高效制备的特殊结构,否则仅"把数据装进去"就抹平了那一道 Hadamard 带来的优势。
  • 读出同样昂贵。梯度藏在振幅里,而测量只能给出概率分布;差分振幅较大的位置出现概率更高,而非一次测量直接返回整幅边缘图。要重建出整幅边缘图,需要海量采样——其次数又随像素数增长,于是输出端再次出现 O(2n) 量级的代价。
  • 这是一个"数据进出"瓶颈的典型样本。线路中段(那一道 Hadamard)确实指数级地快,但载入读出两端把优势夹没了。许多被宣传的量子机器学习/图像处理加速,都栽在同一处。
  • 它真正有价值的场景。当图像本就以量子态形式存在(例如是上游某个量子流程的输出),从而无需付出经典态制备成本,且我们只关心边缘的某种聚合统计、而非逐像素重建时,QHED 才可能名副其实地占优。
读断言先读两端
评判任何"指数级"图像算法时,永远先问两端的成本:数据如何进、结果如何出。QHED 中段的那道 Hadamard 确实漂亮,但加速能否兑现,取决于态制备与读出是否被压在了线路代价之下。中间量子变换可以很浅,但端到端复杂度通常仍受态制备与读出主导。

算法逐步拆解

  1. 把图像像素强度归一化为一个单位范数的态矢量。
  2. 用 QPIE 把这些振幅制备到一个 n 比特态上(2n 个像素装入 n 比特)。
  3. 沿目标坐标轴对相应比特施加一道 Hadamard,相邻振幅由此变为其和与差。
  4. 测量;对采样结果做后处理,从邻像振幅之差中读出边缘。

对照代码理解 图像编码(QPIE)

请对照上文原理来读这一段 QPIE 编码:把归一化后的像素强度 ampsqalgora.amplitudes(q, amplitudes) 制备成态——这正是那一步可能代价高昂、足以吞掉加速的态制备。

import qalgora
import numpy as np

# normalize pixel intensities into a statevector
img = load_image_gray()                 # shape (2^n,)
amps = img / np.linalg.norm(img)

@qalgora.kernel
def encode(amplitudes: list[complex]):
    q = qalgora.qvector(int(np.log2(len(amplitudes))))
    qalgora.amplitudes(q, amplitudes)   # state preparation
规范接口 · 参考实现暂未包含
此示例展示的是 qalgora-Q 规范中的接口(或第三方库),开放参考实现目前尚未内置,仅用于说明预期用法;如需立即运行,请使用参考实现已支持的核心 API。

对照代码理解 Hadamard 边缘检测

这个内核同样先制备振幅,再对 q[0] 施加一道 h(q[0])——就是那道沿一个坐标轴算出梯度、把相邻振幅变为其和与差的单个 Hadamard;末尾的注释点明后处理:测量后从邻像振幅之差中读出边缘。

@qalgora.kernel
def qhed(amplitudes: list[complex]):
    q = qalgora.qvector(int(np.log2(len(amplitudes))))
    qalgora.amplitudes(q, amplitudes)
    h(q[0])                              # gradient along one axis
    mz(q)
# post-process: repeated sampling builds the probability distribution, then
# classically reconstruct the edge map from neighbouring amplitude differences
规范接口 · 参考实现暂未包含
此示例展示的是 qalgora-Q 规范中的接口(或第三方库),开放参考实现目前尚未内置,仅用于说明预期用法;如需立即运行,请使用参考实现已支持的核心 API。
动手试试
对一幅含大片平坦区域的图像做编码并运行 QHED:你会看到测量概率集中在边缘附近的比特串上,平坦区几乎不出现。再回想态制备与读出两端的代价,便能体会"中段加速"为何不等于"整体加速"。态制备页面讨论了载入振幅的真实成本。