Skip to content

[python] Calling builder kernels within decorator kernels is not supported #4071

@lmondada

Description

@lmondada

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

The following tests fail with the error "error: unhandled function call - builder_kernel".

def test_call_builder_kernel_from_decorator_kernel():
    """Tests that a kernel built with make_kernel can be called
    from within a @cudaq.kernel decorated function."""
    builder_kernel, qubit = cudaq.make_kernel(cudaq.qubit)
    builder_kernel.x(qubit)

    @cudaq.kernel
    def caller():
        q = cudaq.qubit()
        builder_kernel(q)

    counts = cudaq.sample(caller, shots_count=10)
    assert counts["1"] == 10


def test_call_builder_kernel_from_decorator_kernel_2():
    """Tests that a parameterized builder kernel can be called
    from within a @cudaq.kernel decorated function."""
    builder_kernel, angle = cudaq.make_kernel(float)
    qubits = builder_kernel.qalloc(1)
    builder_kernel.ry(angle, qubits[0])

    @cudaq.kernel
    def caller():
        builder_kernel(np.pi)

    counts = cudaq.sample(caller, shots_count=10)
    assert counts["1"] == 10

Steps to reproduce the bug

run the above test

Expected behavior

tests pass, or a better error is raised

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

Not a regression

Environment

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

Suggestions

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions