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

custom gate register_operation doesn't seem to work if it is invoked in one kernel which is nested in another. #2575

Open
3 of 4 tasks
mmvandieren opened this issue Feb 2, 2025 · 1 comment

Comments

@mmvandieren
Copy link
Contributor

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

cudaq.register_operation appears not to work in a nested kernel situation. see code example below

Steps to reproduce the bug

my_matrix = np.matrix([[0, 1], [1, 0]])
cudaq.register_operation("my_gate", my_matrix)

@cudaq.kernel
def my_kernel(qubits : cudaq.qview):
    my_gate(qubits[0])
    
@cudaq.kernel
def kernel(num_qubits : int):
    qubits = cudaq.qvector(num_qubits)
    my_kernel(qubits[0]) # Can't call custom gate through another kernel here
    my_gate(qubits[0]) # Can call the custom gate directly here 

result = cudaq.sample(kernel, 2, shots_count=1000)
print(result)

Expected behavior

Expected to call the my_gate through the my_kernel call.

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

Not a regression

Environment

  • **CUDA-Q version **: 0.9.1
  • Python version:
  • C++ compiler:
  • Operating system:

Suggestions

No response

@mmvandieren
Copy link
Contributor Author

this is the error message

error: 'func.call' op operand type mismatch: expected operand type '!quake.veq<?>', but provided '!quake.ref' for operand number 0
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
File /opt/nvidia/cudaq/cudaq/kernel/ast_bridge.py:4020, in compile_to_mlir(astModule, metadata, capturedDataStorage, **kwargs)
   [4019](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/ast_bridge.py:4019) try:
-> [4020](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/ast_bridge.py:4020)     pm.run(bridge.module)
   [4021](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/ast_bridge.py:4021) except:

RuntimeError: Failure while executing pass pipeline.

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
Cell In[34], [line 14](vscode-notebook-cell:?execution_count=34&line=14)
     [11](vscode-notebook-cell:?execution_count=34&line=11)     my_kernel(qubits[0])
     [12](vscode-notebook-cell:?execution_count=34&line=12)     my_gate(qubits[0])
---> [14](vscode-notebook-cell:?execution_count=34&line=14) result = cudaq.sample(kernel, 2, shots_count=1000)
     [15](vscode-notebook-cell:?execution_count=34&line=15) print(result)

File /opt/nvidia/cudaq/cudaq/runtime/sample.py:86, in sample(kernel, shots_count, noise_model, *args)
     [83](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/runtime/sample.py:83)             cudaq_runtime.setExecutionContext(ctx)
     [84](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/runtime/sample.py:84)     return counts
---> [86](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/runtime/sample.py:86) kernel(*args)
     [87](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/runtime/sample.py:87) res = ctx.result
     [88](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/runtime/sample.py:88) cudaq_runtime.resetExecutionContext()

File /opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:423, in PyKernelDecorator.__call__(self, *args)
    [420](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:420) self.capturedDataStorage = self.createStorage()
    [422](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:422) # Compile, no-op if the module is not None
--> [423](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:423) self.compile()
    [425](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:425) if len(args) != len(self.argTypes):
    [426](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:426)     emitFatalError(
    [427](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:427)         f"Incorrect number of runtime arguments provided to kernel `{self.name}` ({len(self.argTypes)} required, {len(args)} provided)"
    [428](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:428)     )

File /opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:210, in PyKernelDecorator.compile(self)
    [207](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:207) if self.module != None:
    [208](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:208)     return
--> [210](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:210) self.module, self.argTypes, extraMetadata = compile_to_mlir(
    [211](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:211)     self.astModule,
    [212](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:212)     self.metadata,
    [213](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:213)     self.capturedDataStorage,
    [214](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:214)     verbose=self.verbose,
    [215](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:215)     returnType=self.returnType,
    [216](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:216)     location=self.location,
    [217](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:217)     parentVariables=self.globalScopedVars)
    [219](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:219) # Grab the dependent capture variables, if any
    [220](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:220) self.dependentCaptures = extraMetadata[
    [221](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/kernel_decorator.py:221)     'dependent_captures'] if 'dependent_captures' in extraMetadata else None

File /opt/nvidia/cudaq/cudaq/kernel/ast_bridge.py:4022, in compile_to_mlir(astModule, metadata, capturedDataStorage, **kwargs)
   [4020](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/ast_bridge.py:4020)     pm.run(bridge.module)
   [4021](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/ast_bridge.py:4021) except:
-> [4022](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/ast_bridge.py:4022)     raise RuntimeError("could not compile code for '{}'.".format(
   [4023](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/ast_bridge.py:4023)         bridge.name))
   [4025](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/ast_bridge.py:4025) if metadata['conditionalOnMeasure']:
   [4026](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/ast_bridge.py:4026)     SymbolTable(
   [4027](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/ast_bridge.py:4027)         bridge.module.operation)[nvqppPrefix +
   [4028](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/ast_bridge.py:4028)                                  bridge.name].attributes.__setitem__(
   [4029](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/ast_bridge.py:4029)                                      'qubitMeasurementFeedback',
   [4030](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f66657276656e745f6d636e756c7479222c2273657474696e6773223a7b22636f6e74657874223a226465736b746f702d6c696e7578227d7d.vscode-resource.vscode-cdn.net/opt/nvidia/cudaq/cudaq/kernel/ast_bridge.py:4030)                                      BoolAttr.get(True, context=bridge.ctx))

RuntimeError: could not compile code for 'kernel'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant