The edge case handling in which all shots have been filtered out is broken. More specifically, this code is causing issues:
Try to compute an expectation value for a set of data in which the post-selection removes all shots for any observable term.
I will work on putting together a simple to reproduce example.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[217], line 1
----> 1 res = post_process_conv(exec_result[0], steps=steps, gamma=gamma, ps="node", trex=True)
Cell In[212], line 27, in post_process_conv(datum, steps, gamma, ps, trex)
24 results = []
25 for i in range(steps, num_randomizations + 1, steps):
26 # Compute mitigated expvals with post-selectoion
---> 27 res = executor_expectation_values(
28 meas[:i],
29 reverser,
30 meas_basis_axis,
31 avg_axis=avg_axis,
32 measurement_flips=flips[:i],
33 pauli_signs=signs[:i] if signs is not None else None,
34 postselect_mask=mask[:i] if mask is not None else None,
35 rescale_factors=trex_scale_factors if trex else None,
36 gamma_factor=gamma,
37 )
38 results.append(res[0])
39 return results
File lib64/python3.13/site-packages/qiskit_addon_utils/exp_vals/expectation_values.py:185, in executor_expectation_values(bool_array, basis_dict, meas_basis_axis, avg_axis, measurement_flips, pauli_signs, postselect_mask, gamma_factor, rescale_factors)
180 basis_rescale_factors = (
181 rescale_factors[meas_basis_idx] if rescale_factors is not None else None
182 )
184 ## AVERAGE OVER SHOTS:
--> 185 (means, standard_errs) = _bitarray_expectation_value(
186 barray_this_basis,
187 observables,
188 shots=num_kept,
189 rescale_each_observable=basis_rescale_factors,
190 )
192 variances = standard_errs**2
193 del standard_errs
File lib64/python3.13/site-packages/qiskit_addon_utils/exp_vals/expectation_values.py:401, in _bitarray_expectation_value(outcomes, observables, shots, rescale_each_observable)
398 # Edge case of counts dict containing outcomes but with total shots, eg {"0": 0}.
399 no_shots = denom == 0
--> 401 expvals_each_term[~no_shots] /= denom[..., np.newaxis]
402 sq_expvals_each_term[~no_shots] /= denom[..., np.newaxis]
403 expvals_each_term[no_shots] = np.nan
ValueError: operands could not be broadcast together with shapes (14,1) (16,1) (14,1)
Environment
What is happening and why is it wrong?
The edge case handling in which all shots have been filtered out is broken. More specifically, this code is causing issues:
qiskit-addon-utils/qiskit_addon_utils/exp_vals/expectation_values.py
Lines 398 to 401 in 9b92faf
How can we reproduce the issue?
Try to compute an expectation value for a set of data in which the post-selection removes all shots for any observable term.
I will work on putting together a simple to reproduce example.
Traceback
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[217], line 1 ----> 1 res = post_process_conv(exec_result[0], steps=steps, gamma=gamma, ps="node", trex=True) Cell In[212], line 27, in post_process_conv(datum, steps, gamma, ps, trex) 24 results = [] 25 for i in range(steps, num_randomizations + 1, steps): 26 # Compute mitigated expvals with post-selectoion ---> 27 res = executor_expectation_values( 28 meas[:i], 29 reverser, 30 meas_basis_axis, 31 avg_axis=avg_axis, 32 measurement_flips=flips[:i], 33 pauli_signs=signs[:i] if signs is not None else None, 34 postselect_mask=mask[:i] if mask is not None else None, 35 rescale_factors=trex_scale_factors if trex else None, 36 gamma_factor=gamma, 37 ) 38 results.append(res[0]) 39 return results File lib64/python3.13/site-packages/qiskit_addon_utils/exp_vals/expectation_values.py:185, in executor_expectation_values(bool_array, basis_dict, meas_basis_axis, avg_axis, measurement_flips, pauli_signs, postselect_mask, gamma_factor, rescale_factors) 180 basis_rescale_factors = ( 181 rescale_factors[meas_basis_idx] if rescale_factors is not None else None 182 ) 184 ## AVERAGE OVER SHOTS: --> 185 (means, standard_errs) = _bitarray_expectation_value( 186 barray_this_basis, 187 observables, 188 shots=num_kept, 189 rescale_each_observable=basis_rescale_factors, 190 ) 192 variances = standard_errs**2 193 del standard_errs File lib64/python3.13/site-packages/qiskit_addon_utils/exp_vals/expectation_values.py:401, in _bitarray_expectation_value(outcomes, observables, shots, rescale_each_observable) 398 # Edge case of counts dict containing outcomes but with total shots, eg {"0": 0}. 399 no_shots = denom == 0 --> 401 expvals_each_term[~no_shots] /= denom[..., np.newaxis] 402 sq_expvals_each_term[~no_shots] /= denom[..., np.newaxis] 403 expvals_each_term[no_shots] = np.nan ValueError: operands could not be broadcast together with shapes (14,1) (16,1) (14,1)Any suggestions?
We need to update our handling of
no_shots. While the intention was good, the implementation is not correct as it stands.It could be as simple as also using
no_shotsfordenombut I need to verify the axis handling.If that works, we still need to account for the potential presence of
np.nanlater on in the code, since we end up summingexpvals_each_termto compute the total observable expectation value. If we don't account fornp.nanthere, we will simply get outnp.nanfor the summed value which is not what we want.