def test_pstring_collection_str(): x = PauliString(spec="X") iz = PauliString(spec="IZ") pcol = PauliStringCollection(x, iz) assert str(pcol) == "X(0) + Z(1)" xz = PauliString(spec="XZ", coeff=-2.4) pcol.add(xz) assert str(pcol) == "X(0) + Z(1) + (-2.4+0j)*X(0)*Z(1)"
def test_pstring_collection_add(): pcol = PauliStringCollection() a = PauliString(spec="ZZ") assert pcol.can_add(a) pcol.add(a) assert pcol.elements == [a] b = PauliString(spec="ZIXZ") assert pcol.can_add(b) pcol.add(b) assert pcol.elements == [a, b] assert pcol.can_add(a) pcol.add(a) assert pcol.elements == [a, a, b] c = PauliString(spec="YY") assert not pcol.can_add(c) with pytest.raises(ValueError, match="Cannot add PauliString"): pcol.add(c)