Torch integrators fail with "maximum not implemented for complex tensors" when the RHS is zero at t0

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

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

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

Research direction

Start in cuda_torchdiffeq_integrator.py, where the adaptive integrator sets up the solver and calls torchdiffeq's _select_initial_step, which fails for complex y0 when f0 is zero. Run the minimal repro from the issue and the TestBug3326 case in test_evolve_dynamics_torch_integrators.py to reproduce the "maximum not implemented for complex tensors" error. Done means the repro completes with the expected occupation and the tests in tests/dynamics/integrators pass.

Written by the indexing model from the issue text.

Description

Exalate stale-notified
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
Summary

CUDATorchDiffEqIntegrator with an adaptive solver (e.g. dopri5) raises RuntimeError: maximum not implemented for complex tensors when the RHS is exactly zero at the initial time. This fails test_evolve_dynamics_torch_integrators.py::test_all_models_default_solver[TestBug3326] in the Python metapackage validation, conda environment jobs (CUDA 12.6 and 13.0.2) on every Validation run since #5562 landed.

Cause

If the initial state commutes with the Hamiltonian, f0 = 0 exactly. torchdiffeq's _select_initial_step then takes the d1 <= 1e-15 and d2 <= 1e-15 branch and calls torch.max(torch.tensor(1e-6, dtype=y0.dtype), ...). With a complex y0 that dtype is complex, and torch.max does not support it. This is a latent torchdiffeq bug (unchanged upstream in 0.2.5).

TestBug3326.case2 uses a diagonal Hamiltonian and a Fock-state density matrix, so it hits this branch. It likely started failing after the operator fusion change (#5562), which probably makes the RHS exactly zero instead of ~1e-16 roundoff. I did not confirm that link.

Minimal repro (no fusion needed)
import cupy as cp, numpy as np, cudaq
from cudaq import Schedule, boson
from cudaq.dynamics.integrators import CUDATorchDiffEqDopri5Integrator

cudaq.set_target("dynamics")
d = 3
rho0 = cudaq.State.from_data(cp.eye(d, dtype=cp.complex128) / d)  # commutes with H
cudaq.evolve(5 * boson.number(0), {0: d}, Schedule(np.linspace(0, 1, 11), ["t"]), rho0,
             observables=[boson.number(0)], collapse_operators=[],
             integrator=CUDATorchDiffEqDopri5Integrator())

Fails on the released cuda-quantum-cu13 0.16 wheel with the same traceback as CI.

Proposed fix

In cuda_torchdiffeq_integrator.py, add a copy of _select_initial_step that uses the real time dtype for step sizes, and set solver.first_step from it on the first integrate call. _before_integrate skips the broken selection when first_step is set. In the non-degenerate case the result is identical to upstream. With the patch the repro completes (mean occupation 1.0) and all 29 tests in tests/dynamics/integrators pass.

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.