# unitaryGate

Unitary matrix gate

Since R2023b

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

## Syntax

``cg = unitaryGate(targetQubits,U)``
``cg = unitaryGate(targetQubits,U,RotationThreshold=thresh)``

## Description

example

````cg = unitaryGate(targetQubits,U)` returns a `quantum.gate.CompositeGate` object that applies a unitary matrix to the target qubits up to a global phase, that is, scaled by a constant factor.```
````cg = unitaryGate(targetQubits,U,RotationThreshold=thresh)` also removes single-qubit rotation gates that have an angle magnitude less than the rotation threshold.```

## Examples

collapse all

Create a unitary matrix gate that applies a unitary matrix to a single qubit.

`U = (1/sqrt(3))*[1 -1+1i; 1+1i 1]`
```U = 2×2 complex 0.5774 + 0.0000i -0.5774 + 0.5774i 0.5774 + 0.5774i 0.5774 + 0.0000i ```
`cg = unitaryGate(1,U)`
```cg = CompositeGate with properties: Name: "" ControlQubits: [1×0 double] TargetQubits: 1 Gates: [3×1 quantum.gate.SimpleGate] ```

Get the matrix representation of the gate. Here, `M` is equal to `U` because the global phase is 1.

`M = getMatrix(cg)`
```M = 2×2 complex 0.5774 + 0.0000i -0.5774 + 0.5774i 0.5774 + 0.5774i 0.5774 + 0.0000i ```

Plot the returned unitary matrix gate to show its internal gates.

`plot(cg)`

Create a unitary matrix gate that applies a unitary matrix to two qubits with indices 1 and 2.

```U = [1 0 0 0; 0 1 0 0; 0 0 1i 0; 0 0 0 1i]; cg = unitaryGate(1:2,U)```
```cg = CompositeGate with properties: Name: "" ControlQubits: [1×0 double] TargetQubits: [1 2] Gates: [12×1 quantum.gate.QuantumGate] ```

Plot the unitary matrix gate. The `unitaryGate` function uses an algorithm that can represent any unitary matrix to return a unitary matrix gate, so it may not be a minimal representation of the input matrix.

`plot(cg)`

Create a unitary matrix gate that applies a random unitary matrix to three qubits with indices 1, 2, and 3.

```U = orth(rand(2^3,"like",1j)); cg = unitaryGate(1:3,U)```
```cg = CompositeGate with properties: Name: "unitary" ControlQubits: [1×0 double] TargetQubits: [1 2 3] Gates: [7×1 quantum.gate.CompositeGate] ```

Plot the unitary matrix gate.

`plot(cg)`

Calculate the global phase.

```M = getMatrix(cg); globalPhase = U(:)'*M(:)/norm(M,'fro')/norm(U,'fro')```
```globalPhase = -0.9762 - 0.2167i ```

Verify that the norm of `M - globalPhase*U` is 0, within machine precision.

`norm(M - globalPhase*U)`
```ans = 3.1653e-15 ```

## Input Arguments

collapse all

Target qubits of the gate, specified as a positive integer scalar index or vector of qubit indices.

Example: `1`

Example: `3:5`

Unitary matrix, specified as a square matrix. The matrix must be of size ${2}^{n}$, where n is the number of target qubits.

Rotation threshold, specified as one of these values:

• positive real number

• `"auto"` — Set the threshold to the default value of `2*pi*eps`.

• `"none"` — Do not remove gates.

The `unitaryGate` function removes single-qubit rotation gates that have an angle magnitude less than this rotation threshold.

collapse all

### Unitary Matrix

An invertible complex square matrix U is unitary if its conjugate transpose is also its inverse, that is, if U*U = UU* = I.

## Tips

• You can use the `unitaryGate` function to decompose any unitary matrix applied to n target qubits into a composite gate of O(4n) simple quantum gates. However, the resulting `CompositeGate` object may not contain the minimal number of gates for an input matrix.

## References

[1] Shende, Vivek V., Stephen S. Bullock, and Igor L. Markov. "Synthesis of Quantum Logic Circuits." IEEE Transactions on Computer-Aided Design of Integrated Circuits and Systems 25, no. 6 (June 2006): 1000–1010. https://doi.org/10.1109/TCAD.2005.855930.

[2] Vatan, Farrokh, and Colin Williams. "Optimal Quantum Circuits for General Two-Qubit Gates." Physical Review A 69, no. 3 (March 22, 2004): 032315. https://doi.org/10.1103/PhysRevA.69.032315.

[3] Drury, Byron, and Peter J. Love. "Constructive Quantum Shannon Decomposition from Cartan Involutions." Journal of Physics A: Mathematical and Theoretical 41, no. 39 (October 3, 2008): 395305. https://doi.org/10.1088/1751-8113/41/39/395305.

## Version History

Introduced in R2023b