Source code for pennylane.data.data_manager.graphql
"""Module for containing graphql functionality for interacting with the Datasets Service API."""importosfromtypingimportAny,OptionalfromrequestsimportpostGRAPHQL_URL=os.getenv("DATASETS_ENDPOINT_URL","https://cloud.pennylane.ai/graphql")classGraphQLError(BaseException):"""Exception for GraphQL"""defget_graphql(url:str,query:str,variables:Optional[dict[str,Any]]=None):""" Args: url: The URL to send a query to. query: The main body of the query to be sent. variables: Additional input variables to the query body. Returns: string: json response. GraphQLError: if there no response is received or errors are received in the json response. """json={"query":query}ifvariables:json["variables"]=variablesresponse=post(url=url,json=json,timeout=10,headers={"content-type":"application/json"})response.raise_for_status()if"errors"inresponse.json():all_errors=",".join(error["message"]forerrorinresponse.json()["errors"])raiseGraphQLError(f"Errors in request: {all_errors}")returnresponse.json()defget_dataset_urls(class_id:str,parameters:dict[str,list[str]])->list[tuple[str,str]]:""" Args: class_id: Dataset class id e.g 'qchem', 'qspin' parameters: Dataset parameters e.g 'molname', 'basis' Returns: list of tuples (dataset_id, dataset_url) Example usage: >>> get_dataset_urls("qchem", {"molname": ["H2"], "basis": ["STO-3G"], "bondlength": ["0.5"]}) [("H2_STO-3G_0.5", "https://cloud.pennylane.ai/datasets/h5/qchem/h2/sto-3g/0.5.h5")] """response=get_graphql(GRAPHQL_URL,""" query GetDatasetsForDownload($datasetClassId: String!, $parameters: [DatasetParameterInput!]) { datasetClass(id: $datasetClassId) { datasets(parameters: $parameters) { id downloadUrl } } } """,{"datasetClassId":class_id,"parameters":parameters},)return[(resp["id"],resp["downloadUrl"])forrespinresponse["data"]["datasetClass"]["datasets"]]
[docs]deflist_data_names()->list[str]:"""Get list of dataclass IDs."""response=get_graphql(GRAPHQL_URL,""" query GetDatasetClasses { datasetClasses { id } } """,)return[dsc["id"]fordscinresponse["data"]["datasetClasses"]]
[docs]deflist_attributes(data_name)->list[str]:r"""List the attributes that exist for a specific ``data_name``. Args: data_name (str): The type of the desired data Returns: list (str): A list of accepted attributes for a given data name .. seealso:: :func:`~.load_interactive`, :func:`~.list_data_names`, :func:`~.load`. **Example** >>> qml.data.list_attributes(data_name="qchem") ['basis_rot_groupings', 'basis_rot_samples', 'dipole_op', ... 'vqe_gates', 'vqe_params'] """response=get_graphql(GRAPHQL_URL,""" query ListAttributes($datasetClassId: String!) { datasetClass(id: $datasetClassId) { attributes { name } } } """,{"datasetClassId":data_name},)return[attribute["name"]forattributeinresponse["data"]["datasetClass"]["attributes"]]
def_get_parameter_tree(class_id)->tuple[list[str],list[str],dict]:"""Returns the (parameters, attributes, parameter_tree) for a given ``class_id``."""response=get_graphql(GRAPHQL_URL,""" query GetParameterTree($datasetClassId: String!) { datasetClass(id: $datasetClassId) { attributes { name } parameters { name } parameterTree } } """,{"datasetClassId":class_id},)parameters=[param["name"]forparaminresponse["data"]["datasetClass"]["parameters"]]attributes=[atr["name"]foratrinresponse["data"]["datasetClass"]["attributes"]]return(parameters,attributes,response["data"]["datasetClass"]["parameterTree"])