Skip to content

Commit

Permalink
Optimize kernel mode for hybrid QNN test case (#1572)
Browse files Browse the repository at this point in the history
Enumerating and looping over all elements in inspect.stack() was incredibly
slow when running in a Jupyter notebook. (Interestingly enough, the
slowness was not apparent when running a Python script with the
python3 command.)
  • Loading branch information
bmhowe23 committed Apr 26, 2024
1 parent 3ce8ef2 commit 00253a8
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions python/cudaq/kernel/kernel_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,24 @@ def compile(self):
# variables from the parent frame that we captured
# have not changed. If they have changed, we need to
# recompile with the new values.
for i, s in enumerate(inspect.stack()):
if s.frame == self.parentFrame:
s = inspect.currentframe()
while s:
if s == self.parentFrame:
# We found the parent frame, now
# see if any of the variables we depend
# on have changed.
self.globalScopedVars = {
k: v for k, v in dict(inspect.getmembers(s.frame))
['f_locals'].items()
k: v
for k, v in dict(inspect.getmembers(s))['f_locals'].items()
}
if self.dependentCaptures != None:
for k, v in self.dependentCaptures.items():
if self.globalScopedVars[k] != v:
# Need to recompile
self.module = None
break
break
s = s.f_back

if self.module != None:
return
Expand Down

0 comments on commit 00253a8

Please sign in to comment.