QInfer / python-qinfer

Library for Bayesian inference via sequential Monte Carlo for quantum parameter estimation.
BSD 3-Clause "New" or "Revised" License
92 stars 31 forks source link

Truth value of an array with more than one element is ambiguous. #136

Closed Nowzerix closed 7 years ago

Nowzerix commented 7 years ago

Hi all, I'm getting an error with qinfer that I don't know what to do with. Sometimes this gets thrown, sometimes not. I was wondering if someone could have a look at it. I am using the TomographyModel with my own heuristic.

Traceback (most recent call last):
  File "qinfer_test.py", line 69, in <module>
    tally[detect(true_state)] += 1
  File "qinfer_test.py", line 58, in detect
    updater.update(data, experiment)
  File "/home/jayce/.local/lib/python3.5/site-packages/qinfer/smc.py", line 425, in update
    weights, norm = self.hypothetical_update(outcome, expparams, return_normalization=True)
  File "/home/jayce/.local/lib/python3.5/site-packages/qinfer/smc.py", line 365, in hypothetical_update
    L = self.model.likelihood(outcomes, locs, expparams).transpose([0, 2, 1])
  File "/home/jayce/.local/lib/python3.5/site-packages/qinfer/tomography/models.py", line 217, in likelihood
    return FiniteOutcomeModel.pr0_to_likelihood_array(outcomes, 1 - pr1)
  File "/home/jayce/.local/lib/python3.5/site-packages/qinfer/abstract_model.py", line 675, in pr0_to_likelihood_array
    for idx in range(safe_shape(outcomes))
  File "/home/jayce/.local/lib/python3.5/site-packages/qinfer/abstract_model.py", line 675, in <listcomp>
    for idx in range(safe_shape(outcomes))
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
ihincks commented 7 years ago

After a quick look, I think the problem is possibly that your variable data is more than 1-dimensional when this error is thrown, which is not supported (ie np.ndim(data) should be less than 2).

This is happening on line 674 of abstract_model.pywhen it is checking which measurement outcomes are 0, and which are 1, which gives the same error as follows:

a=np.array([[0,1],[0,1]])
if a[0] == 0:
    pass
ihincks commented 7 years ago

(If this doesn't fix the problem, we might need some example code that raises this error)

Nowzerix commented 7 years ago

Alright, caught the bug in my code. It's so much easier when I know what I'm looking for. Thanks a bunch.