Commit 0d63a93
Remove the commutation cache from the commutation checker (#15988)
An early performance optimization we made in the commutation analysis
pass in #3878 was to enable caching of the commutation relations between
gates. Back then the commutation was only checked via the matrix
multiplication method where we would compose a pair of gates' unitary
matrices forwards and backwards and determine if the product of those
forward and backward compositions were an identity. This was a fairly
costly operation back then for various reasons and the cache enabled a
large speedup by avoiding repeated computation unnecessarily. However,
since that time > 6 years ago the code base has evolved substantially,
including not relying on matrix multiplication based commutation
determination as the default. Now we have a precomputed library of
commuting gates and also have special handling for cases where we can
know very easily whether gates commute or not. Additionally, all of this
code has been ported to rust so the matrix multiplication based
approach is not nearly as expensive (although it's not free either).
In this new world the cache is actually doing more harm then good because
maintaining the cache, adding extra lookups and hashing while constant
time is not free. We've reached a point where all the complexity of the
cache is no longer worth it, so this commit removes the commutation
cache. One caveat is some aspects of this internal cache have leaked
into the public API. Specifically the CommutationChecker class is
public, and that includes a documented init argument `cache_max_entries`
as well as two public methods `clear_cached_commutations` and
`num_cached_entries`. I believe the original intent for these methods
was either debugging the cache logic was correct (as they were used in
tests) or to enable the user to manage the cache size manually if they
so wished. The `cache_max_entries` argument was used to manage the total
memory size for the cache to avoid using to much memory in certain
applications. Since these are part of the public documented api we
can't remove them without violating our stability guidelines. So instead
this commit opts to just make them no-ops or in the case of the
`num_cached_entries` it will always return 0 (since there are no longer
any cached entries). These are all marked as deprecated in this PR to
mark them for removal in 3.0.
In practice there is a minimal performance difference from this
change which means we don't need the extra code anymore, although in
some very specific benchmarks a small speedup may be seen (those
dominated by commutation checking). However, there is one case where
running without a cache can be slower, in cases when there are a large
number of gate pairs that involves a gate that we don't know
whether it commutes or not without the matrix multiplication (i.e. it's not a
known pauli, rotation gate, or in the library) and we have multiple
repeated pairs of the same gate. In practice this doesn't come up very
frequently because typically in a preset passmanager's workflow we have
lowered to all 1q and 2q gates and that lowering involves standard gates
we know how to work with in the checker. The only edge case is if there
was a circuit with a large number of custom 1q or 2q gates that have
matrix definitions in a circuit (which is not common). But, the asv
benchmark for commutation analysis will likely show a roughly 5x slowdown
with this commit. That benchmark is highlighting this edge case because
it is running the pass on a random circuit with gates up to 3 qubits in
width which will involve multiple repeated checks via matrix
multiplication which does not come up in practice normally. Additionally,
unlike in #3878 the regression being flagged is only on the 5x slowdown
is on the scale of tens of ms, back in 2020 we could only have dreamed
to have the CommutationAnalysis pass execute in < 100ms in asv, let alone
a world where a 5x regression flagged in asv would be so quick. The
entire pass was at least 2 orders of magnitude slower at that point we
introduced the cache.
Co-authored-by: Julien Gacon <jules.gacon@googlemail.com>1 parent fa215e4 commit 0d63a93
5 files changed
Lines changed: 38 additions & 243 deletions
File tree
- crates/transpiler/src
- qiskit/circuit
- releasenotes/notes
- test/python
- circuit
- transpiler
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
296 | 296 | | |
297 | 297 | | |
298 | 298 | | |
299 | | - | |
300 | | - | |
| 299 | + | |
| 300 | + | |
301 | 301 | | |
302 | 302 | | |
303 | 303 | | |
304 | 304 | | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | 305 | | |
309 | 306 | | |
310 | 307 | | |
311 | 308 | | |
312 | 309 | | |
313 | 310 | | |
314 | | - | |
| 311 | + | |
315 | 312 | | |
316 | 313 | | |
317 | 314 | | |
318 | | - | |
319 | 315 | | |
320 | 316 | | |
321 | 317 | | |
322 | | - | |
| 318 | + | |
323 | 319 | | |
324 | 320 | | |
325 | 321 | | |
| |||
388 | 384 | | |
389 | 385 | | |
390 | 386 | | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
397 | | - | |
398 | | - | |
399 | | - | |
400 | | - | |
401 | 387 | | |
402 | 388 | | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
407 | | - | |
408 | | - | |
409 | | - | |
410 | 389 | | |
411 | 390 | | |
412 | 391 | | |
413 | 392 | | |
414 | 393 | | |
415 | 394 | | |
416 | 395 | | |
417 | | - | |
418 | | - | |
419 | | - | |
420 | | - | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
425 | 396 | | |
426 | 397 | | |
427 | 398 | | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | 399 | | |
438 | 400 | | |
439 | 401 | | |
| |||
444 | 406 | | |
445 | 407 | | |
446 | 408 | | |
447 | | - | |
448 | | - | |
| 409 | + | |
449 | 410 | | |
450 | | - | |
451 | | - | |
452 | | - | |
453 | | - | |
454 | | - | |
455 | | - | |
| 411 | + | |
| 412 | + | |
456 | 413 | | |
457 | 414 | | |
458 | 415 | | |
459 | | - | |
460 | | - | |
461 | | - | |
462 | 416 | | |
463 | 417 | | |
464 | 418 | | |
| |||
586 | 540 | | |
587 | 541 | | |
588 | 542 | | |
589 | | - | |
590 | | - | |
591 | | - | |
592 | | - | |
593 | | - | |
594 | | - | |
595 | | - | |
596 | | - | |
597 | | - | |
598 | | - | |
599 | | - | |
600 | | - | |
601 | | - | |
602 | | - | |
603 | | - | |
604 | | - | |
605 | | - | |
606 | | - | |
607 | | - | |
608 | | - | |
609 | | - | |
610 | | - | |
611 | | - | |
612 | | - | |
613 | | - | |
614 | | - | |
615 | | - | |
616 | | - | |
617 | | - | |
618 | | - | |
619 | | - | |
620 | | - | |
621 | | - | |
622 | 543 | | |
623 | 544 | | |
624 | 545 | | |
| |||
628 | 549 | | |
629 | 550 | | |
630 | 551 | | |
631 | | - | |
632 | | - | |
633 | | - | |
634 | | - | |
635 | | - | |
636 | | - | |
637 | | - | |
638 | | - | |
639 | | - | |
640 | | - | |
641 | | - | |
642 | | - | |
643 | | - | |
644 | 552 | | |
645 | 553 | | |
646 | 554 | | |
| |||
656 | 564 | | |
657 | 565 | | |
658 | 566 | | |
659 | | - | |
660 | | - | |
661 | | - | |
662 | | - | |
663 | | - | |
664 | | - | |
665 | | - | |
666 | | - | |
667 | | - | |
668 | | - | |
669 | | - | |
670 | | - | |
671 | | - | |
672 | | - | |
673 | | - | |
674 | | - | |
675 | | - | |
676 | | - | |
677 | | - | |
678 | 567 | | |
679 | 568 | | |
680 | 569 | | |
| |||
773 | 662 | | |
774 | 663 | | |
775 | 664 | | |
776 | | - | |
777 | | - | |
778 | | - | |
779 | | - | |
780 | | - | |
781 | 665 | | |
782 | 666 | | |
783 | 667 | | |
| |||
1085 | 969 | | |
1086 | 970 | | |
1087 | 971 | | |
1088 | | - | |
1089 | | - | |
1090 | | - | |
1091 | | - | |
1092 | | - | |
1093 | | - | |
1094 | | - | |
1095 | | - | |
1096 | | - | |
1097 | | - | |
1098 | | - | |
1099 | | - | |
1100 | | - | |
1101 | | - | |
1102 | | - | |
1103 | | - | |
1104 | | - | |
1105 | | - | |
1106 | | - | |
1107 | | - | |
1108 | | - | |
1109 | | - | |
1110 | | - | |
1111 | | - | |
1112 | | - | |
1113 | | - | |
1114 | | - | |
1115 | | - | |
1116 | | - | |
1117 | | - | |
1118 | | - | |
1119 | | - | |
1120 | | - | |
1121 | | - | |
1122 | | - | |
1123 | | - | |
1124 | | - | |
1125 | | - | |
1126 | | - | |
1127 | | - | |
1128 | | - | |
1129 | | - | |
1130 | | - | |
1131 | | - | |
1132 | | - | |
1133 | | - | |
1134 | | - | |
1135 | | - | |
1136 | | - | |
1137 | | - | |
1138 | | - | |
1139 | | - | |
1140 | | - | |
1141 | | - | |
1142 | | - | |
1143 | | - | |
1144 | | - | |
1145 | | - | |
1146 | | - | |
1147 | | - | |
1148 | | - | |
1149 | | - | |
1150 | | - | |
1151 | | - | |
1152 | | - | |
1153 | | - | |
1154 | | - | |
1155 | | - | |
1156 | | - | |
1157 | | - | |
1158 | | - | |
1159 | | - | |
1160 | | - | |
1161 | | - | |
1162 | | - | |
1163 | | - | |
1164 | | - | |
1165 | | - | |
1166 | | - | |
1167 | | - | |
1168 | | - | |
1169 | | - | |
1170 | | - | |
1171 | | - | |
1172 | | - | |
1173 | | - | |
1174 | | - | |
1175 | | - | |
1176 | | - | |
1177 | | - | |
1178 | 972 | | |
1179 | 973 | | |
1180 | 974 | | |
1181 | 975 | | |
1182 | 976 | | |
1183 | | - | |
1184 | | - | |
1185 | | - | |
1186 | 977 | | |
1187 | 978 | | |
1188 | 979 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
48 | | - | |
49 | | - | |
| 49 | + | |
50 | 50 | | |
51 | 51 | | |
| 52 | + | |
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
59 | | - | |
| 60 | + | |
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
| |||
118 | 119 | | |
119 | 120 | | |
120 | 121 | | |
| 122 | + | |
121 | 123 | | |
122 | | - | |
123 | | - | |
| 124 | + | |
124 | 125 | | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
125 | 132 | | |
126 | | - | |
127 | | - | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
128 | 137 | | |
129 | 138 | | |
130 | 139 | | |
| |||
Lines changed: 15 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
0 commit comments