API Reference#

This section contains the complete API documentation for the second_quantization package.

Package Overview#

Core Modules#

Hilbert Space Operations#

The second_quantization.hilbert_space module provides the core functionality for converting between second-quantized operator expressions and matrix representations.

second_quantization.hilbert_space.basis_operators(operators, sparse)[source]#

Transform from second quantized operators to matrix representation in the Fock-space.

Parameters:
  • operators (list[Expr]) – A list of sympy expressions representing the operators.

  • sparse (bool) – A boolean flag indicating whether to use sparse matrices.

Returns:

A dictionary where the keys are the input operators and the values are their corresponding matrix representations in the Fock-space.

Return type:

dict[Expr, slice(numpy.ndarray | scipy.sparse._csr.csr_array, None, None)]

Note

  • The operators list does not require its elements to be FermionOp.

  • The fermion ordering follows the ordering in the list.

second_quantization.hilbert_space.parity_operator(operators, sparse)[source]#

Generate the parity operator for a list of fermionic operators.

Parameters:
  • operators (list[Expr]) – A list of sympy expressions representing the fermionic operators.

  • sparse (bool) – A boolean flag indicating whether to return a sparse matrix.

Returns:

A diagonal matrix representing the parity operator, where the diagonal elements are 0 for even parity and 1 for odd parity.

Return type:

ndarray | csr_array

second_quantization.hilbert_space.to_matrix(expression, operators, sparse, operator_dict=None)[source]#

Transform a sympy expression containing fermionic operators to matrix representation.

Takes a symbolic expression with fermionic operators and converts it to a dictionary containing the matrix representation of each term, grouped by symbolic coefficients.

Parameters:
  • expression (Expr) – A sympy expression containing fermionic operators.

  • operators (list[FermionOp]) – A list of fermionic operators defining the basis.

  • sparse (bool) – If True, the matrices will be in sparse format. Otherwise, they will be dense.

Returns:

A dictionary where the keys are sympy expressions (symbolic coefficients) and the values are the corresponding matrix representations in either dense or sparse format.

Return type:

dict[Expr, ndarray | csr_array]

Example

```python import sympy as sy from sympy.physics.quantum.fermion import FermionOp from sympy.physics.quantum import Dagger

# Define symbolic parameters and operators t = sy.Symbol(‘t’) c = FermionOp(‘c’) d = FermionOp(‘d’)

# Define an expression expr = t * Dagger(c) * d

# Convert to matrix form matrices = to_matrix(expr, [c, d], sparse=False) ```

second_quantization.hilbert_space.to_operators(matrix, basis)[source]#

Convert matrices back to symbolic fermionic operator expressions.

Parameters:
  • matrix (dict[Expr, ndarray | csr_array] | ndarray | csr_array) – The matrix to be converted. It can be a dictionary with sympy expressions as keys and numpy arrays or scipy sparse arrays as values, or it can be a numpy array or a scipy sparse array directly.

  • basis (list[Expr]) – A list of sympy expressions representing the basis operators.

Returns:

The normal ordered operator expression corresponding to the input matrix.

Return type:

Expr

second_quantization.hilbert_space.find(s, ch)[source]#

Find all indices of a character in a string.

Parameters:
  • s (str) – The string to search in.

  • ch (str) – The character to search for.

Returns:

A list of indices where the character is found.

Return type:

List[int]

second_quantization.hilbert_space.string_basis(fermions)[source]#

Generate a list of bitstrings representing the occupation of fermions.

Parameters:

fermions (List[Expr]) – List of fermionic operators.

Returns:

List of bitstrings representing all possible occupation states.

Return type:

List[str]

Example

For 2 fermions, returns [‘00’, ‘01’, ‘10’, ‘11’] representing the four possible occupation states.

second_quantization.hilbert_space.symbolic_basis(fermions)[source]#

Generate a list of symbolic fermionic operators corresponding to the occupation of fermions.

This function creates the symbolic representation of all possible occupation states in the fermionic Fock space for the given set of fermionic operators.

Parameters:

fermions (List[Expr]) – List of fermionic operators.

Returns:

List of symbolic fermionic operators representing all possible occupation states. The first element is always the vacuum state (represented as sympy.S.One), followed by all single-particle states, two-particle states, etc.

Return type:

List[Expr]

Example

For 2 fermions [c, d], returns: - [1, d†, c†, c†*d†] representing vacuum, single occupations, and double occupation.

second_quantization.hilbert_space.make_dict_callable(hamiltonian_dict)[source]#

Create a callable function from a dictionary of SymPy expressions and NumPy arrays.

This function takes a dictionary where keys are symbolic expressions (containing free symbols) and values are NumPy arrays, and creates a callable function that evaluates the weighted sum of the arrays based on the symbolic expressions.

Parameters:

hamiltonian_dict (dict[Expr, ndarray]) – A dictionary where keys are SymPy expressions (which may contain free symbols) and values are NumPy arrays of the same shape.

Returns:

A callable function that takes values for all free symbols found in the dictionary keys and returns the weighted sum: Σ(expr_value * array) where expr_value is the numerical evaluation of each symbolic expression.

Raises:

ValueError – If any symbol names would be converted to Dummy variables by SymPy’s lambdify function. This typically happens with special characters or reserved names.

Return type:

callable

Example

```python import sympy as sp import numpy as np

# Define symbolic parameters x, y = sp.symbols(‘x y’)

# Create dictionary with symbolic expressions and matrices ham_dict = {

x: np.array([[1, 0], [0, 0]]), y: np.array([[0, 1], [1, 0]])

}

# Create callable function func = make_dict_callable(ham_dict)

# Evaluate at specific parameter values result = func(x=2.0, y=1.5) # Returns 2.0 * first_matrix + 1.5 * second_matrix ```

second_quantization.hilbert_space.partial_trace_generators(subset, all_operators, sparse, operator_dict=None)[source]#

Generate the projectors required to compute the partial trace of a subset of operators.

Parameters:
  • subset (list[Expr]) – Subset of operators to trace out.

  • all_operators (list[Expr]) – A list of sympy expressions representing the fermionic operators of the system.

  • sparse (bool) – Bool to set whether dense numpy arrays or sparse scipy arrays are used.

  • operator_dict (dict) – Optional dictionary mapping operators to their matrix representations.

Returns:

numpy array with the projectors that trace out the subset of operators specified in the argument. The final trace is taken by projecting a matrix M as np.sum([p[:,i,:] @ M @ p[:,i,:].conj().T for i in range(p.shape[1])]).

Return type:

ndarray

Main Functions#

second_quantization.hilbert_space.to_matrix(expression, operators, sparse, operator_dict=None)[source]#

Transform a sympy expression containing fermionic operators to matrix representation.

Takes a symbolic expression with fermionic operators and converts it to a dictionary containing the matrix representation of each term, grouped by symbolic coefficients.

Parameters:
  • expression (Expr) – A sympy expression containing fermionic operators.

  • operators (list[FermionOp]) – A list of fermionic operators defining the basis.

  • sparse (bool) – If True, the matrices will be in sparse format. Otherwise, they will be dense.

Returns:

A dictionary where the keys are sympy expressions (symbolic coefficients) and the values are the corresponding matrix representations in either dense or sparse format.

Return type:

dict[Expr, ndarray | csr_array]

Example

```python import sympy as sy from sympy.physics.quantum.fermion import FermionOp from sympy.physics.quantum import Dagger

# Define symbolic parameters and operators t = sy.Symbol(‘t’) c = FermionOp(‘c’) d = FermionOp(‘d’)

# Define an expression expr = t * Dagger(c) * d

# Convert to matrix form matrices = to_matrix(expr, [c, d], sparse=False) ```

second_quantization.hilbert_space.to_operators(matrix, basis)[source]#

Convert matrices back to symbolic fermionic operator expressions.

Parameters:
  • matrix (dict[Expr, ndarray | csr_array] | ndarray | csr_array) – The matrix to be converted. It can be a dictionary with sympy expressions as keys and numpy arrays or scipy sparse arrays as values, or it can be a numpy array or a scipy sparse array directly.

  • basis (list[Expr]) – A list of sympy expressions representing the basis operators.

Returns:

The normal ordered operator expression corresponding to the input matrix.

Return type:

Expr

second_quantization.hilbert_space.make_dict_callable(hamiltonian_dict)[source]#

Create a callable function from a dictionary of SymPy expressions and NumPy arrays.

This function takes a dictionary where keys are symbolic expressions (containing free symbols) and values are NumPy arrays, and creates a callable function that evaluates the weighted sum of the arrays based on the symbolic expressions.

Parameters:

hamiltonian_dict (dict[Expr, ndarray]) – A dictionary where keys are SymPy expressions (which may contain free symbols) and values are NumPy arrays of the same shape.

Returns:

A callable function that takes values for all free symbols found in the dictionary keys and returns the weighted sum: Σ(expr_value * array) where expr_value is the numerical evaluation of each symbolic expression.

Raises:

ValueError – If any symbol names would be converted to Dummy variables by SymPy’s lambdify function. This typically happens with special characters or reserved names.

Return type:

callable

Example

```python import sympy as sp import numpy as np

# Define symbolic parameters x, y = sp.symbols(‘x y’)

# Create dictionary with symbolic expressions and matrices ham_dict = {

x: np.array([[1, 0], [0, 0]]), y: np.array([[0, 1], [1, 0]])

}

# Create callable function func = make_dict_callable(ham_dict)

# Evaluate at specific parameter values result = func(x=2.0, y=1.5) # Returns 2.0 * first_matrix + 1.5 * second_matrix ```

second_quantization.hilbert_space.parity_operator(operators, sparse)[source]#

Generate the parity operator for a list of fermionic operators.

Parameters:
  • operators (list[Expr]) – A list of sympy expressions representing the fermionic operators.

  • sparse (bool) – A boolean flag indicating whether to return a sparse matrix.

Returns:

A diagonal matrix representing the parity operator, where the diagonal elements are 0 for even parity and 1 for odd parity.

Return type:

ndarray | csr_array

Basis Generation#

second_quantization.hilbert_space.basis_operators(operators, sparse)[source]#

Transform from second quantized operators to matrix representation in the Fock-space.

Parameters:
  • operators (list[Expr]) – A list of sympy expressions representing the operators.

  • sparse (bool) – A boolean flag indicating whether to use sparse matrices.

Returns:

A dictionary where the keys are the input operators and the values are their corresponding matrix representations in the Fock-space.

Return type:

dict[Expr, slice(numpy.ndarray | scipy.sparse._csr.csr_array, None, None)]

Note

  • The operators list does not require its elements to be FermionOp.

  • The fermion ordering follows the ordering in the list.

second_quantization.hilbert_space.string_basis(fermions)[source]#

Generate a list of bitstrings representing the occupation of fermions.

Parameters:

fermions (List[Expr]) – List of fermionic operators.

Returns:

List of bitstrings representing all possible occupation states.

Return type:

List[str]

Example

For 2 fermions, returns [‘00’, ‘01’, ‘10’, ‘11’] representing the four possible occupation states.

second_quantization.hilbert_space.symbolic_basis(fermions)[source]#

Generate a list of symbolic fermionic operators corresponding to the occupation of fermions.

This function creates the symbolic representation of all possible occupation states in the fermionic Fock space for the given set of fermionic operators.

Parameters:

fermions (List[Expr]) – List of fermionic operators.

Returns:

List of symbolic fermionic operators representing all possible occupation states. The first element is always the vacuum state (represented as sympy.S.One), followed by all single-particle states, two-particle states, etc.

Return type:

List[Expr]

Example

For 2 fermions [c, d], returns: - [1, d†, c†, c†*d†] representing vacuum, single occupations, and double occupation.

second_quantization.hilbert_space.find(s, ch)[source]#

Find all indices of a character in a string.

Parameters:
  • s (str) – The string to search in.

  • ch (str) – The character to search for.

Returns:

A list of indices where the character is found.

Return type:

List[int]

Pauli String Decomposition#

The second_quantization.pauli_strings module provides tools for decomposing matrices into Pauli string representations.

second_quantization.pauli_strings.PauliDecomposition(matrix, PauliStringInit='')[source]#

Computes the Pauli decomposition of a square matrix.

Iteratively splits tensor factors off and decomposes those smaller matrices. This is done using submatrices of the original matrix. The Pauli strings are generated in each step.

Parameters:
  • matrix – Matrix to be decomposed (Preferably numpy array/scipysparse).

  • sparse – Whether matrix is in sparse format.

  • PauliStringInit – For recursive computation.

Returns:

String of 1XYZ with their factors.

Return type:

decomposition/outString

second_quantization.pauli_strings.is_zero(matrix)[source]#
second_quantization.pauli_strings.PauliDecomposition(matrix, PauliStringInit='')[source]#

Computes the Pauli decomposition of a square matrix.

Iteratively splits tensor factors off and decomposes those smaller matrices. This is done using submatrices of the original matrix. The Pauli strings are generated in each step.

Parameters:
  • matrix – Matrix to be decomposed (Preferably numpy array/scipysparse).

  • sparse – Whether matrix is in sparse format.

  • PauliStringInit – For recursive computation.

Returns:

String of 1XYZ with their factors.

Return type:

decomposition/outString

second_quantization.pauli_strings.is_zero(matrix)[source]#