Class Context

Nested Relationships

Nested Types

Class Documentation

class Context

Specifies SeQuant context, such as vacuum choice, whether index spaces are orthonormal, sizes of index spaces, etc.

SeQuant context contains the following information:

  • a IndexBasisRegistry object: contains information about the known IndexSpace objects and their attributes; managed by shared_ptr and shared by the copies of a context. A context owns its registry: it moves or copies in a registry it is given, and adopts a shared_ptr to one only if that is the registry’s only owner, so the registry cannot change while any context uses it (short of what Context::set(std::shared_ptr<const IndexBasisRegistry>) warns about).

  • vacuum: the vacuum state used to define normal ordering of NormalOperators

  • deserialization_symmetry, deserialization_hermiticity, deserialization_column_symmetry: the symmetries given to a deserialized tensor that does not specify them; the programmatic Tensor ctors are unaffected (see Tensor::Defaults), hence the deserialization_ prefix. There is no braket_symmetry knob: a tensor’s BraKetSymmetry is derived from its Hermiticity and base field.

  • spbasis: whether the bra/ket bases are spinor (SPBasis::Spinor) or spin-free (SPBasis::Spinfree).

  • first_dummy_index_ordinal: during its operation SeQuant will generate temporary indices with orbitals greater or equal to this; to avoid duplicates user Index objects should have ordinals smaller than this

  • canonicalization_options: if set, this specifies the default options to use for canonicalization of expressions.

  • braket_typesetting: whether to_latex() typesets tensor indices of ket (covariant, primal) modes as superscript (BraKetTypesetting::KetSuper, default)

  • braket_slot_typesetting: whether to_latex() typesets tensor indices using tensor LaTeX package (BraKetSlotTypesetting::TensorPackage, default) or native typesetting (BraKetSlotTypesetting::Naive); the former is preferred for alignment of superscript with subscript slots.

  • tensor_canonicalizers: the TensorCanonicalizer objects applied to tensors, keyed by tensor label; the one keyed by the empty label applies to tensors without a label-specific canonicalizer. The canonicalizer objects are shared by copies of the context and must not be mutated. The empty label maps to a DefaultTensorCanonicalizer unless a map given to the constructor sets it; unset_tensor_canonicalizer() can remove it.

  • index_comparer, index_pair_comparer: the objects that order Index objects (and pairs thereof) during tensor canonicalization; default to TensorCanonicalizer::default_index_comparer() and TensorCanonicalizer::default_index_pair_comparer().

  • cardinal_tensor_labels: Tensor labels with lexicographic preference (in order); default to {reserved::antisymm_label(), reserved::symm_label(), reserved::transposition_label()}.

Note

canonicalization reads tensor_canonicalizers, index_comparer, index_pair_comparer and cardinal_tensor_labels only from the default context for Statistics::Arbitrary; in a context installed for another Statistics they are currently ignored, but keep them identical to those of the Statistics::Arbitrary context, since a future version may consult the statistics-specific context first

Public Functions

Context(Options options = make_default_options())

standard named-parameter constructor

Warning

default constructor does not create an IndexBasisRegistry, thus this->index_basis_registry() will return nullptr Example:

Context ctx({.vacuum = Vacuum::SingleProduct, .spbasis = SPBasis::Spinfree});

~Context() = default
Context(const Context &ctx) = default

copy constructor

Note

created Context uses the same index basis registry as ctx

Parameters:

ctx – [in] a Context

Context &operator=(const Context &ctx) = default

copy assignment

Note

this object will use the same index basis registry as ctx

Parameters:

ctx – [in] a Context

Returns:

reference to this object

std::uint64_t version() const

Note

the other settings (vacuum, first dummy index ordinal, typesetting, deserialization defaults, strict bra-ket checks) do not affect the version

Note

the version does not track in-place mutation of a TensorCanonicalizer or comparer object that this context refers to

Returns:

the version of this context’s canonicalization configuration: a nonzero number that two contexts share if and only if canonicalization sees the same configuration in both, i.e. they have the same index space registry, tensor canonicalizers and index comparers (the same objects), cardinal tensor labels, canonicalization options and SP basis (which determines the symmetry of NormalOperator); a number is never reused for another configuration

Vacuum vacuum() const
Returns:

Vacuum of this context

std::shared_ptr<const IndexBasisRegistry> index_basis_registry() const

Note

the registry does not change; to change it, set a modified copy

Warning

can be null when user did not provide one to Context (i.e., it was default constructed)

Returns:

a constant pointer to the IndexBasisRegistry for this context

inline std::shared_ptr<const IndexBasisRegistry> index_space_registry() const

Deprecated:

use index_basis_registry()

bool assert_strict_braket_symmetry() const

Note

disabling strict bra-ket symmetry assertions is a bad idea if working with complex-valued tensors, or nonunit metric, or in general

Warning

if SeQuant was configured with SEQUANT_ASSERT_BEHAVIOR=IGNORE the strict bra-ket contraction checks are turned off even if this returns true

Returns:

true if strict bra-ket symmetry is asserted; setting this to false (via Context::set(AssertStrictBraKetSymmetry::No)) allows arbitrary contractions of bra/ket modes as if they were aux indices.

SPBasis spbasis() const
Returns:

SPBasis of this context

std::size_t first_dummy_index_ordinal() const
Returns:

first ordinal of the dummy indices generated by calls to Index::next_tmp_index when this context is active

std::optional<CanonicalizeOptions> canonicalization_options() const
Returns:

canonicalization options to use by default, if nonnull

BraKetTypesetting braket_typesetting() const
Returns:

BraKetTypesetting of this context; if this returns BraKetTypesetting::ContraSub covariant (ket, creation) and contravariant (bra, annihilation) indices are typeset in superscript and subscript LaTeX, respectively.

BraKetSlotTypesetting braket_slot_typesetting() const
Returns:

BraKetSlotTypesetting of this context; see BraKetSlotTypesetting for the meaning of the possible values

Symmetry deserialization_symmetry() const
Returns:

the default bra/ket permutational Symmetry for deserialized tensors; programmatic Tensor construction is unaffected by it

Hermiticity deserialization_hermiticity() const
Returns:

the default Hermiticity for deserialized tensors; the braket symmetry of a deserialized tensor is derived from this and its base_field. Programmatic Tensor construction is unaffected by it

ColumnSymmetry deserialization_column_symmetry() const
Returns:

the default ColumnSymmetry (particle-permutation symmetry) for deserialized tensors; programmatic Tensor construction is unaffected by it

std::shared_ptr<TensorCanonicalizer> tensor_canonicalizer_ptr(std::wstring_view label) const
Parameters:

label – a Tensor label

Returns:

the TensorCanonicalizer for label if any, else the one for the empty label; null if neither exists

std::shared_ptr<TensorCanonicalizer> nondefault_tensor_canonicalizer_ptr(std::wstring_view label) const

Parameters:

label – a Tensor label

Returns:

the TensorCanonicalizer for exactly label, or null

const TensorCanonicalizer &tensor_canonicalizer(std::wstring_view label) const

Warning

the reference is valid only while the map entry that holds it exists in this context; use tensor_canonicalizer_ptr() if this context may change

Parameters:

label – a Tensor label

Throws:

Exception – if tensor_canonicalizer_ptr(label) is null

Returns:

the TensorCanonicalizer that tensor_canonicalizer_ptr(label) points to

const tensor_index_comparer_t &index_comparer() const
Returns:

the object used by tensor canonicalizers to compare Index objects

const tensor_index_pair_comparer_t &index_pair_comparer() const
Returns:

the object used by tensor canonicalizers to compare pairs of Index objects

std::shared_ptr<const tensor_index_comparer_t> index_comparer_ptr() const
Returns:

the shared object that index_comparer() refers to; passing it to set_index_comparer() keeps the context equal to its unmodified copies

std::shared_ptr<const tensor_index_pair_comparer_t> index_pair_comparer_ptr() const
Returns:

the shared object that index_pair_comparer() refers to; passing it to set_index_pair_comparer() keeps the context equal to its unmodified copies

const container::vector<std::wstring> &cardinal_tensor_labels() const
Returns:

Tensor labels with lexicographic preference (in order)

Context &set(Vacuum vacuum)

Sets the Vacuum for this context, convenient for chaining

Parameters:

vacuum – Vacuum

Returns:

ref to *this, for chaining

Context &set(IndexBasisRegistry ISR)

sets the IndexBasisRegistry for this context

Parameters:

ISR – an IndexBasisRegistry, moved into this context

Returns:

ref to ‘*this’ for chaining

Context &set(std::shared_ptr<const IndexBasisRegistry> ISR)

sets the IndexBasisRegistry for this context

Warning

a registry that is adopted or moved in is the caller’s object, and whatever else the caller kept that reaches it still does: a pointer or reference to it or into it (e.g. from the non-const IndexBasisRegistry::retrieve_space_ptr()), a std::weak_ptr to it, or a shared_ptr that does not own it (e.g. one with a no-op deleter), which cannot be told from its only owner. Modifying the registry through any of these goes unnoticed by the contexts that hold it.

Parameters:

ISR – a IndexBasisRegistry shared_ptr; adopted if it is the only owner of its object (e.g. a temporary or a moved-from shared_ptr), else the object is copied

Returns:

ref to ‘*this’ for chaining

Context &set(AssertStrictBraKetSymmetry assert_strict_braket_symmetry)

Sets the bra-ket strict assertion flag for this context, convenient for chaining

Parameters:

assert_strict_braket_symmetry – AssertStrictBraKetSymmetry

Returns:

ref to *this, for chaining

Context &set(SPBasis spbasis)

Sets the SPBasis for this context, convenient for chaining

Parameters:

spbasis – SPBasis

Returns:

ref to *this, for chaining

Context &set_first_dummy_index_ordinal(std::size_t first_dummy_index_ordinal)

Sets the first dummy index ordinal for this context, convenient for chaining

Parameters:

first_dummy_index_ordinal – the first dummy index ordinal

Returns:

ref to *this, for chaining

Context &set(CanonicalizeOptions copt)

Specifies the canonicalization options

Returns:

ref to *this, for chaining

Context &set(BraKetTypesetting braket_typeset)

Sets the BraKetTypesetting for this context, convenient for chaining

Parameters:

braket_typeset – BraKetTypesetting

Returns:

ref to *this, for chaining

Context &set(BraKetSlotTypesetting braket_slot_typeset)

Sets the BraKetSlotTypesetting for this context, convenient for chaining

Parameters:

braket_slot_typeset – BraKetSlotTypesetting

Returns:

ref to *this, for chaining

Context &set(Symmetry symmetry)

Sets the default bra/ket permutational Symmetry for deserialized tensors

Returns:

ref to *this, for chaining

Context &set(Hermiticity hermiticity)

Sets the default Hermiticity for deserialized tensors (the braket symmetry of a deserialized tensor is derived from this and its base field)

Returns:

ref to *this, for chaining

Context &set(ColumnSymmetry column_symmetry)

Sets the default ColumnSymmetry for deserialized tensors

Returns:

ref to *this, for chaining

Context &set_tensor_canonicalizer(std::wstring_view label, std::shared_ptr<TensorCanonicalizer> canonicalizer)

Sets the TensorCanonicalizer for Tensor objects labeled label , replacing the existing one, if any; the empty label applies to Tensor objects without a label-specific canonicalizer

Warning

version() identifies the canonicalizer by the object the shared_ptr owns, so a shared_ptr that does not own its object (e.g. one with a no-op deleter) makes the version change when the shared_ptr, not the object, dies

Parameters:

canonicalizer – a nonnull TensorCanonicalizer

Throws:

Exception – if canonicalizer is null

Returns:

ref to *this, for chaining

Context &unset_tensor_canonicalizer(std::wstring_view label)

Removes the TensorCanonicalizer for label , if any

Returns:

ref to *this, for chaining

Context &set_index_comparer(tensor_index_comparer_t comparer)

Sets the Index comparer used by tensor canonicalizers

Parameters:

comparer – a nonempty Index comparer

Returns:

ref to *this, for chaining

Context &set_index_comparer(std::shared_ptr<const tensor_index_comparer_t> comparer)

Sets the Index comparer used by tensor canonicalizers to a shared object

Warning

see the warning of set_tensor_canonicalizer() about a shared_ptr that does not own its object

Parameters:

comparer – a nonnull pointer to a nonempty Index comparer, e.g. one obtained from index_comparer_ptr()

Returns:

ref to *this, for chaining

Context &set_index_pair_comparer(tensor_index_pair_comparer_t comparer)

Sets the Index pair comparer used by tensor canonicalizers

Parameters:

comparer – a nonempty Index pair comparer

Returns:

ref to *this, for chaining

Context &set_index_pair_comparer(std::shared_ptr<const tensor_index_pair_comparer_t> comparer)

Sets the Index pair comparer used by tensor canonicalizers to a shared object

Warning

see the warning of set_tensor_canonicalizer() about a shared_ptr that does not own its object

Parameters:

comparer – a nonnull pointer to a nonempty Index pair comparer, e.g. one obtained from index_pair_comparer_ptr()

Returns:

ref to *this, for chaining

Context &set_cardinal_tensor_labels(container::vector<std::wstring> labels)

Sets the cardinal Tensor labels

Parameters:

labels – the complete list of cardinal labels, without duplicates; the default labels (reserved::antisymm_label(), reserved::symm_label(), reserved::transposition_label()) are not prepended, so include them where they should keep their precedence (mbpt::cardinal_tensor_labels() returns such a complete list)

Returns:

ref to *this, for chaining

Public Static Functions

static inline SEQUANT_PRAGMA_IGNORE_DEPRECATED_END Options make_default_options ()
struct Defaults

Public Static Attributes

static constexpr auto vacuum = Vacuum::Physical
static constexpr auto assert_strict_braket_symmetry = true
static constexpr auto spbasis = SPBasis::Spinor
static constexpr auto first_dummy_index_ordinal = 100
static constexpr auto braket_typesetting = BraKetTypesetting::ContraSub
static constexpr auto braket_slot_typesetting = BraKetSlotTypesetting::TensorPackage
static constexpr auto deserialization_symmetry = Symmetry::Nonsymm
static constexpr auto deserialization_hermiticity = Hermiticity::NonHermitian
static constexpr auto deserialization_column_symmetry = ColumnSymmetry::Nonsymm
struct Options

helper for the named-parameter constructor of Context

see the Context documentation for detailed description

Public Members

SEQUANT_DESIGNATED_INIT_ONLY
std::shared_ptr<const IndexBasisRegistry> index_basis_registry_shared_ptr = nullptr

a shared_ptr to an IndexBasisRegistry object; the Context adopts it if it is the only owner of the object (e.g. a temporary or a moved-from shared_ptr), else copies the object; see the warning of Context::set(std::shared_ptr<const IndexBasisRegistry>)

std::optional<IndexBasisRegistry> index_basis_registry = std::nullopt

an IndexBasisRegistry object, moved into the Context; used if index_basis_registry_shared_ptr is null and it is nonnull

std::shared_ptr<const IndexBasisRegistry> index_space_registry_shared_ptr = nullptr

Deprecated:

use index_basis_registry_shared_ptr

std::optional<IndexBasisRegistry> index_space_registry = std::nullopt

Deprecated:

use index_basis_registry

Vacuum vacuum = Defaults::vacuum

the Vacuum object

bool assert_strict_braket_symmetry = Defaults::assert_strict_braket_symmetry

the flag that controls the strictness of bra-ket checks in tensor network construction

SPBasis spbasis = Defaults::spbasis

the SPBasis object

std::size_t first_dummy_index_ordinal = Defaults::first_dummy_index_ordinal

the first dummy index ordinal

std::optional<CanonicalizeOptions> canonicalization_options = std::nullopt

the default canonicalization options

BraKetTypesetting braket_typesetting = Defaults::braket_typesetting

the BraKetTypesetting object

BraKetSlotTypesetting braket_slot_typesetting = Defaults::braket_slot_typesetting

the BraKetSlotTypesetting object

Symmetry deserialization_symmetry = Defaults::deserialization_symmetry

the default bra/ket permutational Symmetry for deserialized tensors

Hermiticity deserialization_hermiticity = Defaults::deserialization_hermiticity

the default Hermiticity for deserialized tensors; the braket symmetry of a deserialized tensor is derived from this and its base_field

ColumnSymmetry deserialization_column_symmetry = Defaults::deserialization_column_symmetry

the default ColumnSymmetry (particle-permutation symmetry) for deserialized tensors

std::optional<container::map<std::wstring, std::shared_ptr<TensorCanonicalizer>>> tensor_canonicalizers = std::nullopt

label -> TensorCanonicalizer map, without null entries; the empty label maps to a DefaultTensorCanonicalizer unless given

std::optional<tensor_index_comparer_t> index_comparer = std::nullopt

the Index comparer used by tensor canonicalizers; if not set, TensorCanonicalizer::default_index_comparer()

std::optional<tensor_index_pair_comparer_t> index_pair_comparer = std::nullopt

the Index pair comparer used by tensor canonicalizers; if not set, TensorCanonicalizer::default_index_pair_comparer()

std::optional<container::vector<std::wstring>> cardinal_tensor_labels = std::nullopt

the cardinal Tensor labels; if not set, the reserved labels