qalgebra.core.state_algebra module

This module implements the algebra of states in a Hilbert space

For more details see State (Ket-) Algebra.

Summary

Classes:

BasisKet

Local basis state, identified by index or label

Bra

The associated dual/adjoint state for any ket

BraKet

The symbolic inner product between two states

CoherentStateKet

Local coherent state, labeled by a complex amplitude \(\alpha\).

KetBra

Outer product of two states

KetIndexedSum

Indexed sum over Kets.

KetPlus

Sum of states.

KetSymbol

Symbolic state.

LocalKet

A state on a LocalSpace.

OperatorTimesKet

Product of an operator and a state.

ScalarTimesKet

Product of a Scalar coefficient and a ket

State

Base class for states in a Hilbert space

StateDerivative

Symbolic partial derivative of a state

TensorKet

A tensor product of kets.

Data:

TrivialKet

TrivialKet constant (singleton) object.

ZeroKet

ZeroKet constant (singleton) object for the null-state.

__all__: BasisKet, Bra, BraKet, CoherentStateKet, KetBra, KetIndexedSum, KetPlus, KetSymbol, LocalKet, OperatorTimesKet, ScalarTimesKet, State, StateDerivative, TensorKet, TrivialKet, ZeroKet

Reference

class qalgebra.core.state_algebra.BasisKet(label_or_index, *, hs)[source]

Bases: qalgebra.core.state_algebra.LocalKet, qalgebra.core.state_algebra.KetSymbol

Local basis state, identified by index or label

Basis kets are orthornormal, and the next() and prev() methods can be used to move between basis states.

Parameters
  • label_or_index – If str, the label of the basis state (must be an element of hs.basis_labels). If int, the (zero-based) index of the basis state. This works if hs has an unknown dimension. For a symbolic index, label_or_index can be an instance of an appropriate subclass of SymbolicLabelBase

  • hs (LocalSpace) – The Hilbert space in which the basis is defined

Raises
  • ValueError – if label_or_index is not in the Hilbert space

  • TypeError – if label_or_index is not of an appropriate type

  • BasisNotSetError – if label_or_index is a str but no basis is defined for hs

Note

Basis states that are instantiated via a label or via an index are equivalent:

>>> hs = LocalSpace('tls', basis=('g', 'e'))
>>> BasisKet('g', hs=hs) == BasisKet(0, hs=hs)
True
>>> print(ascii(BasisKet(0, hs=hs)))
|g>^(tls)

When instantiating the BasisKet via create(), an integer label outside the range of the underlying Hilbert space results in a ZeroKet:

>>> BasisKet.create(-1, hs=0)
ZeroKet
>>> BasisKet.create(2, hs=LocalSpace('tls', dimension=2))
ZeroKet
simplifications = [<function basis_ket_zero_outside_hs>]
property args

Tuple containing label_or_index as its only element.

property index

The index of the state in the Hilbert space basis.

>>> hs = LocalSpace('tls', basis=('g', 'e'))
>>> BasisKet('g', hs=hs).index
0
>>> BasisKet('e', hs=hs).index
1
>>> BasisKet(1, hs=hs).index
1

For a BasisKet with an indexed label, this may return a sympy expression:

>>> hs = SpinSpace('s', spin='3/2')
>>> i = symbols('i', cls=IdxSym)
>>> lbl = SpinIndex(i/2, hs)
>>> ket = BasisKet(lbl, hs=hs)
>>> ket.index
i/2 + 3/2
next(n=1)[source]

Move up by n steps in the Hilbert space.

>>> hs =  LocalSpace('tls', basis=('g', 'e'))
>>> ascii(BasisKet('g', hs=hs).next())
'|e>^(tls)'
>>> ascii(BasisKet(0, hs=hs).next())
'|e>^(tls)'

We can also go multiple steps:

>>> hs =  LocalSpace('ten', dimension=10)
>>> ascii(BasisKet(0, hs=hs).next(2))
'|2>^(ten)'

An increment that leads out of the Hilbert space returns zero:

>>> BasisKet(0, hs=hs).next(10)
ZeroKet
prev(n=1)[source]

Move down by n steps in the Hilbert space, cf. next().

>>> hs =  LocalSpace('3l', basis=('g', 'e', 'r'))
>>> ascii(BasisKet('r', hs=hs).prev(2))
'|g>^(3l)'
>>> BasisKet('r', hs=hs).prev(3)
ZeroKet
class qalgebra.core.state_algebra.Bra(ket)[source]

Bases: qalgebra.core.state_algebra.State, qalgebra.core.abstract_quantum_algebra.QuantumAdjoint

The associated dual/adjoint state for any ket

property ket

The original State

property bra

The bra associated with a ket

property operand

The original State

property isket

False, by defintion

property isbra

True, by definition

property label
class qalgebra.core.state_algebra.BraKet(bra, ket)[source]

Bases: qalgebra.core.scalar_algebra.ScalarExpression, qalgebra.core.abstract_algebra.Operation

The symbolic inner product between two states

This mathermatically corresponds to:

\[\langle b | k \rangle\]

which we define to be linear in the state \(k\) and anti-linear in \(b\).

Parameters
  • bra (State) – The anti-linear state argument. Note that this is not a Bra instance.

  • ket (State) – The linear state argument.

simplifications = [<function match_replace>]
property ket

The ket of the braket

property bra

The bra of the braket (Bra instance)

class qalgebra.core.state_algebra.CoherentStateKet(ampl, *, hs)[source]

Bases: qalgebra.core.state_algebra.LocalKet

Local coherent state, labeled by a complex amplitude \(\alpha\).

\[\ket{\alpha} = \exp\left[ \alpha \Op{a}^\dagger - \alpha^* \Op{a} \right] \ket{0} = \Op{D}(\alpha) \ket{0}\]

where \(\Op{D}\) is the displacement operator, see Displace.

Parameters
  • hs (LocalSpace) – The local Hilbert space degree of freedom.

  • ampl (Scalar) – The coherent displacement amplitude \(\alpha\).

property args

The tuple of positional arguments for the instantiation of the Expression

property ampl
to_fock_representation(index_symbol='n', max_terms=None)[source]

Return the coherent state written out as an indexed sum over Fock basis states

class qalgebra.core.state_algebra.State(*args, **kwargs)[source]

Bases: qalgebra.core.abstract_quantum_algebra.QuantumExpression

Base class for states in a Hilbert space

property isket

Whether the state represents a ket

property isbra

Wether the state represents a bra (adjoint ket)

property bra

The bra associated with a ket

property ket

The ket associated with a bra

class qalgebra.core.state_algebra.KetBra(ket, bra)[source]

Bases: qalgebra.core.operator_algebra.Operator, qalgebra.core.abstract_algebra.Operation

Outer product of two states

Parameters
  • ket (State) – The left factor in the product

  • bra (State) – The right factor in the product. Note that this is not a Bra instance.

simplifications = [<function match_replace>]
property ket

The left factor in the product

property bra

The co-state right factor in the product

This is a Bra instance (unlike the bra given to the constructor

property space

The Hilbert space of the states being multiplied

class qalgebra.core.state_algebra.KetPlus(*operands)[source]

Bases: qalgebra.core.state_algebra.State, qalgebra.core.abstract_quantum_algebra.QuantumPlus

Sum of states.

simplifications = [<function accept_bras>, <function assoc>, <function orderby>, <function collect_summands>, <function match_replace_binary>]
order_key

alias of qalgebra.utils.ordering.FullCommutativeHSOrder

class qalgebra.core.state_algebra.KetSymbol(label, *sym_args, hs)[source]

Bases: qalgebra.core.abstract_quantum_algebra.QuantumSymbol, qalgebra.core.state_algebra.State

Symbolic state.

See QuantumSymbol.

class qalgebra.core.state_algebra.LocalKet(*args, hs)[source]

Bases: qalgebra.core.state_algebra.State

A state on a LocalSpace.

This does not include operations, even if these operations only involve states acting on the same local space

property space

The HilbertSpace on which the operator acts non-trivially

property kwargs

The dictionary of keyword-only arguments for the instantiation of the Expression

class qalgebra.core.state_algebra.OperatorTimesKet(operator, ket)[source]

Bases: qalgebra.core.state_algebra.State, qalgebra.core.abstract_algebra.Operation

Product of an operator and a state.

simplifications = [<function match_replace>]
property space

The HilbertSpace on which the operator acts non-trivially

property operator
property ket

The ket associated with a bra

class qalgebra.core.state_algebra.ScalarTimesKet(coeff, term)[source]

Bases: qalgebra.core.state_algebra.State, qalgebra.core.abstract_quantum_algebra.ScalarTimesQuantumExpression

Product of a Scalar coefficient and a ket

Parameters
  • coeff (Scalar) – coefficient

  • term (State) – the ket that is multiplied

simplifications = [<function match_replace>]
classmethod create(coeff, term)[source]

Instantiate while applying automatic simplifications.

Instead of directly instantiating cls, it is recommended to use create(), which applies simplifications to the args and keyword arguments according to the simplifications class attribute, and returns an appropriate object (which may or may not be an instance of the original cls).

Two simplifications of particular importance are match_replace() and match_replace_binary() which apply rule-based simplifications.

The temporary_rules() context manager may be used to allow temporary modification of the automatic simplifications that create() uses, in particular the rules for match_replace() and match_replace_binary(). Inside the managed context, the simplifications class attribute may be modified and rules can be managed with add_rule() and del_rules().

class qalgebra.core.state_algebra.TensorKet(*operands)[source]

Bases: qalgebra.core.state_algebra.State, qalgebra.core.abstract_quantum_algebra.QuantumTimes

A tensor product of kets.

Each ket must belong to different degree of freedom (LocalSpace).

simplifications = [<function accept_bras>, <function assoc>, <function orderby>, <function filter_neutral>, <function match_replace_binary>]
order_key

alias of qalgebra.utils.ordering.FullCommutativeHSOrder

classmethod create(*ops)[source]

Instantiate while applying automatic simplifications.

Instead of directly instantiating cls, it is recommended to use create(), which applies simplifications to the args and keyword arguments according to the simplifications class attribute, and returns an appropriate object (which may or may not be an instance of the original cls).

Two simplifications of particular importance are match_replace() and match_replace_binary() which apply rule-based simplifications.

The temporary_rules() context manager may be used to allow temporary modification of the automatic simplifications that create() uses, in particular the rules for match_replace() and match_replace_binary(). Inside the managed context, the simplifications class attribute may be modified and rules can be managed with add_rule() and del_rules().

qalgebra.core.state_algebra.TrivialKet = TrivialKet[source]

TrivialKet constant (singleton) object. This is the neutral element under the state tensor-product.

qalgebra.core.state_algebra.ZeroKet = ZeroKet[source]

ZeroKet constant (singleton) object for the null-state.

class qalgebra.core.state_algebra.KetIndexedSum(term, *, ranges)[source]

Bases: qalgebra.core.state_algebra.State, qalgebra.core.abstract_quantum_algebra.QuantumIndexedSum

Indexed sum over Kets.

simplifications = [<function assoc_indexed>, <function indexed_sum_over_kronecker>, <function indexed_sum_over_const>, <function match_replace>]
classmethod create(term, *, ranges)[source]

Instantiate while applying automatic simplifications.

Instead of directly instantiating cls, it is recommended to use create(), which applies simplifications to the args and keyword arguments according to the simplifications class attribute, and returns an appropriate object (which may or may not be an instance of the original cls).

Two simplifications of particular importance are match_replace() and match_replace_binary() which apply rule-based simplifications.

The temporary_rules() context manager may be used to allow temporary modification of the automatic simplifications that create() uses, in particular the rules for match_replace() and match_replace_binary(). Inside the managed context, the simplifications class attribute may be modified and rules can be managed with add_rule() and del_rules().

class qalgebra.core.state_algebra.StateDerivative(op, *, derivs, vals=None)[source]

Bases: qalgebra.core.abstract_quantum_algebra.QuantumDerivative, qalgebra.core.state_algebra.State

Symbolic partial derivative of a state

See QuantumDerivative.