Python: cudaq.set_noise keeps a dangling pointer to the NoiseModel (segfault, or silently noiseless sampling)

Open
#5,946 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@katepallewarprathmesh-sketch is already working on this.

Since Nov 20, 2026.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp, python

Research direction

Start by tracing the Python binding for cudaq.set_noise and how it stores the NoiseModel; the issue provides a minimal reproducer but names no source files or tests. Run the reproducer in both kept and temporary modes to confirm the lifetime failure. Done means the temporary model remains usable until unset_noise or the API raises a clear error, with a regression test covering the reported crash or incorrect sampling.

Written by the indexing model from the issue text.

Description

stale-notified

Versions

  • CUDA-Q 0.16.0 (62fce8b862302158e0119c65e3ce57538f3af048), pip cuda-quantum-cu12 0.16.0.post1
  • CUDA-Q 0.17.0 (1750574c92c613ff76c52fdbaa9289d864a3d640)
  • Python 3.12.4, Linux x86_64

Targets: density-matrix-cpu (below) and nvidia with option fp64 on an A100 80GB. Same behaviour on both.

Summary. cudaq.set_noise(model) stores a reference to the noise model without keeping the
Python object alive. If the model goes out of scope, the next cudaq.sample reads freed memory.
In the minimal script below this is a segmentation fault inside the first sample call. In a larger
application we instead got noiseless results with no error at all, which we only caught by comparing
noisy trajectory sampling against an exact density-matrix reference. Keeping a reference to the model
avoids both.

Reproducer (repro_cpu.py, run as python -X faulthandler repro_cpu.py kept or ... temporary):

import sys, cudaq
cudaq.set_target("density-matrix-cpu")

@cudaq.kernel
def k():
    q = cudaq.qubit()
    x(q)
    mz(q)

def install():
    m = cudaq.NoiseModel()
    m.add_all_qubit_channel("x", cudaq.BitFlipChannel(1.0))
    cudaq.set_noise(m)          # m goes out of scope here

print("cudaq", cudaq.__version__, flush=True)
if sys.argv[1] == "temporary":
    install()
    print("temporary model:", cudaq.sample(k, shots_count=1000), flush=True)
else:
    keep = cudaq.NoiseModel()
    keep.add_all_qubit_channel("x", cudaq.BitFlipChannel(1.0))
    cudaq.set_noise(keep)
    print("kept model:", cudaq.sample(k, shots_count=1000), flush=True)

Observed (identical on 0.16.0 and 0.17.0, warnings trimmed):

$ python -X faulthandler repro_cpu.py kept
kept model: { 0:1000 }
exit code: 0

$ python -X faulthandler repro_cpu.py temporary
DeprecationWarning: set_noise is deprecated; please use launch arguments or launch options.
Fatal Python error: Segmentation fault
exit code: 139

With bit-flip probability 1.0 on x, the correct result is all 0, as the kept model shows.
No gc.collect() is needed to trigger it.

Expected. set_noise keeps the model alive until unset_noise (or copies it), or raises an
error. set_noise is deprecated, but while it exists, the silent no-noise variant of this failure
can invalidate a noisy-simulation study without any visible error.

Workaround. Keep the model in a long-lived variable, or pass it as noise_model= to
cudaq.sample.

Dominant language
C++
Stars
1.2k
Forks
508
Avg merge
1d 19h
Merged PRs (30d)
100

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

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.

More from NVIDIA/cuda-quantum

All issues in NVIDIA/cuda-quantum

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.