# inv

Inverse of quantum circuit or gate

Since R2023a

Installation Required: This functionality requires MATLAB Support Package for Quantum Computing.

## Syntax

``cinv = inv(c)``

## Description

example

````cinv = inv(c)` returns the inverse of a quantum circuit or gate `c`. The output is another circuit or gate that is the reverse operation of the original circuit or gate. The data type of `cinv` is the same as the data type of `c`.```

## Examples

collapse all

Create a quantum circuit that consists of 3 quantum gates.

```gates = [hGate(1); sGate(2); rxGate(1,pi/3)]; c = quantumCircuit(gates);```

Find the inverse of the quantum circuit. Show the gate operations of the original circuit and the inverted gate operations of the inverse circuit.

```cinv = inv(c); c.Gates ```
```ans = 3×1 SimpleGate array with gates: Id Gate Control Target Angle 1 h 1 2 s 2 3 rx 1 pi/3 ```
```cinv.Gates ```
```ans = 3×1 SimpleGate array with gates: Id Gate Control Target Angle 1 rx 1 -pi/3 2 si 2 3 h 1 ```

Here, `hGate` is its own inverse, `siGate` is the inverse of `sGate`, and the rotation angle of `rxGate` has changed sign to rotate backwards.

Construct a y-axis rotation gate with a rotation angle of π/2.

`g = ryGate(1,pi/2)`
```g = SimpleGate with properties: Type: "ry" ControlQubits: [1×0 double] TargetQubits: 1 Angles: 1.5708```

Find the inverse of the rotation gate.

`ginv = inv(g)`
```ginv = SimpleGate with properties: Type: "ry" ControlQubits: [1×0 double] TargetQubits: 1 Angles: -1.5708```

Construct a composite gate that consists of two inner gates.

```gates = [hGate(1); cxGate(1,2)]; cg = compositeGate(gates,[1 2]);```

Find the inverse of the composite gate. Show the gate operations of the original composite gate and the inverted gate operations of the inverse composite gate.

```cginv = inv(cg); cg.Gates```
```ans = 2×1 SimpleGate array with gates: Id Gate Control Target 1 h 1 2 cx 1 2```
`cginv.Gates`
```ans = 2×1 SimpleGate array with gates: Id Gate Control Target 1 cx 1 2 2 h 1 ```

Create a quantum circuit that consists of the composite gate and its inverse. Show that the operations of these gates cancel each other out and the matrix representation of this circuit is an identity matrix.

```c = quantumCircuit([cg; cginv]); M = getMatrix(c)```
```M = 1.0000 0 0 0 0 1.0000 0 0 0 0 1.0000 0 0 0 0 1.0000```

## Input Arguments

collapse all

Quantum circuit or gate, specified as a `quantumCircuit`, `SimpleGate`, or `CompositeGate` object.

• If `c` is a `quantumCircuit` or `CompositeGate` object, then `inv` reverses the order of the original gates in `c` and replaces each gate with its inverse.

• If `c` is a `SimpleGate` object, then `inv` replaces the gate with its inverse.

## Version History

Introduced in R2023a