# 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."""Utility functions to interact with and extract information from Pauli words and Pauli sentences."""fromfunctoolsimportsingledispatchfromtypingimportUnionfrompennylane.opsimportIdentity,LinearCombination,PauliX,PauliY,PauliZ,Prod,SProdfrom.utilsimportis_pauli_word
[docs]defpauli_word_prefactor(observable):"""If the operator provided is a valid Pauli word (i.e a single term which may be a tensor product of pauli operators), then this function extracts the prefactor. Args: observable (~.Operator): the operator to be examined Returns: Union[int, float, complex]: The scaling/phase coefficient of the Pauli word. Raises: ValueError: If an operator is provided that is not a valid Pauli word. **Example** >>> pauli_word_prefactor(qml.Identity(0)) 1 >>> pauli_word_prefactor(qml.X(0) @ qml.Y(1)) 1 >>> pauli_word_prefactor(qml.X(0) @ qml.Y(0)) 1j """return_pauli_word_prefactor(observable)
@singledispatchdef_pauli_word_prefactor(observable):"""Private wrapper function for pauli_word_prefactor."""raiseValueError(f"Expected a valid Pauli word, got {observable}")@_pauli_word_prefactor.register(PauliX)@_pauli_word_prefactor.register(PauliY)@_pauli_word_prefactor.register(PauliZ)@_pauli_word_prefactor.register(Identity)def_pw_prefactor_pauli(observable:Union[PauliX,PauliY,PauliZ,Identity],):# pylint:disable=unused-argumentreturn1@_pauli_word_prefactor.register(LinearCombination)def_pw_prefactor_ham(observable:LinearCombination):ifis_pauli_word(observable):returnobservable.coeffs[0]raiseValueError(f"Expected a valid Pauli word, got {observable}")@_pauli_word_prefactor.register(Prod)@_pauli_word_prefactor.register(SProd)def_pw_prefactor_prod_sprod(observable:Union[Prod,SProd]):ps=observable.pauli_repifpsisnotNoneandlen(ps)==1:returnlist(ps.values())[0]raiseValueError(f"Expected a valid Pauli word, got {observable}")