> ## Documentation Index
> Fetch the complete documentation index at: https://prod-mint.classiq.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Invert

The *invert* statement applies the adjoint (conjugate transpose) of the unitary operation
specified as a nested statement block. If the nested block specifies the unitary operation
$U$, *invert* applies $U^{\dagger}$.

## Syntax

<Tabs>
  <Tab title="Python">
    [comment]: DO_NOT_TEST

    ```python theme={null}
    def invert(stmt_block: QCallable) -> None:
        pass
    ```
  </Tab>

  <Tab title="Native">
    **invert** **\{** *statements* **}**
  </Tab>
</Tabs>

## Semantics

* The *invert* statement applies the adjoint of the operations specified in the nested
  block, equivalent to the adjoint of each nested statement in reverse order.
* Quantum variables declared outside the *invert* statement and used inside its nested
  block must be initialized prior to it and remain initialized subsequently.
* Quantum variables declared inside the *invert* statement, including in
  nested function calls, must be uninitialized at the end of the statement.

## Example

The following example demonstrates the use of `invert` applied to a single gate-level
function call, and to a statement block in which a user-defined function `foo` is called twice.

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    from classiq.qmod.symbolic import pi
    from classiq import *


    @qfunc
    def foo(target: QBit):
        H(target)
        X(target)


    @qfunc
    def main(qba: Output[QArray[QBit]]):
        allocate(2, qba)
        invert(lambda: RX(pi / 2, qba[0]))
        invert(lambda: [foo(qba[0]), foo(qba[1])])
    ```
  </Tab>

  <Tab title="Native">
    ```
    qfunc foo(target: qbit) {
      H(target);
      X(target);
    }

    qfunc main(output qba: qbit[]) {
      allocate(2, qba);
      invert {
        RX(pi / 2, qba[0]);
      }
      invert {
        foo(qba[0]);
        foo(qba[1]);
      }
    }
    ```
  </Tab>
</Tabs>

Synthesizing this model creates the quantum program shown below. The inversion of
`RX` is simply negating the rotation angle. In the second `invert` statement each call to
`foo` is inverted, applying the gate-level functions in reverse but unchanged (as they
are hermitian).

<img src="https://mintcdn.com/classiq/sA-J-h8chQJV9OAG/qmod-reference/language-reference/statements/resources/invert.png?fit=max&auto=format&n=sA-J-h8chQJV9OAG&q=85&s=35dfa134df634292d0b622bed68ace56" alt="invert.png" width="728" height="144" data-path="qmod-reference/language-reference/statements/resources/invert.png" />
