PySparQ.pysparq.dynamic_operator.operator_wrapper

动态算子包装模块

使用 ctypes 调用动态库中的 C++ 算子,创建 Python 代理类。

Attributes

Exceptions

DynamicOperatorError

动态算子错误

DynamicOperatorFactoryError

工厂函数调用错误

DynamicOperatorLoadError

动态库加载错误

Classes

CppOperatorWrapper

C++ 算子包装器

Functions

cleanup_all_instances()

清理所有活跃的动态算子实例

create_operator_class(→ Type)

创建动态算子 Python 类

Module Contents

exception PySparQ.pysparq.dynamic_operator.operator_wrapper.DynamicOperatorError[源代码]

Bases: Exception

动态算子错误

Initialize self. See help(type(self)) for accurate signature.

exception PySparQ.pysparq.dynamic_operator.operator_wrapper.DynamicOperatorFactoryError[源代码]

Bases: DynamicOperatorError

工厂函数调用错误

Initialize self. See help(type(self)) for accurate signature.

exception PySparQ.pysparq.dynamic_operator.operator_wrapper.DynamicOperatorLoadError[源代码]

Bases: DynamicOperatorError

动态库加载错误

Initialize self. See help(type(self)) for accurate signature.

class PySparQ.pysparq.dynamic_operator.operator_wrapper.CppOperatorWrapper(lib_path: str)[源代码]

C++ 算子包装器

负责加载动态库、调用工厂函数创建/销毁 C++ 算子对象

初始化包装器

参数:

lib_path -- 动态库路径

apply(ptr: int, state_cpp_ptr: int)[源代码]

应用算子到 SparseState

参数:
  • ptr -- 算子对象地址

  • state_cpp_ptr -- C++ SparseState* 指针(通过 state._cpp_ptr() 获取)

apply_dag(ptr: int, state_cpp_ptr: int)[源代码]

应用 dagger 到 SparseState

参数:
  • ptr -- 算子对象地址

  • state_cpp_ptr -- C++ SparseState* 指针(通过 state._cpp_ptr() 获取)

close()[源代码]

关闭动态库并释放资源

注意:在 Windows 上,必须确保所有 C++ 对象都已销毁 才能成功删除动态库文件

create(*args) int[源代码]

创建 C++ 算子实例

参数:

*args -- 构造函数参数

返回:

C++ 对象地址(作为 Python int)

destroy(ptr: int)[源代码]

销毁 C++ 算子实例

参数:

ptr -- C++ 对象地址

get_base_class() str[源代码]

获取基类名称

get_name() str[源代码]

获取算子名称

load(arg_types: List[str] = None)[源代码]

加载动态库

参数:

arg_types -- 构造函数参数类型列表

抛出:

DynamicOperatorLoadError -- 加载失败

lib_path[源代码]
PySparQ.pysparq.dynamic_operator.operator_wrapper.cleanup_all_instances()[源代码]

清理所有活跃的动态算子实例

PySparQ.pysparq.dynamic_operator.operator_wrapper.create_operator_class(name: str, lib_path: str, base_class: str = 'BaseOperator', constructor_args: List[Tuple[str, str]] = None) Type[源代码]

创建动态算子 Python 类

参数:
  • name -- 算子类名

  • lib_path -- 动态库路径

  • base_class -- 基类名 ("BaseOperator" 或 "SelfAdjointOperator")

  • constructor_args -- 构造函数参数列表 [(type, name), ...]

返回:

动态创建的算子类

PySparQ.pysparq.dynamic_operator.operator_wrapper.SparseState = None[源代码]