[docs]defmay_compile(self,pkg:Package)->bool:"""Refer to `self.check_may_compile` for details."""returnself.check_may_compile(pkg)isNone
[docs]defcheck_may_compile(self,pkg:Package)->UncompilableError|None:"""Check whether any issues can be detected that would arise when trying to compile the given package, e.g. the package containing unsupported gates. Note that this function returning without error is not a guarantee that a subsequent call to `encode` will succeed."""
[docs]@dataclass(frozen=True)classReplacementCompiler(LogicalCompiler):"""A composable pass that replaces extension ops in a `Hugr` according to the given mappings. See `guppyft._bindings._run_replacement_compiler` for semantic details. """op_replacements:dict[tuple[str,str],tuple[str,str,list[int|str]]]"""Maps each source `(extension_name, op_name)` pair (taking no type args) to a target `(extension_name, op_name, args)` triple, where `args` is the list of type args (integers or strings) used to instantiate the target op."""compound_op_replacements:dict[tuple[str,str],Hugr[Any]|GuppyFunctionDefinition[[Any],Any]|Package]=field(default_factory=dict)"""Replaces each source `(extension_name, op_name)` pair (taking no type args) with the given HUGR. When a Guppy function is given as a replacement, it is compiled to HUGR first."""ty_replacements:dict[tuple[str,str],tuple[str,str]]=field(default_factory=dict)"""Optional mapping between src and tgt types to be replaced globally during encoding where types are provided in the form `(extension_name, ty_name)`. """extensions:ExtensionRegistry|None=None"""Optional JSON-encoded list of additional extension definitions, used to resolve target ops/types that are not already registered on the input Hugr."""def__post_init__(self)->None:duplicates=self.op_replacements.keys()&self.compound_op_replacements.keys()ifduplicates:raiseValueError("Duplicate op replacement(s) found in both `op_replacements` and "f"`compound_op_replacements`: {sorted(duplicates)}")forop,replinself.compound_op_replacements.items():ifisinstance(repl,Package)andlen(repl.modules)!=1:raiseValueError(f"Package replacement for op {op} must contain exactly one "f"module, got {len(repl.modules)}")
[docs]defcheck_may_compile(self,pkg:Package)->UncompilableError|None:assertlen(pkg.modules)==1fornode,datainpkg.modules[0].nodes():ifnotisinstance(data.op,ExtOp):continueop_def=data.op.op_def()if(op_def.get_extension().name=="tket.quantum"and("tket.quantum",op_def.name)notinself.op_replacementsand("tket.quantum",op_def.name)notinself.compound_op_replacements):returnUncompilableError(f"Error encoding `{op_def.qualified_name()}` at node {node}. ""Operation not yet supported during encoding.")returnNone