Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom measurement operations to Q# #1967

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

orpuente-MS
Copy link
Contributor

@orpuente-MS orpuente-MS commented Oct 16, 2024

This PR adds the ability to declare custom measurements in Q#. Here is an example:

@Measurement()
operation __quantum__qis__mx__body(target : Qubit) : Result {
  body intrinsic;
}

The generated QIR will be,

declare void @__quantum__qis__mx__body(%Qubit*, %Result*) #1

Design Notes

  1. I choose the @Measurement() attribute over a new measurement keyword to avoid putting extra syntax into the language.
  2. A custom measurement must be a body intrinsic or a @SimulatableIntrinsic().
  3. A measurement must take only Qubits as inputs and have Results as outputs.

Notes to reviewers

This PR consists of the following steps:

  1. Adding a @Measurement() attribute to the language and piping it down to FIR.
  2. Make the generated QIR have the #1 attribute indicating an irreversible operation.
  3. Treating a measurement as a dynamic operation in RCA.
  4. Handling custom measurements correctly during partial evaluation: moving the output Results to result registers in the input.
  5. Handling undesired semantics as errors using an HIR pass.
  6. Adding the @Measurement() attribute to the completions list.
  7. Unit tests in codegen and in the HIR pass.

Next steps

After this PR is merged we can add the @Measurement() attribute to the measurement operations in the standard library and remove the hard coded logic we have for them in the compiler.

@orpuente-MS orpuente-MS changed the title Oscarpuente/custom measurements Add custom measurement operations to Q# Oct 16, 2024
@@ -289,7 +289,7 @@ impl CompletionListBuilder {
}

fn push_attributes(&mut self) {
static ATTRIBUTES: [&str; 2] = ["@EntryPoint()", "@Config()"];
static ATTRIBUTES: [&str; 3] = ["@EntryPoint()", "@Config()", "@Measurement()"];
Copy link
Member

Choose a reason for hiding this comment

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

Heads up, this code is changing in my PR. When you hit the merge conflict, I suggest just reverting this and searching again for the attribute list instead of trying to resolve it as a merge conflict...

@minestarks
Copy link
Member

@orpuente-MS Do we really need an explicit @Measurement attribute if we simply define a "measurement intrinsic" as an "intrinsic that returns a Result"?

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.

2 participants