symop.ccr.algebra.density¶
Symbolic density-operator algebra for bosonic CCR systems.
This package implements purely symbolic manipulations of density polynomials of the form
where each term consists of a complex coefficient and a pair of normally ordered ladder-operator monomials. All computations are performed using canonical commutation relations (CCR) and symbolic normal ordering. No matrix representations or finite-dimensional truncations are constructed.
Functionality provided in this package includes:
Construction of pure states
\[\rho = \lvert \psi \rangle \langle \psi \rvert.\]Trace evaluation and trace normalization.
Hilbert–Schmidt inner products
\[\langle A, B \rangle = \mathrm{Tr}(A^\dagger B),\]and purity
\[\mathrm{Tr}(\rho^2).\]Left and right application of operator words.
Partial trace over selected modes.
Canonicalization by combining like terms.
Symbolic expansion of monomial and word products.
The module-level API is functional and stateless. The class
DensityPoly provides a thin immutable wrapper exposing these
operations in object-oriented form while delegating the actual algebra
to the underlying functional routines.
Design principles¶
Fully symbolic CCR-based manipulation.
Support for non-orthogonal modes via overlap factors.
Deterministic canonical ordering using monomial signatures.
Strict separation between algebraic logic and representation.
Public API¶
- class DensityPoly(terms: tuple[DensityTerm, ...] = ()) None¶
Bases:
objectSymbolic density polynomial.
A
DensityPolystores a tuple of density terms, each term being a coefficient times an outer product of monomials:\[c \, |L\rangle\langle R|.\]- Parameters:
terms (
tuple[DensityTerm,...]) – Tuple of density terms. Terms are not automatically canonicalized on construction; callcombine_like_terms()(ornormalize()) when needed.
Notes
This class is intentionally logic-only. It wraps functional algebra routines and provides convenient structural queries and standard linear-algebra-like operations (addition and scalar scaling).
- apply_left(word: Iterable[LadderOp]) DensityPoly¶
Apply an operator word to the left:
word * rho.This symbolically applies the ladder-operator word to each term’s left monomial.
- Parameters:
word (
Iterable[LadderOp]) – Ordered ladder-operator word to apply on the left.- Returns:
Resulting density polynomial.
- Return type:
- apply_right(word: Iterable[LadderOp]) DensityPoly¶
Apply an operator word to the right:
rho * word.This symbolically applies the ladder-operator word to each term’s right monomial.
- Parameters:
word (
Iterable[LadderOp]) – Ordered ladder-operator word to apply on the right.- Returns:
Resulting density polynomial.
- Return type:
- combine_like_terms(*, eps: float = 1e-12, approx: bool = False, decimals: int = 12, ignore_global_phase: bool = False) DensityPoly¶
Return a canonicalized polynomial by merging identical terms.
Density terms are bucketed by signatures of their left/right monomials, and coefficients within each bucket are summed.
- Parameters:
eps (
float) – Buckets whose summed coefficient magnitude is smaller thanepsare removed.approx (
bool) – IfTrue, use approximate signatures for matching.decimals (
int) – Number of decimals used when rounding floating components in approximate signatures.ignore_global_phase (
bool) – IfTrue, component signatures may ignore global phase when constructing approximate signatures.
- Returns:
Polynomial with like terms merged and near-zero terms removed.
- Return type:
Notes
This is purely signature-based merging. It does not apply CCR commutation relations or reorder monomials.
- hs_norm2() float¶
Return the squared Hilbert-Schmidt norm.
\[\| \rho \|_{\mathrm{HS}}^2 = \langle \rho, \rho \rangle_{\mathrm{HS}}.\]- Returns:
Squared Hilbert-Schmidt norm (real-valued).
- Return type:
- static identity() DensityPoly¶
Return the identity operator as a density polynomial.
This returns the single-term polynomial
\[|I\rangle\langle I|,\]where \(I\) is the identity monomial (empty word).
- Returns:
Identity density polynomial.
- Return type:
- inner(other: DensityPoly) complex¶
Return the Hilbert-Schmidt inner product
<self, other>.This delegates to
density_inner()and is typically equivalent to\[\langle A, B \rangle_{\mathrm{HS}} = \mathrm{Tr}(A^\dagger B).\]- Parameters:
other (
DensityPoly) – Another density polynomial.- Returns:
Hilbert-Schmidt inner product.
- Return type:
- is_block_diagonal_by_modes() bool¶
Return True iff each term matches left/right mode lists.
This checks that the ordered list of mode signatures appearing in the left monomial matches that of the right monomial for every term.
- Return type:
- property is_creator_only: bool¶
Return True iff every term is creator-only or identity on both sides.
- property is_creator_only_left: bool¶
Return True iff every term is creator-only or identity on the left.
- property is_creator_only_right: bool¶
Return True iff every term is creator-only or identity on the right.
- property is_diagonal_in_monomials: bool¶
Return True iff each term has identical left/right monomials.
- is_trace_normalized(eps: float = 1e-12) bool¶
Return True if \(\mathrm{Tr}(\rho)=1\) within tolerance.
- multiply(other: DensityPoly, *, eps: float = 1e-12) DensityPoly¶
Return the symbolic product
self * other.For density terms
|L><R|and|A><B|, multiplication is defined as\[|L\rangle\langle R|\,|A\rangle\langle B| = \langle R \mid A \rangle\, |L\rangle\langle B|,\]extended linearly over all terms.
- Parameters:
other (
DensityPoly) – Right-hand density polynomial.eps (
float) – Contributions with negligible intermediate overlap are dropped.
- Returns:
Resulting symbolic density polynomial.
- Return type:
- normalize(*, eps: float = 1e-12, approx: bool = False, decimals: int = 12, ignore_global_phase: bool = False) DensityPoly¶
Return a normalized structural form of the polynomial.
This is a convenience alias for
combine_like_terms().- Returns:
Polynomial with merged like terms.
- Return type:
- Parameters:
- normalize_trace(*, eps: float = 1e-14) DensityPoly¶
Return a trace-normalized density polynomial.
This scales the density so that its trace is unity.
- Parameters:
eps (
float) – If|Tr(rho)| < epsaValueErroris raised.- Returns:
Trace-normalized density polynomial.
- Return type:
- Raises:
ValueError – If the trace is too small to safely normalize.
- partial_trace(trace_over_modes: Iterable[object]) DensityPoly¶
Return the partial trace over a set of modes.
- static pure(ket: KetPoly) DensityPoly¶
Construct a pure-state density polynomial
|psi><psi|.- Parameters:
ket (
KetPoly) – Ket polynomial describing \(|\psi\rangle\).- Returns:
Density polynomial representing \(|\psi\rangle\langle\psi|\).
- Return type:
- purity() float¶
Return the purity \(\mathrm{Tr}(\rho^2)\).
- Returns:
Purity of the density polynomial.
- Return type:
- require_trace_normalized(eps: float = 1e-12) None¶
Raise
ValueErrorunless the density is trace-normalized.- Parameters:
eps (float)
- Return type:
None
- scaled(c: complex) DensityPoly¶
Return
c * self.- Parameters:
c (
complex) – Scalar multiplier.- Returns:
Scaled density polynomial.
- Return type:
- terms: tuple[DensityTerm, ...] = ()¶
- trace() complex¶
Return the trace \(\mathrm{Tr}(\rho)\).
- Returns:
Trace of the density polynomial.
- Return type:
- property unique_modes: tuple[ModeOp, ...]¶
Return unique modes appearing in the density (first-seen order).
Modes are extracted from both left and right monomials and uniqued by exact mode signature.
- Returns:
Unique modes in first-seen order.
- Return type:
tuple[ModeOpProto, …]
- static zero() DensityPoly¶
Return the zero density operator.
- Returns:
The empty polynomial (no terms).
- Return type:
Modules
Left-application of operator words to density polynomials. |
|
Right-application of operator words to density polynomials. |
|
Combine like terms in symbolic density-operator polynomials. |
|
Expansion of a monomial multiplied by an operator word. |
|
Expansion of an operator word acting on a monomial from the left. |
|
Hilbert--Schmidt inner product for symbolic density polynomials. |
|
Density polynomial multiplication. |
|
Trace normalization for symbolic density polynomials. |
|
Symbolic overlaps between monomials via normal ordering. |
|
Partial trace for symbolic density polynomials. |
|
Symbolic density polynomials for CCR algebra. |
|
Pure-state density construction for symbolic kets. |
|
Purity of symbolic density polynomials. |
|
Scaling of symbolic density polynomials. |
|
Trace of symbolic density polynomials. |