Coverage for /home/runner/work/tket/tket/pytket/pytket/utils/measurements.py: 100%
25 statements
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-02 12:44 +0000
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-02 12:44 +0000
1# Copyright Quantinuum
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
15from collections.abc import Iterable
17from pytket.circuit import Bit, Circuit
18from pytket.pauli import Pauli, QubitPauliString
20from .operators import QubitPauliOperator
23def append_pauli_measurement(pauli_string: QubitPauliString, circ: Circuit) -> None:
24 """Appends measurement instructions to a given circuit, measuring each qubit in a
25 given basis.
27 :param pauli_string: The pauli string to measure
28 :param circ: Circuit to add measurement to.
29 """
30 measured_qbs = []
31 for qb, p in pauli_string.map.items():
32 if p == Pauli.I:
33 continue
34 measured_qbs.append(qb)
35 if p == Pauli.X:
36 circ.H(qb)
37 elif p == Pauli.Y:
38 circ.Rx(0.5, qb)
39 for b_idx, qb in enumerate(measured_qbs):
40 unit = Bit(b_idx)
41 circ.add_bit(unit, False)
42 circ.Measure(qb, unit)
45def _all_pauli_measurements(
46 operator: QubitPauliOperator, circ: Circuit
47) -> Iterable[Circuit]:
48 """For each term in the operator, yields a copy of the given circuit with the
49 appropriate measurements on each qubit. The trivial term is omitted.
51 :param operator: The operator
52 :param circ: The circuit generating the desired state
53 :return: List of circuits in order of term from the operator
54 """
55 for pauli_string in operator._dict.keys(): # noqa: SLF001, SIM118
56 if not pauli_string.map:
57 continue
58 copy = circ.copy()
59 append_pauli_measurement(pauli_string, copy)
60 yield copy