Skip to content

[C++] Type distinction for measurement result#3800

Open
khalatepradnya wants to merge 49 commits intoNVIDIA:mainfrom
khalatepradnya:measure_result
Open

[C++] Type distinction for measurement result#3800
khalatepradnya wants to merge 49 commits intoNVIDIA:mainfrom
khalatepradnya:measure_result

Conversation

@khalatepradnya
Copy link
Copy Markdown
Collaborator

@khalatepradnya khalatepradnya commented Jan 27, 2026

This PR introduces comprehensive support for the measure_result type throughout the CUDA-Q C++ frontend, enabling deferred discrimination of measurement results.

  • New standalone cudaq::measure_result class with std::int64_t value and std::int64_t unique_id fields (16 bytes).
  • Kernels with measure_result in their signature are classified as device-only.
  • quake.discriminate is inserted only at points where a classical value is needed: if/ternary conditions, bool/int/double casts, comparisons, arithmetic, explicit to_bool_vector() / to_integer() calls and return from entry-point kernels.

These changes collectively ensure that quantum measurement results are treated as first-class citizens in the type system and IR lowering.

Breaking Changes

  • auto kernel() { return mz(q); } now returns measure_result, making the kernel device-only.
  • std::vector<bool> v = mz(qubits) no longer compiles; use cudaq::to_bool_vector(mz(qubits)).
  • int x = mz(q) requires static_cast<int>(mz(q)).
  • Entry-point kernels cannot return measure_result or std::vector<measure_result>.

Note: Python front-end changes will be covered in a separate PR.

github-actions bot pushed a commit that referenced this pull request Jan 27, 2026
@github-actions
Copy link
Copy Markdown

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Jan 30, 2026
@github-actions
Copy link
Copy Markdown

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Jan 31, 2026
@github-actions
Copy link
Copy Markdown

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Feb 2, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 2, 2026

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Feb 3, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 3, 2026

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Feb 3, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 3, 2026

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

@khalatepradnya khalatepradnya force-pushed the measure_result branch 3 times, most recently from 4af9350 to c402e3e Compare February 5, 2026 23:28
github-actions bot pushed a commit that referenced this pull request Feb 6, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 6, 2026

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Feb 6, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 6, 2026

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

@khalatepradnya khalatepradnya force-pushed the measure_result branch 2 times, most recently from 5be53bc to 45b8c68 Compare February 6, 2026 06:32
github-actions bot pushed a commit that referenced this pull request Feb 6, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 6, 2026

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

  in `qis` directory, remove the macro for library mode and drop the
  typedef to bool.
* Retain variable names assigned to measurement results
* Disallow measure_result as argument to entry-point kernels
* Drop the intrinsic funciton fro converting to Boolean vector
* Insert discriminate op wherever necessary
* Adjust tests
* WIP

Signed-off-by: Pradnya Khalate <pkhalate@nvidia.com>
  frontend
* Add support for RESULT type in the log parser - construct a
  `measure_result` object.
* Two tests are not working with `auto` - should be fixed
* Clean-up
* Handle the size for `measure_result` since it behaves differently on
  host and on device
* Log result output

Signed-off-by: Pradnya Khalate <pkhalate@nvidia.com>
Copy link
Copy Markdown
Collaborator

@atgeller atgeller left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @khalatepradnya! Left a comment.

* Update expand measurement pass to handle more than one discriminate op

Signed-off-by: Pradnya Khalate <pkhalate@nvidia.com>
github-actions bot pushed a commit that referenced this pull request Mar 20, 2026
@github-actions
Copy link
Copy Markdown

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Mar 21, 2026
@github-actions
Copy link
Copy Markdown

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

Comment on lines +3357 to +3358
// The source is a pointer to measure_result (!cc.ptr<!quake.measure>)
// Just load and return the value
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This comment specifies something invalid.

In the IR, we cannot have pointers to abstract quake types. We cannot load the abstraction and we cannot store it. (Think of a measurement type as some volatile stream of random data of infinite size. There is no way to allocate a finite amount of space for it, and even if you could, as soon as you tried to load or store it, the data would change and be something else.)

@sacpis sacpis self-requested a review March 23, 2026 17:01
Comment on lines +1767 to +1769
using ComputePtrOpPattern = OpInterfacePattern<cudaq::cc::ComputePtrOp>;
using StdvecInitOpPattern = OpInterfacePattern<cudaq::cc::StdvecInitOp>;
using StdvecDataOpPattern = OpInterfacePattern<cudaq::cc::StdvecDataOp>;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think we're using the wrong idiom here in retrospect. A normal stdvec<T> value is a memory buffer, but a stdvec<measure> never was. It was always expanded and converted to stdvec<bool> immediately.

Again, loading and storing quake.anytype from a stack location makes no sense.

}
};

struct LogOutputOpPattern : public OpConversionPattern<cudaq::cc::LogOutputOp> {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can't this one be another OpInterfacePattern instance?

github-actions bot pushed a commit that referenced this pull request Mar 23, 2026
@github-actions
Copy link
Copy Markdown

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

@khalatepradnya
Copy link
Copy Markdown
Collaborator Author

…ureResult` functionality.

Signed-off-by: Pradnya Khalate <pkhalate@nvidia.com>
  `isMeasureResultSequenceType` into single function.

Signed-off-by: Pradnya Khalate <pkhalate@nvidia.com>
  - assert on argument passed to `to_bool_vector`
  - revisit load / pointer logic after changes to `MeasurementsType`

Signed-off-by: Pradnya Khalate <pkhalate@nvidia.com>
  - simply constructor
  - remove redundant code

Signed-off-by: Pradnya Khalate <pkhalate@nvidia.com>
github-actions bot pushed a commit that referenced this pull request Mar 25, 2026
@github-actions
Copy link
Copy Markdown

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Mar 25, 2026
@github-actions
Copy link
Copy Markdown

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Mar 25, 2026
@github-actions
Copy link
Copy Markdown

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

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

Labels

breaking change Change breaks backwards compatibility enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants