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)?


2 Answers 2


As 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)

  • 1
    $\begingroup$ Thank you. I didn't know that the adjoint is also the inverse for unitary matrices. $\endgroup$ Commented Jun 16, 2019 at 17:44

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:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.