From 0671962fce82676f82f1f78f37d42e365247bc3a Mon Sep 17 00:00:00 2001 From: Melf Date: Thu, 10 Aug 2023 17:35:02 +0100 Subject: [PATCH 1/2] add 64bit support to wasm --- pytket/pytket/circuit/__init__.py | 12 ++++++++---- pytket/pytket/wasm/wasm.py | 31 ++++++++++++++++++++++--------- pytket/tests/classical_test.py | 20 +++++++++++++++++++- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/pytket/pytket/circuit/__init__.py b/pytket/pytket/circuit/__init__.py index df9732fde1..05450e1aa2 100644 --- a/pytket/pytket/circuit/__init__.py +++ b/pytket/pytket/circuit/__init__.py @@ -95,12 +95,16 @@ def overload_add_wasm( # type: ignore args_wasm = [0] for x in list_i: - if x > 32: - raise ValueError("only functions with i32 type are allowed") + if x > filehandler._int_size: + raise ValueError( + f"only functions with i{filehandler._int_size} type are allowed" + ) for x in list_o: - if x > 32: - raise ValueError("only functions with i32 type are allowed") + if x > filehandler._int_size: + raise ValueError( + f"only functions with i{filehandler._int_size} type are allowed" + ) if filehandler.check_function(funcname, len(list_i), len(list_o)): if (len(args_wasm)) > 0: diff --git a/pytket/pytket/wasm/wasm.py b/pytket/pytket/wasm/wasm.py index be8690b5b6..5a06bb3037 100644 --- a/pytket/pytket/wasm/wasm.py +++ b/pytket/pytket/wasm/wasm.py @@ -42,7 +42,7 @@ class WasmFileHandler: LANG_TYPE_EMPTY: None, } - def __init__(self, filepath: str, check_file: bool = True): + def __init__(self, filepath: str, check_file: bool = True, int_size: int = 32): """ Construct a wasm file handler @@ -51,7 +51,18 @@ def __init__(self, filepath: str, check_file: bool = True): :param check_file: If ``True`` checks file for compatibility with wasm standards. If ``False`` checks are skipped. :type check_file: bool + :param int_size: length of the integer that is used in the wasm file + :type int_size: int """ + self._int_size = int_size + if int_size == 32: + self._int_type = self.type_lookup[LANG_TYPE_I32] + elif int_size == 64: + self._int_type = self.type_lookup[LANG_TYPE_I64] + else: + raise ValueError( + "given integer length not valid, only 32 and 64 are allowed" + ) self._filepath = filepath @@ -103,7 +114,7 @@ def __init__(self, filepath: str, check_file: bool = True): ] * entry.return_count else: raise ValueError( - f"Only parameter and return values of i32 types are" + f"Only parameter and return values of i{self._int_size} types are" + f"allowed, found type: {entry.return_type}" ) elif entry.return_count == 1: @@ -124,13 +135,13 @@ def __init__(self, filepath: str, check_file: bool = True): self._function_types = cur_sec_data.payload.types for i, x in enumerate(function_names): - # check for only i32 type in parameters and return values + # check for only integer type in parameters and return values supported_function = True for t in function_signatures[self._function_types[i]]["parameter_types"]: - if t != "i32": + if t != self._int_type: supported_function = False for t in function_signatures[self._function_types[i]]["return_types"]: - if t != "i32": + if t != self._int_type: supported_function = False if supported_function: @@ -161,8 +172,10 @@ def __repr__(self) -> str: """str representation of the contents of the wasm file""" result = f"Functions in wasm file with the uid {self._wasmfileuid}:\n" for x in self._functions: - result += f"function '{x}' with {self._functions[x][0]} i32 parameter(s)" - result += f" and {self._functions[x][1]} i32 return value(s)\n" + result += f"function '{x}' with {self._functions[x][0]} i{self._int_size} parameter(s)" + result += ( + f" and {self._functions[x][1]} i{self._int_size} return value(s)\n" + ) for x in self._unsupported_function: result += ( @@ -179,9 +192,9 @@ def check_function( :param function_name: name of the function that is checked :type function_name: str - :param number_of_parameters: number of i32 parameters of the function + :param number_of_parameters: number of integer parameters of the function :type number_of_parameters: int - :param number_of_returns: number of i32 return values of the function + :param number_of_returns: number of integer return values of the function :type number_of_returns: int :return: true if the signature and the name of the function is correct""" diff --git a/pytket/tests/classical_test.py b/pytket/tests/classical_test.py index 356c8299b5..adbd682357 100644 --- a/pytket/tests/classical_test.py +++ b/pytket/tests/classical_test.py @@ -415,7 +415,7 @@ def test_wasmfilehandler_without_init() -> None: def test_wasmfilehandler_repr() -> None: - w = wasm.WasmFileHandler("testfile.wasm") + w = wasm.WasmFileHandler("testfile.wasm", int_size=32) assert ( repr(w) == """Functions in wasm file with the uid 6a0a29e235cd5c60353254bc2b459e631d381cdd0bded7ae6cb44afb784bd2de: @@ -432,6 +432,24 @@ def test_wasmfilehandler_repr() -> None: ) +def test_wasmfilehandler_repr_64() -> None: + w = wasm.WasmFileHandler("testfile.wasm", int_size=64) + assert ( + repr(w) + == """Functions in wasm file with the uid 6a0a29e235cd5c60353254bc2b459e631d381cdd0bded7ae6cb44afb784bd2de: +function 'init' with 0 i64 parameter(s) and 0 i64 return value(s) +function 'add_something' with 1 i64 parameter(s) and 1 i64 return value(s) +unsupported function with unvalid parameter or result type: 'add_one' +unsupported function with unvalid parameter or result type: 'multi' +unsupported function with unvalid parameter or result type: 'add_two' +unsupported function with unvalid parameter or result type: 'add_eleven' +unsupported function with unvalid parameter or result type: 'no_return' +unsupported function with unvalid parameter or result type: 'no_parameters' +unsupported function with unvalid parameter or result type: 'new_function' +""" + ) + + def gen_reg( name: str, size: int, reg_type: Union[Type[BitRegister], Type[QubitRegister]] ) -> Union[BitRegister, QubitRegister]: From cf35841c5a41b5019da84302d7505fcba8e8ac5c Mon Sep 17 00:00:00 2001 From: Melf Date: Fri, 11 Aug 2023 10:37:44 +0100 Subject: [PATCH 2/2] fix format --- pytket/pytket/wasm/wasm.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pytket/pytket/wasm/wasm.py b/pytket/pytket/wasm/wasm.py index 5a06bb3037..00677f26ce 100644 --- a/pytket/pytket/wasm/wasm.py +++ b/pytket/pytket/wasm/wasm.py @@ -114,8 +114,9 @@ def __init__(self, filepath: str, check_file: bool = True, int_size: int = 32): ] * entry.return_count else: raise ValueError( - f"Only parameter and return values of i{self._int_size} types are" - + f"allowed, found type: {entry.return_type}" + f"Only parameter and return values of " + + f"i{self._int_size} types are" + + f" allowed, found type: {entry.return_type}" ) elif entry.return_count == 1: function_signatures[idx]["return_types"] = [ @@ -172,7 +173,8 @@ def __repr__(self) -> str: """str representation of the contents of the wasm file""" result = f"Functions in wasm file with the uid {self._wasmfileuid}:\n" for x in self._functions: - result += f"function '{x}' with {self._functions[x][0]} i{self._int_size} parameter(s)" + result += f"function '{x}' with " + result += f"{self._functions[x][0]} i{self._int_size} parameter(s)" result += ( f" and {self._functions[x][1]} i{self._int_size} return value(s)\n" )