Coverage for /home/runner/work/tket/tket/pytket/pytket/utils/measurements.py: 100%
25 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-14 11:30 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-14 11:30 +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 :type pauli_string: QubitPauliString
29 :param circ: Circuit to add measurement to.
30 :type circ: Circuit
31 """
32 measured_qbs = []
33 for qb, p in pauli_string.map.items():
34 if p == Pauli.I:
35 continue
36 measured_qbs.append(qb)
37 if p == Pauli.X:
38 circ.H(qb)
39 elif p == Pauli.Y:
40 circ.Rx(0.5, qb)
41 for b_idx, qb in enumerate(measured_qbs):
42 unit = Bit(b_idx)
43 circ.add_bit(unit, False)
44 circ.Measure(qb, unit)
47def _all_pauli_measurements(
48 operator: QubitPauliOperator, circ: Circuit
49) -> Iterable[Circuit]:
50 """For each term in the operator, yields a copy of the given circuit with the
51 appropriate measurements on each qubit. The trivial term is omitted.
53 :param operator: The operator
54 :type operator: QubitPauliOperator
55 :param circ: The circuit generating the desired state
56 :type circ: Circuit
57 :return: List of circuits in order of term from the operator
58 :rtype: Iterable[Circuit]
59 """
60 for pauli_string in operator._dict.keys():
61 if not pauli_string.map:
62 continue
63 copy = circ.copy()
64 append_pauli_measurement(pauli_string, copy)
65 yield copy