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

[compiler bug] Python MLIR doesn't catch incorrect arguments to rotation gates #1641

Open
3 of 4 tasks
anthony-santana opened this issue May 8, 2024 · 2 comments
Open
3 of 4 tasks
Labels
python bridge Involves the python bridge to quake

Comments

@anthony-santana
Copy link
Collaborator

anthony-santana commented May 8, 2024

Required prerequisites

  • Consult the security policy. If reporting a security vulnerability, do not report the bug using this form. Use the process described in the policy to report the issue.
  • Make sure you've read the documentation. Your issue may be addressed there.
  • Search the issue tracker to verify that this hasn't already been reported. +1 or comment there if it has.
  • If possible, make a PR with a failing test to give us a starting point to work on!

Describe the bug

In python MLIR, we aren't catching when a user calls rotation gates with incorrect arguments.

Steps to reproduce the bug

@cudaq.kernel
def kernel():
    q = cudaq.qubit()
    # Examples of things we can get away with without error:
    rx(3.14)
    rx("random_argument")
    # Our regular gates do throw an error like this:
    # x("random_argument") 
   

print(kernel)

result = cudaq.sample(kernel)
print(result)

Expected behavior

Expect an error similar to the conventional gates, such as:

cudaq.kernel.ast_bridge.CompilerError: test.py:37: error: quantum operation x on incorrect quantum type <TYPENAME>.
         (offending source -> <VALUE>)

Is this a regression? If it is, put the last known working version (or commit) here.

Not a regression

Environment

  • CUDA Quantum version:
  • Python version:
  • C++ compiler:
  • Operating system:
    All latest

Suggestions

No response

@anthony-santana
Copy link
Collaborator Author

I do also think the general error message could use some improvement. Something that mentions function arguments. E.g

cudaq.kernel.ast_bridge.CompilerError: test.py:37: error: quantum operation <OP_NAME> received incorrect argument type <TYPENAME>.
         (offending source -> <VALUE>)

@amccaskey
Copy link
Collaborator

@anthony-santana you could add the following

diff --git a/python/cudaq/kernel/ast_bridge.py b/python/cudaq/kernel/ast_bridge.py
index 412174880..649e5618e 100644
--- a/python/cudaq/kernel/ast_bridge.py
+++ b/python/cudaq/kernel/ast_bridge.py
@@ -1365,11 +1365,16 @@ class PyASTBridge(ast.NodeVisitor):
 
             if node.func.id in ["rx", "ry", "rz", "r1"]:
                 numValues = len(self.valueStack)
+                if numValues < 2:
+                    self.emitFatalError(f'Invalid number of arguments ({numValues}) passed to {node.func.id} (requires at least 2 arguments)', node)
                 qubitTargets = [self.popValue() for _ in range(numValues - 1)]
                 qubitTargets.reverse()
                 param = self.popValue()
                 if IntegerType.isinstance(param.type):
                     param = arith.SIToFPOp(self.getFloatType(), param).result
+                if not F64Type.isinstance(param.type):
+                    self.emitFatalError('rotational parameter must be a float.', node)
+                self.checkControlAndTargetTypes([], qubitTargets)
                 self.__applyQuantumOperation(node.func.id, [param],
                                              qubitTargets)
                 return

@schweitzpgi schweitzpgi added the python bridge Involves the python bridge to quake label Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python bridge Involves the python bridge to quake
Projects
None yet
Development

No branches or pull requests

3 participants