# How to compute the inverse of an operation in Q#?

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

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)

• Thank you. I didn't know that the adjoint is also the inverse for unitary matrices. – Sorin Bolos Jun 16 '19 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

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