Source code for pennylane.data.attributes.dictionary
# Copyright 2018-2023 Xanadu Quantum Technologies Inc.# Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at# http://www.apache.org/licenses/LICENSE-2.0# Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License."""Contains an DatasetAttribute that allows for heterogenous dictionariesof Dataset attributes."""fromcollections.abcimportIterator,Mapping,MutableMappingfromtypingimportGeneric,Unionfrompennylane.data.base.attributeimportDatasetAttributefrompennylane.data.base.hdf5importHDF5Any,HDF5Groupfrompennylane.data.base.mapperimportMapperMixinfrompennylane.data.base.typing_utilimportT
[docs]classDatasetDict(# pylint: disable=too-many-ancestorsGeneric[T],DatasetAttribute[HDF5Group,Mapping[str,T],Mapping[str,T]],MutableMapping[str,T],MapperMixin,):"""Provides a dict-like collection for Dataset attribute types. Keys must be strings."""type_id="dict"def__post_init__(self,value:Mapping[str,T]):super().__post_init__(value)self.update(value)
[docs]defcopy(self)->dict[str,T]:"""Returns a copy of this mapping as a builtin ``dict``, with all elements copied."""returnself.copy_value()
def__getitem__(self,__key:str)->T:self._check_key(__key)returnself._mapper[__key].get_value()def__setitem__(self,__key:str,__value:Union[T,DatasetAttribute[HDF5Any,T,T]])->None:self._check_key(__key)if__keyinself:delself[__key]self._mapper[__key]=__valuedef__delitem__(self,__key:str)->None:self._check_key(__key)delself._mapper[__key]def__len__(self)->int:returnlen(self.bind)def__eq__(self,__value:object)->bool:ifnotisinstance(__value,Mapping):returnFalseifnotlen(self)==len(__value):returnFalseifself.keys()!=__value.keys():returnFalsereturnall(__value[key]==self[key]forkeyin__value.keys())def__iter__(self)->Iterator[str]:return(keyforkeyinself.bind.keys())def__str__(self)->str:returnstr(dict(self))def__repr__(self)->str:returnrepr(dict(self))def_check_key(self,__key:str)->None:"""Checks that __key is a string, and raises a ``TypeError`` if it isn't."""ifnotisinstance(__key,str):raiseTypeError(f"'{type(self).__name__}' keys must be strings.")