量子傅里叶变换 (QFT)

量子傅里叶变换是众多量子算法的核心组件——如相位估计与 Shor 分解——算法库 中有现成实现。

概述

QFT 算子总览

算子

操作

幺正类

QFT

量子傅里叶变换

BaseOperator

InverseQFT

逆量子傅里叶变换

BaseOperator

数学定义

量子傅里叶变换定义:

\[QFT|x\rangle = \frac{1}{\sqrt{2^n}} \sum_{y=0}^{2^n-1} e^{2\pi i xy / 2^n} |y\rangle\]

逆量子傅里叶变换:

\[QFT^\dagger|y\rangle = \frac{1}{\sqrt{2^n}} \sum_{x=0}^{2^n-1} e^{-2\pi i xy / 2^n} |x\rangle\]

---

QFT

class pysparq.QFT

Quantum Fourier Transform on a register.

Applies the QFT to transform between computational and Fourier bases. Commonly used in phase estimation and Shor's algorithm.

参数:

reg_name -- Name of the register to transform (str) or register ID (int).

示例

QFT("data")(state) # Apply QFT # ... computation ... InverseQFT("data")(state) # Apply inverse QFT

clear_control_all_ones(self: pysparq._core.QFT) → None

Clear all control conditions of the specified type.

clear_control_by_bit(self: pysparq._core.QFT) → None

Clear all control conditions of the specified type.

clear_control_by_value(self: pysparq._core.QFT) → None

Clear all control conditions of the specified type.

clear_control_nonzeros(self: pysparq._core.QFT) → None

Clear all control conditions of the specified type.

property condition_variable_all_ones
property condition_variable_by_bit
property condition_variable_by_value
property condition_variable_nonzeros
conditioned_by_all_ones(*args, **kwargs)

Overloaded function.

  1. conditioned_by_all_ones(self: pysparq._core.QFT, cond: str) -> pysparq._core.QFT

Condition this operation on registers where all bits are 1.

Calling this method replaces prior all-ones conditions. Pass the list overload to require several registers simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int) to condition on.

  • conds -- List of register names or IDs for multi-condition.

返回:

Self, for method chaining.

  1. conditioned_by_all_ones(self: pysparq._core.QFT, conds: collections.abc.Sequence[str]) -> pysparq._core.QFT

Condition this operation on registers where all bits are 1.

Calling this method replaces prior all-ones conditions. Pass the list overload to require several registers simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int) to condition on.

  • conds -- List of register names or IDs for multi-condition.

返回:

Self, for method chaining.

  1. conditioned_by_all_ones(self: pysparq._core.QFT, cond: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.QFT

Condition this operation on registers where all bits are 1.

Calling this method replaces prior all-ones conditions. Pass the list overload to require several registers simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int) to condition on.

  • conds -- List of register names or IDs for multi-condition.

返回:

Self, for method chaining.

  1. conditioned_by_all_ones(self: pysparq._core.QFT, conds: collections.abc.Sequence[typing.SupportsInt | typing.SupportsIndex]) -> pysparq._core.QFT

Condition this operation on registers where all bits are 1.

Calling this method replaces prior all-ones conditions. Pass the list overload to require several registers simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int) to condition on.

  • conds -- List of register names or IDs for multi-condition.

返回:

Self, for method chaining.

conditioned_by_bit(*args, **kwargs)

Overloaded function.

  1. conditioned_by_bit(self: pysparq._core.QFT, cond: str, pos: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.QFT

Condition this operation on a specific bit position.

Calling this method replaces prior bit conditions. Pass the list-of-pairs overload to require several bits simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int).

  • pos -- Bit position to check (0-indexed).

  • conds -- List of (register, position) pairs.

返回:

Self, for method chaining.

  1. conditioned_by_bit(self: pysparq._core.QFT, conds: collections.abc.Sequence[tuple[str, typing.SupportsInt | typing.SupportsIndex]]) -> pysparq._core.QFT

Condition this operation on a specific bit position.

Calling this method replaces prior bit conditions. Pass the list-of-pairs overload to require several bits simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int).

  • pos -- Bit position to check (0-indexed).

  • conds -- List of (register, position) pairs.

返回:

Self, for method chaining.

  1. conditioned_by_bit(self: pysparq._core.QFT, cond: typing.SupportsInt | typing.SupportsIndex, pos: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.QFT

Condition this operation on a specific bit position.

Calling this method replaces prior bit conditions. Pass the list-of-pairs overload to require several bits simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int).

  • pos -- Bit position to check (0-indexed).

  • conds -- List of (register, position) pairs.

返回:

Self, for method chaining.

  1. conditioned_by_bit(self: pysparq._core.QFT, conds: collections.abc.Sequence[tuple[typing.SupportsInt | typing.SupportsIndex, typing.SupportsInt | typing.SupportsIndex]]) -> pysparq._core.QFT

Condition this operation on a specific bit position.

Calling this method replaces prior bit conditions. Pass the list-of-pairs overload to require several bits simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int).

  • pos -- Bit position to check (0-indexed).

  • conds -- List of (register, position) pairs.

返回:

Self, for method chaining.

conditioned_by_nonzeros(*args, **kwargs)

Overloaded function.

  1. conditioned_by_nonzeros(self: pysparq._core.QFT, cond: str) -> pysparq._core.QFT

Condition this operation on registers with nonzero values.

Calling this method replaces prior nonzero-register conditions. Pass the list overload to require several registers simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int) to condition on.

  • conds -- List of register names or IDs for multi-condition.

返回:

Self, for method chaining.

示例

op.conditioned_by_nonzeros('control_reg')(state)

  1. conditioned_by_nonzeros(self: pysparq._core.QFT, conds: collections.abc.Sequence[str]) -> pysparq._core.QFT

Condition this operation on registers with nonzero values.

Calling this method replaces prior nonzero-register conditions. Pass the list overload to require several registers simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int) to condition on.

  • conds -- List of register names or IDs for multi-condition.

返回:

Self, for method chaining.

示例

op.conditioned_by_nonzeros('control_reg')(state)

  1. conditioned_by_nonzeros(self: pysparq._core.QFT, cond: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.QFT

Condition this operation on registers with nonzero values.

Calling this method replaces prior nonzero-register conditions. Pass the list overload to require several registers simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int) to condition on.

  • conds -- List of register names or IDs for multi-condition.

返回:

Self, for method chaining.

示例

op.conditioned_by_nonzeros('control_reg')(state)

  1. conditioned_by_nonzeros(self: pysparq._core.QFT, conds: collections.abc.Sequence[typing.SupportsInt | typing.SupportsIndex]) -> pysparq._core.QFT

Condition this operation on registers with nonzero values.

Calling this method replaces prior nonzero-register conditions. Pass the list overload to require several registers simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int) to condition on.

  • conds -- List of register names or IDs for multi-condition.

返回:

Self, for method chaining.

示例

op.conditioned_by_nonzeros('control_reg')(state)

conditioned_by_value(*args, **kwargs)

Overloaded function.

  1. conditioned_by_value(self: pysparq._core.QFT, cond: str, pos: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.QFT

Condition this operation on registers holding a specific value.

Calling this method replaces prior value conditions. Pass the list-of-pairs overload to require several values simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int).

  • pos -- Value to match.

  • conds -- List of (register, value) pairs.

返回:

Self, for method chaining.

  1. conditioned_by_value(self: pysparq._core.QFT, conds: collections.abc.Sequence[tuple[str, typing.SupportsInt | typing.SupportsIndex]]) -> pysparq._core.QFT

Condition this operation on registers holding a specific value.

Calling this method replaces prior value conditions. Pass the list-of-pairs overload to require several values simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int).

  • pos -- Value to match.

  • conds -- List of (register, value) pairs.

返回:

Self, for method chaining.

  1. conditioned_by_value(self: pysparq._core.QFT, cond: typing.SupportsInt | typing.SupportsIndex, pos: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.QFT

Condition this operation on registers holding a specific value.

Calling this method replaces prior value conditions. Pass the list-of-pairs overload to require several values simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int).

  • pos -- Value to match.

  • conds -- List of (register, value) pairs.

返回:

Self, for method chaining.

  1. conditioned_by_value(self: pysparq._core.QFT, conds: collections.abc.Sequence[tuple[typing.SupportsInt | typing.SupportsIndex, typing.SupportsInt | typing.SupportsIndex]]) -> pysparq._core.QFT

Condition this operation on registers holding a specific value.

Calling this method replaces prior value conditions. Pass the list-of-pairs overload to require several values simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int).

  • pos -- Value to match.

  • conds -- List of (register, value) pairs.

返回:

Self, for method chaining.

dag(self: pysparq._core.QFT, state: pysparq._core.SparseState) → None

Apply the adjoint (inverse) of this operation.

参数:

state -- The quantum state to operate on.

Note: Only available for self-adjoint operators.

操作: 对整数寄存器执行量子傅里叶变换

类型约束: UnsignedInteger 或 SignedInteger

位约束: 寄存器大小决定变换维度

import pysparq as ps

ps.System.clear()

# 4 比特寄存器
ps.System.add_register("q", ps.UnsignedInteger, 4)

state = ps.SparseState()

# 初始化为基态 |1⟩
ps.Init_Unsafe("q", 1)(state)

print("初始状态:")
ps.pprint(state)
# |q=1⟩ : (1+0j)

# 应用 QFT
op = ps.QFT("q")
op(state)

print("\nQFT 后:")
ps.pprint(state)
# 产生相位均匀分布的叠加态

# 撤销(逆变换)
op.dag(state)

InverseQFT

class pysparq.InverseQFT

Inverse Quantum Fourier Transform on a register.

Applies the inverse QFT to transform from Fourier basis back to computational basis.

参数:

reg_name -- Name of the register (str) or register ID (int).

clear_control_all_ones(self: pysparq._core.InverseQFT) → None

Clear all control conditions of the specified type.

clear_control_by_bit(self: pysparq._core.InverseQFT) → None

Clear all control conditions of the specified type.

clear_control_by_value(self: pysparq._core.InverseQFT) → None

Clear all control conditions of the specified type.

clear_control_nonzeros(self: pysparq._core.InverseQFT) → None

Clear all control conditions of the specified type.

property condition_variable_all_ones
property condition_variable_by_bit
property condition_variable_by_value
property condition_variable_nonzeros
conditioned_by_all_ones(*args, **kwargs)

Overloaded function.

  1. conditioned_by_all_ones(self: pysparq._core.InverseQFT, cond: str) -> pysparq._core.InverseQFT

Condition this operation on registers where all bits are 1.

Calling this method replaces prior all-ones conditions. Pass the list overload to require several registers simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int) to condition on.

  • conds -- List of register names or IDs for multi-condition.

返回:

Self, for method chaining.

  1. conditioned_by_all_ones(self: pysparq._core.InverseQFT, conds: collections.abc.Sequence[str]) -> pysparq._core.InverseQFT

Condition this operation on registers where all bits are 1.

Calling this method replaces prior all-ones conditions. Pass the list overload to require several registers simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int) to condition on.

  • conds -- List of register names or IDs for multi-condition.

返回:

Self, for method chaining.

  1. conditioned_by_all_ones(self: pysparq._core.InverseQFT, cond: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.InverseQFT

Condition this operation on registers where all bits are 1.

Calling this method replaces prior all-ones conditions. Pass the list overload to require several registers simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int) to condition on.

  • conds -- List of register names or IDs for multi-condition.

返回:

Self, for method chaining.

  1. conditioned_by_all_ones(self: pysparq._core.InverseQFT, conds: collections.abc.Sequence[typing.SupportsInt | typing.SupportsIndex]) -> pysparq._core.InverseQFT

Condition this operation on registers where all bits are 1.

Calling this method replaces prior all-ones conditions. Pass the list overload to require several registers simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int) to condition on.

  • conds -- List of register names or IDs for multi-condition.

返回:

Self, for method chaining.

conditioned_by_bit(*args, **kwargs)

Overloaded function.

  1. conditioned_by_bit(self: pysparq._core.InverseQFT, cond: str, pos: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.InverseQFT

Condition this operation on a specific bit position.

Calling this method replaces prior bit conditions. Pass the list-of-pairs overload to require several bits simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int).

  • pos -- Bit position to check (0-indexed).

  • conds -- List of (register, position) pairs.

返回:

Self, for method chaining.

  1. conditioned_by_bit(self: pysparq._core.InverseQFT, conds: collections.abc.Sequence[tuple[str, typing.SupportsInt | typing.SupportsIndex]]) -> pysparq._core.InverseQFT

Condition this operation on a specific bit position.

Calling this method replaces prior bit conditions. Pass the list-of-pairs overload to require several bits simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int).

  • pos -- Bit position to check (0-indexed).

  • conds -- List of (register, position) pairs.

返回:

Self, for method chaining.

  1. conditioned_by_bit(self: pysparq._core.InverseQFT, cond: typing.SupportsInt | typing.SupportsIndex, pos: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.InverseQFT

Condition this operation on a specific bit position.

Calling this method replaces prior bit conditions. Pass the list-of-pairs overload to require several bits simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int).

  • pos -- Bit position to check (0-indexed).

  • conds -- List of (register, position) pairs.

返回:

Self, for method chaining.

  1. conditioned_by_bit(self: pysparq._core.InverseQFT, conds: collections.abc.Sequence[tuple[typing.SupportsInt | typing.SupportsIndex, typing.SupportsInt | typing.SupportsIndex]]) -> pysparq._core.InverseQFT

Condition this operation on a specific bit position.

Calling this method replaces prior bit conditions. Pass the list-of-pairs overload to require several bits simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int).

  • pos -- Bit position to check (0-indexed).

  • conds -- List of (register, position) pairs.

返回:

Self, for method chaining.

conditioned_by_nonzeros(*args, **kwargs)

Overloaded function.

  1. conditioned_by_nonzeros(self: pysparq._core.InverseQFT, cond: str) -> pysparq._core.InverseQFT

Condition this operation on registers with nonzero values.

Calling this method replaces prior nonzero-register conditions. Pass the list overload to require several registers simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int) to condition on.

  • conds -- List of register names or IDs for multi-condition.

返回:

Self, for method chaining.

示例

op.conditioned_by_nonzeros('control_reg')(state)

  1. conditioned_by_nonzeros(self: pysparq._core.InverseQFT, conds: collections.abc.Sequence[str]) -> pysparq._core.InverseQFT

Condition this operation on registers with nonzero values.

Calling this method replaces prior nonzero-register conditions. Pass the list overload to require several registers simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int) to condition on.

  • conds -- List of register names or IDs for multi-condition.

返回:

Self, for method chaining.

示例

op.conditioned_by_nonzeros('control_reg')(state)

  1. conditioned_by_nonzeros(self: pysparq._core.InverseQFT, cond: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.InverseQFT

Condition this operation on registers with nonzero values.

Calling this method replaces prior nonzero-register conditions. Pass the list overload to require several registers simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int) to condition on.

  • conds -- List of register names or IDs for multi-condition.

返回:

Self, for method chaining.

示例

op.conditioned_by_nonzeros('control_reg')(state)

  1. conditioned_by_nonzeros(self: pysparq._core.InverseQFT, conds: collections.abc.Sequence[typing.SupportsInt | typing.SupportsIndex]) -> pysparq._core.InverseQFT

Condition this operation on registers with nonzero values.

Calling this method replaces prior nonzero-register conditions. Pass the list overload to require several registers simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int) to condition on.

  • conds -- List of register names or IDs for multi-condition.

返回:

Self, for method chaining.

示例

op.conditioned_by_nonzeros('control_reg')(state)

conditioned_by_value(*args, **kwargs)

Overloaded function.

  1. conditioned_by_value(self: pysparq._core.InverseQFT, cond: str, pos: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.InverseQFT

Condition this operation on registers holding a specific value.

Calling this method replaces prior value conditions. Pass the list-of-pairs overload to require several values simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int).

  • pos -- Value to match.

  • conds -- List of (register, value) pairs.

返回:

Self, for method chaining.

  1. conditioned_by_value(self: pysparq._core.InverseQFT, conds: collections.abc.Sequence[tuple[str, typing.SupportsInt | typing.SupportsIndex]]) -> pysparq._core.InverseQFT

Condition this operation on registers holding a specific value.

Calling this method replaces prior value conditions. Pass the list-of-pairs overload to require several values simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int).

  • pos -- Value to match.

  • conds -- List of (register, value) pairs.

返回:

Self, for method chaining.

  1. conditioned_by_value(self: pysparq._core.InverseQFT, cond: typing.SupportsInt | typing.SupportsIndex, pos: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.InverseQFT

Condition this operation on registers holding a specific value.

Calling this method replaces prior value conditions. Pass the list-of-pairs overload to require several values simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int).

  • pos -- Value to match.

  • conds -- List of (register, value) pairs.

返回:

Self, for method chaining.

  1. conditioned_by_value(self: pysparq._core.InverseQFT, conds: collections.abc.Sequence[tuple[typing.SupportsInt | typing.SupportsIndex, typing.SupportsInt | typing.SupportsIndex]]) -> pysparq._core.InverseQFT

Condition this operation on registers holding a specific value.

Calling this method replaces prior value conditions. Pass the list-of-pairs overload to require several values simultaneously; conditions of different kinds are combined with logical AND.

参数:
  • cond -- Register name (str) or ID (int).

  • pos -- Value to match.

  • conds -- List of (register, value) pairs.

返回:

Self, for method chaining.

操作: 对整数寄存器执行逆量子傅里叶变换

用途: 撤销 QFT 或解析相位信息

ps.System.clear()

ps.System.add_register("q", ps.UnsignedInteger, 4)

state = ps.SparseState()

# 创建叠加态
ps.Hadamard_Int_Full("q")(state)

# 应用逆 QFT
ps.InverseQFT("q")(state)

# 逆 QFT 和 QFT 互为逆运算
ps.QFT("q")(state)

---

使用场景

周期发现(Shor 算法)

import pysparq as ps
import numpy as np

ps.System.clear()

# 相位寄存器
n_bits = 8
ps.System.add_register("phase", ps.UnsignedInteger, n_bits)

state = ps.SparseState()

# 模拟相位估计结果(假设周期 r = 5)
# |phase⟩ 包含周期性相位信息
ps.Hadamard_Int_Full("phase")(state)

# 应用逆 QFT 解析周期
ps.InverseQFT("phase")(state)

# 观测相位寄存器,确定周期
# ...

相位估计

# 假设已有相位估计电路
# 逆 QFT 将相位信息转换到计算基

# 1. 制备初态
ps.Hadamard_Int_Full("estimate")(state)

# 2. 执行受控幺正操作
# controlled_U(...)(state)

# 3. 逆 QFT 提取相位
ps.InverseQFT("estimate")(state)

# 4. 测量得到相位估计
# ...

量子信号处理

# QFT 用于频域分析
ps.System.add_register("signal", ps.UnsignedInteger, 16)

state = ps.SparseState()
# 加载信号数据...

# 变换到频域
ps.QFT("signal")(state)

# 频域处理...

# 变换回时域
ps.InverseQFT("signal")(state)

性能考虑

  • 基态数量: QFT 作用于叠加态时,会产生 \(2^n\) 个基态

  • 内存限制: 当 n 较大时(如 n > 20),可能导致内存问题

  • 稀疏优化: PySparQ 只存储非零振幅基态,对于某些输入可能高效

备注

对于非完整的叠加态,QFT 的稀疏实现可能比全态模拟更高效。

实际示例

完整 QFT + InverseQFT 循环

import pysparq as ps
import numpy as np

ps.System.clear()

n = 3
ps.System.add_register("q", ps.UnsignedInteger, n)

state = ps.SparseState()

# 初始值
initial_value = 5
ps.Init_Unsafe("q", initial_value)(state)

print("初始状态:")
ps.pprint(state)

# QFT
ps.QFT("q")(state)
print("\nQFT 后:")
ps.pprint(state)

# InverseQFT
ps.InverseQFT("q")(state)
print("\nInverseQFT 后:")
ps.pprint(state)
# 恢复到 |q=5⟩