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

Avoid deepcopy in pulse parser (backport #9063) #9068

Merged
merged 1 commit into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion qiskit/pulse/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def visit_Expression(self, node: ast.Expression) -> ast.Expression:
Returns:
Evaluated value.
"""
tmp_node = copy.deepcopy(node)
tmp_node = copy.copy(node)
tmp_node.body = self.visit(tmp_node.body)

return tmp_node
Expand Down Expand Up @@ -240,6 +240,7 @@ def visit_UnaryOp(self, node: ast.UnaryOp) -> Union[ast.UnaryOp, ast.Constant]:
Returns:
Evaluated value.
"""
node = copy.copy(node)
node.operand = self.visit(node.operand)
if isinstance(node.operand, (ast.Constant, ast.Num)):
val = ast.Constant(n=self._match_ops(node.op, self._unary_ops, node.operand.n))
Expand All @@ -255,6 +256,7 @@ def visit_BinOp(self, node: ast.BinOp) -> Union[ast.BinOp, ast.Constant]:
Returns:
Evaluated value.
"""
node = copy.copy(node)
node.left = self.visit(node.left)
node.right = self.visit(node.right)
if isinstance(node.left, (ast.Constant, ast.Num)) and isinstance(
Expand All @@ -280,6 +282,7 @@ def visit_Call(self, node: ast.Call) -> Union[ast.Call, ast.Constant]:
"""
if not isinstance(node.func, ast.Name):
raise PulseError("Unsafe expression is detected.")
node = copy.copy(node)
node.args = [self.visit(arg) for arg in node.args]
if all(isinstance(arg, (ast.Constant, ast.Num)) for arg in node.args):
if node.func.id not in self._math_ops.keys():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
The Pulse expression parser should no longer periodically hang when called
from Jupyter notebooks. This is achieved by avoiding an internal ``deepycopy``
of a recursive object that seemed to be particularly difficult for the
memoization to evaluate.