Skip to content

Add warning for nn.ModuleList usage with quantum gates - #320

Open
yurekami wants to merge 1 commit into
mit-han-lab:mainfrom
yurekami:fix/issue-178-modulelist-warning
Open

Add warning for nn.ModuleList usage with quantum gates#320
yurekami wants to merge 1 commit into
mit-han-lab:mainfrom
yurekami:fix/issue-178-modulelist-warning

Conversation

@yurekami

Copy link
Copy Markdown

Summary

Added a UserWarning when nn.ModuleList or nn.ModuleDict is used instead of tq.QuantumModuleList or tq.QuantumModuleDict in quantum modules.

Problem

When users use nn.ModuleList to contain quantum gates, tq2qiskit conversion fails because:

  • nn.ModuleList doesn't have static_on() / static_off() methods
  • nn.ModuleList doesn't have a .graph attribute for the flattening logic
  • The recursive traversal in build_flat_module_list() can't handle regular containers

Solution

Issue a warning during static_on() to help users understand the issue and guide them to use the correct container types.

# Problematic:
self.gates = nn.ModuleList([tq.RX(), tq.RY()])

# Recommended:
self.gates = tq.QuantumModuleList([tq.RX(), tq.RY()])

Test plan

  • Verify warning is issued when using nn.ModuleList
  • Verify no warning for tq.QuantumModuleList

Fixes #178

🤖 Generated with Claude Code

Added a UserWarning when nn.ModuleList or nn.ModuleDict is used
instead of tq.QuantumModuleList or tq.QuantumModuleDict. This
helps users understand why static mode and tq2qiskit conversion
may fail.

The warning is issued during static_on() when non-quantum
container types are detected with quantum gate children.

Fixes mit-han-lab#178

🤖 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.

tq2qiskit fails when the QuantumModule contains a nn.ModuleList() of gates

1 participant