!
register!
using()
using
block. As you note, you can't use measurement to do that in an adjointable operation; given that resetting is a special case of measurement, the Reset
operation will also fail to preserve adjointability. The trick is that you need to coherently reset the qubit to |0⟩ without measurement. You can do that because you know exactly what state it's in at the end of the using
, and can unprepare it using only unitary operations. The within
/apply
feature of Q# or the ApplyWithCA
operation will be very handy here.
By the way, @crazy4pi314 will be streaming with @bettinaheim about the Q# compiler today in a little under three hours if anyone is interested to join at https://twitch.tv/crazy4pi314. More details at https://twitter.com/crazy4pi314/status/1271837229221101569.
'T
and that returns an output of type 'U
is written as 'T => 'U
; thus, unitary : (Qubit = > Unit is Adj + Ctl)
indicates that unitary takes a Qubit
input and returns Unit
. The is Adj + Ctl
tells you that unitary
is adjointable and controllable (very important for some of the contest problems!). In this case, we can read the signature of Solve
as telling us that it takes an adjointable and controllable single-qubit operation, and returns an Int
.
Adjoint
functor to an adjointable operation doesn't change the type of that operation. For example, Microsoft.Quantum.Intrinsic.X
has type Qubit => Unit is Adj + Ctl
, such that Adjoint X
also has type Qubit => Unit is Adj + Ctl
. In the example from the contest problem, Adjoint unitary
is an operation with type (Qubit => Unit is Adj + Ctl)
. By contrast, for a controllable operation op
, Controlled op
does modify the type to add the new control register. For more detail, you can checkout my book with @crazy4pi314 at bit.ly/qsharp-book, or the Microsoft Quantum docs at https://docs.microsoft.com/quantum/user-guide/using-qsharp/operations-functions#controlled-and-adjoint-operations.