Add classical control. - #1093
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces classical control support to the qsim simulator, enabling dynamic circuit execution with conditional branching, loops, and runtime variable assignments. Key changes include a new expression parser, symbol table management, and updates to the core simulator runner to handle these classical control constructs. Additionally, the codebase has been updated to C++20, and several deprecated run methods have been replaced. The review comments provided actionable feedback on potential undefined behavior in measurement gates and bitwise operations, as well as necessary input validation for simulation parameters, all of which have been incorporated into the review process.
| if (r < cp) { | ||
| DeferOps(kop.ops, deferred_ops); | ||
| non_unitary = non_unitary || !kop.unitary; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| unsigned t = times_to_measure_at[cur_time_index]; | ||
| if (r < cp) continue; |
There was a problem hiding this comment.
When a value (such as a norm) is used as a reference for cumulative sums in a subsequent loop, ensure the calculation method (e.g., scalar vs. vectorized) matches the loop's method to maintain consistency and avoid floating-point round-off discrepancies. This prevents issues where round-off errors cause the random threshold r to exceed the cumulative sum cp at the last iteration.
References
- When a value (such as a norm) is used as a reference for cumulative sums in a subsequent loop, ensure the calculation method (e.g., scalar vs. vectorized) matches the loop's method to maintain consistency and avoid floating-point round-off discrepancies.
There was a problem hiding this comment.
We would appreciate further clarification on this comment. Note that round-off errors are addressed in lines 382–391.
Summary
This PR introduces end-to-end support for classical control flow in
qsim. It adds a classical runtime evaluation engine (variables, symbol tables, expressions, control structures) directly into the simulator, extends the input language, and introduces the new CLI tool to support dynamic quantum-classical workflows.Key Changes
Classical Runtime & Operations
if/elsif/else,repeat, anddo/whileloops, along with variable assignments, discard statements, and debug/message logging.Language & CLI Tooling
input_formatinto a formal circuit description language supporting classical control flow and simple classical computations.qsimCLI Application: Introduced a command-line simulator executable in theqsim/directory supporting classical control, clean and noisy simulations, and parallel repetition sampling (currently only outputting measurement histograms).Infrastructure & Testing
Limitations & Future Work
qsimcirqis not included in this PR and will be added in a future update.Footnote: Some docstrings and tests were generated with assistance from Gemini (Google AI); documentation was polished with Gemini.