Skip to content

Fix NoiseDevice buffer device placement - #321

Open
yurekami wants to merge 1 commit into
mit-han-lab:mainfrom
yurekami:fix/issue-210-device-buffer-loading
Open

Fix NoiseDevice buffer device placement#321
yurekami wants to merge 1 commit into
mit-han-lab:mainfrom
yurekami:fix/issue-210-device-buffer-loading

Conversation

@yurekami

Copy link
Copy Markdown

Summary

Added .to(self.device) when initializing the density tensor in NoiseDevice to match the behavior of QuantumDevice.

Problem

NoiseDevice buffers remained on CPU even when device='cuda' was specified:

# In NoiseDevice.__init__ (before fix):
_density = torch.zeros(2 ** (2 * self.n_wires), dtype=C_DTYPE)
_density[0] = 1 + 0j
_density = torch.reshape(_density, [2] * (2 * self.n_wires))  # NOT moved to device!

# In QuantumDevice.__init__ (correct behavior):
_state = torch.reshape(_state, [2] * self.n_wires).to(self.device)  # Moved to device

This caused:

  • model.load_state_dict() to fail with device mismatch errors
  • GPU-based noise simulations to fail unexpectedly
  • State dicts saved from GPU models couldn't be loaded properly

Solution

Added .to(self.device) to the density tensor initialization, consistent with QuantumDevice.

Test plan

  • Verify NoiseDevice buffers are on correct device after initialization
  • Test save/load with GPU models using NoiseDevice

Fixes #210

🤖 Generated with Claude Code

Added .to(self.device) when initializing the density tensor in
NoiseDevice to match the behavior of QuantumDevice. Previously,
NoiseDevice buffers would remain on CPU even when device='cuda'
was specified, causing device mismatch errors when loading state
dictionaries or using the device on GPU.

This fixes issues where:
- model.load_state_dict() fails with device mismatch
- GPU-based noise simulations fail unexpectedly
- State dicts saved from GPU models can't be loaded

Fixes mit-han-lab#210

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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

Successfully merging this pull request may close these issues.

Quantum classical hybrid model cannot load parameters from dictionary

1 participant