microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.25.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/pip/tests/test_gpu_simulator.py

460lines · modecode

1# Copyright (c) Microsoft Corporation.
2# Licensed under the MIT License.
3
4from collections import Counter
5from pathlib import Path
6from typing import Sequence, cast
7import math
8import os
9import random
10
11import pytest
12import sys
13
14from qsharp._native import Result
15
16# Skip all tests in this module if QDK_GPU_TESTS is not set
17if not os.environ.get("QDK_GPU_TESTS"):
18 pytest.skip("Skipping GPU tests (QDK_GPU_TESTS not set)", allow_module_level=True)
19
20SKIP_REASON = "GPU is not available"
21
22gpu_info = "Unknown"
23
24try:
25 from qsharp._native import try_create_gpu_adapter
26
27 gpu_info = try_create_gpu_adapter()
28 # Printing to stderr so that it is visible if CI run fails
29 print(f"*** USING GPU: {gpu_info}", file=sys.stderr)
30
31 GPU_AVAILABLE = True
32except OSError as e:
33 GPU_AVAILABLE = False
34 SKIP_REASON = str(e)
35
36
37import qsharp
38from qsharp import TargetProfile
39from qsharp import openqasm
40
41from qsharp._simulation import run_qir_gpu, NoiseConfig
42
43current_file_path = Path(__file__)
44# Get the directory of the current file
45current_dir = current_file_path.parent
46
47
48def read_file(file_name: str) -> str:
49 return Path(file_name).read_text(encoding="utf-8")
50
51
52def read_file_relative(file_name: str) -> str:
53 return Path(current_dir / file_name).read_text(encoding="utf-8")
54
55
56def result_array_to_string(results: Sequence[Result]) -> str:
57 chars = []
58 for value in results:
59 if value == Result.Zero:
60 chars.append("0")
61 elif value == Result.One:
62 chars.append("1")
63 else:
64 chars.append("-")
65 return "".join(chars)
66
67
68@pytest.mark.skipif(not GPU_AVAILABLE, reason=SKIP_REASON)
69def test_gpu_seeding_no_noise():
70 qsharp.init(target_profile=TargetProfile.Base)
71 qsharp.eval(
72 """
73 operation BellTest() : Result[] {
74 use qs = Qubit[2];
75 H(qs[0]);
76 CNOT(qs[0], qs[1]);
77 MResetEachZ(qs)
78 }
79 """
80 )
81
82 qir = str(qsharp.compile("BellTest()"))
83
84 results = [run_qir_gpu(qir, 1, None, seed)[0] for seed in range(12)]
85 print(results)
86
87 # Results will be an array of 12 lists [Result, Result]
88 # Each result should be [Zero, Zero] or [One, One]
89 # As evident from a manual experiment running with the seeds of 0..11
90 # gives 6 results of each. Experiment should be repeatable for fixed seeds.
91
92 # Verify we have 6 of each result
93 count_00 = sum(1 for r in results if r == [Result.Zero, Result.Zero])
94 count_11 = sum(1 for r in results if r == [Result.One, Result.One])
95 assert count_00 == 6
96 assert count_11 == 6
97
98
99@pytest.mark.skipif(not GPU_AVAILABLE, reason=SKIP_REASON)
100def test_gpu_no_noise():
101 """Simple test that GPU simulator works without noise."""
102 qsharp.init(target_profile=TargetProfile.Base)
103 qsharp.eval(read_file_relative("CliffordIsing.qs"))
104
105 input = qsharp.compile(
106 "IsingModel2DEvolution(5, 5, PI() / 2.0, PI() / 2.0, 10.0, 10)"
107 )
108
109 output = run_qir_gpu(str(input))
110 print(output)
111 # Expecting deterministic output, no randomization seed needed.
112 assert output == [[Result.Zero] * 25], "Expected result of 0s with pi/2 angles."
113
114
115@pytest.mark.skipif(not GPU_AVAILABLE, reason=SKIP_REASON)
116def test_gpu_bitflip_noise():
117 """Bitflip noise for GPU simulator."""
118 qsharp.init(target_profile=TargetProfile.Base)
119 qsharp.eval(read_file_relative("CliffordIsing.qs"))
120
121 input = qsharp.compile(
122 "IsingModel2DEvolution(5, 5, PI() / 2.0, PI() / 2.0, 10.0, 10)"
123 )
124
125 p_noise = 0.005
126 noise = NoiseConfig()
127 noise.rx.set_bitflip(p_noise)
128 noise.rzz.set_pauli_noise("XX", p_noise)
129 noise.mresetz.set_bitflip(p_noise)
130
131 output = run_qir_gpu(str(input), shots=3, noise=noise, seed=17)
132 result = [result_array_to_string(cast(Sequence[Result], x)) for x in output]
133 print(result)
134 # Reasonable results obtained from manual run
135 assert result == [
136 "0000000000011100000000110",
137 "0001001100000000000100110",
138 "0000000000011000000000000",
139 ]
140
141
142@pytest.mark.skipif(not GPU_AVAILABLE, reason=SKIP_REASON)
143def test_gpu_mixed_noise():
144 qsharp.init(target_profile=TargetProfile.Base)
145 qsharp.eval(read_file_relative("CliffordIsing.qs"))
146
147 input = qsharp.compile(
148 "IsingModel2DEvolution(5, 5, PI() / 2.0, PI() / 2.0, 4.0, 4)"
149 )
150
151 noise = NoiseConfig()
152 noise.rz.set_bitflip(0.005)
153 noise.rz.loss = 0.003
154 noise.rzz.set_depolarizing(0.005)
155 noise.rzz.loss = 0.003
156
157 output = run_qir_gpu(str(input), shots=3, noise=noise, seed=53)
158 result = [result_array_to_string(cast(Sequence[Result], x)) for x in output]
159 print(result)
160 # Reasonable results obtained from manual run
161 assert result == [
162 "00000-00000000-0000000000",
163 "00100001000-0000000000-00",
164 "000000010000000-000000000",
165 ]
166
167@pytest.mark.skipif(not GPU_AVAILABLE, reason=SKIP_REASON)
168def test_gpu_isolated_loss():
169 qsharp.init(target_profile=TargetProfile.Base)
170 program = """
171import Std.Math.PI;
172operation Main() : Result[] {
173 use qs = Qubit[3];
174 X(qs[0]);
175 X(qs[1]);
176 CNOT(qs[0], qs[1]);
177 // When loss is configured for X gate, qubit 2 should be unaffected.
178 Rx(PI() / 2.0, qs[2]);
179 Rx(PI() / 2.0, qs[2]);
180 MeasureEachZ(qs)
181}
182 """
183 qsharp.eval(program)
184
185 input = qsharp.compile(
186 "Main()"
187 )
188
189 noise = NoiseConfig()
190 noise.x.loss = 0.1
191
192 output = run_qir_gpu(str(input), shots=1000, noise=noise)
193 result = [result_array_to_string(cast(Sequence[Result], x)) for x in output]
194 histogram = Counter(result)
195 total = sum(histogram.values())
196 allowed_percent = {
197 "101": 0.81,
198 "1-1": 0.09,
199 "-11": 0.09,
200 "--1": 0.01,
201 }
202 tolerance = 0.2 * total
203 for bitstring, actual_count in histogram.items():
204 assert bitstring in allowed_percent, f"Unexpected measurement string: '{bitstring}'."
205 expected_count = allowed_percent[bitstring] * total
206 assert abs(actual_count - expected_count) <= tolerance, (
207 f"Count for {bitstring} outside 20% tolerance. "
208 f"Actual={actual_count}, Expected≈{expected_count:.0f}, Shots={total}."
209 )
210 # We don't check for missing strings, as low-probability strings may not appear in finite shots.
211
212
213@pytest.mark.skipif(not GPU_AVAILABLE, reason=SKIP_REASON)
214def test_gpu_isolated_loss_and_noise():
215 qsharp.init(target_profile=TargetProfile.Base)
216 program = """
217import Std.Math.PI;
218operation Main() : Result[] {
219 use qs = Qubit[5];
220 for _ in 1..100 {
221 X(qs[0]);
222 X(qs[1]);
223 CNOT(qs[0], qs[1]);
224 }
225 Rx(PI() / 2.0, qs[4]);
226 Rx(PI() / 2.0, qs[4]);
227 MeasureEachZ(qs)
228}
229 """
230 qsharp.eval(program)
231
232 input = qsharp.compile(
233 "Main()"
234 )
235
236 noise = NoiseConfig()
237 noise.x.set_bitflip(0.001)
238 noise.x.loss = 0.001
239
240 output = run_qir_gpu(str(input), shots=1000, noise=noise)
241 result = [result_array_to_string(cast(Sequence[Result], x)) for x in output]
242 histogram = Counter(result)
243 total = sum(histogram.values())
244 assert total > 0, "No measurement results recorded."
245 for bitstring in histogram:
246 assert bitstring.endswith("001"), f"Unexpected suffix in '{bitstring}'."
247 probability_00001 = histogram.get("00001", 0) / total
248 assert 0.5 < probability_00001 < 0.8, (
249 f"Probability of 00001 outside expected range. "
250 f"Actual={probability_00001:.2%}, Shots={total}."
251 )
252
253
254def build_x_chain_qir(n_instances: int, n_x: int) -> str:
255 # Construct multiple instances of x gate chains
256 prefix = f"""
257 OPENQASM 3.0;
258 include "stdgates.inc";
259 bit[{n_instances}] c;
260 qubit[{n_instances}] q;
261 """
262
263 infix = """
264 x q;
265 """
266
267 suffix = """
268 c = measure q;
269 """
270
271 src_parallel = prefix + infix * n_x + suffix
272
273 # Compile resulting program
274 qsharp.init(target_profile=TargetProfile.Base)
275 qir_parallel = openqasm.compile(src_parallel)
276 return str(qir_parallel)
277
278
279@pytest.mark.skipif(not GPU_AVAILABLE, reason=SKIP_REASON)
280@pytest.mark.parametrize(
281 "p_noise, n_x, n_instances, n_shots, max_percent",
282 [
283 (0.0005, 500, 10, 8192, 1.0),
284 (0.005, 500, 10, 4096, 2.0),
285 (0.0005, 20, 20, 100, 4.0), # 100 shots is low, so higher error tolerated
286 ],
287)
288def test_gpu_x_chain(
289 p_noise: float, n_x: int, n_instances: int, n_shots: int, max_percent: float
290):
291 """
292 Simulate multi-instance X-chain with bitflip noise many times
293 Compare result frequencies with analytically computed probabilities
294 """
295 # Use the GPU simulator with noise
296 noise = NoiseConfig()
297 noise.x.set_bitflip(p_noise)
298
299 qir = build_x_chain_qir(n_instances, n_x)
300 output = run_qir_gpu(qir, shots=n_shots, noise=noise, seed=18)
301 histogram = [0 for _ in range(n_instances + 1)]
302 for shot in output:
303 shot_results = cast(Sequence[Result], shot)
304 count_1 = shot_results.count(Result.One)
305 histogram[count_1] += 1
306
307 # Probability of obtaining 0 and 1 at the end of the X chain.
308 p_0 = ((2.0 * p_noise - 1.0) ** n_x + 1.0) / 2.0
309 p_1 = 1.0 - p_0
310
311 # Number of results with k ones that should be there.
312 p_N = [
313 p_0 ** ((n_instances - k)) * (p_1**k) * math.comb(n_instances, k) * n_shots
314 for k in range(n_instances + 1)
315 ]
316
317 # Error % for deviation from analytical value
318 error_percent = [abs(a - b) * 100.0 / n_shots for (a, b) in zip(histogram, p_N)]
319 print(", ".join(f"{a} (Δ≈{b:.1f}%)" for (a, b) in zip(histogram, error_percent)))
320
321 # We tolerate configured percentage error.
322 assert all(
323 err < max_percent for err in error_percent
324 ), f"Error percent too high: {error_percent}"
325
326
327def generate_op_sequence(
328 n_qubits: int, n_ops: int, n_rand: int
329) -> list[tuple[int, int]]:
330 """Return operation tuples and randomly swap neighboring pairs n_rand times."""
331 if n_qubits < 0 or n_ops < 0 or n_rand < 0:
332 raise ValueError("Tuple bounds must be non-negative")
333
334 ops = [(q, op) for op in range(n_ops) for q in range(n_qubits)]
335
336 if len(ops) < 2 or n_rand == 0:
337 return ops
338
339 max_index = len(ops) - 1
340 for _ in range(n_rand):
341 idx = random.randrange(max_index)
342 left, right = ops[idx], ops[idx + 1]
343 if left[0] != right[0]:
344 ops[idx], ops[idx + 1] = right, left
345
346 return ops
347
348
349@pytest.mark.skipif(not GPU_AVAILABLE, reason=SKIP_REASON)
350@pytest.mark.parametrize("noisy_gate, noise_number", [(0, 2), (1, 1), (2, 2), (3, 2)])
351def test_gpu_permuted_rotations(noisy_gate : int, noise_number : int):
352 qsharp.init(target_profile=TargetProfile.Base)
353
354 n_shots = 2000
355 n_qubits = 15
356 seed = 2026
357 p_loss = 0.1
358 tolerance_percent = 2.0
359 assert n_qubits >= 2, "Need at least two qubits"
360
361 random.seed(seed)
362 i1, i2 = random.sample(range(n_qubits), 2)
363 prefix = f"""
364operation tiny_coeffs() : Result[] {{
365 use q = Qubit[{n_qubits}];
366 let i1 = {i1};
367 let i2 = {i2};
368"""
369
370 # The following sequence of rotations is equivalent to identity:
371 # 0. H <- could be any rotation
372 # 1. Rx(1.123456789)
373 # 2. Ry(1.212121212)
374 # 3. Rz(1.14856940153986)
375 # 4. Ry(-1.41836046203971)
376 # 5. Rz(-0.325946593598928)
377 # 6. H <- adjoint to step 0
378 # We will perform these rotations on every qubit, but randomly intermix sequences for different qubits.
379 # This should still result in identity on all qubits as gates on different qubits commute.
380 # noise_number = how many times noisy gate appears in sequence.
381
382 n_ops = 7
383 ops = generate_op_sequence(n_qubits, n_ops, n_qubits*n_ops*100)
384 infix = ""
385 for qubit, op in ops:
386 match op:
387 case 0 | 6:
388 infix += f" H(q[{qubit}]);\n"
389 case 1:
390 infix += f" Rx(1.123456789, q[{qubit}]);\n"
391 case 2:
392 infix += f" Ry(1.212121212, q[{qubit}]);\n"
393 case 3:
394 infix += f" Rz(1.14856940153986, q[{qubit}]);\n"
395 case 4:
396 infix += f" Ry(-1.41836046203971, q[{qubit}]);\n"
397 case 5:
398 infix += f" Rz(-0.325946593598928, q[{qubit}]);\n"
399
400 suffix = """
401 let m1 = M(q[i1]);
402 let m2 = M(q[i2]);
403 ResetAll(q);
404 return [m1, m2];
405}
406"""
407
408 program = prefix + infix + suffix
409 qsharp.eval(program)
410 input = qsharp.compile(
411 "tiny_coeffs()"
412 )
413
414 noise = NoiseConfig()
415 p_combined_loss = 1.0 - ((1.0-p_loss)**noise_number)
416 match noisy_gate:
417 case 0:
418 noise.h.loss = p_loss
419 case 1:
420 noise.rx.loss = p_loss
421 case 2:
422 noise.ry.loss = p_loss
423 case 3:
424 noise.rz.loss = p_loss
425 case _:
426 raise ValueError("Invalid noisy_gate value")
427
428 output = run_qir_gpu(str(input), shots=n_shots, noise=noise, seed=seed)
429 result_strings = [
430 result_array_to_string(cast(Sequence[Result], shot)) for shot in output
431 ]
432 assert len(result_strings) == n_shots, f"Shot count mismatch. Actual={len(result_strings)}, Expected={n_shots}"
433
434 p_minus = p_combined_loss
435 p_0 = 1.0 - p_minus
436 allowed = [
437 ("00", n_shots * p_0 * p_0),
438 ("0-", n_shots * p_0 * p_minus),
439 ("-0", n_shots * p_minus * p_0),
440 ("--", n_shots * p_minus * p_minus),
441 ]
442
443 counts = {pattern: 0 for pattern, _ in allowed}
444 for entry in result_strings:
445 assert entry in counts, f"Unexpected measurement string: '{entry}'. Program={program}."
446 counts[entry] += 1
447
448 tolerance = tolerance_percent / 100.0 * n_shots
449 print(f"Permuted rotations test: n_qubits={n_qubits}, n_shots={n_shots}, seed={seed}, noise#{noise_number}, Δ<={tolerance:.0f} i1={i1}, i2={i2}")
450 summary_msg = ", ".join(
451 f"'{pattern}': {counts[pattern]} (Δ={abs(counts[pattern] - expected_count):.0f})"
452 for pattern, expected_count in allowed
453 )
454 print(summary_msg)
455 for pattern, expected_count in allowed:
456 actual_count = counts[pattern]
457 assert (
458 abs(actual_count - expected_count) <= tolerance
459 ), f"Count for {pattern} off by more than {tolerance_percent:.1f}% of shots. Actual={actual_count}, Expected={expected_count:.0f}, noise#{noise_number}, Program={program}."
460
461