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
745 stars 187 forks source link

GBS for molecular vibronic spectra: “probabilities do not sum to 1” #620

Closed pwndr3 closed 3 years ago

pwndr3 commented 3 years ago

Issue description

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

Traceback (most recent call last):
  File "", line 1, in 
  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

I've tried with different molecules, but this error would only occur to one of them.

The data used comes from Firefly electronic structure calculation and model formic acid. Here is a snippet (data included) that will eventually throw an error (at iteration ~15-30)

import numpy as np
import strawberryfields.apps as sfapps

# Parameters
T = 0
n_samples = 5

# Formic acid data
ri = np.array([-4.10429777, 1.95863505, 0, -2.97359248, 1.65884384, 0, -4.5168655, 3.22060948, 0., -4.91529804,
    1.22985388, 0., -5.45687621, 3.28310776, 0.
])
rf = np.array([-4.181513, 1.97989361, 0., -2.96486262, 1.67914717,
    0., -4.50635792, 3.1776378, 0., -4.87172855,
    1.14361689, 0., -5.44246791, 3.37075454, 0.
])
m = np.array([12., 12., 12., 15.99491, 15.99491, 15.99491,
    15.99491, 15.99491, 15.99491, 1.00782, 1.00782, 1.00782,
    1.00782, 1.00782, 1.00782
])
wi = np.array([515.76, 729.78, 1175.09, 1208.94, 1397.75, 1572.93, 2044.39,
    3172.64, 4164.87
])
wf = np.array([630.94, 687.49, 1159.22, 1228.42, 1353.35, 1510.28, 1821.1,
    3279.46, 3974.14
])
Li = np.array([
    [0.0000000e+00, -9.8140210e-02, 0.0000000e+00, -4.7132200e-03, -1.8014820e-02, -4.6370800e-03, -2.1169782e-01, 6.5057870e-02,
        1.6193300e-03
    ],
    [0.0000000e+00, -9.6557850e-02, 0.0000000e+00, -1.4869758e-01, -1.3096883e-01, -6.7279260e-02, 6.9807180e-02, 5.2395160e-02,
        1.5722300e-03
    ],
    [-4.7989960e-02, 0.0000000e+00, -1.7801944e-01, 0.0000000e+00,
        0.0000000e+00, 0.0000000e+00, 0.0000000e+00, 0.0000000e+00,
        0.0000000e+00
    ],
    [0.0000000e+00, -4.7751200e-02, 0.0000000e+00, 6.2302690e-02, -1.6194540e-02, 6.8103180e-02, 1.4447430e-01, -4.0080500e-03, -8.0988000e-04],
    [0.0000000e+00, 1.4138457e-01, 0.0000000e+00, -1.9646410e-02,
        2.8528080e-02, 1.3132230e-02, -3.5434800e-02, 8.5052000e-04,
        2.8242000e-04
    ],
    [4.2968500e-02, 0.0000000e+00, 4.8095640e-02, 0.0000000e+00,
        0.0000000e+00, 0.0000000e+00, 0.0000000e+00, 0.0000000e+00,
        0.0000000e+00
    ],
    [0.0000000e+00, 1.1678619e-01, 0.0000000e+00, -6.2443640e-02,
        3.4763900e-02, -2.2002360e-02, 1.6004190e-02, -8.8494000e-04,
        6.0623610e-02
    ],
    [0.0000000e+00, -7.6881690e-02, 0.0000000e+00, 1.1007772e-01,
        1.2130023e-01, -5.9535500e-03, 1.5352600e-03, -4.6579000e-04, -6.0789800e-03
    ],
    [-5.6497360e-02, 0.0000000e+00, 3.8070410e-02, 0.0000000e+00,
        0.0000000e+00, 0.0000000e+00, 0.0000000e+00, 0.0000000e+00,
        0.0000000e+00
    ],
    [0.0000000e+00, -6.8125740e-02, 0.0000000e+00, 9.2457030e-02, -4.7105040e-02, -6.5997085e-01, -2.8192830e-02, -7.0305862e-01, -5.2780000e-03],
    [0.0000000e+00, -1.2457814e-01, 0.0000000e+00, -2.4517382e-01, -1.1249267e-01, 6.4488156e-01, -1.7421667e-01, -6.4370268e-01,
        5.2238000e-04
    ],
    [-1.4353369e-01, 0.0000000e+00, 7.4507267e-01, 0.0000000e+00,
        0.0000000e+00, 0.0000000e+00, 0.0000000e+00, 0.0000000e+00,
        0.0000000e+00
    ],
    [0.0000000e+00, 1.4103123e-01, 0.0000000e+00, -3.4099020e-02, -3.3104880e-02, -1.6469450e-02, 1.9338200e-03, 6.0811300e-03, -9.6328987e-01],
    [0.0000000e+00, 2.5056968e-01, 0.0000000e+00, 5.8048087e-01, -7.0596715e-01, 4.2270450e-02, -1.1895514e-01, 1.3736180e-02,
        7.2753150e-02
    ],
    [9.2965123e-01, 0.0000000e+00, 7.0551700e-03, 0.0000000e+00,
        0.0000000e+00, 0.0000000e+00, 0.0000000e+00, 0.0000000e+00,
        0.0000000e+00
    ]
])
Lf = np.array([
    [-9.8604300e-02, 0.0000000e+00, 0.0000000e+00, 4.1044540e-02, -7.5739000e-02, 9.1631960e-02, -1.7557186e-01, 5.4435320e-02, -2.0422500e-03],
    [-1.1026347e-01, 0.0000000e+00, 0.0000000e+00, 7.5552200e-03, -1.2769670e-02, 1.4068378e-01, 1.5285070e-01, 6.7229860e-02, -1.6463400e-03],
    [0.0000000e+00, -5.2201420e-02, 1.7075658e-01, 0.0000000e+00,
        0.0000000e+00, 0.0000000e+00, 0.0000000e+00, 0.0000000e+00,
        0.0000000e+00
    ],
    [-4.6932000e-02, 0.0000000e+00, 0.0000000e+00, -9.2130960e-02,
        1.0953447e-01, -7.0135340e-02, 6.8935770e-02, -2.1760300e-03,
        1.6216800e-03
    ],
    [1.3907272e-01, 0.0000000e+00, 0.0000000e+00, 3.5597880e-02, -3.4510700e-03, 1.5084330e-02, -2.2383020e-02, 8.1236000e-04, -9.3288000e-04],
    [0.0000000e+00, 4.3217120e-02, -3.6461110e-02, 0.0000000e+00,
        0.0000000e+00, 0.0000000e+00, 0.0000000e+00, 0.0000000e+00,
        0.0000000e+00
    ],
    [1.1697579e-01, 0.0000000e+00, 0.0000000e+00, 7.0344100e-02, -7.5763400e-03, 2.2372800e-03, 4.4796110e-02, -4.0358000e-04, -6.0179010e-02],
    [-6.4687220e-02, 0.0000000e+00, 0.0000000e+00, 7.1979300e-03, -7.7539800e-03, -1.5609177e-01, -6.5717760e-02, -6.0615800e-03,
        1.4351130e-02
    ],
    [0.0000000e+00, -5.9666900e-02, -4.6631400e-02, 0.0000000e+00,
        0.0000000e+00, 0.0000000e+00, 0.0000000e+00, 0.0000000e+00,
        0.0000000e+00
    ],
    [-1.1882609e-01, 0.0000000e+00, 0.0000000e+00, -3.9050400e-02, -6.7489833e-01, -1.0353774e-01, 2.7207186e-01, -6.2089583e-01,
        9.1443400e-03
    ],
    [-9.2718910e-02, 0.0000000e+00, 0.0000000e+00, 7.8668130e-02,
        4.9502141e-01, 3.5541320e-01, -2.1862369e-01, -7.1886246e-01,
        4.4662100e-03
    ],
    [0.0000000e+00, -5.0696330e-02, -7.6602069e-01, 0.0000000e+00,
        0.0000000e+00, 0.0000000e+00, 0.0000000e+00, 0.0000000e+00,
        0.0000000e+00
    ],
    [1.8123937e-01, 0.0000000e+00, 0.0000000e+00, -1.0388675e-01, -4.1439790e-02, 9.0084210e-02, 1.3425460e-02, 1.3683970e-02,
        9.4451934e-01
    ],
    [2.2505464e-01, 0.0000000e+00, 0.0000000e+00, -8.4782775e-01, -1.6514455e-01, 2.0737801e-01, -2.0311955e-01, 1.6769800e-03, -1.9782121e-01],
    [0.0000000e+00, 9.3331433e-01, 5.1590540e-02, 0.0000000e+00,
        0.0000000e+00, 0.0000000e+00, 0.0000000e+00, 0.0000000e+00,
        0.0000000e+00
    ]
])

# Compute spectrum
U, delta = sfapps.qchem.duschinsky(
            Li,
            Lf,
            ri,
            rf,
            wf,
            m
        )

t, U1, r, U2, alpha = sfapps.qchem.vibronic.gbs_params(
            wi, 
            wf, 
            U, 
            delta,
            T)

for iteration in range(100):
    # The issue would occur at iteration ~15-30
    print("Iteration {}".format(iteration + 1))
    samples = sfapps.qchem.vibronic.sample(t, U1, r, U2, alpha, n_samples)

Strawberry Fields: a Python library for continuous-variable quantum circuits. Copyright 2018-2020 Xanadu Quantum Technologies Inc.

Python version: 3.9.6 Platform info: macOS-11.5.1-x86_64-i386-64bit Installation path: /usr/local/lib/python3.9/site-packages/strawberryfields Strawberry Fields version: 0.18.0 Numpy version: 1.19.4 Scipy version: 1.5.4 SymPy version: 1.8 NetworkX version: 2.5.1 The Walrus version: 0.15.1 Blackbird version: 0.3.0 TensorFlow version: 2.5.0-rc2

wait-a-sec commented 3 years ago

I have gotten this error on small molecules too but when I increase the number of samples above some threshold. Maybe this is the limit of simulator.

soranjh commented 3 years ago

Hi @pwndr3 and @wait-a-sec. The error "probabilities do not sum to 1" is a known numpy issue that can be fixed by renormalizing the probability vector. Please note that we have fixed this in the latest release, Release 0.16.0, of The Walrus. Please install the latest version of The Walrus (0.16.0) and that will solve the problem. Thanks.

wait-a-sec commented 3 years ago

Thank you for informing @soranjh !

pwndr3 commented 3 years ago

Thanks @soranjh, closing issue!