Commit 910cdd8
[Circuit diagrams] 4 - Show conditionals in circuits based on RIR debug metadata (#2943)
This PR completes the feature for rendering classical control flow and
conditionals in quantum circuit diagrams. The changes span the compiler,
code generation, circuit visualization, and VS Code integration.
**Circuit Generation from RIR**
- Implemented new RIR-to-circuit conversion (rir_to_circuit.rs module)
that reconstructs control flow structure from RIR blocks
- Added structured control flow analysis to properly traverse branches
in execution order
- Built logic to handle complex scenarios:
- Measurement-based conditionals (if-else based on qubit measurement
results)
- Nested conditionals and loops with quantum operations
- Binary operation short-circuit evaluation
- Multiple possible values in function arguments based on control flow
branches
**VS Code Extension Fallback Logic**
- Added fallback mechanism: if circuit generation fails due to adaptive
profile compliance issues when using "static" method, the extension
automatically retries with "classicalEval" method and "Unrestricted"
profile
- Preserves existing fallback for result comparison errors in
"classicalEval" mode
### Usage Example
For Q# code with measurement-based conditionals:
```qsharp
operation TestConditional(q : Qubit) : Unit {
H(q);
let r = M(q);
if (r == One) {
X(q);
}
}
```
The circuit diagram now displays the conditional control flow structure,
showing which gates execute based on measurement outcomes, alongside the
quantum operations.
---------
Co-authored-by: Mine Starks <>
Co-authored-by: Stefan J. Wernli <swernli@microsoft.com>1 parent fcaf99f commit 910cdd8
40 files changed
Lines changed: 32615 additions & 5050 deletions
File tree
- source
- compiler
- qsc_circuit
- src
- builder
- tests
- circuit
- rir_to_circuit
- tests
- qsc/src
- interpret
- npm/qsharp
- test
- circuits-cases
- ux/circuit-vis/formatters
- vscode
- src
- gh-copilot
- wasm/src
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
4 | 6 | | |
5 | 7 | | |
6 | 8 | | |
| |||
25 | 27 | | |
26 | 28 | | |
27 | 29 | | |
| 30 | + | |
28 | 31 | | |
29 | | - | |
| 32 | + | |
30 | 33 | | |
31 | 34 | | |
32 | 35 | | |
| |||
68 | 71 | | |
69 | 72 | | |
70 | 73 | | |
71 | | - | |
| 74 | + | |
72 | 75 | | |
73 | 76 | | |
74 | 77 | | |
| |||
122 | 125 | | |
123 | 126 | | |
124 | 127 | | |
| 128 | + | |
| 129 | + | |
125 | 130 | | |
126 | 131 | | |
127 | 132 | | |
| |||
331 | 336 | | |
332 | 337 | | |
333 | 338 | | |
334 | | - | |
335 | | - | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
336 | 343 | | |
337 | 344 | | |
338 | 345 | | |
| |||
348 | 355 | | |
349 | 356 | | |
350 | 357 | | |
351 | | - | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
352 | 361 | | |
353 | 362 | | |
354 | 363 | | |
355 | 364 | | |
356 | 365 | | |
| 366 | + | |
357 | 367 | | |
358 | 368 | | |
359 | 369 | | |
| |||
1041 | 1051 | | |
1042 | 1052 | | |
1043 | 1053 | | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
1044 | 1069 | | |
1045 | 1070 | | |
1046 | 1071 | | |
1047 | 1072 | | |
1048 | 1073 | | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 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 | + | |
1049 | 1166 | | |
1050 | 1167 | | |
1051 | 1168 | | |
| |||
1309 | 1426 | | |
1310 | 1427 | | |
1311 | 1428 | | |
| 1429 | + | |
| 1430 | + | |
| 1431 | + | |
1312 | 1432 | | |
1313 | 1433 | | |
1314 | 1434 | | |
| |||
0 commit comments