auto_contractor.eval — Expression Evaluation Engine¶
Source: qlat/auto_contractor/eval.py
Note: Update this document when updating the source file.
Outline¶
Overview¶
eval is the runtime evaluation module of the auto-contractor framework. It
takes a compiled CExpr (from compile), generates Python or Cython code,
compiles it if needed, and provides the eval_cexpr entry point for computing
correlator values from propagator lookup functions. It supports AMA
(all-mode averaging) for multi-precision evaluation.
The typical workflow is:
Obtain a
CExprviacompile.contract_simplify_compile(*exprs).Call
cache_compiled_cexpr(calc_cexpr, path)to optimise, generate code, optionally compile Cython, and return aCCExpr.Call
eval_cexpr(ccexpr, positions_dict=..., get_prop=...)for each gauge configuration.
CCExpr Class¶
CCExpr wraps a compiled and loaded expression module.
ccexpr = CCExpr(cexpr_all, module, base_positions_dict=None, options=None)
Attribute |
Type |
Description |
|---|---|---|
|
dict |
Contains |
|
module |
Loaded Python/Cython module with |
|
dict |
Default position mappings merged into every call |
|
dict |
|
|
callable |
The raw |
|
int |
FLOP count per sloppy evaluation |
|
list[str] |
Names of all expressions |
|
list |
Diagram type definitions |
|
list[str] |
Position variable names |
Methods¶
Method |
Description |
|---|---|
|
Return expression name list |
|
Evaluate, merging |
Compilation and Caching¶
cache_compiled_cexpr(calc_cexpr, path, *, is_cython=True, is_distillation=False, base_positions_dict=None)¶
The main compilation entry point. Performs the following steps:
Call
calc_cexpr()to obtain theCExpr(cached via pickle).Call
cexpr.optimize()to run CSE (cached via pickle).Generate Python/Cython code via
cexpr_code_gen_py(cached via pickle).If Cython: write
meson.build, runmeson setupandmeson compile.Load the compiled module and return a
CCExpr.
All intermediate results are pickled to path/ for reuse. The path is
suffixed with _cy or _py depending on is_cython. Only node 0 performs
the build; other nodes wait for the pickle file to appear.
meson_build_content¶
A string containing the Meson build definition for compiling Cython extension
modules. It links against qlat-utils, NumPy, Eigen, OpenMP, and zlib.
Evaluation¶
eval_cexpr(ccexpr, *, positions_dict, get_prop, is_ama_and_sloppy=False)¶
Evaluate a compiled expression.
Parameter |
Type |
Description |
|---|---|---|
|
|
Compiled expression object |
|
dict |
Maps position names to |
|
callable |
|
|
bool |
If True, return |
Return value: A 1-D np.array of complex values (one per expression), or
a tuple of two such arrays when is_ama_and_sloppy=True.
The get_prop callback is called as:
get_prop("l", pos_snk, pos_src)— for quark propagatorsget_prop("U", tag, pos, mu)— for gauge links
get_expr_names(ccexpr)¶
Return the list of expression names. Accepts both CExpr and CCExpr.
get_diagram_type_dict(cexpr)¶
Return diagram_type_dict[diagram_type] = name. Accepts both CExpr and
CCExpr.
Benchmarking¶
benchmark_eval_cexpr(cexpr, *, benchmark_size=10, benchmark_num=10, benchmark_num_ama=2, benchmark_rng_state=None, base_positions_dict=None)¶
Run a correctness and performance benchmark of eval_cexpr. Generates random
positions and propagators, evaluates the expression multiple times, and verifies
that results are consistent across runs and between AMA and sloppy modes.
Returns (check, check_ama) — lists of complex fingerprints derived from
dotting the result arrays with random check vectors.
Random Matrix Generators¶
These functions create random test matrices for benchmarking:
Function |
Return Type (normal) |
Return Type (distillation) |
|---|---|---|
|
|
|
|
|
|
|
|
|
When is_distillation=True, colour dimension nc=10 and spin dimension ns=4.
Utility Functions¶
Function |
Description |
|---|---|
|
Format a check vector as a space-separated scientific notation string |
|
Square real and imaginary parts independently |
|
Square root of real and imaginary parts independently |
|
Element-wise |
|
Element-wise |
Examples¶
import qlat as q
import auto_contractor as ac
q.begin_with_mpi([[1, 1, 1, 4]])
# Build and compile a simple expression
def calc_cexpr():
expr = ac.Qb("u", "x", "s1", "c1") * ac.G(5, "s1", "s2") * ac.Qv("u", "x", "s2", "c1")
return ac.contract_simplify_compile(expr, is_isospin_symmetric_limit=True)
ccexpr = ac.cache_compiled_cexpr(calc_cexpr, "cache/pi_test", is_cython=False)
# Evaluate with a dummy propagator
size = q.Coordinate([4, 4, 4, 8])
positions_dict = {
"size": size,
"x": ("point", q.Coordinate([0, 0, 0, 0])),
}
def get_prop(ptype, *args):
if ptype == "U":
return q.ColorMatrix()
wm = q.WilsonMatrix()
arr = wm[:]
arr[:] = 0
return wm
result = ac.eval_cexpr(ccexpr, positions_dict=positions_dict, get_prop=get_prop)
print(f"Result: {result}")
q.end_with_mpi()