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

Unexpected behavior when passing np.ndarray to PauliOp constructor #23

Open
jamesETsmith opened this issue Sep 13, 2024 · 0 comments
Open

Comments

@jamesETsmith
Copy link
Contributor

General

@stand-by noticed some strange ctor behavior outlined below

Detail

      .def("__init__",
           [](fp::PauliOp<float_type> *new_obj, nb::ndarray<cfloat_t> coeffs,
              std::vector<fp::PauliString> const &pauli_strings) {
             fmt::println("ctor 1");
             auto [coeffs_vec, _] =
                 fp::__detail::ndarray_to_raw<cfloat_t, 1>(coeffs);
             new (new_obj) fp::PauliOp<float_type>(coeffs_vec, pauli_strings);
           })
      .def("__init__",
           [](fp::PauliOp<float_type> *new_obj,
              std::vector<cfloat_t> coeffs_vec,
              std::vector<fp::PauliString> const &pauli_strings) {
             fmt::println("ctor 2");
             new (new_obj) fp::PauliOp<float_type>(coeffs_vec, pauli_strings);
           })
      .def("__init__",
           [](fp::PauliOp<float_type> *new_obj,
              std::vector<cfloat_t> coeffs_vec,
              std::vector<std::string> const &strings) {
             fmt::println("ctor 3");
             std::vector<fp::PauliString> pauli_strings;
             std::transform(strings.begin(), strings.end(),
                            std::back_inserter(pauli_strings),
                            [](std::string const &pauli) {
                              return fp::PauliString(pauli);
                            });
             new (new_obj) fp::PauliOp<float_type>(coeffs_vec, pauli_strings);
           })
import fast_pauli._fast_pauli as fp
import numpy as np

n_strings = 10
coeffs = np.random.rand(n_strings).astype(np.complex128)
strings = ["".join(np.random.choice(["I", "X", "Y", "Z"], 4)) for _ in range(n_strings)]
print(strings)

print("should be in ctor 1")
op = fp.PauliOp(coeffs, [fp.PauliString(s) for s in strings])


print("should be in ctor 2")
op = fp.PauliOp(coeffs.tolist(), [fp.PauliString(s) for s in strings])

print("should be in ctor 3")
op = fp.PauliOp(coeffs.tolist(), strings)
op = fp.PauliOp(coeffs, strings)  # Why does this not error at runtime?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant