33
44namespace qmp_hamiltonian {
55
6- // The `prepare` function is responsible for parsing a raw Python dictionary representing Hamiltonian terms
7- // and transforming it into a structured tuple of tensors. This tuple is then stored on the Python side
8- // and utilized in subsequent calls to the PyTorch operators for further processing.
6+ // `prepare` 函数负责解析代表哈密顿量项的原始 Python 字典,
7+ // 并将其转换为结构化的张量元组。该元组随后存储在 Python 端,
8+ // 并用于后续对 PyTorch 算子的调用中。
99//
10- // The function takes a Python dictionary `hamiltonian` as input, where each key-value pair represents a term
11- // in the Hamiltonian. The key is a tuple of tuples, where each inner tuple contains two elements:
12- // - The first element is an integer representing the site index of the operator.
13- // - The second element is an integer representing the type of operator (0 for annihilation, 1 for creation).
14- // The value is either a float or a complex number representing the coefficient of the term.
10+ // 该函数接收一个 Python 字典 `hamiltonian` 作为输入,其中每个键值对代表哈密顿量中的一项。
11+ // 键是一个由元组组成的元组,其中每个内部元组包含两个元素:
12+ // - 第一个元素是一个整数,代表算符的格点索引( site index)。
13+ // - 第二个元素是一个整数,代表算符的类型(0 为湮灭算符,1 为产生算符)。
14+ // 值是浮点实数或浮点复数,代表该项的系数。
1515//
16- // The function processes the dictionary and constructs three tensors:
17- // - `site`: An int16 tensor of shape [term_number, max_op_number], representing the site indices of the operators for
18- // each term.
19- // - `kind`: An uint8 tensor of shape [term_number, max_op_number], representing the type of operator for each term.
20- // The value are encoded as follows:
21- // - 0: Annihilation operator
22- // - 1: Creation operator
23- // - 2: Empty (identity operator)
24- // - `coef`: A float64 tensor of shape [term_number, 2], representing the coefficients of each term, with two elements
25- // for real and imaginary parts.
16+ // 该函数处理字典并构建三个张量:
17+ // - `site`: 一个形状为 [term_number, max_op_number] 的 int16 张量,代表每项中算符的格点索引。
18+ // - `kind`: 一个形状为 [term_number, max_op_number] 的 uint8 张量,代表每项中算符的类型。
19+ // 值编码如下:
20+ // - 0: 湮灭算符
21+ // - 1: 产生算符
22+ // - 2: 空(单位算符)
23+ // - `coef`: 一个形状为 [term_number, 2] 的 float64 张量,代表每项的系数,包含实部和虚部两个元素。
2624//
27- // The `max_op_number` template argument specifies the maximum number of operators per term, typically set to 4 for
28- // 2-body interactions.
25+ // `max_op_number` 模板参数指定了每项中算符的最大数量,对于二体相互作用通常设置为 4。
2926template <std::int64_t max_op_number>
3027auto prepare (py::dict hamiltonian) {
3128 std::int64_t term_number = hamiltonian.size ();
3229
3330 auto site = torch::empty ({term_number, max_op_number}, torch::TensorOptions ().dtype (torch::kInt16 ).device (torch::kCPU ));
34- // No need to initialize
31+ // 无需初始化
3532 auto kind = torch::full ({term_number, max_op_number}, 2 , torch::TensorOptions ().dtype (torch::kUInt8 ).device (torch::kCPU ));
36- // Initialize to 2 for identity as default
33+ // 默认初始化为 2(单位算符)
3734 auto coef = torch::empty ({term_number, 2 }, torch::TensorOptions ().dtype (torch::kFloat64 ).device (torch::kCPU ));
38- // No need to initialize
35+ // 无需初始化
3936
4037 auto site_accessor = site.accessor <std::int16_t , 2 >();
4138 auto kind_accessor = kind.accessor <std::uint8_t , 2 >();
@@ -71,7 +68,7 @@ auto prepare(py::dict hamiltonian) {
7168#endif
7269
7370#if N_QUBYTES == 0
74- // Expose the `prepare` function to Python.
71+ // 将 `prepare` 函数暴露给 Python。
7572PYBIND11_MODULE (qmp_hamiltonian, m) {
7673 m.def (" prepare" , prepare</* max_op_number=*/ 4 >, py::arg (" hamiltonian" ));
7774}
@@ -82,7 +79,8 @@ PYBIND11_MODULE(qmp_hamiltonian, m) {
8279#define QMP_LIBRARY (x, y ) QMP_LIBRARY_HELPER(x, y)
8380TORCH_LIBRARY_FRAGMENT (QMP_LIBRARY(N_QUBYTES, PARTICLE_CUT), m) {
8481 m.def (" apply_within(Tensor configs_i, Tensor psi_i, Tensor configs_j, Tensor site, Tensor kind, Tensor coef) -> Tensor" );
85- m.def (" find_relative(Tensor configs_i, Tensor psi_i, int count_selected, Tensor site, Tensor kind, Tensor coef, Tensor configs_exclude) -> Tensor"
82+ m.def (
83+ " find_relative(Tensor configs_i, Tensor psi_i, int count_selected, Tensor site, Tensor kind, Tensor coef, Tensor configs_exclude) -> Tensor"
8684 );
8785 m.def (" diagonal_term(Tensor configs, Tensor site, Tensor kind, Tensor coef) -> Tensor" );
8886}
0 commit comments