Coverage for /home/runner/work/tket/tket/pytket/pytket/circuit/__init__.py: 93%

39 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-17 10:53 +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. 

14 

15"""The circuit module provides an API to interact with the 

16tket :py:class:`Circuit` data structure. 

17 This module is provided in binary form during the PyPI installation.""" 

18from typing import ( 

19 Any, 

20 Callable, 

21 Optional, 

22 Sequence, 

23 Union, 

24) 

25 

26from pytket import wasm 

27from pytket._tket.circuit import * 

28from pytket._tket.circuit import Circuit 

29from pytket._tket.pauli import Pauli 

30from pytket._tket.unit_id import * 

31 

32# prefixes for assertion bits 

33from pytket._tket.unit_id import ( 

34 _DEBUG_ONE_REG_PREFIX, 

35 _DEBUG_ZERO_REG_PREFIX, 

36 Bit, 

37 BitRegister, 

38) 

39 

40from .logic_exp import ( 

41 BinaryOp, 

42 Ops, 

43 if_bit, 

44 if_not_bit, 

45 reg_eq, 

46 reg_geq, 

47 reg_gt, 

48 reg_leq, 

49 reg_lt, 

50 reg_neq, 

51) 

52 

53 

54def add_wasm( 

55 self: Circuit, 

56 funcname: str, 

57 filehandler: wasm.WasmModuleHandler, 

58 list_i: Sequence[int], 

59 list_o: Sequence[int], 

60 args: Union[Sequence[int], Sequence[Bit]], 

61 args_wasm: Optional[Sequence[int]] = None, 

62 **kwargs: Any, 

63) -> Circuit: 

64 """Add a classical function call from a wasm file to the circuit. 

65 \n\n:param funcname: name of the function that is called 

66 \n:param filehandler: wasm file or module handler to identify the wasm module 

67 \n:param list_i: list of the number of bits in the input variables 

68 \n:param list_o: list of the number of bits in the output variables 

69 \n:param args: vector of circuit bits the wasm op should be added to 

70 \n:param args_wasm: vector of wasmstates the wasm op should be added to 

71 \n:param kwargs: additional arguments passed to `add_gate_method` . 

72 Allowed parameters are `opgroup`, `condition` , `condition_bits`, 

73 `condition_value` 

74 \n:return: the new :py:class:`Circuit`""" 

75 

76 if args_wasm is None: 

77 args_wasm = [0] 

78 

79 for x in list_i: 

80 if x > filehandler._int_size: 

81 raise ValueError( 

82 f"only functions with i{filehandler._int_size} type are allowed" 

83 ) 

84 

85 for x in list_o: 

86 if x > filehandler._int_size: 

87 raise ValueError( 

88 f"only functions with i{filehandler._int_size} type are allowed" 

89 ) 

90 

91 if filehandler.check_function(funcname, len(list_i), len(list_o)): 

92 if (len(args_wasm)) > 0: 92 ↛ 94line 92 didn't jump to line 94 because the condition on line 92 was always true

93 self._add_w_register(max(args_wasm) + 1) 

94 return self._add_wasm( 

95 funcname, str(filehandler), list_i, list_o, args, args_wasm, **kwargs 

96 ) 

97 

98 raise ValueError(f"{funcname} not found, check {repr(filehandler)}") 

99 

100 

101setattr(Circuit, "add_wasm", add_wasm) 

102 

103 

104def add_wasm_to_reg( 

105 self: Circuit, 

106 funcname: str, 

107 filehandler: wasm.WasmModuleHandler, 

108 list_i: Sequence[BitRegister], 

109 list_o: Sequence[BitRegister], 

110 args_wasm: Optional[Sequence[int]] = None, 

111 **kwargs: Any, 

112) -> Circuit: 

113 """Add a classical function call from a wasm file to the circuit. 

114 \n\n:param funcname: name of the function that is called 

115 \n:param filehandler: wasm file or module handler to identify the wasm module 

116 \n:param list_i: list of the classical registers assigned to 

117 the input variables of the function call 

118 \n:param list_o: list of the classical registers assigned to 

119 the output variables of the function call 

120 \n:param args_wasm: vector of wasmstates the wasm op should be added to 

121 \n:param kwargs: additional arguments passed to `add_gate_method` . 

122 Allowed parameters are `opgroup`, `condition` , `condition_bits`, 

123 `condition_value` 

124 \n:return: the new :py:class:`Circuit`""" 

125 

126 if args_wasm is None: 126 ↛ 129line 126 didn't jump to line 129 because the condition on line 126 was always true

127 args_wasm = [0] 

128 

129 if filehandler.checked: 

130 for reg in list_i: 

131 if reg.size > 32: 

132 raise ValueError( 

133 """wasm is only supporting 32 bit size registers, 

134please use only registers of at most 32 bits""" 

135 ) 

136 

137 for reg in list_o: 

138 if reg.size > 32: 138 ↛ 139line 138 didn't jump to line 139 because the condition on line 138 was never true

139 raise ValueError( 

140 """wasm is only supporting 32 bit size registers, 

141please use only registers of at most 32 bits""" 

142 ) 

143 

144 # If the filehandler has not been checked we allow it to 

145 # be added without checking the function arity. 

146 if not filehandler.checked or filehandler.check_function( 

147 funcname, len(list_i), len(list_o) 

148 ): 

149 if (len(args_wasm)) > 0: 149 ↛ 151line 149 didn't jump to line 151 because the condition on line 149 was always true

150 self._add_w_register(max(args_wasm) + 1) 

151 return self._add_wasm( 

152 funcname, str(filehandler), list_i, list_o, args_wasm, **kwargs 

153 ) 

154 

155 raise ValueError(f"{funcname} not found, check {repr(filehandler)}") 

156 

157 

158setattr(Circuit, "add_wasm_to_reg", add_wasm_to_reg)