Hadamard 操作

Hadamard 算子在寄存器上创建量子叠加态,是量子算法的基础操作。它们是制备均匀叠加态的标准方式(如在 QRAM 查询 之前);宽寄存器上全寄存器 Hadamard 的内存行为见 QFT 的性能说明。

概述

Hadamard 算子总览

算子

操作

类型约束

幺正类

Hadamard_Int

整数寄存器 Hadamard

整数类型

SelfAdjoint

Hadamard_Int_Full

完整 Hadamard(所有输出状态)

整数类型

SelfAdjoint

Hadamard_Bool

单量子比特 Hadamard

Boolean(size=1)

SelfAdjoint

Hadamard_Partial

部分量子比特 Hadamard

整数类型

SelfAdjoint

---

Hadamard_Int

class pysparq.Hadamard_Int

Apply Hadamard transform to an integer register.

Creates an equal superposition over all integer values from 0 to 2^n - 1 for the specified number of digits.

参数:
  • reg_in -- Name/ID of the input register.

  • n_digits -- Number of digits (qubits) to apply Hadamard to.

示例

Hadamard_Int("q", 4)(state) # Superpose q over 0..15

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

Clear all control conditions of the specified type.

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

Clear all control conditions of the specified type.

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

Clear all control conditions of the specified type.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

操作: 对整数寄存器的指定量子比特应用 Hadamard

数学定义:

\[H|x\rangle = \frac{1}{\sqrt{2}}(|0\rangle + |1\rangle) \quad \text{for each qubit}\]

类型约束: UnsignedInteger 或 SignedInteger

import pysparq as ps

ps.System.clear()

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

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

# 对前 2 比特应用 Hadamard
ps.Hadamard_Int("q", 2)(state)

print("\nHadamard 后:")
ps.pprint(state)
# [2 basis states]
# |q=0⟩ : (0.707+0j)
# |q=2⟩ : (0.707+0j)

---

Hadamard_Int_Full

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

Clear all control conditions of the specified type.

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

Clear all control conditions of the specified type.

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

Clear all control conditions of the specified type.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

操作: 对整个寄存器应用完整 Hadamard,创建所有 \(2^n\) 个状态的均匀叠加

数学定义:

\[H^n|x\rangle = \frac{1}{\sqrt{2^n}}\sum_{y=0}^{2^n-1}|y\rangle\]

类型约束: 整数类型

注意: 会创建 \(2^n\) 个基态,当 n 较大时可能导致内存问题。

ps.System.clear()

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

state = ps.SparseState()

# 完整 Hadamard:创建 2^2 = 4 个状态
ps.Hadamard_Int_Full("q")(state)

ps.pprint(state)
# [4 basis states]
# |q=0⟩ : (0.5+0j)
# |q=1⟩ : (0.5+0j)
# |q=2⟩ : (0.5+0j)
# |q=3⟩ : (0.5+0j)

# 再次应用 = 撤销(自伴)
ps.Hadamard_Int_Full("q")(state)
# 回到单基态

---

Hadamard_Bool

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

Clear all control conditions of the specified type.

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

Clear all control conditions of the specified type.

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

Clear all control conditions of the specified type.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

操作: 单量子比特 Hadamard

矩阵:

\[\begin{split}H = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}\end{split}\]

类型约束: ``Boolean``(寄存器大小必须为 1)

位约束: 无(默认操作第 0 位)

ps.System.clear()

# 单量子比特寄存器
ps.System.add_register("qubit", ps.Boolean, 1)  # 必须是 1 比特!

state = ps.SparseState()

ps.Hadamard_Bool("qubit")(state)

ps.pprint(state)
# [2 basis states]
# |qubit=0⟩ : (0.707+0j)
# |qubit=1⟩ : (0.707+0j)

---

Hadamard_Partial

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

Clear all control conditions of the specified type.

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

Clear all control conditions of the specified type.

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

Clear all control conditions of the specified type.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

操作: 对寄存器中的指定量子比特应用 Hadamard

类型约束: 整数类型

位约束: 指定位置必须在寄存器范围内

ps.System.clear()

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

state = ps.SparseState()

# 只对第 1 和第 3 位应用 Hadamard
positions = {1, 3}
ps.Hadamard_Partial("q", positions)(state)

# 创建 2^2 = 4 个叠加状态(仅翻转位置 1 和 3)

---

使用场景

均匀叠加态

用于量子搜索、量子采样等算法:

# 创建地址寄存器的均匀叠加
ps.System.add_register("addr", ps.UnsignedInteger, n)
state = ps.SparseState()
ps.Hadamard_Int_Full("addr")(state)

# 现在 addr 在所有可能值上均匀分布

单个量子比特初始化

用于初始化控制比特:

ps.System.add_register("ctrl", ps.Boolean, 1)
ps.Hadamard_Bool("ctrl")(state)

# ctrl 处于 |+⟩ = (|0⟩ + |1⟩)/√2 状态

部分叠加态

用于选择性叠加:

# 只叠加低 2 比特
ps.Hadamard_Int("reg", 2)(state)

# 或叠加特定位置
ps.Hadamard_Partial("reg", {0, 2})(state)