Coverage for /home/runner/work/tket/tket/pytket/pytket/utils/term_sequence.py: 100%

30 statements  

« 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. 

14from typing import cast 

15 

16from pytket import Circuit 

17from pytket.circuit import CircBox, PauliExpBox 

18from pytket.partition import ( 

19 GraphColourMethod, 

20 PauliPartitionStrat, 

21 term_sequence, 

22) 

23 

24from .._tket.unit_id import UnitID 

25from .operators import QubitPauliOperator 

26 

27 

28def gen_term_sequence_circuit( 

29 operator: QubitPauliOperator, 

30 reference_state: Circuit, 

31 partition_strat: PauliPartitionStrat = PauliPartitionStrat.CommutingSets, 

32 colour_method: GraphColourMethod = GraphColourMethod.Lazy, 

33) -> Circuit: 

34 """ 

35 Sequences the terms of a :py:class:`QubitPauliOperator` :math:`P` to generate 

36 a circuit approximating :math:`e^{-i \\frac{\\pi}{2} P}`. This method 

37 performs Trotterisation on :math:`P` with a single Trotter step. 

38 

39 This method uses a given partitioning strategy and a graph colouring 

40 method for term sequencing. 

41 

42 The resulting Circuit will contain a sequence of CircBoxes. Each CircBox 

43 corresponds to a set of Pauli strings. Each exponentiated Pauli string 

44 in the set is realised as a PauliExpBox. 

45 

46 The ordering of terms prioritises reducing the two qubit gate count of the 

47 circuit when the PauliSimp or GuidedPauliSimp passes are applied rather 

48 than minimising the trotter error. 

49 

50 :param operator: The operator terms to sequence 

51 :type operator: QubitPauliOperator 

52 :param reference_state: reference state to add sequenced terms to. 

53 :type reference_state: Circuit 

54 :param partition_strat: a Partition strategy 

55 :type partition_strat: PauliPartitionStrat, Optional 

56 :param colour_method: a graph colouring method 

57 :type colour_method: GraphColourMethod, Optional 

58 """ 

59 qps_list = list(operator._dict.keys()) 

60 qps_list_list = term_sequence(qps_list, partition_strat, colour_method) 

61 n_qbs = reference_state.n_qubits 

62 circ = reference_state.copy() 

63 qbs = circ.qubits 

64 for out_qps_list in qps_list_list: 

65 circ_to_box = Circuit(n_qbs) 

66 for qps in out_qps_list: 

67 coeff = operator[qps] 

68 qps_map = qps.map 

69 if qps_map: 

70 qubits = list() 

71 paulis = list() 

72 for qb, pauli in qps_map.items(): 

73 qubits.append(qb) 

74 paulis.append(pauli) 

75 pbox = PauliExpBox(paulis, coeff) 

76 circ_to_box.add_pauliexpbox(pbox, qubits) 

77 else: 

78 circ_to_box.add_phase(-coeff / 2) 

79 cbox = CircBox(circ_to_box) 

80 unit_ids = cast(list[UnitID], qbs) 

81 circ.add_circbox(cbox, unit_ids) 

82 return circ