Skip to content

Refactor gate interface.#1043

Open
sergeisakov wants to merge 8 commits intomainfrom
refactor_gate_interface
Open

Refactor gate interface.#1043
sergeisakov wants to merge 8 commits intomainfrom
refactor_gate_interface

Conversation

@sergeisakov
Copy link
Copy Markdown
Collaborator

Summary

This PR implements a major refactoring of the gate interface and circuit representation within qsim. To improve type safety and extensibility, the monolithic Gate struct has been decomposed into specialized types. The core circuit element is now transitioned to an Operation variant, allowing for more granular handling of gates, measurements, and noise channels. This is a breaking change for internal APIs.

Key Changes

  • Type Decomposition: Split the original Gate struct into distinct types:
    • Gate: Standard unitary operations.
    • ControlledGate: Operations with control.
    • Measurement: Measurement operations.
    • Channel: Unitary and non-unitary quantum channels.
    • DecomposedGate: Schmidt-decomposed gates.
  • New Operation Model: Introduced std::variant to define the basic unit of a circuit.
    • Standard: using Operation = std::variant<Gate, ControlledGate, Measurement, Channel>;
    • Fused: using Operation = std::variant<FusedGate, Measurement, const Operation*>;
  • Infrastructure Updates: Updated quantum trajectory and hybrid simulators, circuit parsers, fusion logic, gate application dispatching logic, and pybind11 interface to consume the new Operation types. Unified clean and noisy circuits.

Future-Proofing & Extensibility

By moving to a variant-based Operation model, the architecture is now significantly more flexible. This refactoring explicitly paves the way for:

  • Classical Control: Easier integration of conditional operations and classical feed-forward logic.
  • Custom Entities: Simplified path for adding new specialized entities without bloating the core Gate logic.

@github-actions github-actions bot added the size: XL lines changed >1000 label Apr 8, 2026
Copy link
Copy Markdown
Contributor

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

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 refactors the gate system by introducing a generic Operation variant and updating fusers, runners, and Python bindings to support it. Critical bugs were identified in lib/fuser_mqubit.h where an undefined variable time was used in error messages. Additionally, the use of std::terminate() in lib/operation_base.h for error handling was flagged as inappropriate for a library, with suggestions to use exceptions or static assertions instead.

@sergeisakov sergeisakov requested a review from mhucka April 8, 2026 19:29
@sergeisakov
Copy link
Copy Markdown
Collaborator Author

/gemini review

Copy link
Copy Markdown
Contributor

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

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 refactors the core operation model by introducing a generic std::variant-based Operation type, replacing specific gate structures across the library, applications, and Python bindings. This change necessitates a transition to C++17 and updates to fusers, runners, and simulators to handle the new polymorphic operation container. The review identifies a critical logic error in the noise-application functions, AddNoise and MakeNoisy, where Measurement operations are incorrectly treated as unitary gates. This results in invalid state-collapse simulations due to erroneous timestamp modifications and the inappropriate addition of noise channels. The feedback suggests implementing specialized handling for measurements to maintain simulation integrity.

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