From 5a25617c3b550b60b8ea0844e08cc568efd6f155 Mon Sep 17 00:00:00 2001 From: Melf Date: Mon, 23 Oct 2023 15:08:08 +0100 Subject: [PATCH 1/2] add additional check for >32 bit sizze registers used in wasm --- pytket/pytket/circuit/__init__.py | 13 ++++++++++ pytket/tests/classical_test.py | 43 ++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/pytket/pytket/circuit/__init__.py b/pytket/pytket/circuit/__init__.py index a9192d15bf..232fee5fb5 100644 --- a/pytket/pytket/circuit/__init__.py +++ b/pytket/pytket/circuit/__init__.py @@ -135,6 +135,19 @@ def overload_add_wasm_to_reg( if args_wasm is None: args_wasm = [0] + if filehandler._check_file: + for reg in list_i: + if reg.size > 32: + raise ValueError( + "wasm is only supporting 32 bit size registers, please use only registers of at most 32 bits" + ) + + for reg in list_o: + if reg.size > 32: + raise ValueError( + "wasm is only supporting 32 bit size registers, please use only registers of at most 32 bits" + ) + if filehandler.check_function(funcname, len(list_i), len(list_o)): if (len(args_wasm)) > 0: self._add_w_register(max(args_wasm) + 1) diff --git a/pytket/tests/classical_test.py b/pytket/tests/classical_test.py index 2f0d651844..a5f63860fd 100644 --- a/pytket/tests/classical_test.py +++ b/pytket/tests/classical_test.py @@ -390,10 +390,51 @@ def test_wasm_function_check_5() -> None: c = Circuit(20, 20) c0 = c.add_c_register("c0", 53) c1 = c.add_c_register("c1", 4) + + with pytest.raises(ValueError): + c.add_wasm_to_reg("add_one", w, [c0], [c1]) + + +def test_wasm_function_check_6() -> None: + w = wasm.WasmFileHandler("testfile.wasm") + c = Circuit(20, 20) + c0 = c.add_c_register("c0", 32) + c1 = c.add_c_register("c1", 4) + + c.add_wasm_to_reg("add_one", w, [c0], [c1]) + assert c.depth() == 1 + + +def test_wasm_function_check_7() -> None: + w = wasm.WasmFileHandler("testfile.wasm", int_size=32) + c = Circuit(20, 20) + c0 = c.add_c_register("c0", 32) + c1 = c.add_c_register("c1", 4) + + c.add_wasm_to_reg("add_one", w, [c0], [c1]) + assert c.depth() == 1 + + +def test_wasm_function_check_8() -> None: + w = wasm.WasmFileHandler("testfile.wasm", int_size=64) + c = Circuit(20, 20) + c0 = c.add_c_register("c0", 32) + c1 = c.add_c_register("c1", 4) + c2 = c.add_c_register("c2", 5) + + c.add_wasm_to_reg("add_something", w, [c0], [c1]) + assert c.depth() == 1 + + +def test_wasm_function_check_9() -> None: + w = wasm.WasmFileHandler("testfile.wasm", int_size=64) + c = Circuit(20, 20) + c0 = c.add_c_register("c0", 53) + c1 = c.add_c_register("c1", 4) c2 = c.add_c_register("c2", 5) with pytest.raises(ValueError): - c.add_wasm_to_reg("add_one", w, [c0], [c1, c2]) + c.add_wasm_to_reg("add_something", w, [c0], [c1]) def test_add_wasm_to_reg() -> None: From fc72546427b80594976df6f65e34a74718d2b4d3 Mon Sep 17 00:00:00 2001 From: Melf Date: Mon, 23 Oct 2023 15:11:38 +0100 Subject: [PATCH 2/2] shorter line --- pytket/pytket/circuit/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pytket/pytket/circuit/__init__.py b/pytket/pytket/circuit/__init__.py index 232fee5fb5..6941829c91 100644 --- a/pytket/pytket/circuit/__init__.py +++ b/pytket/pytket/circuit/__init__.py @@ -139,13 +139,15 @@ def overload_add_wasm_to_reg( for reg in list_i: if reg.size > 32: raise ValueError( - "wasm is only supporting 32 bit size registers, please use only registers of at most 32 bits" + """wasm is only supporting 32 bit size registers, +please use only registers of at most 32 bits""" ) for reg in list_o: if reg.size > 32: raise ValueError( - "wasm is only supporting 32 bit size registers, please use only registers of at most 32 bits" + """wasm is only supporting 32 bit size registers, +please use only registers of at most 32 bits""" ) if filehandler.check_function(funcname, len(list_i), len(list_o)):