|
| 1 | +using QuantumSymbolics |
| 2 | +import QuantumOpticsBase |
| 3 | +import QuantumClifford |
| 4 | + |
| 5 | +using BenchmarkTools |
| 6 | + |
| 7 | +const SUITE = BenchmarkGroup() |
| 8 | +@ket k1; @ket k2; @bra b1; @bra b2; @op A; @op B; @op C; |
| 9 | + |
| 10 | +# Time to import and first usage of simple operations |
| 11 | +SUITE["latency"] = BenchmarkGroup(["slow"]) |
| 12 | +load_command = `julia --quiet --project=./ --eval="using QuantumSymbolics"` |
| 13 | +ttfx_command = ` |
| 14 | + julia --quiet --project=./ --eval=""" |
| 15 | + using QuantumSymbolics; |
| 16 | + @ket k1; @op A; |
| 17 | + A * commutator(A,X) * k1 |
| 18 | + """` |
| 19 | +SUITE["latency"]["using"] = @benchmarkable run(load_command) samples=3 seconds=15 |
| 20 | +SUITE["latency"]["ttf_operation"] = @benchmarkable run(ttfx_command) samples=3 seconds=15 |
| 21 | +SUITE["latency"]["ttf_simplify"] = @benchmarkable qsimplify(X*Y) samples=1 evals=1 |
| 22 | + |
| 23 | + |
| 24 | +# Symbolic object creation |
| 25 | +SUITE["creation"] = BenchmarkGroup(["symbolic"]) |
| 26 | +SUITE["creation"]["ket"] = @benchmarkable @ket _k |
| 27 | +SUITE["creation"]["op"] = @benchmarkable @op _A |
| 28 | +SUITE["creation"]["super_op"] = @benchmarkable @superop _S |
| 29 | + |
| 30 | +SUITE["creation"]["large_trees"] = BenchmarkGroup(["allocs"]) |
| 31 | +function large_tree_with_plenty_reallocations(layers) |
| 32 | + expr_op = QuantumSymbolics.I |
| 33 | + expr_ket = X1 |
| 34 | + for _ in 1:layers |
| 35 | + expr_op = rand([X, Y, Z, H]) + (rand([X, Y, Z, H]) * expr_op) |
| 36 | + expr_ket = (expr_op * expr_ket) + rand([X1, X2, Y1, Y2, Z1, Z2]) |
| 37 | + end |
| 38 | + return expr_op, expr_ket |
| 39 | +end |
| 40 | +SUITE["creation"]["large_trees"]["10_layers"] = @benchmarkable large_tree_with_plenty_reallocations(10) |
| 41 | +SUITE["creation"]["large_trees"]["50_layers"] = @benchmarkable large_tree_with_plenty_reallocations(50) |
| 42 | + |
| 43 | + |
| 44 | +# Basic operations |
| 45 | +SUITE["operations"] = BenchmarkGroup(["symbolic"]) |
| 46 | +SUITE["operations"]["scaling"] = BenchmarkGroup() |
| 47 | +SUITE["operations"]["scaling"]["ket"] = @benchmarkable 2 * $k1 |
| 48 | +SUITE["operations"]["scaling"]["op"] = @benchmarkable 3 * $A |
| 49 | + |
| 50 | +SUITE["operations"]["addition"] = BenchmarkGroup() |
| 51 | +SUITE["operations"]["addition"]["ket"] = @benchmarkable $k1 + $k2 |
| 52 | +SUITE["operations"]["addition"]["op"] = @benchmarkable $A + $B |
| 53 | + |
| 54 | +SUITE["operations"]["multiplication"] = BenchmarkGroup() |
| 55 | +SUITE["operations"]["multiplication"]["bra_ket"] = @benchmarkable $b1 * $k1 |
| 56 | +SUITE["operations"]["multiplication"]["op_ket"] = @benchmarkable $A * $k1 |
| 57 | +SUITE["operations"]["multiplication"]["bra_op"] = @benchmarkable $b1 * $A |
| 58 | +SUITE["operations"]["multiplication"]["inner"] = @benchmarkable $b1 * $k1 |
| 59 | +SUITE["operations"]["multiplication"]["outer"] = @benchmarkable $k1 * $b1 |
| 60 | +SUITE["operations"]["multiplication"]["op"] = @benchmarkable $A * $B |
| 61 | +SUITE["operations"]["multiplication"]["many"] = @benchmarkable $k1 * $b2 * $k2 * $b1 * $A * $B |
| 62 | + |
| 63 | +SUITE["operations"]["tensor"] = BenchmarkGroup() |
| 64 | +SUITE["operations"]["tensor"]["ket"] = @benchmarkable $k1 ⊗ $k2 |
| 65 | +SUITE["operations"]["tensor"]["op"] = @benchmarkable $A ⊗ $B |
| 66 | +SUITE["operations"]["tensor"]["many"] = @benchmarkable $A ⊗ $B ⊗ $C ⊗ $A ⊗ $B ⊗ $C |
| 67 | + |
| 68 | + |
| 69 | +# Linear algebra operations |
| 70 | +SUITE["linalg"] = BenchmarkGroup(["symbolic"]) |
| 71 | +SUITE["linalg"]["trace"] = @benchmarkable tr($A) |
| 72 | +SUITE["linalg"]["ptrace"] = @benchmarkable ptrace($A ⊗ $B, 1) |
| 73 | +SUITE["linalg"]["inverse"] = @benchmarkable inv($A) |
| 74 | +SUITE["linalg"]["dagger"] = @benchmarkable dagger($A) |
| 75 | +SUITE["linalg"]["conjugate"] = @benchmarkable conj($A) |
| 76 | +SUITE["linalg"]["transpose"] = @benchmarkable transpose($A) |
| 77 | +SUITE["linalg"]["commutator"] = @benchmarkable commutator($A, $B) |
| 78 | +SUITE["linalg"]["anticommutator"] = @benchmarkable anticommutator($A, $B) |
| 79 | + |
| 80 | + |
| 81 | +# Simplification benchmarks |
| 82 | +SUITE["manipulation"] = BenchmarkGroup(["symbolic"]) |
| 83 | +compact_pauli_expr = (H * (X+Y+Z)) * (H * (Y1+Y2+Z1)) |
| 84 | +expanded_pauli_expr = qexpand(compact_pauli_expr) |
| 85 | +commutator_expr = commutator(X, commutator(Y, commutator(X, Z))) |
| 86 | + |
| 87 | +SUITE["manipulation"]["expand"]["distribution"] = @benchmarkable qexpand($compact_pauli_expr) |
| 88 | +SUITE["manipulation"]["expand"]["commutator"] = @benchmarkable qexpand($commutator_expr) |
| 89 | +SUITE["manipulation"]["simplify"]["applicable_rules"] = @benchmarkable qsimplify($expanded_pauli_expr, rewriter=qsimplify_pauli) |
| 90 | +# Expression isn't simplified with fock rules. Testing how fast we go through inapplicable rules. |
| 91 | +SUITE["manipulation"]["simplify"]["irrelevant_rules"] = @benchmarkable qsimplify($expanded_pauli_expr, rewriter=qsimplify_fock) |
| 92 | +SUITE["manipulation"]["simplify"]["commutator"] = @benchmarkable qsimplify($commutator_expr, rewriter=qsimplify_commutator) |
| 93 | + |
| 94 | + |
| 95 | +# Expression benchmarks |
| 96 | +SUITE["express"] = BenchmarkGroup(["express"]) |
| 97 | +clear_cache!(x) = empty!(x.metadata.express_cache) |
| 98 | +simple_op = X⊗Y |
| 99 | +simple_ket = Z1⊗Z1 |
| 100 | +pauli_op_4 = YCX * transpose(Z⊗Y) * YCZ * dagger(Y⊗X) * conj(ZCY) * exp(X ⊗ (2-3im)X) |
| 101 | +pauli_state_8 = (conj(XCY⊗Y) + dagger(Z⊗YCX) + transpose(0.5im*Y ⊗ exp(XCY))) * (X1⊗Y1⊗Z1) |
| 102 | + |
| 103 | +SUITE["express"]["optics"]["simple_op"] = @benchmarkable express($simple_op) setup=clear_cache!(simple_op) evals=1 |
| 104 | +SUITE["express"]["optics"]["simple_ket"] = @benchmarkable express($simple_ket) setup=clear_cache!(simple_ket) evals=1 |
| 105 | +SUITE["express"]["optics"]["pauli_op_4"] = @benchmarkable express($pauli_op_4) setup=clear_cache!(pauli_op_4) evals=1 |
| 106 | +SUITE["express"]["optics"]["pauli_state_8"] = @benchmarkable express($pauli_state_8) setup=clear_cache!(pauli_state_8) evals=1 |
| 107 | + |
| 108 | +# TODO: add additional clifford expressions |
| 109 | +SUITE["express"]["clifford"]["simple_ket"] = @benchmarkable express($simple_ket, CliffordRepr()) setup=clear_cache!(simple_ket) evals=1 |
| 110 | +SUITE["express"]["clifford"]["simple_observable"] = @benchmarkable express($simple_op, CliffordRepr(), UseAsObservable()) setup=clear_cache!(simple_op) evals=1 |
| 111 | + |
0 commit comments