GBS for molecular vibronic spectra: "probabilities do not sum to 1"

Hello!

When running GBS for molecular vibronic spectra, I would occasionally (~4% of the time) get the following error from numpy:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/site-packages/strawberryfields/apps/qchem/vibronic.py", line 264, in sample
    s = eng.run(gbs, shots=n_samples).samples
  File "/usr/local/lib/python3.9/site-packages/strawberryfields/engine.py", line 494, in run
    result = super()._run(
  File "/usr/local/lib/python3.9/site-packages/strawberryfields/engine.py", line 277, in _run
    _, self.samples, self.all_samples = self._run_program(p, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/strawberryfields/engine.py", line 349, in _run_program
    val = cmd.op.apply(cmd.reg, self.backend, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/strawberryfields/ops.py", line 302, in apply
    values = super().apply(reg, backend, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/strawberryfields/ops.py", line 228, in apply
    return self._apply(temp, backend, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/strawberryfields/ops.py", line 1134, in _apply
    samples = backend.measure_fock(reg, shots=shots, select=self.select, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/strawberryfields/backends/gaussianbackend/backend.py", line 239, in measure_fock
    samples = hafnian_sample_state(reduced_cov, shots, mean=reduced_mean)
  File "/usr/local/lib/python3.9/site-packages/thewalrus/samples.py", line 292, in hafnian_sample_state
    return _hafnian_sample(params)
  File "/usr/local/lib/python3.9/site-packages/thewalrus/samples.py", line 230, in _hafnian_sample
    result = generate_hafnian_sample(
  File "/usr/local/lib/python3.9/site-packages/thewalrus/samples.py", line 163, in generate_hafnian_sample
    result.append(np.random.choice(a=range(len(probs3)), p=probs3))
  File "mtrand.pyx", line 932, in numpy.random.mtrand.RandomState.choice
ValueError: probabilities do not sum to 1

This error seems to also occur somewhere else based on this other thread so I’m not sure if the fix would be the same.

To help you debug the issue, I’d like to attach a test script with data to this thread, but I can’t since I’m a relatively new user. Please let me know how I should share the files.

Thank you!

Andre

Hey @Andre, thanks so much for reporting. If you suspect it is a bug (i.e. if you are just running a typical workflow, maybe even copied from the demos or documentation), then an easier way will be to open an issue on the strawberryfields github. I think the best way to share code is to upload it somewhere and share the link? But there is actually a good reason to prohibit upload of files: almost any problem can be cast as a minimum working example, which you should be able to paste in the post itself - and this makes it a lot easier to identify bugs :). Please let us know if there are problems with that!

Just a random thought, we had this stochastic normalisation failure before in PennyLane and it was a problem with precision - some parts of the long pipeline would normalise with precision 1e-10 and another would check normalisation with precision 1e-8…but if so, the problem may be deep down in the stack and a dev team member needs to look at it!

Hi @Maria_Schuld, thanks for the quick reply! I believe it’s a bug so I’ve opened an issue on github. I’ve marked this thread as solved. Thanks!

Hi @andre,

Just in case, please check @sjahangiri’s comment HERE

Thank you!

Wonderful, the issue is fixed when updating The Walrus to version 0.16.0. Thanks!