I want to implement amplitude amplification using Q#. I have the operation $A$ that prepares my initial state and I need to compute $ A^{-1} $ to implement the algorithm.
Is there an easy way to do that in Q# (a keyword or operation)?
Quantum Computing Stack Exchange is a question and answer site for engineers, scientists, programmers, and computing professionals interested in quantum computing. It only takes a minute to sign up.
Sign up to join this communityAs given in the documentation, if your operation is unitary, you can add the statement adjoint auto;
within the operation after the body block. This will generate the adjoint (which is the inverse for unitary).
Then, to use the inverse call Adjoint A(parameters)
In the case that your operation can be represented by a unitary operator $U$ (this is typically the case if your operation doesn't use any measurements), you can indicate that by adding is Adj
to your operation's signature, letting the Q# compiler know that your operation is adjointable:
open Microsoft.Quantum.Math as Math;
/// # Summary
/// Prepares a qubit in a state representing a classical probability
/// distribution {p, 1 - p}.
/// # Description
/// Given a qubit in the |0⟩, prepares √p |0⟩ + √(1 - p) |1⟩
/// for a given probability p.
operation PrepareDistribution(probability : Double, target : Qubit) : Unit
is Adj {
let rotationAngle = 2.0 * Math.ArcCos(Math.Sqrt(1.0 - probability));
Ry(rotationAngle, target);
}
You can then call Adjoint PrepareDistribution
to "undo" the PrepareDistribution
operation. The Adjoint
keyword is an example of a Q# functor, and tells Q# that you want the inverse operation. In this case, the Q# compiler will apply Ry(-rotationAngle, target)
.
For more information: