"""Compiling and building emulator instances for guppy programs."""from__future__importannotationsfromdataclassesimportdataclass,replacefromtypingimportTYPE_CHECKING,Anyimportselene_simfromtyping_extensionsimportSelffrom.instanceimportEmulatorInstanceifTYPE_CHECKING:fromcollections.abcimportSequencefrompathlibimportPathfromhugr.packageimportPackagefromselene_coreimportBuildPlanner,QuantumInterface,Utility
[docs]@dataclass(frozen=True)classEmulatorBuilder:"""A builder class for creating EmulatorInstance objects. Supports configuration parameters for compilation of emulator instances. """# interface supported parameters_name:str|None=None_build_dir:Path|None=None_verbose:bool=False# selene_sim supported parameters, may be added in the future:_planner:BuildPlanner|None=None_utilities:Sequence[Utility]|None=None_interface:QuantumInterface|None=None_progress_bar:bool=False_strict:bool=False_save_planner:bool=False_custom_args:dict[str,Any]|None=None@propertydefname(self)->str|None:"""User specified name for the emulator instance. Defaults to None."""returnself._name@propertydefbuild_dir(self)->Path|None:"""Directory to store intermediate build files and execution results. Defaults to None, in which case a temporary directory is used."""returnself._build_dir@propertydefverbose(self)->bool:"""Whether to print verbose output during the build process."""returnself._verbose@propertydefcustom_args(self)->dict[str,Any]|None:"""Custom build arguments passed to selene_sim.build."""returndict(self._custom_args)ifself._custom_argsisnotNoneelseNone
[docs]defwith_name(self,value:str|None)->Self:"""Set the name for the emulator instance."""returnreplace(self,_name=value)
[docs]defwith_build_dir(self,value:Path|None)->Self:"""Set the build directory for the emulator instance, see `EmulatorBuilder.build_dir`."""returnreplace(self,_build_dir=value)
[docs]defwith_verbose(self,value:bool)->Self:"""Set whether to print verbose output during the build process."""returnreplace(self,_verbose=value)
[docs]defwith_build_arg(self,key:str,value:Any)->Self:"""Selene builds may support additional customisable arguments, e.g. to override the choice of compilation route. This is passed to selene_sim.build as kwargs. For example: .. code-block:: python .with_build_arg("build_method", "via-llvm_ir") is equivalent to .. code-block:: python selene_sim.build(..., build_method="via-llvm_ir") which saves LLVM IR into the build directory rather than saving bitcode. """ifself._custom_argsisNone:returnreplace(self,_custom_args={key:value})else:returnreplace(self,_custom_args=self._custom_args|{key:value})
[docs]defbuild(self,package:Package,n_qubits:int)->EmulatorInstance:"""Build an EmulatorInstance from a compiled package. Args: package: The compiled HUGR package to build the emulator from. n_qubits: The number of qubits to allocate for the emulator instance. Returns: An EmulatorInstance that can be used to run the compiled program. """instance=selene_sim.build(# type: ignore[attr-defined]package,name=self._name,build_dir=self._build_dir,interface=self._interface,utilities=self._utilities,verbose=self._verbose,planner=self._planner,progress_bar=self._progress_bar,strict=self._strict,save_planner=self._save_planner,**self._custom_argsor{},)returnEmulatorInstance(_instance=instance,_n_qubits=n_qubits)