# qnet.algebra.state_algebra module¶

This module implements the algebra of states in a Hilbert space

For more details see State (Ket-) Algebra.

## Summary¶

Exceptions:

Classes:

 BasisKet Local basis state, identified by index or label. Bra The associated dual/adjoint state for any Ket object k is given by Bra(k). BraKet The symbolic inner product between two states, represented as Bra and CoherentStateKet Local coherent state, labeled by a scalar amplitude. Ket Basic Ket algebra class to represent Hilbert Space states KetBra A symbolic operator formed by the outer product of two states KetPlus A sum of Ket states. KetSymbol Ket symbol class, parametrized by an identifier string and an associated Hilbert space. LocalKet A state that lives on a single local Hilbert space. OperatorTimesKet Multiply an operator by an operator ScalarTimesKet Multiply a Ket by a scalar coefficient. TensorKet A tensor product of kets each belonging to different degrees of freedom.

Functions:

 act_locally act_locally_times_tensor check_kets_same_space Check that all operands are from the same Hilbert space. check_op_ket_space Check that all operands are from the same Hilbert space. tensor_decompose_kets

Data:

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

## Reference¶

exception qnet.algebra.state_algebra.UnequalSpaces[source]
exception qnet.algebra.state_algebra.OverlappingSpaces[source]
exception qnet.algebra.state_algebra.SpaceTooLargeError[source]
qnet.algebra.state_algebra.check_kets_same_space(cls, ops, kwargs)[source]

Check that all operands are from the same Hilbert space.

qnet.algebra.state_algebra.check_op_ket_space(cls, ops, kwargs)[source]

Check that all operands are from the same Hilbert space.

class qnet.algebra.state_algebra.Ket[source]

Bases: object

Basic Ket algebra class to represent Hilbert Space states

space

The associated HilbertSpace

adjoint()[source]

The adjoint of a Ket state, i.e., the corresponding Bra.

dag

The adjoint of a Ket state, i.e., the corresponding Bra.

expand()[source]

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

Ket symbol class, parametrized by an identifier string and an associated Hilbert space.

Parameters: label (str) – Symbol identifier hs (HilbertSpace) – Associated Hilbert space.
args
kwargs
space
label
all_symbols()[source]
class qnet.algebra.state_algebra.LocalKet(label, *, hs)[source]

A state that lives on a single local Hilbert space. This does not include operations, even if these operations only involve states acting on the same local space

all_symbols()[source]
qnet.algebra.state_algebra.ZeroKet = ZeroKet[source]

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

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

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

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

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 (str or int) – 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 hs (LocalSpace) – The Hilbert space in which the basis is defined ValueError – if label_or_index is not in the Hilbert space TypeError – if label_or_index is not an int or str. 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)

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

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

Local coherent state, labeled by a scalar amplitude.

Parameters: hs (LocalSpace) – The local Hilbert space degree of freedom. amp (SCALAR_TYPES) – The coherent displacement amplitude.
ampl
args
label
kwargs
all_symbols()[source]
class qnet.algebra.state_algebra.KetPlus(*operands)[source]

A sum of Ket states.

Instantiate as:

KetPlus(*summands)

Parameters: summands (Ket) – State summands.
neutral_element = ZeroKet

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

order_key

alias of FullCommutativeHSOrder

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

A tensor product of kets each belonging to different degrees of freedom. Instantiate as:

TensorKet(*factors)

Parameters: factors (Ket) – Ket factors.
neutral_element = TrivialKet

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

order_key

alias of FullCommutativeHSOrder

classmethod create(*ops)[source]
factor_for_space(space)[source]

Factor into a Ket defined on the given space and a Ket on the remaining Hilbert space

space
label

Combined label of the product state if the state is a simple product of LocalKets, raise AttributeError otherwise

class qnet.algebra.state_algebra.ScalarTimesKet(*operands, **kwargs)[source]

Multiply a Ket by a scalar coefficient.

Instantiate as::
ScalarTimesKet(coefficient, term)
Parameters: coefficient (SCALAR_TYPES) – Scalar coefficient. term (Ket) – The ket that is multiplied.
space
coeff
term
class qnet.algebra.state_algebra.OperatorTimesKet(*operands, **kwargs)[source]

Multiply an operator by an operator

Instantiate as:

OperatorTimesKet(op, ket)

Parameters: op (Operator) – The multiplying operator. ket (Ket) – The ket that is multiplied.
space
operator
ket
class qnet.algebra.state_algebra.Bra(ket)[source]

The associated dual/adjoint state for any Ket object k is given by Bra(k).

Parameters: k (Ket) – The state to represent as Bra.
ket

The state that is represented as a Bra.

Return type: Ket
operand

The state that is represented as a Bra.

Return type: Ket
adjoint()[source]

The adjoint of a Bra is just the original Ket again.

dag

The adjoint of a Bra is just the original Ket again.

expand()[source]

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

The symbolic inner product between two states, represented as Bra and Ket

In math notation this corresponds to:

$\langle b | k \rangle$

which we define to be linear in the state $$k$$ and anti-linear in $$b$$.

Parameters: bra (Ket) – The anti-linear state argument. ket (Ket) – The linear state argument.
ket
bra
space
class qnet.algebra.state_algebra.KetBra(ket, bra)[source]

A symbolic operator formed by the outer product of two states

Parameters: ket (Ket) – The first state that defines the range of the operator. bra (Ket) – The second state that defines the Kernel of the operator.
ket
bra
space
qnet.algebra.state_algebra.act_locally(op, ket)[source]
qnet.algebra.state_algebra.act_locally_times_tensor(op, ket)[source]
qnet.algebra.state_algebra.tensor_decompose_kets(a, b, operation)[source]