Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RegisterLayout.static_hash() #492

Merged
merged 2 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pulser-core/pulser/register/register_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,22 @@ def _safe_hash(self) -> bytes:
hash.update(self.coords.tobytes())
return hash.digest()

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 hexstring without
the '0x' prefix (unlike what is returned by 'hex()').
"""
return self._safe_hash().hex()

def __eq__(self, other: Any) -> bool:
if not isinstance(other, RegisterLayout):
return False
Expand Down
6 changes: 6 additions & 0 deletions tests/test_register_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ def test_repr(layout):
assert repr(layout) == f"RegisterLayout_{hash_.hexdigest()}"


def test_static_hash(layout):
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):
assert RegisterLayout([[0, 0], [1, 0]]) != Register.from_coordinates(
[[0, 0], [1, 0]]
Expand Down