7 from squander
import CNOT, U3
10 if hasattr(os,
"sched_setaffinity"):
11 os.sched_setaffinity(0, {0})
15 """Return True if the CPU supports AVX2 (needed for efficient float32 SIMD).""" 17 with open(
"/proc/cpuinfo")
as f:
18 return any(
"avx2" in line
for line
in f)
28 MIN_FLOAT32_SPEEDUP = 2.0
29 MIN_CNOT_SPEEDUP = 0.25
33 return CNOT(QUBIT_NUM, 0, QUBIT_NUM - 1)
37 return U3(QUBIT_NUM, 0)
41 pnum = gate.get_Parameter_Num()
43 return np.asarray([], dtype=dtype)
44 return np.linspace(0.1, 0.1 * pnum, pnum, dtype=dtype)
48 rng = np.random.default_rng(20260527)
49 data64 = np.ascontiguousarray(
50 rng.standard_normal((1 << QUBIT_NUM, COLS))
51 + 1j * rng.standard_normal((1 << QUBIT_NUM, COLS)),
54 return data64, data64.astype(np.complex64)
57 def _apply(gate, matrix, params, is_f32):
58 if gate.get_Parameter_Num() == 0:
59 gate.apply_to(matrix, parallel=0, is_f32=is_f32)
61 gate.apply_to(matrix, parameters=params, parallel=0, is_f32=is_f32)
66 is_f32 = dtype == np.float32
67 matrix = matrix32
if is_f32
else matrix64
70 for _
in range(WARMUP):
71 _apply(gate, matrix, params, is_f32=is_f32)
73 start = time.process_time()
74 for _
in range(REPEATS):
75 _apply(gate, matrix, params, is_f32=is_f32)
76 return time.process_time() - start
79 @pytest.mark.parametrize(
80 "gate_factory,gate_name,min_speedup",
82 pytest.param(_make_u3,
"U3", MIN_FLOAT32_SPEEDUP, id=
"U3"),
83 pytest.param(_make_cnot,
"CNOT", MIN_CNOT_SPEEDUP, id=
"CNOT"),
87 """Float32 should be a real HPC path, not just accepted at the API boundary.""" 89 pytest.skip(
"CPU lacks AVX2 â float32 SIMD path cannot meet speedup threshold")
99 if t32_check >= t64_check:
101 f
"Machine in degraded state â float32 ({t32_check:.3f}s) not faster " 102 f
"than float64 ({t64_check:.3f}s)" 106 for _
in range(TRIALS):
109 timings.append(t64 / t32)
112 speedup = float(np.min(timings[1:]))
113 assert speedup >= min_speedup, (
114 f
"{gate_name} float32 speedup {speedup:.2f}x is below " 115 f
"{min_speedup:.1f}x; timings={timings}"