Coverage for /home/runner/work/tket/tket/pytket/pytket/utils/term_sequence.py: 95%
31 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-09 15:08 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-09 15:08 +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.
14from typing import TYPE_CHECKING, cast
16from pytket import Circuit
17from pytket.circuit import CircBox, PauliExpBox
18from pytket.partition import (
19 GraphColourMethod,
20 PauliPartitionStrat,
21 term_sequence,
22)
24if TYPE_CHECKING: 24 ↛ 25line 24 didn't jump to line 25 because the condition on line 24 was never true
25 from .._tket.unit_id import UnitID
27from .operators import QubitPauliOperator
30def gen_term_sequence_circuit(
31 operator: QubitPauliOperator,
32 reference_state: Circuit,
33 partition_strat: PauliPartitionStrat = PauliPartitionStrat.CommutingSets,
34 colour_method: GraphColourMethod = GraphColourMethod.Lazy,
35) -> Circuit:
36 """
37 Sequences the terms of a :py:class:`QubitPauliOperator` :math:`P` to generate
38 a circuit approximating :math:`e^{-i \\frac{\\pi}{2} P}`. This method
39 performs Trotterisation on :math:`P` with a single Trotter step.
41 This method uses a given partitioning strategy and a graph colouring
42 method for term sequencing.
44 The resulting Circuit will contain a sequence of CircBoxes. Each CircBox
45 corresponds to a set of Pauli strings. Each exponentiated Pauli string
46 in the set is realised as a PauliExpBox.
48 The ordering of terms prioritises reducing the two qubit gate count of the
49 circuit when the PauliSimp or GuidedPauliSimp passes are applied rather
50 than minimising the trotter error.
52 :param operator: The operator terms to sequence
53 :type operator: QubitPauliOperator
54 :param reference_state: reference state to add sequenced terms to.
55 :type reference_state: Circuit
56 :param partition_strat: a Partition strategy
57 :type partition_strat: PauliPartitionStrat, Optional
58 :param colour_method: a graph colouring method
59 :type colour_method: GraphColourMethod, Optional
60 """
61 qps_list = list(operator._dict.keys()) # noqa: SLF001
62 qps_list_list = term_sequence(qps_list, partition_strat, colour_method)
63 n_qbs = reference_state.n_qubits
64 circ = reference_state.copy()
65 qbs = circ.qubits
66 for out_qps_list in qps_list_list:
67 circ_to_box = Circuit(n_qbs)
68 for qps in out_qps_list:
69 coeff = operator[qps]
70 qps_map = qps.map
71 if qps_map:
72 qubits = []
73 paulis = []
74 for qb, pauli in qps_map.items():
75 qubits.append(qb)
76 paulis.append(pauli)
77 pbox = PauliExpBox(paulis, coeff)
78 circ_to_box.add_pauliexpbox(pbox, qubits)
79 else:
80 circ_to_box.add_phase(-coeff / 2)
81 cbox = CircBox(circ_to_box)
82 unit_ids = cast("list[UnitID]", qbs)
83 circ.add_circbox(cbox, unit_ids)
84 return circ