qdk icon indicating copy to clipboard operation
qdk copied to clipboard

[Bug] Neutral-atom simulation modifies the caller's NoiseConfig

Open amcasey opened this issue 4 weeks ago • 2 comments

  • Symptom: Wrong result (silent: no error is reported)
  • Release 5a7b2ee32: Non-regression
  • Where: Base · NeutralAtomDevice.simulate

Summary. NeutralAtomDevice.simulate changes the caller's NoiseConfig in place (copying rz noise into z, s, t tables), so later runs still apply noise the user removed.

Python repro

# NeutralAtomBackend (via NeutralAtomDevice.simulate) mutates the caller's NoiseConfig: when rz noise is set,
# t/t_adj/s/s_adj/z tables are overwritten in place and the copied values persist after rz noise is cleared.
import math
import sys
from qiskit import QuantumCircuit
from qdk.qiskit import NeutralAtomBackend
from qdk.simulation import NoiseConfig

# SX . Rz(pi) . SX |0> == |0>; with no noise the result is always '0'
qc = QuantumCircuit(1, 1)
qc.sx(0); qc.rz(math.pi, 0); qc.sx(0); qc.measure(0, 0)

def run(noise):
    return NeutralAtomBackend().run(qc, shots=20, noise=noise, seed=5, simulator_type="stabilizer",
                                    skip_transpilation=True).result().get_counts()

noise = NoiseConfig()
first = run(noise)
noise.rz.z = 1.0              # phase flip after every Rz
run(noise)
z_after = noise.z.z           # the caller never touched noise.z
noise.rz.z = 0.0              # turn rz noise off again -> config is noiseless
third = run(noise)
print("fresh NoiseConfig          :", first, "(expected {'0': 20})")
print("after rz.z=1.0, noise.z.z  :", z_after, "(expected 0.0)")
print("rz.z reset to 0.0 (noiseless):", third, "(expected {'0': 20})")
if first != {"0": 20}:
    print("VERDICT: INCONCLUSIVE - noiseless control did not give {'0': 20}")
    sys.exit(2)
if z_after != 0.0 or third != {"0": 20}:
    print(f"VERDICT: REPRODUCED - caller's noise.z.z became {z_after}; noiseless rerun gives {third}")
    sys.exit(1)
print("VERDICT: NOT REPRODUCED - NoiseConfig is not modified")
sys.exit(0)
Build Result Evidence
release 5a7b2ee32 bug present caller's noise.z.z became 1.0; noiseless rerun gives {'1': 20}
main 42c843a20 bug present caller's noise.z.z became 1.0; noiseless rerun gives {'1': 20}

Observed: after rz.z=1.0 run: caller's noise.z.z = 1.0; after rz.z reset to 0.0 the run returns {'1': 20}

Expected: noise.z.z stays 0.0; third run {'0': 20}

Probable cause. _device/_atom/init.py:274-300 copies rz noise into the caller's t, t_adj, s, s_adj, z tables without copying the config first.

Originally reported as A18

amcasey avatar Nov 05 '26 00:11 amcasey

Hi! I'd be interested in investigating and fixing this issue.

I traced the mutation to NeutralAtomDevice.simulate(), where the supplied NoiseConfig is modified when RZ noise is propagated to otherwise noiseless gates.

My proposed approach is to ensure this normalization happens on an internal configuration rather than mutating the caller's object, and add regression tests verifying that the original configuration remains unchanged and subsequent simulations behave correctly.

Could a maintainer confirm that this issue is valid and available for contribution?

anshikagrover15 avatar Nov 05 '26 13:11 anshikagrover15

@anshikagrover15 We could copy the NoiseConfig and mutate a copy as you suggest. The fix only needs to be careful to do a shallow copy of NoiseConfig.intrinsics, because that table can be very large. So, I would also right a test verifying they are the same object in memory (i.e. assert noise.intrinsics is mutated_noise.intrinsics) Other than that, sounds good to me.

orpuente-MS avatar Nov 06 '26 18:11 orpuente-MS