Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update() method fails when tracking non-numeric values #6998

Open
umer066 opened this issue Feb 24, 2025 · 3 comments
Open

update() method fails when tracking non-numeric values #6998

umer066 opened this issue Feb 24, 2025 · 3 comments

Comments

@umer066
Copy link

umer066 commented Feb 24, 2025

https://github.com/PennyLaneAI/pennylane/blob/master/pennylane/tracker.py
In the tracker.py file

The update() method errors out when handling certain non-numeric values, despite the isinstance(value, Number) check. This leads to unexpected crashes instead of safely storing values in history.

Steps to Reproduce:

  1. Call update() with mixed data types:
    python tracker.update(metric_1=10, metric_2="string", metric_3=None)
  2. Observe an error instead of graceful handling.

Expected Behavior:

  • Store all values in history.
  • Sum only numeric values in totals.
  • Log a warning instead of raising an exception.

Possible Cause:

  • isinstance(value, Number) may not cover all cases (e.g., NoneType, custom objects).

Proposed Fix:

  • Ensure isinstance(value, Number) correctly filters numeric types.
  • Log a warning instead of failing.
@albi3ro
Copy link
Contributor

albi3ro commented Feb 24, 2025

I just ran:

 with qml.Tracker() as tracker:
     tracker.update(metric_1=10, metric_2="string", metric_3=None)
tracker.totals

And it worked fine without error.

Mind posting the full traceback you get and the exact code you are running?

@umer066
Copy link
Author

umer066 commented Feb 27, 2025

Actually, The original code was working fine, but my goal is to optimize it for better maintainability and debugging. Previously, the code used print to handle errors, which is not ideal for larger applications. By replacing it with warnings.warn, we ensure that issues are logged without disrupting execution while still alerting developers. This approach also allows for better control, as warnings can be filtered or escalated as needed. Let me know if you see any drawbacks to this change!"

@CatalinaAlbornoz
Copy link
Contributor

Hi @umer066 , thank you for opening this issue!

In your "Steps to Reproduce" you mentioned "Observe an error instead of graceful handling".
I'd like to try replicating your error so that we can decide how to proceed. Could you please share a minimal reproducible example so that we can look for the root of the problem?

A minimal reproducible example (or minimal working example) is the simplest version of the code that reproduces the problem. It should be self-contained, including all necessary imports, data, functions, etc., so that we can copy-paste the code and reproduce the problem. However it shouldn't contain any unnecessary data, functions, etc. (for example gates and functions that can be removed to simplify the code).

On the other hand, I didn't notice any print statements being used to handle errors. Could you please point us to the specific file and line of code that you're looking at?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants