Protocols

This module provides various protocols used throughout the package.

A typing.Protocol is an interface that a class can fulfill structurally, without explicitly inheriting from it. For example, any class that implements a __len__ method satisfies Sized, whether or not it says so anywhere in its class hierarchy.

The protocols in this module follow the same idea, but each one centers on a single, otherwise unremarkable dunder-style method (for example _linear_operator_). A class implementing that one method thereby satisfies the corresponding protocol – SupportsLinearOperator in this example – and becomes usable wherever that protocol is expected, without any explicit registration. Note that “protocol” is sometimes also used loosely to refer to a type-agnostic dispatch function built on top of a protocol (for example fermion_operator()) rather than the interface itself; the distinction should be clear from context.

Some protocols below are paired with a small, type-agnostic helper function that dispatches to the protocol method on whatever object it is given. The simulation protocols (SupportsApplyUnitary, SupportsLinearOperator and SupportsTrace) have none: they are the same contracts ffsim defines, so ffsim.apply_unitary(), ffsim.linear_operator() and ffsim.trace() dispatch to them already.

Protocol

Method

Helper function

SupportsFermionOperator

_fermion_operator_(self)

fermion_operator()

SupportsMajoranaOperator

_majorana_operator_(self)

majorana_operator()

SupportsLinearOperator

_linear_operator_(self, norb, nelec)

SupportsTrace

_trace_(self, norb, nelec)

SupportsApplyUnitary

_apply_unitary_(self, vec, norb, nelec, copy)

SupportsApplyUnitaryPlaced

_apply_unitary_placed_(self, vec, norb, nelec, copy, freg_indices)

SupportsCommutators

_commutator_, _anti_commutator_, _double_commutator_ (all @staticmethod)

commutator(), anti_commutator(), double_commutator()

SupportsApplyUnitary(*args, **kwargs)

The state-vector simulation contract this package implements.

SupportsApplyUnitaryPlaced(*args, **kwargs)

A package-specific extension of SupportsApplyUnitary carrying a mode placement.

SupportsCommutators(*args, **kwargs)

A Protocol indicating support for efficient commutator generation.

SupportsFermionOperator(*args, **kwargs)

This package's operator-conversion contract, converting an operator to a FermionOperator.

SupportsLinearOperator(*args, **kwargs)

The linear-operator conversion contract this package implements.

SupportsMajoranaOperator(*args, **kwargs)

An equivalent of the SupportsFermionOperator protocol, targeting Majorana operators.

SupportsTrace(*args, **kwargs)

The trace contract this package implements.