Skip to content

Add classical control. - #1093

Open
sergeisakov wants to merge 13 commits into
mainfrom
add-classical-control
Open

Add classical control.#1093
sergeisakov wants to merge 13 commits into
mainfrom
add-classical-control

Conversation

@sergeisakov

Copy link
Copy Markdown
Collaborator

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

  • Variables & Expression Engine: Added scoped symbol tables, expression parsing, and runtime variable evaluation and assignment.
  • Control Structures: Implemented classically controlled operations supporting if/elsif/else, repeat, and do/while loops, along with variable assignments, discard statements, and debug/message logging.
  • Runner Helper: Added a unified runner helper capable of executing both clean and noisy circuits with classical control flow.

Language & CLI Tooling

  • Circuit Description Language: Extended the legacy input_format into a formal circuit description language supporting classical control flow and simple classical computations.
  • qsim CLI Application: Introduced a command-line simulator executable in the qsim/ directory supporting classical control, clean and noisy simulations, and parallel repetition sampling (currently only outputting measurement histograms).

Infrastructure & Testing

  • C++ Standard Upgrade: Bumped the required C++ standard version to C++20.
  • Test Coverage: Added a full GoogleTest suite covering all newly introduced classical control components.

Limitations & Future Work

  • Support for qsimcirq is 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.

@github-actions github-actions Bot added the size: XL lines changed >1000 label Jul 27, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread lib/circuit_qsim_parser.h
Comment thread lib/classical_control_parser.h
Comment thread lib/run_qsim.h
Comment on lines +348 to +355
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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
  1. 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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would appreciate further clarification on this comment. Note that round-off errors are addressed in lines 382–391.

Comment thread qsim/qsim.cc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: XL lines changed >1000

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant