microsoft / microsoft/Qcodes

debugging confusion created by DeferredOperations

Open
#454 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
459
Forks
359
Avg merge
3d 6h
Merged PRs (30d)
73

Description

@nataliejpg ran into an issue trying to debug this code:

```python
demod_list = np.array([getattr(self, 'demod_freq_{}'.format(n)) for n in range(self.res_length)])
demod_mat = np.kron(demod_list, np.ones(self.samples_per_record)).reshape(self.res_length, self.samples_per_record)
integer_mat = np.kron(np.ones(self.res_length), np.arange(self.samples_per_record)).reshape((self.res_length, self.samples_per_record))
angle_mat = np.multiply(2 * np.pi * integer_mat, demod_mat) / self.sample_rate
self.cos_mat = np.cos(angle_mat)
```

Which throws an error:
```
AttributeError: 'DeferredOperations' object has no attribute 'cos'
```

The root cause is that the `getattr` call yields a `Parameter`, not a parameter value, so needs to be evaluated right away:
```
demod_list = np.array([getattr(self, 'demod_freq_{}'.format(n))() for n in range(self.res_length)])
```

But because the `np.multiply` call actually *succeeded* (burying the `Parameter` in a new `DeferredOperations` object), this was tricky to debug.

Had `Parameter` not been a `DeferredOperations` at all, the error would have happened in `np.multiply`, with something like:
```
TypeError: unsupported operand type(s) for *: 'int' and 'StandardParameter'
```

Which is a lot easier to tie back to the root cause. Perhaps we can have `DeferredOperations` override `__getattr__` to point back to its roots with the error message, something like:
```
AttributeError: 'DeferredOperations' object has no attribute 'cos' -
Did you mean to evaluate your parameter earlier?
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the DeferredOperations and Parameter entry points and reproduce the shown NumPy expression to trace where the Parameter becomes deferred. Done means an attempted attribute such as cos reports that the underlying parameter should be evaluated earlier, without hiding the original cause.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
developer-experience
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.