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

Fix issue caused by docstrings inside Python kernels #1459

Merged
merged 3 commits into from
Apr 1, 2024
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
17 changes: 15 additions & 2 deletions python/cudaq/kernel/ast_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,21 @@ def isQuantumTy(ty):
self.symbolTable.clear()
self.valueStack.clear()

# [RFC]:
# Examine if we really want to extend Python with a dedicated syntax.
def visit_Expr(self, node):
"""
Implement `ast.Expr` visitation to screen out all
multi-line `docstrings`. These are differentiated from other strings
at the node-type level. Strings we may care about will have been
assigned to a variable (hence `ast.Assign` nodes), while other strings will exist
as standalone expressions with no uses.
"""
if hasattr(node, 'value') and isinstance(node.value, ast.Constant):
constant = node.value
if isinstance(constant.value, str):
return

self.visit(node.value)

def visit_Lambda(self, node):
"""
Map a lambda expression in a CUDA Quantum kernel to a CC Lambda (a Value of `cc.callable` type
Expand Down
26 changes: 26 additions & 0 deletions python/tests/kernel/test_kernel_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,3 +1018,29 @@ def kernel(angle:float):
╰───╯
'''
assert circuit == expected_str


def test_with_docstring_2():
@cudaq.kernel
def simple(n:int):
'''
A docstring with triple single quote
'''
qubits = cudaq.qvector(n)
exp_pauli(2.2, qubits, 'YYYY')
"""
A docstring in the middle of kernel
"""
for q in qubits:
'''
A multi-line string.
Should be ignored.
'''
h(q)

@cudaq.kernel
def kernel():
simple(4)

kernel.compile()
print(kernel)
Loading