isi-usc-edu / pyLIQTR

Lincoln Laboratory Quantum Algorithm Test and Research
https://isi-usc-edu.github.io/pyLIQTR/
MIT License
26 stars 6 forks source link

DoubleFactorized block encoding fails when sf_error_threshold is zero #21

Closed max-radin closed 3 weeks ago

max-radin commented 2 months ago

I'm seeing the error below when initializing DoubleFactorized objects with sf_error_threshold set to zero. (Context: I'm setting this to zero to make the truncation consistent with OpenFermion's logic for estimating truncation error.) It looks like the issue is just that num_ops does not get initialized if none of the eigenvalues are thrown out.

I think this is not a big blocker for our work though because it seems like we can work around it by just setting sf_error_threshold to some very small value.

Traceback (most recent call last):
  File "/Users/maxradin/Development/qb-gsee-benchmark/examples/get_qre.py", line 14, in <module>
    circuit, n_shots, allowable_error = get_df_qpe_circuit(
                                        ^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/Development/qb-gsee-benchmark/src/qb_gsee_benchmark/qre.py", line 250, in get_df_qpe_circuit
    encoding = DoubleFactorized(
               ^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/pyLIQTR/BlockEncodings/DoubleFactorized.py", line 67, in __init__
    one_body_array, two_body_array, self.Xi_l_data, self.givens_angle_tensor = self.PI.yield_DF_Info(df_error_threshold=df_error_threshold,sf_error_threshold=sf_error_threshold)
                                                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/pyLIQTR/ProblemInstances/ChemicalHamiltonian.py", line 193, in yield_DF_Info
    DF_frags = self.DF_fragments(sf_error_threshold)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/pyLIQTR/ProblemInstances/ChemicalHamiltonian.py", line 130, in DF_fragments
    return DF_decomposition(h0, one_body_tensor, two_body_tensor,tol=sf_error_threshold)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/pyLIQTR/utils/df_utils.py", line 238, in DF_decomposition
    vals = all_vals[:num_ops]
                     ^^^^^^^
UnboundLocalError: cannot access local variable 'num_ops' where it is not associated with a value
kjm538 commented 2 months ago

It looks like the issue is just that num_ops does not get initialized if none of the eigenvalues are thrown out.

Looks like you're right. I'll push the fix with the next release, thanks for pointing it out.

rroodll commented 3 weeks ago

Fixed in 1.3.0