microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/pip/benchmarks/bench_qre.py
105lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # Licensed under the MIT License. |
| 3 | |
| 4 | import timeit |
| 5 | from dataclasses import dataclass, KW_ONLY, field |
| 6 | from qsharp.qre import linear_function, generic_function |
| 7 | from qsharp.qre._architecture import _make_instruction |
| 8 | from qsharp.qre.models import ( |
| 9 | GateBased, |
| 10 | SurfaceCode, |
| 11 | TwoDimensionalYokedSurfaceCode, |
| 12 | Litinski19Factory, |
| 13 | ) |
| 14 | from qsharp.qre._enumeration import _enumerate_instances |
| 15 | |
| 16 | |
| 17 | def bench_enumerate_instances(): |
| 18 | # Measure performance of enumerating instances with a large domain |
| 19 | @dataclass |
| 20 | class LargeDomain: |
| 21 | _: KW_ONLY |
| 22 | param1: int = field(default=0, metadata={"domain": range(1000)}) |
| 23 | param2: bool |
| 24 | |
| 25 | number = 100 |
| 26 | |
| 27 | duration = timeit.timeit( |
| 28 | "list(_enumerate_instances(LargeDomain))", |
| 29 | globals={ |
| 30 | "_enumerate_instances": _enumerate_instances, |
| 31 | "LargeDomain": LargeDomain, |
| 32 | }, |
| 33 | number=number, |
| 34 | ) |
| 35 | |
| 36 | print(f"Enumerating instances took {duration / number:.6f} seconds on average.") |
| 37 | |
| 38 | |
| 39 | def bench_enumerate_isas(): |
| 40 | ctx = GateBased(gate_time=50, measurement_time=100).context() |
| 41 | |
| 42 | # Hierarchical factory using from_components |
| 43 | query = ( |
| 44 | SurfaceCode.q() |
| 45 | * TwoDimensionalYokedSurfaceCode.q(source=SurfaceCode.q()) |
| 46 | * Litinski19Factory.q() |
| 47 | ) |
| 48 | |
| 49 | number = 100 |
| 50 | duration = timeit.timeit( |
| 51 | "list(query.enumerate(ctx))", |
| 52 | globals={ |
| 53 | "query": query, |
| 54 | "ctx": ctx, |
| 55 | }, |
| 56 | number=number, |
| 57 | ) |
| 58 | |
| 59 | print(f"Enumerating ISAs took {duration / number:.6f} seconds on average.") |
| 60 | |
| 61 | |
| 62 | def bench_function_evaluation_linear(): |
| 63 | fl = linear_function(12) |
| 64 | |
| 65 | inst = _make_instruction(42, 0, None, 1, fl, None, 1.0, {}) |
| 66 | number = 1000 |
| 67 | duration = timeit.timeit( |
| 68 | "inst.space(5)", |
| 69 | globals={ |
| 70 | "inst": inst, |
| 71 | }, |
| 72 | number=number, |
| 73 | ) |
| 74 | |
| 75 | print( |
| 76 | f"Evaluating linear function took {duration / number:.6f} seconds on average." |
| 77 | ) |
| 78 | |
| 79 | |
| 80 | def bench_function_evaluation_generic(): |
| 81 | def func(arity: int) -> int: |
| 82 | return 12 * arity |
| 83 | |
| 84 | fg = generic_function(func) |
| 85 | |
| 86 | inst = _make_instruction(42, 0, None, 1, fg, None, 1.0, {}) |
| 87 | number = 1000 |
| 88 | duration = timeit.timeit( |
| 89 | "inst.space(5)", |
| 90 | globals={ |
| 91 | "inst": inst, |
| 92 | }, |
| 93 | number=number, |
| 94 | ) |
| 95 | |
| 96 | print( |
| 97 | f"Evaluating linear function took {duration / number:.6f} seconds on average." |
| 98 | ) |
| 99 | |
| 100 | |
| 101 | if __name__ == "__main__": |
| 102 | bench_enumerate_instances() |
| 103 | bench_enumerate_isas() |
| 104 | bench_function_evaluation_linear() |
| 105 | bench_function_evaluation_generic() |
| 106 | |