Code Generation¶
Once an equation has been derived, simplified, and optimized, the final step is turning it
into code that a numerical tensor library or quantum-chemistry program can actually run. SeQuant’s export framework
(SeQuant/core/export) does this by walking a binarized evaluation tree and driving a pluggable code-generation backend, so
adding support for a new target language means implementing one small interface rather than writing a new tree-walker.
The Generator interface¶
A backend is a subclass of sequant::Generator: a set of callbacks
(create, load, compute, unload, declare, …) that get invoked, in the right order, while
sequant::export_expression() traverses a ResultExpr’s
evaluation tree. Writing a new backend means implementing this callback interface; the tree-walking, scalar-factor bookkeeping, and
intermediate-reuse logic are handled once, centrally, for every backend — documented for contributors in the developer guide. sequant::TextGenerator is a minimal,
dependency-free backend that renders these callbacks as human-readable pseudocode — useful both as documentation of the callback
sequence and as a debugging aid for a real backend under development:
// a small tensor equation: R^{a_1}_{i_1} = f^{a_1}_{i_1} + t^{a_1}_{i_1}
auto result = ResultExpr(Tensor(L"R", bra{L"i_1"}, ket{L"a_1"}),
ex<Tensor>(L"f", bra{L"i_1"}, ket{L"a_1"}) +
ex<Tensor>(L"t", bra{L"i_1"}, ket{L"a_1"}));
// export_expression() walks a binarized evaluation tree and invokes the
// matching Generator callback (create/load/compute/unload/...) for every
// step; TextGenerator is a dependency-free backend that renders those
// callbacks as human-readable pseudocode -- useful for prototyping a new
// Generator subclass, or for debugging what a real backend would emit
TextGenerator<TextGeneratorContext> generator;
TextGeneratorContext ctx;
export_expression(to_export_tree(result), generator, ctx);
std::cout << generator.get_generated_code() << std::endl;
Available backends¶
Beyond TextGenerator, SeQuant ships several backends targeting real numerical tensor libraries and domain-specific formats:
sequant::ItfGeneratorgenerates ITF (Integrated Tensor Framework) code, a domain-specific tensor-contraction language used by parts of the Molpro quantum chemistry program.sequant::PythonEinsumGeneratorBaseand its NumPy/PyTorch specializations generate Python code usingeinsum-style tensor contractions.sequant::JuliaTensorOperationsGenerator,sequant::JuliaITensorGenerator, andsequant::JuliaTensorKitGeneratorgenerate Julia code targeting, respectively, the TensorOperations.jl, ITensors.jl, and TensorKit.jl packages.
Each backend has its own Context type (e.g. ItfContext) for backend-specific settings such as naming conventions; consult its
reference documentation for details. Numerically evaluating an expression directly in-process, rather than generating source code
for it, is a separate concern handled by SeQuant/core/eval (see Evaluation Trace Analysis for its trace/profiling tooling).