qnet.algebra.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
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 qnet.algebra.core.state_algebra.State(*args, **kwargs)[source]

Bases: qnet.algebra.core.abstract_quantum_algebra.QuantumExpression

Base class for states in a Hilbert space

isket

Whether the state represents a ket

isbra

Wether the state represents a bra (adjoint ket)

bra

The bra associated with a ket

ket

The ket associated with a bra

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

Bases: qnet.algebra.core.abstract_quantum_algebra.QuantumSymbol, qnet.algebra.core.state_algebra.State

Symbolic state

See QuantumSymbol.

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

Bases: qnet.algebra.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

space

The HilbertSpace on which the operator acts non-trivially

kwargs

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

qnet.algebra.core.state_algebra.ZeroKet = ZeroKet[source]

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

qnet.algebra.core.state_algebra.TrivialKet = TrivialKet[source]

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

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

Bases: qnet.algebra.core.state_algebra.LocalKet, qnet.algebra.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>]
args

Tuple containing label_or_index as its only element.

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 qnet.algebra.core.state_algebra.CoherentStateKet(ampl, *, hs)[source]

Bases: qnet.algebra.core.state_algebra.LocalKet

Local coherent state, labeled by a complex amplitude

Parameters:
  • hs (LocalSpace) – The local Hilbert space degree of freedom.
  • ampl (Scalar) – The coherent displacement amplitude.
args

The tuple of positional arguments for the instantiation of the Expression

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 qnet.algebra.core.state_algebra.KetPlus(*operands)[source]

Bases: qnet.algebra.core.state_algebra.State, qnet.algebra.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 qnet.utils.ordering.FullCommutativeHSOrder

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

Bases: qnet.algebra.core.state_algebra.State, qnet.algebra.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 qnet.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().

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

Bases: qnet.algebra.core.state_algebra.State, qnet.algebra.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 qnet.algebra.core.state_algebra.OperatorTimesKet(operator, ket)[source]

Bases: qnet.algebra.core.state_algebra.State, qnet.algebra.core.abstract_algebra.Operation

Product of an operator and a state.

simplifications = [<function match_replace>]
space

The HilbertSpace on which the operator acts non-trivially

operator
ket

The ket associated with a bra

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

Bases: qnet.algebra.core.abstract_quantum_algebra.QuantumDerivative, qnet.algebra.core.state_algebra.State

Symbolic partial derivative of a state

See QuantumDerivative.

class qnet.algebra.core.state_algebra.Bra(ket)[source]

Bases: qnet.algebra.core.state_algebra.State, qnet.algebra.core.abstract_quantum_algebra.QuantumAdjoint

The associated dual/adjoint state for any ket

ket

The original State

bra

The bra associated with a ket

operand

The original State

isket

False, by defintion

isbra

True, by definition

label
class qnet.algebra.core.state_algebra.BraKet(bra, ket)[source]

Bases: qnet.algebra.core.scalar_algebra.ScalarExpression, qnet.algebra.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>]
ket

The ket of the braket

bra

The bra of the braket (Bra instance)

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

Bases: qnet.algebra.core.operator_algebra.Operator, qnet.algebra.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>]
ket

The left factor in the product

bra

The co-state right factor in the product

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

space

The Hilbert space of the states being multiplied

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

Bases: qnet.algebra.core.state_algebra.State, qnet.algebra.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().