From dfadf94b784f54d5779635b48b274e16bad126e0 Mon Sep 17 00:00:00 2001 From: Alec Edgington <54802828+cqc-alec@users.noreply.github.com> Date: Fri, 26 Jan 2024 13:39:11 +0000 Subject: [PATCH] [feature] Add method to get bytecode from a `WasmFileHandler` (#1235) --- pytket/docs/changelog.rst | 7 +++++++ pytket/pytket/wasm/wasm.py | 6 ++++-- pytket/tests/classical_test.py | 4 ++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pytket/docs/changelog.rst b/pytket/docs/changelog.rst index cd269c455f..fa638f0e9c 100644 --- a/pytket/docs/changelog.rst +++ b/pytket/docs/changelog.rst @@ -1,6 +1,13 @@ Changelog ========= +Unreleased +---------- + +Features: + +* Add ``WasmFileHandler.bytecode()`` method to retrieve the WASM as bytecode. + 1.24.0 (January 2024) --------------------- diff --git a/pytket/pytket/wasm/wasm.py b/pytket/pytket/wasm/wasm.py index 45a651d4d1..bf4f57cd68 100644 --- a/pytket/pytket/wasm/wasm.py +++ b/pytket/pytket/wasm/wasm.py @@ -91,7 +91,6 @@ def __init__(self, filepath: str, check_file: bool = True, int_size: int = 32): self._unsupported_function = [] if self._check_file: - mod_iter = iter(decode_module(self._wasm_file)) _, _ = next(mod_iter) @@ -143,7 +142,6 @@ def __init__(self, filepath: str, check_file: bool = True, int_size: int = 32): self._function_types = cur_sec_data.payload.types for x in function_names: - # check for only integer type in parameters and return values supported_function = True idx = _func_lookup[x][1] @@ -214,6 +212,10 @@ def __repr__(self) -> str: wasm file was checked""" ) + def bytecode(self) -> bytes: + """The WASM content as bytecode""" + return self._wasm_file + def check_function( self, function_name: str, number_of_parameters: int, number_of_returns: int ) -> bool: diff --git a/pytket/tests/classical_test.py b/pytket/tests/classical_test.py index bf8ff4642a..9a6c24cf2f 100644 --- a/pytket/tests/classical_test.py +++ b/pytket/tests/classical_test.py @@ -178,6 +178,10 @@ def test_wasm_2() -> None: def test_wasm_3() -> None: w = wasm.WasmFileHandler("testfile.wasm") + with open("testfile.wasm", "rb") as f: + bytecode = f.read() + assert w.bytecode() == bytecode + c = Circuit(0, 6) c.add_wasm("add_one", w, [1], [1], [Bit(0), Bit(1)])