NVIDIA / NVIDIA/cuda-quantum

`cudaq.adjoint()` gives the wrong rotation angle

Open
#5,403 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
1.1k
Forks
455
Avg merge
1d 22h
Merged PRs (30d)
165

Description

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

We built a small quantum Fourier transform (QFT) kernel, and asked CUDA-Q to automatically build its inverse using cudaq.adjoint(). The inverse it built is wrong. One rotation gate gets the wrong angle.

Steps to reproduce the bug
import cudaq
import numpy as np

def bits_of(value, n):
    # turn an integer into a list of bools, most-significant bit first
    l = [bool((value >> i) & 1) for i in range(n)]
    l.reverse()
    return l 

@cudaq.kernel
def qft(qs: cudaq.qview):
    l = qs.size()
    for tgt in range(0, l):
        h(qs[tgt])
        r_angel = np.pi
        for ctrl in range(tgt+1, l):
            r_angel /= 2
            r1.ctrl(r_angel, qs[ctrl], qs[tgt])

@cudaq.kernel
def roundtrip(a: list[bool]):
    q = cudaq.qvector(3)
    for i in range(3):
        if a[i]:
            x(q[i])
    qft(q)
    cudaq.adjoint(qft, q)
    mz(q[0]); mz(q[1]); mz(q[2])

print("Expected: for every starting value 'a', we should measure 'a' back 100% of the time.")
print("Actual result:")
for aval in range(8):
    a_l = bits_of(aval, 3)
    result = cudaq.sample(roundtrip, a_l, shots_count=1000)
    print(f"  a={aval} ({''.join('1' if b else '0' for b in a_l)}) -> {dict(result.items())}")


@cudaq.kernel
def main():
    a = [False, True, True]  # a = 3 (binary 011)
    roundtrip(a)

print(cudaq.draw(main))
Expected behavior

For every starting value 'a', we should measure 'a' back 100% of the time, because of the reversibility of the quantum circuit.

For the cudaq.draw(main):

The adjoint should exactly undo this, in reverse order with every angle negated: Attached mark the gate with wrong rotation angel.

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

Not a regression

Environment
  • CUDA-Q version: 0.15.0
  • Python version: 3.11.15
  • C++ compiler:
  • Operating system: Ubuntu 26.04 LTS (run on Windows using WSL 2 )
Suggestions

To narrow down the cause, we rewrote qft() in two ways:

  1. Unroll the outer loop
@cudaq.kernel
def qft(qs: cudaq.qview):
    l = qs.size()

    tgt = 0
    h(qs[tgt])
    r_angel = np.pi
    for ctrl in range(tgt+1, l):
        r_angel /= 2
        r1.ctrl(r_angel, qs[ctrl], qs[tgt])

    tgt += 1
    h(qs[tgt])
    r_angel = np.pi
    for ctrl in range(tgt+1, l):
        r_angel /= 2
        r1.ctrl(r_angel, qs[ctrl], qs[tgt])

    tgt += 1
    h(qs[tgt])
    r_angel = np.pi
    for ctrl in range(tgt+1, l):
        r_angel /= 2
        r1.ctrl(r_angel, qs[ctrl], qs[tgt])

@cudaq.kernel
def roundtrip(a: list[bool]):
    q = cudaq.qvector(3)
    for i in range(3):
        if a[i]:
            x(q[i])
    qft(q)
    cudaq.adjoint(qft, q)
    mz(q[0]); mz(q[1]); mz(q[2])

try:
    a = bits_of(3, 3)
    result = cudaq.sample(roundtrip, a, shots_count=1000)
    print(result)
except RuntimeError as e:
    print(f"CRASH: {e}")
  1. Unroll the inner loop
@cudaq.kernel
def qft(qs: cudaq.qview):
    l = qs.size()
    for tgt in range(0, l):
        h(qs[tgt])
        if tgt == 0:
            r1.ctrl(np.pi/2, qs[1], qs[tgt])
            r1.ctrl(np.pi/4, qs[2], qs[tgt])
        if tgt == 1:
            r1.ctrl(np.pi/2, qs[2], qs[tgt])

@cudaq.kernel
def roundtrip(a: list[bool]):
    q = cudaq.qvector(3)
    for i in range(3):
        if a[i]:
            x(q[i])
    qft(q)
    cudaq.adjoint(qft, q)
    mz(q[0]); mz(q[1]); mz(q[2])

print("Expected: for every starting value 'a', we should measure 'a' back 100% of the time.")
print("Actual result:")
for aval in range(8):
    a_l = bits_of(aval, 3)
    result = cudaq.sample(roundtrip, a_l, shots_count=1000)
    print(f"  a={aval} ({''.join('1' if b else '0' for b in a_l)}) -> {dict(result.items())}")

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Python reproduction on CUDA-Q 0.15.0 and inspect cudaq.draw(main) alongside the measurement results. Compare the generated adjoint for the looped QFT with the two unrolled variants; done means the inverse reverses the gate order and negates every rotation angle, restoring each starting value.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, quantum-computing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.