quantrs2-core 0.1.3

Core types and traits for the QuantRS2 quantum computing framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! Platform detection implementation

use super::capabilities::*;
use std::env;

/// Detect comprehensive platform capabilities
pub fn detect_platform_capabilities() -> PlatformCapabilities {
    // Try to use SciRS2's platform detection if available
    // TODO: Use SciRS2's platform detection when available

    // Fallback to our own detection
    PlatformCapabilities {
        cpu: detect_cpu_capabilities(),
        gpu: detect_gpu_capabilities(),
        memory: detect_memory_capabilities(),
        platform_type: detect_platform_type(),
        os: detect_operating_system(),
        architecture: detect_architecture(),
    }
}

/// Detect CPU capabilities
fn detect_cpu_capabilities() -> CpuCapabilities {
    let logical_cores = num_cpus::get();
    let physical_cores = num_cpus::get_physical();

    CpuCapabilities {
        physical_cores,
        logical_cores,
        simd: detect_simd_capabilities(),
        cache: detect_cache_info(),
        base_clock_mhz: detect_cpu_frequency(),
        vendor: detect_cpu_vendor(),
        model_name: detect_cpu_model(),
    }
}

/// Detect CPU frequency in MHz
fn detect_cpu_frequency() -> Option<f32> {
    use sysinfo::System;

    let mut sys = System::new();
    sys.refresh_cpu_all();

    // Get frequency from first CPU (all cores typically have same base frequency)
    sys.cpus().first().map(|cpu| cpu.frequency() as f32)
}

/// Detect SIMD capabilities
fn detect_simd_capabilities() -> SimdCapabilities {
    // Try to use SciRS2's SIMD detection if available
    // TODO: Use SciRS2's SIMD capability detection when available

    #[cfg(target_arch = "x86_64")]
    {
        SimdCapabilities {
            sse: is_x86_feature_detected!("sse"),
            sse2: is_x86_feature_detected!("sse2"),
            sse3: is_x86_feature_detected!("sse3"),
            ssse3: is_x86_feature_detected!("ssse3"),
            sse4_1: is_x86_feature_detected!("sse4.1"),
            sse4_2: is_x86_feature_detected!("sse4.2"),
            avx: is_x86_feature_detected!("avx"),
            avx2: is_x86_feature_detected!("avx2"),
            avx512: cfg!(target_feature = "avx512f"),
            fma: is_x86_feature_detected!("fma"),
            neon: false,
            sve: false,
        }
    }

    #[cfg(target_arch = "aarch64")]
    {
        SimdCapabilities {
            sse: false,
            sse2: false,
            sse3: false,
            ssse3: false,
            sse4_1: false,
            sse4_2: false,
            avx: false,
            avx2: false,
            avx512: false,
            fma: false,
            neon: cfg!(target_feature = "neon"),
            sve: cfg!(target_feature = "sve"),
        }
    }

    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
    {
        SimdCapabilities {
            sse: false,
            sse2: false,
            sse3: false,
            ssse3: false,
            sse4_1: false,
            sse4_2: false,
            avx: false,
            avx2: false,
            avx512: false,
            fma: false,
            neon: false,
            sve: false,
        }
    }
}

/// Detect cache information
const fn detect_cache_info() -> CacheInfo {
    // Basic implementation - can be enhanced with platform-specific detection
    CacheInfo {
        l1_data: Some(32 * 1024),        // 32KB default
        l1_instruction: Some(32 * 1024), // 32KB default
        l2: Some(256 * 1024),            // 256KB default
        l3: Some(8 * 1024 * 1024),       // 8MB default
        line_size: Some(64),             // 64 byte cache line default
    }
}

/// Detect CPU vendor
fn detect_cpu_vendor() -> String {
    use sysinfo::System;

    let mut sys = System::new();
    sys.refresh_cpu_all();

    // Extract vendor from CPU brand string
    if let Some(cpu) = sys.cpus().first() {
        let brand = cpu.brand();
        if brand.contains("Intel") {
            return "Intel".to_string();
        } else if brand.contains("AMD") {
            return "AMD".to_string();
        } else if brand.contains("Apple") {
            return "Apple".to_string();
        } else if brand.contains("ARM") {
            return "ARM".to_string();
        } else if brand.contains("Qualcomm") {
            return "Qualcomm".to_string();
        }
        // Return brand if no known vendor found
        brand.to_string()
    } else {
        "Unknown".to_string()
    }
}

/// Detect CPU model
fn detect_cpu_model() -> String {
    use sysinfo::System;

    let mut sys = System::new();
    sys.refresh_cpu_all();

    // Get CPU brand/model name
    sys.cpus()
        .first()
        .map(|cpu| cpu.brand().to_string())
        .unwrap_or_else(|| "Unknown".to_string())
}

/// Detect GPU capabilities
const fn detect_gpu_capabilities() -> GpuCapabilities {
    // Check for GPU availability
    let devices = Vec::new();

    // Try to detect WebGPU devices (cross-platform)
    // Note: This is a placeholder - actual implementation would use wgpu

    GpuCapabilities {
        available: false,
        devices,
        primary_device: None,
    }
}

/// Detect memory capabilities
fn detect_memory_capabilities() -> MemoryCapabilities {
    use sysinfo::System;

    let mut sys = System::new_all();
    sys.refresh_memory();

    MemoryCapabilities {
        total_memory: sys.total_memory() as usize,
        available_memory: sys.available_memory() as usize,
        bandwidth_gbps: detect_memory_bandwidth(),
        numa_nodes: detect_numa_nodes(),
        hugepage_support: detect_hugepage_support(),
    }
}

/// Detect memory bandwidth in GB/s
fn detect_memory_bandwidth() -> Option<f32> {
    #[cfg(target_os = "linux")]
    {
        // Try to read DMI information
        if let Ok(output) = std::process::Command::new("dmidecode")
            .args(["-t", "memory"])
            .output()
        {
            if output.status.success() {
                if let Ok(text) = String::from_utf8(output.stdout) {
                    // Look for "Speed:" lines in DMI output
                    for line in text.lines() {
                        if line.contains("Speed:") && line.contains("MT/s") {
                            // Extract speed value
                            if let Some(speed_str) = line.split_whitespace().nth(1) {
                                if let Ok(speed_mts) = speed_str.parse::<f32>() {
                                    // Estimate bandwidth: speed (MT/s) * bus width (8 bytes) / 1000
                                    // This is a rough estimate assuming DDR with 64-bit bus
                                    let bandwidth_gbps = (speed_mts * 8.0) / 1000.0;
                                    return Some(bandwidth_gbps);
                                }
                            }
                        }
                    }
                }
            }
        }

        // Fallback: estimate based on total memory
        // Modern DDR4: ~20-40 GB/s, DDR5: ~40-80 GB/s
        Some(25.0) // Conservative estimate
    }

    #[cfg(target_os = "macos")]
    {
        // macOS: Use sysctl to get memory info
        if let Ok(output) = std::process::Command::new("sysctl")
            .arg("hw.memsize")
            .output()
        {
            if output.status.success() {
                // Estimate based on Apple Silicon vs Intel
                // M1/M2/M3: ~100-400 GB/s unified memory
                // Intel: ~20-40 GB/s
                if std::process::Command::new("sysctl")
                    .arg("machdep.cpu.brand_string")
                    .output()
                    .ok()
                    .and_then(|o| String::from_utf8(o.stdout).ok())
                    .map(|s| s.contains("Apple"))
                    .unwrap_or(false)
                {
                    return Some(200.0); // Apple Silicon estimate
                }
                return Some(30.0); // Intel Mac estimate
            }
        }
        Some(30.0)
    }

    #[cfg(target_os = "windows")]
    {
        // Windows: Rough estimate based on typical RAM speeds
        // DDR4-3200: ~25 GB/s, DDR4-2666: ~21 GB/s
        Some(25.0)
    }

    #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
    {
        None
    }
}

/// Detect number of NUMA nodes
fn detect_numa_nodes() -> usize {
    #[cfg(target_os = "linux")]
    {
        // Check /sys/devices/system/node/ for node directories
        if let Ok(entries) = std::fs::read_dir("/sys/devices/system/node") {
            let node_count = entries
                .filter_map(|e| e.ok())
                .filter(|e| {
                    e.file_name().to_string_lossy().starts_with("node") && e.file_name() != "node"
                })
                .count();

            if node_count > 0 {
                return node_count;
            }
        }

        // Fallback: try numactl
        if let Ok(output) = std::process::Command::new("numactl")
            .arg("--hardware")
            .output()
        {
            if output.status.success() {
                if let Ok(text) = String::from_utf8(output.stdout) {
                    // Look for "available: N nodes"
                    for line in text.lines() {
                        if line.contains("available:") && line.contains("nodes") {
                            if let Some(word) = line.split_whitespace().nth(1) {
                                if let Ok(n) = word.parse::<usize>() {
                                    return n;
                                }
                            }
                        }
                    }
                }
            }
        }

        1 // Default to 1 NUMA node
    }

    #[cfg(target_os = "macos")]
    {
        // macOS typically doesn't expose NUMA topology on consumer hardware
        // Server-grade Mac Pros might have NUMA, but it's not common
        1
    }

    #[cfg(target_os = "windows")]
    {
        // Windows: Could use GetNumaHighestNodeNumber, but requires unsafe FFI
        // For now, assume single NUMA node unless on server hardware
        // Most desktop/laptop systems have 1 NUMA node
        1
    }

    #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
    {
        1
    }
}

/// Detect hugepage support
fn detect_hugepage_support() -> bool {
    #[cfg(target_os = "linux")]
    {
        std::path::Path::new("/sys/kernel/mm/hugepages").exists()
    }
    #[cfg(not(target_os = "linux"))]
    {
        false
    }
}

/// Detect platform type
fn detect_platform_type() -> PlatformType {
    // Check for cloud/container environments
    if env::var("KUBERNETES_SERVICE_HOST").is_ok()
        || env::var("ECS_CONTAINER_METADATA_URI").is_ok()
        || env::var("AWS_EXECUTION_ENV").is_ok()
        || env::var("GOOGLE_CLOUD_PROJECT").is_ok()
        || env::var("AZURE_FUNCTIONS_ENVIRONMENT").is_ok()
    {
        return PlatformType::Cloud;
    }

    // Check for mobile platforms
    if cfg!(target_os = "android") || cfg!(target_os = "ios") {
        return PlatformType::Mobile;
    }

    // Detect server vs desktop based on hardware characteristics
    let logical_cores = num_cpus::get();
    let physical_cores = num_cpus::get_physical();

    use sysinfo::System;
    let mut sys = System::new_all();
    sys.refresh_memory();
    let total_memory_gb = sys.total_memory() / (1024 * 1024 * 1024);

    // Server heuristics:
    // - High core count (>16 logical cores)
    // - Large memory (>64 GB)
    // - NUMA nodes > 1
    // - Specific CPU model indicators
    let is_server = logical_cores > 16
        || total_memory_gb > 64
        || detect_numa_nodes() > 1
        || detect_cpu_model().contains("Xeon")
        || detect_cpu_model().contains("EPYC")
        || detect_cpu_model().contains("Threadripper");

    if is_server {
        PlatformType::Server
    } else if cfg!(any(target_arch = "arm", target_arch = "aarch64")) && !cfg!(target_os = "macos")
    {
        // ARM but not macOS might be embedded
        PlatformType::Embedded
    } else {
        PlatformType::Desktop
    }
}

/// Detect operating system
const fn detect_operating_system() -> OperatingSystem {
    #[cfg(target_os = "linux")]
    {
        OperatingSystem::Linux
    }
    #[cfg(target_os = "windows")]
    {
        OperatingSystem::Windows
    }
    #[cfg(target_os = "macos")]
    {
        OperatingSystem::MacOS
    }
    #[cfg(target_os = "freebsd")]
    {
        OperatingSystem::FreeBSD
    }
    #[cfg(target_os = "android")]
    {
        OperatingSystem::Android
    }
    #[cfg(not(any(
        target_os = "linux",
        target_os = "windows",
        target_os = "macos",
        target_os = "freebsd",
        target_os = "android"
    )))]
    {
        OperatingSystem::Unknown
    }
}

/// Detect architecture
const fn detect_architecture() -> Architecture {
    #[cfg(target_arch = "x86_64")]
    {
        Architecture::X86_64
    }
    #[cfg(target_arch = "aarch64")]
    {
        Architecture::Aarch64
    }
    #[cfg(target_arch = "riscv64")]
    {
        Architecture::Riscv64
    }
    #[cfg(target_arch = "wasm32")]
    {
        Architecture::Wasm32
    }
    #[cfg(not(any(
        target_arch = "x86_64",
        target_arch = "aarch64",
        target_arch = "riscv64",
        target_arch = "wasm32"
    )))]
    {
        Architecture::Unknown
    }
}