ValueError: pvals < 0, pvals > 1 or pvals contains NaNs

I am trying to run an algorithm for a problem with 2 modes. The code fails to run with the below error. There seems to be some issue with the Homodyne measurement. Could you tell me where the problem might stem from? Unfortunately, I am unable to track. The code used to work without any issues in the past.

/Users/anaconda3/envs/project/lib/python3.11/site-packages/strawberryfields/backends/fockbackend/circuit.py:763: RuntimeWarning: invalid value encountered in divide
Traceback (most recent call last):

  result = eng.run(prog)
            ^^^^^^^^^^^^^
 File "/Users/anaconda3/envs/project/lib/python3.11/site-packages/strawberryfields/engine.py", line 570, in run
   return super()._run(
          ^^^^^^^^^^^^^
 File "/Users/anaconda3/envs/project/lib/python3.11/site-packages/strawberryfields/engine.py", line 306, in _run
   _, self.samples, self.samples_dict = self._run_program(p, **kwargs)
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "/Users/anaconda3/envs/project/lib/python3.11/site-packages/strawberryfields/engine.py", line 430, in _run_program
   val = cmd.op.apply(cmd.reg, self.backend, **kwargs)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "/Users/anaconda3/envs/project/lib/python3.11/site-packages/strawberryfields/ops.py", line 325, in apply
   values = super().apply(reg, backend, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "/Users/anaconda3/envs/project/lib/python3.11/site-packages/strawberryfields/ops.py", line 228, in apply
   return self._apply(temp, backend, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "/Users/anaconda3/envs/project/lib/python3.11/site-packages/strawberryfields/ops.py", line 1276, in _apply
   return s * backend.measure_homodyne(p[0], *reg, shots=shots, select=select, **kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "/Users/anaconda3/envs/project/lib/python3.11/site-packages/strawberryfields/backends/fockbackend/backend.py", line 198, in measure_homodyne
   return self.circuit.measure_homodyne(phi, self._remap_modes(mode), select=select, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "/Users/anaconda3/envs/project/lib/python3.11/site-packages/strawberryfields/backends/fockbackend/circuit.py", line 770, in measure_homodyne
   sample_hist = np.random.multinomial(1, probs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "mtrand.pyx", line 4337, in numpy.random.mtrand.RandomState.multinomial
 File "_common.pyx", line 391, in numpy.random._common.check_array_constraint
 File "_common.pyx", line 377, in numpy.random._common._check_array_cons_bounded_0_1
ValueError: pvals < 0, pvals > 1 or pvals contains NaNs

Hey @Rina, welcome back!

Can you attach a minimal but complete working example of the code that gives you this error? That will help me see if there’s an easy solution :slight_smile:

Thank you! :slight_smile: Unfortunately, the code is quite big and I can’t reduce it as there are many dependencies in the code.

The error can be reproduced with the code below.

import strawberryfields as sf
from strawberryfields.ops import *

eng = sf.Engine("fock", backend_options={"cutoff_dim": 15})

iterations = 10
shots = 10

def run_circuit():
    prog = sf.Program(2)

    with prog.context as q:

        Squeezed(-0.5,0) | q[0]
        Squeezed(-0.5,0) | q[1]
    
        Pgate(4 * -3.96053327835541) | q[0]
        CZgate(2 * -7.92106655671081) | (q[0], q[1])
        Zgate(2* 87.1317321238189) | q[0]
        Pgate(4 * -7.92106655671081) | q[1]
        Zgate(2* 134.658131464084) | q[1]
        Pgate(4 * -1.9802666391777) | q[0]
        CZgate(2 * -3.96053327835541) | (q[0], q[1])
        Zgate(2* 39.6053327835541) | q[0]
        Pgate(4 * -1.9802666391777) | q[1]

        MeasureX | q[0]
        MeasureX | q[1]

        state = eng.run(prog)
       
        eng.print_applied()
        eng.reset()
    
    return q[0].val

Hope the code is sufficient for you to understand the issue. :slight_smile:

Thanks!

Those are some massive displacements and really high amounts of squeezing, so only keeping a Fock cutoff of 15 is probably why your probability distribution is NaNs.

I suggest you switch to the Gaussian backend. You’ll have to finagle your gates a bit, but that seems like a better option compared to increasing your Fock cutoff.

Let me know if that helps!

Thanks for your quick reply, Isaac!
In my original code, I have actually cutoff=90, but still I get the same error message. Is it known which displacement range can SF handle?
And could you please explain how high squeezing can contribute to the issue? :slight_smile:

Also, the issue with using the Gaussian backend is that I have non-Gaussian operations in my circuit. Since the Cubic gate is non-Gaussian and can’t be used on the Gaussian backend, I won’t be able to decompose the operations.

Hey @Rina, I’ll have to get back to you for a detailed answer! Sit tight while I ask internally for an answer :slight_smile:

Here’s what I got back:

Both displacements and squeezing can add photons to a system, so for a fixed Fock cutoff, there will always be a value for the displacement and a level for the squeezing where you can add enough photons into the system to significantly extend the support of the state beyond the cutoff.

So that’s your second last question addressed!

Can you share the circuit you’re trying to simulate that has a Cubic gate in it?