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

Random failure with measure_fock method #670

Open
say4n opened this issue Jan 5, 2022 · 7 comments
Open

Random failure with measure_fock method #670

say4n opened this issue Jan 5, 2022 · 7 comments
Labels
bug Something isn't working

Comments

@say4n
Copy link

say4n commented Jan 5, 2022

Hi folks, while working with a very simple quantum circuit with the fock backend, I came across an issue where the script crashes and this stacktrace shows up sporadically.

  File "/Users/Sayan/.pyenv/versions/3.9.7/lib/python3.9/site-packages/strawberryfields/ops.py", line 1173, in _apply
    samples = backend.measure_fock(reg, shots=shots, select=self.select, **kwargs)
  File "/Users/Sayan/.pyenv/versions/3.9.7/lib/python3.9/site-packages/strawberryfields/backends/fockbackend/backend.py", line 295, in measure_fock
    return self.circuit.measure_fock(self._remap_modes(modes), select=select)
  File "/Users/Sayan/.pyenv/versions/3.9.7/lib/python3.9/site-packages/strawberryfields/backends/fockbackend/circuit.py", line 683, in measure_fock
    i = np.random.choice(list(range(len(dist))), p=dist / sum(dist))
  File "mtrand.pyx", line 928, in numpy.random.mtrand.RandomState.choice
ValueError: probabilities contain NaN

The quantum circuit that I am simulating is

with program.context as q:
  # Prepare the coherent states for the layer. Appropriately scales
  # the amplitudes for each of the layers.
  for m in range(config.NUM_MODES):
      sf.ops.Coherent(amplitudes[layer_number] * params[f"input_codeword_arg_{m}"]) | q[m]
  
  # Displace each of the modes by using the displacement magnitudes
  # generated by the ML backend.
  for m in range(config.NUM_MODES):
      sf.ops.Dgate(params[f"displacement_magnitudes_for_each_mode_arg_{m}"]) | q[m]
  
  # Perform measurements.
  sf.ops.MeasureFock() | q

Any pointers as to what might be the issue here?

Other relevant details

Platform: macOS Monetery 12.1
Python Version: 3.9.7
Strawberry Fields Version: 0.21.0

@say4n say4n changed the title Random failure withmeasure_fock method Random failure with measure_fock method Jan 5, 2022
@say4n
Copy link
Author

say4n commented Jan 6, 2022

Folks I did a little further digging and it turns out that this crash is being caused by the vector dist to constitute of all zeros.
This crash makes sense because we normalize the probabilities in the next lines if the sum is not 1 by dividing the values by the sum of the values.

But, the question now is why is the probability distribution a zero vector?

> /Users/Sayan/.pyenv/versions/3.9.7/lib/python3.9/site-packages/strawberryfields/backends/fockbackend/circuit.py(684)measure_fock()
-> if sum(dist) != 1:
(Pdb) p dist
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
(Pdb)

Could this be a side-effect of the fixes introduced as a response to #354 (and #364)?

@josh146
Copy link
Member

josh146 commented Jan 6, 2022

Thanks for digging into this a bit further @say4n! When you are debugging, is the line where the crash is occurring at the bugfix?

# Due to floating point precision error, values in the calculated probability distribution
# may have a very small negative value of -epsilon. The following sets
# these small negative values to 0.
probs[np.abs(probs) < 1e-10] = 0

@say4n
Copy link
Author

say4n commented Jan 6, 2022

@josh146 happy to help!

The crash occurs in these lines:

dist = dist * ~np.isclose(dist, 0.0)
# Make a random choice
if sum(dist) != 1:
# WARNING: distribution is not normalized, could hide errors
i = np.random.choice(list(range(len(dist))), p=dist / sum(dist))
else:
i = np.random.choice(list(range(len(dist))), p=dist)

Particulary, it crashes because of p=dist / sum(dist) in L#687. Maybe L#682 is making the values all 0?

@josh146 josh146 added the bug Something isn't working label Jan 7, 2022
@say4n
Copy link
Author

say4n commented Jan 10, 2022

Hi folks, any updates on triaging the issue?

@thisac
Copy link
Contributor

thisac commented Jan 10, 2022

Hi @say4n. Could you provide a minimal non-working example of your code? Specifically, I'd like to know which parameter values that you're using in the circuit, how many modes you are using (or does it fail no matter how many modes the circuit has?), the cutoff-value that you're passing to the Fock backend, and any other backend or run options that you might have. This could help me replicating the issue and solve it.

@say4n
Copy link
Author

say4n commented Jan 19, 2022

Hi @thisac I haven't had the time to prepare a MWE for the code yet, but I found that replacing the line

# kill spurious tiny values (which are sometimes negative)
dist = dist * ~np.isclose(dist, 0.0)

to the following seems to have solved my crash

- dist = dist * ~np.isclose(dist, 0.0)
+ dist = np.abs(dist)

Once, I have a little more time at hand I will craft the MWE to reproduce the crash and post it here.

@thisac
Copy link
Contributor

thisac commented Jan 19, 2022

Thank you @say4n. I can see why that could solve the issue with tiny negative values, although I'm still a bit curious why your dist, i.e., the reduced state, only consists of values very close to zero in the first place. Would you expect this to be the case?

Once, I have a little more time at hand I will craft the MWE to reproduce the crash and post it here.

That would be fantastic. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants