-
Notifications
You must be signed in to change notification settings - Fork 188
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
Reduce the use of SymPy lambdify #579
Conversation
Codecov Report
@@ Coverage Diff @@
## master #579 +/- ##
=======================================
Coverage 98.38% 98.38%
=======================================
Files 75 75
Lines 8410 8414 +4
=======================================
+ Hits 8274 8278 +4
Misses 136 136
Continue to review full report at Codecov.
|
Profiling The following script was profiled for both memory and time: import strawberryfields as sf
from strawberryfields.ops import *
from strawberryfields.parameters import par_funcs as pf
import numpy as np
eng = sf.Engine("fock", backend_options={"cutoff_dim": 2})
def evx(param, n):
prog = sf.Program(2)
with prog.context as q:
ntrot = 10
for i in range(n):
step = param/ntrot
for itrot in range(ntrot):
s = step
r = pf.asinh(-s / 2)
theta = 0.5 * pf.atan2(-1.0 / pf.cosh(r), -pf.tanh(r))
for _ in range(20):
# Decomposition of CXgate
BSgate(theta, 0) | (q[0], q[1]),
Sgate(r, 0) | q[0],
Sgate(-r, 0) | q[1],
BSgate(theta + np.pi / 2, 0) | (q[0], q[1]),
state = eng.run(prog).state
return state.number_expectation([0])
parm = 0.05
for i in range(10):
if eng.run_progs:
eng.reset()
res = evx(parm, 40)
print(i, flush=True)
|
@antalszava : don't forget to update the changelog. Also you might want to make the CodeFactor bot happy. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great 🕵️ work @antalszava!
# sympy.lambdify caches data using linecache, if called many times this | ||
# can make up for a lot of memory used. We clear the cache here to | ||
# avoid that. | ||
linecache.clearcache() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice catch @antalszava!
Context:
Some optimization tasks using Strawberry Fields were showing increased memory allocation, although no state was being left (i.e., the engine was reset, state objects recreated, etc.).
Parameters in Strawberry Fields might be numeric or symbolic. To represent both, SymPy is used. When using gate parameters that are internally created, Strawberry Fields evaluates the parameter to get the numeric value using SymPy.
Evaluating the parameters depends on the
sympy.lambdify
function, which internally caches data usinglinecache
. The memory increase was found to be due to this caching.Description of the Change:
Changes the
par_evaluate
function which does the parameter evaluation such that:float
orcomplex
), then we perform the conversion without usingsympy.lambdify
sympy.lambdify
we also clear the cache usinglinecache
.Benefits:
sympy.lambdify
can be avoidedPossible Drawbacks:
N/A
Related GitHub Issues:
N/A
See profiling data here.