Set sympy.evaluate(False) on frequent code paths for phase tracking.#6072
Conversation
|
I think this makes sense, since I think we should prioritize the performance of binding parameters over showing a nice expression for the global phase (of course if we can have both in the future that would be even better). Could you add a release note pointing out that the global phase is not simplified? |
Modulo performance differences(at what point the sympy simplification happens), this shouldn't have a user facing impact (unless someone is directly accessing |
If you add another |
That's a good point (this came up in discussions with @Cryoris as well). Given that the simplification is not behavior we're entirely settled on, I can add a note pointing out that a parameterized global phase might no longer be simplified after transpilation. |
|
On this branch, numbers from example code in #5405 : |
|
Regarding the introduction of potential bugs, if that happens, it would be frustrating not being able to recover the original behavior. Why not adding a parameter (i.e. |
…acking. (Qiskit#6072)" With recent adoption of symengine for the performance critical path with ParameterExpressions the workaround for large expression evaluation in Qiskit#6072 is no longer needed or applicable, except in environments without symengine. However, with symengine available using the sympy evaluate context adds noticeable overhead, both to import sympy (which wouldn't be used otherwise) and to load the sympy evalue context wrapper. This commit removes these as we shouldn't compromise performance in the performance path to try and speed up a path with degraded performance because symengine isn't available (32bit platforms). This reverts commit f436f5a.
…acking. (#6072)" (#6386) With recent adoption of symengine for the performance critical path with ParameterExpressions the workaround for large expression evaluation in #6072 is no longer needed or applicable, except in environments without symengine. However, with symengine available using the sympy evaluate context adds noticeable overhead, both to import sympy (which wouldn't be used otherwise) and to load the sympy evalue context wrapper. This commit removes these as we shouldn't compromise performance in the performance path to try and speed up a path with degraded performance because symengine isn't available (32bit platforms). This reverts commit f436f5a. Co-authored-by: Kevin Krsulich <kevin.krsulich@ibm.com>
Summary
Resolves #5405 by disabling sympy evaluation on code paths that are hit frequently for parameterized global phase tracking.
Details and comments
I attempted to apply this more broadly (in
ParameterExpression._apply_operationandParameterExpression._call) which resulted in several failures for the gradient tests. Maybe this can be looked into more after the release.N.B. This PR also introduces the potential for bugs/complications as the
ParameterExpression._symbol_exprfor.global_phasewill become an unevaluated sympy expression, so some sympy behaviors which we may have come to depend on (e.g. commutativity of addition/multiplication) will no longer behave as they did previously.(The most likely consequence that comes to mind is false negatives for global phase equality.)(@levbishop confirmed that the changes toParameterExpression.__eq_in #5760 should make this a non-issue.)Evaluating the global phase expression after the fact (via
.doit()) resolves these issues but nullifies the performance fix. Likewise for collecting global phase contributions in theBasisTranslatorand summing them in a single expression.This PR is a short term fix at best, and will need some follow up. Would welcome any suggestions or alternatives.