Skip to content

Conversation

@bmhowe23
Copy link
Collaborator

@bmhowe23 bmhowe23 commented Dec 11, 2025

Overview

This PR introduces support for defining detectors in CUDA-Q kernels, a feature for Quantum Error Correction (QEC), and adds functionality to export kernels to the Stim circuit format.

Details

1. Detectors Support

  • New Operation: Introduced cudaq::detector (C++) and cudaq.detector (Python) to define detectors. Detectors compare measurement results to check for errors (parity checks).
  • IR & Dialect: Added quake.detector operation to the Quake dialect and its lowering to QIR (__quantum__qis__detector).
  • Compiler: Added an EraseDetectors pass to remove detector operations when targeting backends that do not support them (e.g., standard hardware execution, or if explicitly undesired).
  • Runtime:
    • Updated ExecutionManager and simulators to support tracing and handling detector operations.
    • Implemented cudaq.detectors() utility to extract detector metadata (measurement indices) from a kernel.

2. Stim Integration

  • cudaq.to_stim(kernel): Added a new Python utility that converts a CUDA-Q kernel into a Stim circuit representation.
    • Supports gates, measurements, noise models, and the new detector operations.
  • Stim Simulator: Updated StimCircuitSimulator to support a "to_stim" execution mode for string generation.

3. Tracing Improvements

  • Measurement Tracing: Updated BasicExecutionManager to explicitly append measurement instructions (mz) to the kernel trace even in tracer mode.
    • Side Effect: The cudaq.draw output now explicitly shows measurement operations, updating expectations in test_kernel_features.py and draw_test.cpp.

Tests

  • New Tests:
    • python/tests/backends/test_to_stim.py: Validates Stim circuit generation, including noise models and detectors.
    • python/tests/kernel/test_detectors.py: Verifies cudaq.detectors() analysis.
    • unittests/integration/detector_tester.cpp: C++ integration tests for detector IR and runtime behavior.
  • Updated Tests: Adjusted existing visualization tests to account for the improved visibility of measurements in kernel traces.

// "quake.detector"(%6) : (!cc.stdvec<i64>) -> ()
// clang-format on

// TODO - decide whether or not to keep this path.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is almost certainly too fragile or dangerous to keep it this way. If there is a standard MLIR way to do this, we should do it here.

}
}

reportClangError(x, mangler, "detector argument types not supported: ");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just want to check that we'd have some info after ':' in the error message.

Copy link
Collaborator

@schweitzpgi schweitzpgi left a comment

Choose a reason for hiding this comment

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

First, nice work pulling together this prototype from end to end. Very cool.

After talking to the architects, I think we should hold off merging this for now until we have a consensus on the user facing aspects, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants