API documentation
Hilbert Space
basis_operators(operators, sparse, truncation=None)
Split fermionic and bosonic operators and build their matrix bases.
Fermionic operators are mapped through fermion_basis; bosonic operators
are mapped through boson_basis with the provided truncation. The return
value is a pair of dictionaries whose keys are 1 and the operator
symbols/powers that are explicitly constructed.
Raises
ValueError If non-fermion/boson symbols are provided or bosonic truncation is omitted when bosons are present.
Source code in second_quantization/hilbert_space.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
make_dict_callable(hamiltonian_dict)
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:
| Name | Type | Description | Default |
|---|---|---|---|
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. |
required |
Returns:
| Type | Description |
|---|---|
callable
|
A callable function that takes values for all free symbols found in the |
callable
|
dictionary keys and returns the weighted sum: Σ(expr_value * array) where |
callable
|
expr_value is the numerical evaluation of each symbolic expression. |
Raises:
| Type | Description |
|---|---|
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. |
Example
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
Source code in second_quantization/hilbert_space.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
parity_operator(operators, sparse, truncation=None)
Construct the combined parity operator for fermionic/bosonic mixtures.
Fermionic modes contribute their occupation-parity bit; bosonic modes
contribute (-1) raised to their occupation number (requiring a
truncation). The individual parities are tensored in the order fermions
then bosons.
Source code in second_quantization/hilbert_space.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
partial_trace_generators(subset, all_operators, sparse, operator_dict=None, truncation=None)
Projectors for tracing out a fermionic subset.
Constructs vectors that project onto each configuration of the complement of
subset while fixing subset in vacuum, enabling partial traces via the
recipe documented in the return value description.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subset
|
list[Expr]
|
Fermionic modes to remove via tracing. |
required |
all_operators
|
list[Expr]
|
Ordered list of all fermionic modes in the system. |
required |
sparse
|
bool
|
Whether to keep intermediate matrices sparse. |
required |
operator_dict
|
dict
|
Optional cached operator dictionaries. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of projectors |
ndarray
|
traces out |
Source code in second_quantization/hilbert_space.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | |
symbolic_basis(operators, truncation)
Generate a list of symbolic operators corresponding to their occupation.
This function creates the symbolic representation of all possible occupation states in the fermionic Fock space for the given set of fermionic operators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fermions
|
List of fermionic operators. |
required |
Returns:
| Type | Description |
|---|---|
list[Expr]
|
List of symbolic fermionic operators representing all possible occupation states. |
list[Expr]
|
The first element is always the vacuum state (represented as |
list[Expr]
|
followed by all single-particle states, two-particle states, etc. |
Example
For 2 fermions [c, d], returns:
- [1, d†, c†, c†*d†] representing vacuum, single occupations, and double occupation.
Source code in second_quantization/hilbert_space.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
to_matrix(expression, operators, sparse, operator_dict=None, truncation=None)
Convert a symbolic operator expression to matrices.
Expands expression into normal products of the provided fermionic and/or
bosonic operators, builds matrix representations via tensor products, and
groups terms by their purely symbolic prefactor. Creation operators are
inferred via Dagger and implemented as conjugate-transposed annihilation
matrices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
Expr
|
SymPy expression containing the operators to expand. |
required |
operators
|
list[FermionOp | BosonOp]
|
Ordered basis of modes (fermions, bosons, or both) that sets the tensor-product ordering. |
required |
sparse
|
bool
|
If True, use sparse matrices for all operations. |
required |
operator_dict
|
dict
|
Optional cached |
None
|
truncation
|
int | list[int]
|
Truncation passed to bosonic basis construction when needed. |
None
|
Returns:
| Type | Description |
|---|---|
dict[Expr, ndarray | csr_array]
|
Dict mapping symbolic prefactors to the summed matrix representation of |
dict[Expr, ndarray | csr_array]
|
all terms that share that prefactor. |
Source code in second_quantization/hilbert_space.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
to_operators(matrix, basis, truncation=None)
Recover a normal-ordered operator expression from a matrix.
Dispatches to the appropriate inversion backend based on the operator
types present in basis:
- Fermionic only – Pauli decomposition + Jordan–Wigner inversion (original behaviour, requires dim = 2^n).
- Bosonic only – diagonal falling-factorial decomposition with
finite-difference inversion (requires
truncation). - Mixed – Pauli decomposition on the fermionic tensor structure
followed by bosonic inversion on each coefficient block (requires
truncation).
If a dictionary of matrices is supplied, each entry is converted and scaled by its symbolic key before summing.
Parameters
matrix : ndarray, csr_array, or dict thereof
Square matrix (or dict of matrices) in the Hilbert space defined by
basis and the tensor-product ordering of :func:to_matrix.
basis : list of FermionOp / BosonOp
Ordered operators defining the Hilbert space. Fermionic modes must
come before bosonic modes (matching :func:to_matrix).
truncation : int, list of int, or None
Required when basis contains bosonic operators; passed to the
bosonic inversion backend.
Returns
sympy.Expr Normal-ordered symbolic expression.
Source code in second_quantization/hilbert_space.py
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 | |
Pauli strings
PauliDecomposition(matrix, PauliStringInit='', recursion_depth=None, threshold=0.0)
Decompose a square matrix into Pauli strings.
Recursively slices the matrix into 2x2 blocks, decomposes each block, and
accumulates tensor-product labels composed of I, X, Y, and
Z. Works with dense numpy arrays, scipy.sparse matrices, and
sympy.Matrix so long as the dimension is a power of two.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
Square matrix to decompose. |
required | |
PauliStringInit
|
Prefix used internally for recursion. |
''
|
Returns:
defaultdict mapping Pauli strings (e.g., "IXZ") to their
complex or symbolic coefficients.
Source code in second_quantization/pauli_strings.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
string_to_fermion_operators(pauli_str, fermion_ops)
Convert a Pauli string to its Jordan-Wigner fermionic expression.
Implements the standard JW inverse with accumulated parity strings
X_i -> (Z_0 x ... x Z_{i-1}) (c^dagger_i + c_i) Y_i -> (Z_0 x ... x Z_{i-1}) i(c^dagger_i - c_i) Z_i -> -(2 c^dagger_i c_i - 1) I_i -> identity (but still contributes to the parity string)
Parameters
pauli_str : str
Pauli string of length len(fermion_ops), e.g. "IXZ".
fermion_ops : list of FermionOp
Ordered fermionic modes.
Returns
sympy.Expr Fermionic operator expression (not yet normal-ordered).
Source code in second_quantization/pauli_strings.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |