XanaduAI / strawberryfields

Strawberry Fields is a full-stack Python library for designing, simulating, and optimizing continuous variable (CV) quantum optical circuits.
https://strawberryfields.ai
Apache License 2.0
747 stars 186 forks source link

Some cat state functions use int32 arrays #554

Closed tguillaume closed 3 years ago

tguillaume commented 3 years ago

The functions strawberryfields.ops.Catstateandstrawberryfields.utils.states.cat_statedo not work properly with a large Fock dimension cut-off. For example, when I try to run strawberryfields.utils.states.cat_state(a=4, p=0, fock_dim=50), the returned Fock probabilities over n=16 are zero.

Inside these functions, the ket's array is initialized as type int32 due to (I think) an earlier use of np.arange with int32 argument. As a result, some of the maths that is performed further on in the function evaluates to 0 when the number is sufficiently large.

I was able to fix this by initializing the array describing the ket as a float, e.g.k = np.arange(fock_dim, dtype='float') or l = np.arange(D, dtype='float').

nquesada commented 3 years ago

Hi @tguillaume --- Thanks for catching this bug! Since you already have a solution, would you mind making a PR with your proposed fix?

antalszava commented 3 years ago

With #556 merged, closing this, thank you so much @tguillaume @nquesada! 🙂