Skip to content

Commit

Permalink
Tests for c++ objects against python implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
stand-by committed Jul 30, 2024
1 parent 3705399 commit 4ac7f0f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
File renamed without changes.
69 changes: 69 additions & 0 deletions tests/approvals/test_pauli_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""Test pauli c++ objects against python implementations."""

import itertools as it

import numpy as np
import pytest

import fast_pauli._fast_pauli as fp
import fast_pauli.pypauli.operations as pp


@pytest.fixture
def sample_strings() -> list[str]:
"""Provide sample strings for testing."""
strings = it.chain(
["I", "X", "Y", "Z"],
it.permutations("IXYZ", 2),
it.permutations("IXYZ", 3),
["ZIZI", "YZYZ", "XYZXYZ", "ZZZIII", "XYIZXYZ", "XXIYYIZZ", "ZIXIZYXX"],
)
return list(map("".join, strings))


def test_pauli_object(paulis: dict) -> None:
"""Test that C++ Pauli object is numerically equivalent to Python Pauli object."""
# ideally, here we want to test against corresponding Pauli struct
# from python land, but currently we don't have one
for c in ["I", "X", "Y", "Z"]:
p = fp.Pauli(c)
np.testing.assert_array_equal(
np.array(p.to_tensor()),
paulis[c],
)
np.testing.assert_string_equal(str(p), c)


def test_pauli_string_representations(sample_strings: list[str]) -> None:
"""Test that C++ PauliString is numerically equivalent to Python PauliString."""
for s in sample_strings:
pcpp = fp.PauliString(s)
ppy = pp.PauliString(s)

np.testing.assert_allclose(
pcpp.to_tensor(),
ppy.dense(),
atol=1e-15,
)
np.testing.assert_string_equal(str(pcpp), str(ppy))
assert pcpp.dims == ppy.dim
assert pcpp.weight == ppy.weight


def test_pauli_string_apply_batch(sample_strings: list[str]) -> None:
"""Test that C++ PauliString is numerically equivalent to Python PauliString."""
rng = np.random.default_rng(321)

for s in sample_strings:
n = 2 ** len(s)
psis = rng.random((n, 42))

np.testing.assert_allclose(
np.array(fp.PauliString(s).apply_batch(psis.tolist())),
pp.PauliString(s).multiply(psis),
atol=1e-15,
)


if __name__ == "__main__":
pytest.main([__file__])

0 comments on commit 4ac7f0f

Please sign in to comment.