Grouping by Order
Overview
This module contains a collection of utility functions that play a crucial role in the symbolic manipulation of quantum operators within the SymPT framework. These functions are used to determine the “order” of various factors (operators, numeric values, functions, etc.) and to group terms in expressions or matrices based on their perturbative order.
The two primary utilities provided are:
get_order
: Determines the order and classification (e.g., 'finite', 'infinite', or 'other') of a given factor.group_by_order
: Organizes the terms of an expression or the entries of a matrix into groups based on their computed orders.
Note:
Some functions are implemented using multimethod overloading, allowing them to behave differently depending on the type of the input (e.g.,RDOperator
,BosonOp
, numeric types, trigonometric functions, etc.).
get_order(factor)
The get_order
function is overloaded (using the @multimethod
decorator) to handle different types of inputs. Its purpose is to compute the perturbative order of an operator factor and return a tuple containing the order and a classification.
Parameters
factor
: The input to evaluate, which can be one of the following types:RDOperator
: Returns order0
with classification('finite', None)
.BosonOp
: Returns order0
with classification('infinite', key)
, wherekey
is the number operator for the given bosonic subspace.exp
,cos
,sin
: Computes order from the function’s argument.- Numeric types (
int
,float
,complex
,Integer
, etc.): Always returns(0, ('other', None))
. Pow
: Computes order as the base's order multiplied by the exponent.conjugate
: Returns the same order and classification as its inner argument.Symbol
: If anRDSymbol
, returns(RDSymbol.order, ('other', None))
. Otherwise, defaults to(0, ('other', None))
.
Returns
A tuple (order, classification)
, where order
is a float
and classification
provides additional context based on the factor's type. The classification is a tuple containing a string identifying the factor type ('finite'
, 'infinite'
, or 'other'
) and an optional key used for infinite operators.
group_by_order(expr)
Organizes terms of a matrix or an expression into groups based on their perturbative order.
Parameters
expr
: The input expression or matrix to evaluate, which can be:
Returns
A dictionary whose keys are the orders of the terms in the input expression or matrix. The values are lists of dictionaries encoding information about the factors of each term of expr
. The dictionaries then contains keys such as 'other'
, 'finite'
, and 'infinite
to categorize the factors.
Example Usage
Below is an example demonstrating how to use the get_order
and group_by_order
functions:
from sympy import Matrix, sin, cos, Pow, symbols, Add, Mul
from sympt import get_order, group_by_order, RDOperator, BosonOp
import numpy as np
# Example symbols and operators
x = symbols('x')
my_rd_operator = RDOperator('A', Matrix([[1, 0], [0, 1]]), 'spin')
my_boson_op = BosonOp("b")
# Get order for different factors
order1, cls1 = get_order(my_rd_operator) # Typically (0, ('finite', None))
order2, cls2 = get_order(my_boson_op) # Typically (0, ('infinite', key))
order3, cls3 = get_order(sin(x)) # Returns order based on the argument
# Group terms in a symbolic expression by order.
expr = Add(Pow(my_rd_operator, 1)*sin(x), Pow(my_rd_operator, 2)*cos(x))
display(expr)
grouped = group_by_order(expr)
print("\nGrouped Expression by Order:")
print(grouped)
# For a matrix example:
mat = Matrix([[expr, 0], [0, expr]])
display(mat)
grouped_matrix = group_by_order(mat)
print("\nGrouped Matrix by Order:")
print(grouped_matrix)
Grouped Expression by Order:
{0: [{'other': [sin(x)], 'finite': [A], 'infinite': {}}, {'other': [cos(x)], 'finite': [A**2], 'infinite': {}}]}
\(\displaystyle \left[\begin{matrix}\sin{\left(x \right)} A + \cos{\left(x \right)} A^{2} & 0\\0 & \sin{\left(x \right)} A + \cos{\left(x \right)} A^{2}\end{matrix}\right]\)
Grouped Matrix by Order:
{0: [{'other': [sin(x), Matrix([
[1, 0],
[0, 0]])], 'finite': [A], 'infinite': {}}, {'other': [cos(x), Matrix([
[1, 0],
[0, 0]])], 'finite': [A**2], 'infinite': {}}, {'other': [sin(x), Matrix([
[0, 0],
[0, 1]])], 'finite': [A], 'infinite': {}}, {'other': [cos(x), Matrix([
[0, 0],
[0, 1]])], 'finite': [A**2], 'infinite': {}}]}
License
SymPT is licensed under the MIT License. See the LICENSE
file for details.
Citation
If you use SymPT in your research, please cite the following paper:
BibTeX Entry:
@misc{diotallevi2024symptcomprehensivetoolautomating,
title={SymPT: a comprehensive tool for automating effective Hamiltonian derivations},
author={Giovanni Francesco Diotallevi and Leander Reascos and Mónica Benito},
year={2024},
eprint={2412.10240},
archivePrefix={arXiv},
primaryClass={quant-ph},
url={https://arxiv.org/abs/2412.10240},
}
APA Citation:
Diotallevi, G. F., Reascos, L., & Benito, M. (2024). SymPT: a comprehensive tool for automating effective Hamiltonian derivations. arXiv preprint arXiv:2412.10240.
IEEE Citation:
G. F. Diotallevi, L. Reascos, and M. Benito, "SymPT: a comprehensive tool for automating effective Hamiltonian derivations," arXiv preprint arXiv:2412.10240, 2024.