class opt_einsum.paths.BranchBound(nbranch=None, cutoff_flops_factor=4, minimize='flops', cost_fn='memory-removed')[source]

Explores possible pair contractions in a depth-first recursive manner like the optimal approach, but with extra heuristic early pruning of branches as well sieving by memory_limit and the best path found so far. Returns the lowest cost path. This algorithm still scales factorially with respect to the elements in the list input_sets if nbranch is not set, but it scales exponentially like nbranch**len(input_sets) otherwise.

  • nbranch (None or int, optional) – How many branches to explore at each contraction step. If None, explore all possible branches. If an integer, branch into this many paths at each step. Defaults to None.

  • cutoff_flops_factor (float, optional) – If at any point, a path is doing this much worse than the best path found so far was, terminate it. The larger this is made, the more paths will be fully explored and the slower the algorithm. Defaults to 4.

  • minimize ({‘flops’, ‘size’}, optional) – Whether to optimize the path with regard primarily to the total estimated flop-count, or the size of the largest intermediate. The option not chosen will still be used as a secondary criterion.

  • cost_fn (callable, optional) – A function that returns a heuristic ‘cost’ of a potential contraction with which to sort candidates. Should have signature cost_fn(size12, size1, size2, k12, k1, k2).

__init__(nbranch=None, cutoff_flops_factor=4, minimize='flops', cost_fn='memory-removed')[source]

Initialize self. See help(type(self)) for accurate signature.


__call__(inputs, output, size_dict[, …])

  • input_sets (list) – List of sets that represent the lhs side of the einsum subscript

__delattr__(name, /)

Implement delattr(self, name).


Default dir() implementation.

__eq__(value, /)

Return self==value.

__format__(format_spec, /)

Default object formatter.

__ge__(value, /)

Return self>=value.

__getattribute__(name, /)

Return getattr(self, name).

__gt__(value, /)

Return self>value.


Return hash(self).

__init__([nbranch, cutoff_flops_factor, …])

Initialize self.


This method is called when a class is subclassed.

__le__(value, /)

Return self<=value.

__lt__(value, /)

Return self<value.

__ne__(value, /)

Return self!=value.


Create and return a new object.


Helper for pickle.

__reduce_ex__(protocol, /)

Helper for pickle.


Return repr(self).

__setattr__(name, value, /)

Implement setattr(self, name, value).


Size of object in memory, in bytes.


Return str(self).


Abstract classes can override this to customize issubclass().

_check_args_against_first_call(inputs, …)

Utility that stateful optimizers can use to ensure they are not called with different contractions across separate runs.






list of weak references to the object (if defined)