旋转与态制备

旋转与态制备算子提供任意维幺正旋转和从 |0⟩ 制备目标量子态的能力。基于它们构建的 QRAM 态制备见 算法库。

概述

旋转与态制备算子总览

算子

操作

幺正类

Rot_GeneralUnitary

作用任意维幺正矩阵

BaseOperator

Rot_GeneralStatePrep

从 |0⟩ 制备目标量子态

BaseOperator

---

Rot_GeneralUnitary(通用幺正旋转)

class pysparq.Rot_GeneralUnitary
clear_control_all_ones(self: pysparq._core.Rot_GeneralUnitary) → None

Clear all control conditions of the specified type.

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

Clear all control conditions of the specified type.

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

Clear all control conditions of the specified type.

clear_control_nonzeros(self: pysparq._core.Rot_GeneralUnitary) → 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.Rot_GeneralUnitary, cond: str) -> pysparq._core.Rot_GeneralUnitary

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.Rot_GeneralUnitary, conds: collections.abc.Sequence[str]) -> pysparq._core.Rot_GeneralUnitary

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.Rot_GeneralUnitary, cond: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.Rot_GeneralUnitary

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.Rot_GeneralUnitary, conds: collections.abc.Sequence[typing.SupportsInt | typing.SupportsIndex]) -> pysparq._core.Rot_GeneralUnitary

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.Rot_GeneralUnitary, cond: str, pos: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.Rot_GeneralUnitary

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.Rot_GeneralUnitary, conds: collections.abc.Sequence[tuple[str, typing.SupportsInt | typing.SupportsIndex]]) -> pysparq._core.Rot_GeneralUnitary

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.Rot_GeneralUnitary, cond: typing.SupportsInt | typing.SupportsIndex, pos: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.Rot_GeneralUnitary

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.Rot_GeneralUnitary, conds: collections.abc.Sequence[tuple[typing.SupportsInt | typing.SupportsIndex, typing.SupportsInt | typing.SupportsIndex]]) -> pysparq._core.Rot_GeneralUnitary

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.Rot_GeneralUnitary, cond: str) -> pysparq._core.Rot_GeneralUnitary

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.Rot_GeneralUnitary, conds: collections.abc.Sequence[str]) -> pysparq._core.Rot_GeneralUnitary

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.Rot_GeneralUnitary, cond: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.Rot_GeneralUnitary

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.Rot_GeneralUnitary, conds: collections.abc.Sequence[typing.SupportsInt | typing.SupportsIndex]) -> pysparq._core.Rot_GeneralUnitary

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.Rot_GeneralUnitary, cond: str, pos: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.Rot_GeneralUnitary

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.Rot_GeneralUnitary, conds: collections.abc.Sequence[tuple[str, typing.SupportsInt | typing.SupportsIndex]]) -> pysparq._core.Rot_GeneralUnitary

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.Rot_GeneralUnitary, cond: typing.SupportsInt | typing.SupportsIndex, pos: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.Rot_GeneralUnitary

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.Rot_GeneralUnitary, conds: collections.abc.Sequence[tuple[typing.SupportsInt | typing.SupportsIndex, typing.SupportsInt | typing.SupportsIndex]]) -> pysparq._core.Rot_GeneralUnitary

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.

操作: 对寄存器作用一个任意 \(2^n \times 2^n\) 幺正矩阵。

参数:

  • reg — 目标寄存器(名称或 ID)

  • matrix — 幺正矩阵(DenseMatrix_complex 或 NumPy 数组)

Dagger: 作用矩阵的共轭转置。

用途: 当标准门库无法直接表达所需的变换时,可以通过矩阵形式直接指定。

备注

矩阵维度必须与寄存器的希尔伯特空间维度 \(2^n\) 匹配,其中 \(n\) 为寄存器比特数。

import numpy as np
import pysparq as ps

ps.System.clear()
ps.System.add_register("q", ps.UnsignedInteger, 2)

state = ps.SparseState()

# 定义 4x4 幺正矩阵
matrix = np.eye(4, dtype=complex)
matrix[0, 0] = 0
matrix[0, 3] = 1
matrix[3, 3] = 0
matrix[3, 0] = 1

op = ps.Rot_GeneralUnitary("q", matrix)
op(state)

# 撤销
op.dag(state)

---

Rot_GeneralStatePrep(量子态制备)

class pysparq.Rot_GeneralStatePrep
clear_control_all_ones(self: pysparq._core.Rot_GeneralStatePrep) → None

Clear all control conditions of the specified type.

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

Clear all control conditions of the specified type.

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

Clear all control conditions of the specified type.

clear_control_nonzeros(self: pysparq._core.Rot_GeneralStatePrep) → 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.Rot_GeneralStatePrep, cond: str) -> pysparq._core.Rot_GeneralStatePrep

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.Rot_GeneralStatePrep, conds: collections.abc.Sequence[str]) -> pysparq._core.Rot_GeneralStatePrep

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.Rot_GeneralStatePrep, cond: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.Rot_GeneralStatePrep

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.Rot_GeneralStatePrep, conds: collections.abc.Sequence[typing.SupportsInt | typing.SupportsIndex]) -> pysparq._core.Rot_GeneralStatePrep

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.Rot_GeneralStatePrep, cond: str, pos: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.Rot_GeneralStatePrep

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.Rot_GeneralStatePrep, conds: collections.abc.Sequence[tuple[str, typing.SupportsInt | typing.SupportsIndex]]) -> pysparq._core.Rot_GeneralStatePrep

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.Rot_GeneralStatePrep, cond: typing.SupportsInt | typing.SupportsIndex, pos: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.Rot_GeneralStatePrep

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.Rot_GeneralStatePrep, conds: collections.abc.Sequence[tuple[typing.SupportsInt | typing.SupportsIndex, typing.SupportsInt | typing.SupportsIndex]]) -> pysparq._core.Rot_GeneralStatePrep

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.Rot_GeneralStatePrep, cond: str) -> pysparq._core.Rot_GeneralStatePrep

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.Rot_GeneralStatePrep, conds: collections.abc.Sequence[str]) -> pysparq._core.Rot_GeneralStatePrep

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.Rot_GeneralStatePrep, cond: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.Rot_GeneralStatePrep

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.Rot_GeneralStatePrep, conds: collections.abc.Sequence[typing.SupportsInt | typing.SupportsIndex]) -> pysparq._core.Rot_GeneralStatePrep

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.Rot_GeneralStatePrep, cond: str, pos: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.Rot_GeneralStatePrep

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.Rot_GeneralStatePrep, conds: collections.abc.Sequence[tuple[str, typing.SupportsInt | typing.SupportsIndex]]) -> pysparq._core.Rot_GeneralStatePrep

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.Rot_GeneralStatePrep, cond: typing.SupportsInt | typing.SupportsIndex, pos: typing.SupportsInt | typing.SupportsIndex) -> pysparq._core.Rot_GeneralStatePrep

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.Rot_GeneralStatePrep, conds: collections.abc.Sequence[tuple[typing.SupportsInt | typing.SupportsIndex, typing.SupportsInt | typing.SupportsIndex]]) -> pysparq._core.Rot_GeneralStatePrep

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.

操作: 将寄存器从 |0⟩ 态制备为目标量子态。

参数:

  • reg — 目标寄存器(名称或 ID)

  • state_vector — 目标态矢量(复数列表或 NumPy 数组)

Dagger: 将目标态映射回 |0⟩。

数学表示: 给定目标态 \(|\psi\rangle = \sum_i \alpha_i |i\rangle\),构造幺正 \(U\) 使得 \(U|0\rangle = |\psi\rangle\)。

警告

该算子要求寄存器当前处于 |0⟩ 态。如果寄存器已有非零值,行为未定义。

import numpy as np
import pysparq as ps

ps.System.clear()
ps.System.add_register("q", ps.UnsignedInteger, 2)

state = ps.SparseState()

# 制备 Bell 态的简化版:均匀叠加
target = np.array([0.5, 0.5, 0.5, 0.5], dtype=complex)

op = ps.Rot_GeneralStatePrep("q", target)
op(state)

ps.pprint(state)
# |q=0⟩ : (0.5+0j)
# |q=1⟩ : (0.5+0j)
# |q=2⟩ : (0.5+0j)
# |q=3⟩ : (0.5+0j)

辅助函数

stateprep_unitary_build_schmidt

pysparq.stateprep_unitary_build_schmidt(state_vector: collections.abc.Sequence[SupportsComplex | SupportsFloat | SupportsIndex]) → pysparq._core.DenseMatrix_complex

Build unitary for state preparation

操作: 使用 Schmidt 分解构造态制备所需的幺正矩阵。

用途: 当需要手动构建或检查 Rot_GeneralStatePrep 的内部矩阵时使用。