Skip to content

Commit

Permalink
add 64bit support to wasm (#964)
Browse files Browse the repository at this point in the history
* add 64bit support to wasm

* fix format
  • Loading branch information
cqc-melf authored Aug 11, 2023
1 parent 7553dc7 commit ddcb693
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
12 changes: 8 additions & 4 deletions pytket/pytket/circuit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
35 changes: 25 additions & 10 deletions pytket/pytket/wasm/wasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -103,8 +114,9 @@ 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"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"] = [
Expand All @@ -124,13 +136,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:
Expand Down Expand Up @@ -161,8 +173,11 @@ 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 "
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"
)

for x in self._unsupported_function:
result += (
Expand All @@ -179,9 +194,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"""

Expand Down
20 changes: 19 additions & 1 deletion pytket/tests/classical_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]:
Expand Down

0 comments on commit ddcb693

Please sign in to comment.