# Copyright 2018-2021 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."""This package contains unified functions for framework-agnostic tensor and arraymanipulation. Given the input tensor-like object, the call is dispatchedto the corresponding array manipulation framework, allowing for end-to-enddifferentiation to be preserved... warning:: These functions are experimental, and only a subset of common functionality is supported. Furthermore, the names and behaviour of these functions may differ from similar functions in common frameworks; please refer to the function docstrings for more details.The following frameworks are currently supported:* NumPy* Autograd* TensorFlow* PyTorch* JAX"""importautorayasarfrom.is_independentimportis_independentfrom.matrix_manipulationimportexpand_matrix,expand_vector,reduce_matrices,get_batch_sizefrom.multi_dispatchimport(add,array,block_diag,concatenate,detach,diag,dot,einsum,expm,eye,frobenius_inner_product,gammainc,get_trainable_indices,iscomplex,kron,matmul,multi_dispatch,norm,svd,ones_like,scatter,scatter_element_add,set_index,stack,tensordot,unwrap,where,)from.quantumimport(cov_matrix,dm_from_state_vector,expectation_value,marginal_prob,mutual_info,partial_trace,purity,reduce_dm,reduce_statevector,relative_entropy,sqrt_matrix,vn_entropy,vn_entanglement_entropy,max_entropy,min_entropy,trace_distance,)from.fidelityimportfidelity,fidelity_statevectorfrom.utilsimport(allclose,allequal,cast,cast_like,convert_like,in_backprop,requires_grad,is_abstract,)from.interface_utilsimport(get_canonical_interface_name,SupportedInterfaceUserInput,SUPPORTED_INTERFACE_NAMES,get_deep_interface,get_interface,Interface,)from.gradimportgrad,jacobiansum=ar.numpy.sumtoarray=ar.numpy.to_numpyT=ar.numpy.transpose
[docs]defget_dtype_name(x)->str:"""An interface independent way of getting the name of the datatype. >>> x = tf.Variable(0.1) >>> qml.math.get_dtype_name(tf.Variable(0.1)) 'float32' """returnar.get_dtype_name(x)
classNumpyMimic(ar.autoray.NumpyMimic):"""Subclass of the Autoray NumpyMimic class in order to support the NumPy fft submodule"""# pylint: disable=too-few-public-methodsdef__getattribute__(self,fn):iffn=="fft":returnnumpy_fftreturnsuper().__getattribute__(fn)numpy_mimic=NumpyMimic()numpy_fft=ar.autoray.NumpyMimic("fft")# small constant for numerical stability that the user can modifyeps=1e-14def__getattr__(name):returngetattr(numpy_mimic,name)__all__=["add","allclose","allequal","array","block_diag","cast","cast_like","concatenate","convert_like","cov_matrix","detach","diag","dm_from_state_vector","dot","einsum","expand_matrix","expand_vector","expectation_value","eye","fidelity","fidelity_statevector","frobenius_inner_product","get_dtype_name","get_interface","get_canonical_interface_name","get_deep_interface","get_trainable_indices","grad","in_backprop","is_abstract","is_independent","iscomplex","jacobian","marginal_prob","max_entropy","min_entropy","multi_dispatch","mutual_info","ones_like","partial_trace","purity","reduce_dm","reduce_statevector","relative_entropy","requires_grad","sqrt_matrix","scatter_element_add","stack","svd","tensordot","trace_distance","unwrap","vn_entropy","vn_entanglement_entropy","where",]