Repository: pytorch
Issue: Lobpcg for complex gives wrong results.
Labels: triaged,module: complex,module: linear algebra,module: correctness (silent)

### 🐛 Describe the bug

As the following example, I create a complex hermitian matrix a and a vector x first, then call lobpcg in pytorch and that in scipy, at last, eigen exact solver is invoked. Obviously, scipy lobpcg give the expected result but pytorch lobpcg does not.

```test.py
import torch
import scipy

torch.manual_seed(42)
d = 5
a = torch.randn(d, d, dtype=torch.complex128)
a = a.H + a
x = torch.randn(d, 1, dtype=torch.complex128)
print(a, x)
print(torch.lobpcg(a, X=x))
print(scipy.sparse.linalg.lobpcg(a.numpy(), x.numpy()))
print(torch.linalg.eigh(a))
```

```output.log
tensor([[ 0.4236+0.0000j,  0.5033-0.3287j,  0.4483-0.1792j, -0.6542-1.8410j,
         -0.1899+0.8263j],
        [ 0.5033+0.3287j, -1.4446+0.0000j,  1.2191-1.1183j,  0.7611+0.6195j,
          0.1108-0.3507j],
        [ 0.4483+0.1792j,  1.2191+1.1183j,  0.1188+0.0000j, -1.1936+0.7518j,
          0.2993+1.7354j],
        [-0.6542+1.8410j,  0.7611-0.6195j, -1.1936-0.7518j,  0.6263+0.0000j,
          0.8081+0.1312j],
        [-0.1899-0.8263j,  0.1108+0.3507j,  0.2993-1.7354j,  0.8081-0.1312j,
          0.8176+0.0000j]], dtype=torch.complex128) tensor([[-1.1505-0.9865j],
        [-0.1688-0.3571j],
        [-1.7502-0.6587j],
        [-0.0944+0.2415j],
        [-0.0506-0.0643j]], dtype=torch.complex128)
(tensor([1002710.1510+0.j], dtype=torch.complex128), tensor([[ 110.2221+269.0265j],
        [-188.2007+179.2590j],
        [ 296.8929+139.1052j],
        [ 262.9413-88.3040j],
        [ -50.8417+275.2815j]], dtype=torch.complex128))
(array([3.55880664]), array([[-0.00702178+0.29407347j],
       [ 0.12385841-0.18879456j],
       [ 0.54458674+0.13835477j],
       [-0.31491075-0.4613058j ],
       [ 0.15042302-0.46062081j]]))
torch.return_types.linalg_eigh(
eigenvalues=tensor([-3.3956, -1.9695,  0.1831,  2.1651,  3.5588], dtype=torch.float64),
eigenvectors=tensor([[ 0.2714+0.0000j,  0.5647-0.0000j,  0.2598+0.0000j, -0.6733+0.0000j,
          0.2942+0.0000j],
        [-0.4423+0.4263j, -0.0485-0.5086j,  0.3862+0.3237j, -0.1537-0.1820j,
         -0.1917-0.1193j],
        [ 0.4172-0.2703j, -0.4410-0.0812j,  0.0061+0.3763j, -0.1446-0.2712j,
          0.1253-0.5477j],
        [ 0.2259-0.3479j,  0.0792-0.4511j, -0.1122-0.2507j, -0.0840-0.4730j,
         -0.4537+0.3258j],
        [ 0.1085+0.3436j,  0.0726+0.0624j, -0.6481+0.2036j, -0.3482+0.2085j,
         -0.4641-0.1394j]], dtype=torch.complex128))
```

### Versions

Collecting environment information...
PyTorch version: 2.5.0.dev20240911+cpu
Is debug build: False
CUDA used to build PyTorch: Could not collect
ROCM used to build PyTorch: N/A

OS: Arch Linux (x86_64)
GCC version: (GCC) 14.2.1 20240805
Clang version: 18.1.8
CMake version: version 3.30.3
Libc version: glibc-2.40

Python version: 3.12.5 (main, Aug  9 2024, 08:20:41) [GCC 14.2.1 20240805] (64-bit runtime)
Python platform: Linux-6.6.49-1-lts-x86_64-with-glibc2.40
Is CUDA available: False
CUDA runtime version: 12.6.68
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3060
Nvidia driver version: 560.35.03
cuDNN version: Probably one of the following:
/usr/lib/libcudnn.so.9.2.1
/usr/lib/libcudnn_adv.so.9.2.1
/usr/lib/libcudnn_cnn.so.9.2.1
/usr/lib/libcudnn_engines_precompiled.so.9.2.1
/usr/lib/libcudnn_engines_runtime_compiled.so.9.2.1
/usr/lib/libcudnn_graph.so.9.2.1
/usr/lib/libcudnn_heuristic.so.9.2.1
/usr/lib/libcudnn_ops.so.9.2.1
HIP runtime version: N/A
MIOpen runtime version: N/A                                                                                                                                              Is XNNPACK available: True                                                                                                                                                                                                                                                                                                                        CPU:
Architecture:                         x86_64                                                                                                                             CPU op-mode(s):                       32-bit, 64-bit
Address sizes:                        39 bits physical, 48 bits virtual
Byte Order:                           Little Endian
CPU(s):                               12
On-line CPU(s) list:                  0-11
Vendor ID:                            GenuineIntel
Model name:                           12th Gen Intel(R) Core(TM) i5-12400F
CPU family:                           6
Model:                                151
Thread(s) per core:                   2
Core(s) per socket:                   6
Socket(s):                            1
Stepping:                             5
CPU(s) scaling MHz:                   72%
CPU max MHz:                          4400.0000
CPU min MHz:                          800.0000
BogoMIPS:                             4993.00
Flags:                                fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect user_shstk avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi vnmi umip pku ospke waitpkg gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize arch_lbr ibt flush_l1d arch_capabilities
Virtualization:                       VT-x
L1d cache:                            288 KiB (6 instances)
L1i cache:                            192 KiB (6 instances)
L2 cache:                             7.5 MiB (6 instances)
L3 cache:                             18 MiB (1 instance)
NUMA node(s):                         1
NUMA node0 CPU(s):                    0-11
Vulnerability Gather data sampling:   Not affected
Vulnerability Itlb multihit:          Not affected
Vulnerability L1tf:                   Not affected
Vulnerability Mds:                    Not affected
Vulnerability Meltdown:               Not affected
Vulnerability Mmio stale data:        Not affected
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed:               Not affected
Vulnerability Spec rstack overflow:   Not affected                                                                                                                       Vulnerability Spec store bypass:      Mitigation; Speculative Store Bypass disabled via prctl                                                                            Vulnerability Spectre v1:             Mitigation; usercopy/swapgs barriers and __user pointer sanitization                                                               Vulnerability Spectre v2:             Mitigation; Enhanced / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI BHI_DIS_S                       Vulnerability Srbds:                  Not affected                                                                                                                       Vulnerability Tsx async abort:        Not affected

Versions of relevant libraries:
[pip3] mypy==1.11.2
[pip3] mypy_extensions==1.0.0
[pip3] numpy==1.26.4
[pip3] torch==2.5.0.dev20240911+cpu
[pip3] torch_geometric==2.5.3
[pip3] torchaudio==2.5.0.dev20240911+cpu
[pip3] torchvision==0.20.0.dev20240911+cpu
[conda] Could not collect

cc @ezyang @anjali411 @dylanbespalko @mruberry @Lezcano @nikitaved @amjames @jianyuh @pearu @walterddr @xwang233