From 2497fc07adb7824f4ae4d94d1da36520c8dbd27f Mon Sep 17 00:00:00 2001 From: HGSilveri Date: Wed, 29 Mar 2023 16:13:46 +0200 Subject: [PATCH 1/2] Add RegisterLayout.static_hash() --- pulser-core/pulser/register/register_layout.py | 15 +++++++++++++++ tests/test_register_layout.py | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/pulser-core/pulser/register/register_layout.py b/pulser-core/pulser/register/register_layout.py index cce9f255b..4a7fb0d26 100644 --- a/pulser-core/pulser/register/register_layout.py +++ b/pulser-core/pulser/register/register_layout.py @@ -302,6 +302,21 @@ def _safe_hash(self) -> bytes: hash.update(self.coords.tobytes()) return hash.digest() + def static_hash(self) -> int: + """Returns the layout's idempotent hash. + + Python's standard hash is not idempotent as it changes between + sessions. This hash can be used when an idempotent hash is + required. + + Note: + This hash will be returned as an 'int'. When converting to + to 'hex', 'hex()' will prepend '0x' to the string, + unlike in other cases (e.g. 'bytes.hex()'). + To avoid this mismatch, prefer 'f{i:x}' over hex(i). + """ + return int(self._safe_hash().hex(), base=16) + def __eq__(self, other: Any) -> bool: if not isinstance(other, RegisterLayout): return False diff --git a/tests/test_register_layout.py b/tests/test_register_layout.py index 01b542fde..4343912ee 100644 --- a/tests/test_register_layout.py +++ b/tests/test_register_layout.py @@ -136,6 +136,13 @@ def test_repr(layout): assert repr(layout) == f"RegisterLayout_{hash_.hexdigest()}" +def test_static_hash(layout): + assert layout.static_hash() == int.from_bytes( + layout._safe_hash(), byteorder="big" + ) + assert repr(layout) == f"RegisterLayout_{layout.static_hash():x}" + + def test_eq(layout, layout3d): assert RegisterLayout([[0, 0], [1, 0]]) != Register.from_coordinates( [[0, 0], [1, 0]] From 785ef1d57cd5d971189b504eda34f40a73ebc8e6 Mon Sep 17 00:00:00 2001 From: HGSilveri Date: Wed, 29 Mar 2023 16:44:14 +0200 Subject: [PATCH 2/2] Switch to hexstring --- pulser-core/pulser/register/register_layout.py | 13 +++++++------ tests/test_register_layout.py | 7 +++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pulser-core/pulser/register/register_layout.py b/pulser-core/pulser/register/register_layout.py index 4a7fb0d26..cc89f6b8e 100644 --- a/pulser-core/pulser/register/register_layout.py +++ b/pulser-core/pulser/register/register_layout.py @@ -302,20 +302,21 @@ def _safe_hash(self) -> bytes: hash.update(self.coords.tobytes()) return hash.digest() - def static_hash(self) -> int: + def static_hash(self) -> str: """Returns the layout's idempotent hash. Python's standard hash is not idempotent as it changes between sessions. This hash can be used when an idempotent hash is required. + Returns: + str: An hexstring encoding the hash. + Note: - This hash will be returned as an 'int'. When converting to - to 'hex', 'hex()' will prepend '0x' to the string, - unlike in other cases (e.g. 'bytes.hex()'). - To avoid this mismatch, prefer 'f{i:x}' over hex(i). + This hash will be returned as an hexstring without + the '0x' prefix (unlike what is returned by 'hex()'). """ - return int(self._safe_hash().hex(), base=16) + return self._safe_hash().hex() def __eq__(self, other: Any) -> bool: if not isinstance(other, RegisterLayout): diff --git a/tests/test_register_layout.py b/tests/test_register_layout.py index 4343912ee..8c3a1cfa4 100644 --- a/tests/test_register_layout.py +++ b/tests/test_register_layout.py @@ -137,10 +137,9 @@ def test_repr(layout): def test_static_hash(layout): - assert layout.static_hash() == int.from_bytes( - layout._safe_hash(), byteorder="big" - ) - assert repr(layout) == f"RegisterLayout_{layout.static_hash():x}" + int_hash = int.from_bytes(layout._safe_hash(), byteorder="big") + assert layout.static_hash() == f"{int_hash:x}" + assert repr(layout) == f"RegisterLayout_{layout.static_hash()}" def test_eq(layout, layout3d):