# Copyright (C) 2024 qBraid## This file is part of the qBraid-SDK## The qBraid-SDK is free software released under the GNU General Public License v3# or later. You can redistribute and/or modify it under the terms of the GPL v3.# See the LICENSE file in the project root or <https://www.gnu.org/licenses/gpl-3.0.html>.## THERE IS NO WARRANTY for the qBraid-SDK, as per Section 15 of the GPL v3."""Module defining base program type specification"""fromtypingimportAny,Callable,Optional,Typefrom.experimentimportExperimentTypefrom.registryimport(derive_program_type_alias,get_native_experiment_type,is_registered_alias_native,register_program_type,)
[docs]classProgramSpec:"""Base class used to register program type and type alias."""
@propertydefprogram_type(self)->Type[Any]:"""Return the registered program type."""returnself._program_type@propertydefalias(self)->str:"""Return the alias of the registered program type."""returnself._alias@propertydefnative(self)->bool:"""True if program is natively supported by qBraid, False otherwise."""returnself._native@propertydefexperiment_type(self)->Optional[ExperimentType]:"""Getter for experiment type."""returnself._experiment_type@experiment_type.setterdefexperiment_type(self,value:Optional[ExperimentType]):"""Setter for experiment type with logic for native aliases."""ifvalueisnotNone:self._experiment_type=valueelifself._native:self._experiment_type=get_native_experiment_type(self._alias)else:self._experiment_type=Nonedefserialize(self,program:Any)->Any:""" Convert the given program to a format suitable for submission the qBraid API using the serialize lambda. Args: program (Any): The program to convert. Returns: Any: The serialized program, or the program itself if serialize is None. """returnself._serialize(program)defvalidate(self,program:Any)->None:""" Validate the given program using the validate lambda. Args: program (Any): The program to validate. Raises: ValueError: If the program is invalid. """self._validate(program)def__str__(self)->str:returnf"ProgramSpec({self._program_type.__name__}, {self.alias})"def__repr__(self)->str:return(f"<ProgramSpec('{self._program_type.__module__}.{self._program_type.__name__}', "f"'{self.alias}')>")def__eq__(self,other:object)->bool:""" Compare this ProgramSpec object with another object for equality based on type and alias. Args: other (object): Another object to compare against. Returns: bool: True if both objects are instances of ProgramSpec and have the same type and alias, False otherwise. """ifnotisinstance(other,ProgramSpec):returnFalsethis_spec=(self._program_type,self._alias,self._native)other_spec=(other._program_type,other._alias,other._native)returnthis_spec==other_spec