[Bug][QDP] Align Iris GPU training path with sibling pipelines (real dtype + leaf weights)#1412
Merged
Merged
Conversation
bfdf141 to
ae1863f
Compare
The GPU training path in iris_amplitude.py derived its dtype directly from the QDP-encoded state vector (`dtype = encoded_train.dtype`), which is complex64/complex128. That complex dtype was then inherited by the trainable weights (used as qml.Rot rotation angles), the bias, and the ±1 labels — all of which must be real-valued. Mirror the existing pattern in mnist_amplitude.py and svhn_iqp.py: map complex128 -> float64 and otherwise use float32 for weights, bias and labels, while keeping the encoded state vector complex for StatePrep. Fixes apache#1160
`weights = 0.01 * torch.randn(..., requires_grad=True)` produces a non-leaf tensor (the multiply has a grad_fn), so torch.optim.SGD rejects it with "can't optimize a non-leaf Tensor" and the GPU path crashes before training starts. Use the same idiom as mnist_amplitude.py and svhn_iqp.py: `(0.01 * torch.randn(...)).detach().requires_grad_(True)`, which yields a proper leaf parameter the optimizer accepts. Found while verifying the dtype fix in the previous commit: with both fixes the GPU training path runs end to end (default.qubit, complex64 state-vector input) and weights/bias/labels are real-valued.
ae1863f to
256e70d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issues
Closes #1160
Changes
Why
The Iris amplitude-encoding GPU training path (
_run_training_gpu) haddrifted from its sibling pipelines (
mnist_amplitude.py,svhn_iqp.py)in two ways, both of which break it:
Complex dtype leaks into trainable params/labels (issue [need triage] we cast from complex64 to real value everytime #1160).
dtype = encoded_train.dtypeiscomplex64/complex128from the QDPamplitude encoding. That complex dtype was inherited by the weights
(used as
qml.Rotangles), the bias, and the ±1 labels — all ofwhich must be real.
Non-leaf weight tensor.
weights = 0.01 * torch.randn(..., requires_grad=True)is a non-leaf tensor (the multiply has a
grad_fn), sotorch.optim.SGDrejects it withcan't optimize a non-leaf Tensor,crashing the path before training starts.
Both are already handled correctly in the sibling pipelines; Iris was
simply not aligned.
How
Mirror the siblings:
real_dtype = torch.float64 if encoded_train.dtype == torch.complex128 else torch.float32, used for weights, bias and labels (encoded state vector stays complex forStatePrep).(0.01 * torch.randn(...)).detach().requires_grad_(True).Split into two commits so each fix can be reviewed independently.
Verification
qumat_qdp/lightning.gpu/ CUDA were not available in my environment,so I exercised the real
_run_training_gpudirectly on CPU withdefault.qubitand a synthetic complex64 state-vector batch (stubbingthe unused
qumat_qdpimport). Before/after:complex64float32Note: this confirms the code path executes with correct real dtypes and
gradients flow; it does not validate model accuracy (synthetic input, not
real QDP-encoded Iris). End-to-end accuracy validation still needs the
qumat_qdpextension + a CUDA/lightning.gpustack.Checklist
No automated tests exist for these benchmark pipelines (the GPU path needs CUDA + lightning.gpu); verified manually as described above.