-
Notifications
You must be signed in to change notification settings - Fork 98
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
Conversation
@orpuente-MS Do we really need an explicit |
I was having the same thought, and I think it's worth exploring. With the attribute, you must include the attribute for something to get treated as a
If instead these restrictions became checks during lowering from AST to HIR, then any operation that meets these requirements could be lowered into |
My one concern with this is that the compiler won't help the user with friendly errors if the user doesn't use the right combination of inputs & outputs when trying to write a measurement. For example, if the user writes this, operation __quantum__qis__mx__body(target : Qubit, invalid_arg: Int) : (Int, String) {
body intrinsic;
} and they intended to write a measurement, we have no way to guide them towards the right path by telling them:
because this could perfectly be a non-measurement intrinsic, so we can't issue errors in this case |
That's a good point, and I think we can focus in on that problem statement: if someone writes something that is valid Q# in general but we know it will fail QIR codegen because it doesn't match the set of intrinsic qualities we support, how do we tell them? I have some thoughts on what that feedback could look like, so perhaps you, @cesarzc and I could brainstorm a little. Overall, I do think a lot of the infrastructure in this PR could be used to get toward this goal, even if the specific detail of an |
In general I don't like the idea of having a special kind of operation (i.e. measurements) hidden behind a specific set of inputs & outputs. I think we should be explicit about measurement declarations. Initially I wanted to add a measurement Mx(target: Qubit) : Result {
body intrinsic;
} I like this because it feels like measurements are actually part of the language, which is nice because they are treated differently in literature and in our codegen. The reason I decided not to go this route is because it adds a new keyword to the language, and that could break existing user code. |
I forgot to mention that Note that this is not a special case because you can think of it as "a measurement must return 0 or more Results". @Measurement()
operation Projection(target: Qubit) : Unit {
body intrinsic;
} In this case it is critical that we have a operation H(target: Qubit) : Unit {
body intrinsic;
} |
Yes, I think you've identified another issue here: how do we (or the user) express that an operation is destructive to the state? As you point out, maybe it's that scenario that requires the attribute because it can't be differentiated based on signature alone. |
Ohh, I didn't know that Reset was that different, thank you for clarifying. I edited my last message to use |
Co-authored-by: César Zaragoza Cortés <cesarzc@microsoft.com>
…microsoft/qsharp into oscarpuente/custom-measurements
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One minor fix for help message left, otherwise it looks good!
This PR adds the ability to declare custom measurements in Q#. Here is an example:
The generated QIR will be,
Design Notes
@Measurement()
attribute over a newmeasurement
keyword to avoid putting extra syntax into the language.body intrinsic
or a@SimulatableIntrinsic()
.Qubits
as inputs and haveResults
as outputs.Notes to reviewers
This PR consists of the following steps:
@Measurement()
attribute to the language and piping it down to FIR.#1
attribute indicating an irreversible operation.@Measurement()
attribute to the completions list.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.