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

kernel.control() raises error for multi-qubit control argument #2731

Open
3 of 4 tasks
fedonman opened this issue Mar 13, 2025 · 0 comments
Open
3 of 4 tasks

kernel.control() raises error for multi-qubit control argument #2731

fedonman opened this issue Mar 13, 2025 · 0 comments

Comments

@fedonman
Copy link

fedonman commented Mar 13, 2025

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

I am trying to create controlled versions of X gate. For the CX (1 control qubit), the code works correctly, but for CCX (2 control qubits) or more, I get an error.

Steps to reproduce the bug

The following works correcty:

nqubits = 3

kernel = cudaq.make_kernel()
qubits = kernel.qalloc(nqubits)

target_kernel, qubit = cudaq.make_kernel(cudaq.qubit)
target_kernel.x(qubit)

kernel.control(target_kernel, qubits[0], qubits[2])

When I try to add more control qubits, I get an error, no matter the syntax:

nqubits = 3

kernel = cudaq.make_kernel()
qubits = kernel.qalloc(nqubits)

target_kernel, qubit = cudaq.make_kernel(cudaq.qubit)
target_kernel.x(qubit)

kernel.control(target_kernel, [qubits[0], qubits[1]], qubits[2])  # This raises an error
kernel.control(target_kernel, qubits[0], qubits[1], qubits[2])  # This raises an error as well

In the first case the error is:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[7], line 10
      7 target_kernel.x(qubit)
      9 kernel.h(qubits[0])
---> 10 kernel.control(target_kernel, [qubits[0], qubits[1]], qubits[nqubits - 1])  # This raises an error
     11 # kernel.control(target_kernel, qubits[0], qubits[1], qubits[nqubits - 1])  # This raises an error as well

File ~/projects/qilisdk/.venv/lib/python3.10/site-packages/cudaq/kernel/kernel_builder.py:1239, in PyKernel.control(self, target, control, *target_arguments)
   1205 def control(self, target, control, *target_arguments):
   1206     """
   1207     Apply the `target` kernel as a controlled operation in-place to 
   1208     `self`.Uses the provided `control` as control qubit/s for the operation.
   (...)
   1237     ```
   1238     """
-> 1239     self.__applyControlOrAdjoint(target, False, [control.mlirValue],
   1240                                  *target_arguments)
   1241     return

AttributeError: 'list' object has no attribute 'mlirValue'

In the second case the error is:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[6], line 11
      9 kernel.h(qubits[0])
     10 # kernel.control(target_kernel, [qubits[0], qubits[1]], qubits[nqubits - 1])  # This raises an error
---> 11 kernel.control(target_kernel, qubits[0], qubits[1], qubits[nqubits - 1])  # This raises an error as well

File ~/projects/qilisdk/.venv/lib/python3.10/site-packages/cudaq/kernel/kernel_builder.py:1239, in PyKernel.control(self, target, control, *target_arguments)
   1205 def control(self, target, control, *target_arguments):
   1206     """
   1207     Apply the `target` kernel as a controlled operation in-place to 
   1208     `self`.Uses the provided `control` as control qubit/s for the operation.
   (...)
   1237     ```
   1238     """
-> 1239     self.__applyControlOrAdjoint(target, False, [control.mlirValue],
   1240                                  *target_arguments)
   1241     return

File ~/projects/qilisdk/.venv/lib/python3.10/site-packages/cudaq/kernel/kernel_builder.py:666, in PyKernel.__applyControlOrAdjoint(self, target, isAdjoint, controls, *args)
    664 mlirValues = []
    665 for i, v in enumerate(args):
--> 666     argTy = otherFTy[i].type
    667     if not isinstance(v, QuakeValue):
    668         # here we have to map constant Python data
    669         # to an MLIR Value
    670         value = self.__getMLIRValueFromPythonArg(v, argTy)

IndexError: index out of range

Expected behavior

I would expect the correct CCX gate to be created as is the case with the decorator syntax:

@cudaq.kernel
def kernel():
    qubits = cudaq.qvector(3)
    x.ctrl([qubits[0], qubits[1]], qubits[2])

print(cudaq.draw(kernel))

# Prints:
q0 : ──●──
       │  
q1 : ──●──
     ╭─┴─╮
q2 : ┤ x ├
     ╰───╯

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

I haven't tested it with older versions.

Environment

  • CUDA-Q version: 0.9.1
  • Python version: 3.10+
  • C++ compiler: Irrelevant
  • Operating system: Ubuntu 24.04

Suggestions

The error probably lies within how kernel_builder.__applyControlOrAdjoint() handles the QuakeValue of control argument.

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