# Copyright 2019-2024 Cambridge Quantum Computing## 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.fromtypingimportIterablefrompytket.circuitimportCircuit,Bitfrompytket.pauliimportPauli,QubitPauliStringfrom.operatorsimportQubitPauliOperator
[docs]defappend_pauli_measurement(pauli_string:QubitPauliString,circ:Circuit)->None:"""Appends measurement instructions to a given circuit, measuring each qubit in a given basis. :param pauli_string: The pauli string to measure :type pauli_string: QubitPauliString :param circ: Circuit to add measurement to. :type circ: Circuit """measured_qbs=[]forqb,pinpauli_string.map.items():ifp==Pauli.I:continuemeasured_qbs.append(qb)ifp==Pauli.X:circ.H(qb)elifp==Pauli.Y:circ.Rx(0.5,qb)forb_idx,qbinenumerate(measured_qbs):unit=Bit(b_idx)circ.add_bit(unit,False)circ.Measure(qb,unit)
def_all_pauli_measurements(operator:QubitPauliOperator,circ:Circuit)->Iterable[Circuit]:"""For each term in the operator, yields a copy of the given circuit with the appropriate measurements on each qubit. The trivial term is omitted. :param operator: The operator :type operator: QubitPauliOperator :param circ: The circuit generating the desired state :type circ: Circuit :return: List of circuits in order of term from the operator :rtype: Iterable[Circuit] """forpauli_stringinoperator._dict.keys():ifnotpauli_string.map:continuecopy=circ.copy()append_pauli_measurement(pauli_string,copy)yieldcopy