# Train a diagonal unitary matrix？

**URL:** https://discuss.pennylane.ai/t/train-a-diagonal-unitary-matrix/3524
**Category:** PennyLane Help
**Created:** [October 8, 2023, 12:06am UTC](https://discuss.pennylane.ai/t/train-a-diagonal-unitary-matrix/3524 "2023-10-08T00:06:32Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![RX1](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/rx1/32/698_2.png) [@RX1](https://discuss.pennylane.ai/u/RX1)
#### Post date: [October 8, 2023, 12:06am UTC](https://discuss.pennylane.ai/t/train-a-diagonal-unitary-matrix/3524/1 "2023-10-08T00:06:32Z")

</div>

\left[\begin{array}{\*{35}{l}} 1 & {} & {} & {} & {} & {} & {} & {} \\ {} & -1 & {} & {} & {} & {} & {} & {} \\ {} & {} & 1 & {} & {} & {} & {} & {} \\ {} & {} & {} & 1 & {} & {} & {} & {} \\ {} & {} & {} & {} & -1 & {} & {} & {} \\ {} & {} & {} & {} & {} & 1 & {} & {} \\ {} & {} & {} & {} & {} & {} & -1 & {} \\ {} & {} & {} & {} & {} & {} & {} & 1 \\ \end{array} \right]  
is the initial diagonalunitary matrix. \left[\begin{array}{\*{35}{l}} 1 & {} & {} & {} & {} & {} & {} & {} \\ {} & -1 & {} & {} & {} & {} & {} & {} \\ {} & {} & -1 & {} & {} & {} & {} & {} \\ {} & {} & {} & -1 & {} & {} & {} & {} \\ {} & {} & {} & {} & -1 & {} & {} & {} \\ {} & {} & {} & {} & {} & 1 & {} & {} \\ {} & {} & {} & {} & {} & {} & -1 & {} \\ {} & {} & {} & {} & {} & {} & {} & 1 \\ \end{array} \right]  
is the target diagonalunitary matrix. How can we ensure that each iteration produces a diagonalunitary matrix? End up with \left[\begin{array}{\*{35}{l}} 1 & {} & {} & {} & {} & {} & {} & {} \\ {} & -1 & {} & {} & {} & {} & {} & {} \\ {} & {} & -1 & {} & {} & {} & {} & {} \\ {} & {} & {} & -1 & {} & {} & {} & {} \\ {} & {} & {} & {} & -1 & {} & {} & {} \\ {} & {} & {} & {} & {} & 1 & {} & {} \\ {} & {} & {} & {} & {} & {} & -1 & {} \\ {} & {} & {} & {} & {} & {} & {} & 1 \\ \end{array} \right]. That is, the output of each iteration step can be obtained as a diagonal unitary matrix according to the parameters. Is this trainable? Please show Dome.

---

<div class="post-metadata">

### Author: ![Guillermo\_Alonso](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/guillermo_alonso/32/816_2.png) [@Guillermo\_Alonso](https://discuss.pennylane.ai/u/Guillermo_Alonso)
#### Post date: [October 10, 2023, 12:30pm UTC](https://discuss.pennylane.ai/t/train-a-diagonal-unitary-matrix/3524/2 "2023-10-10T12:30:47Z")

</div>

I leave here the reference to the other conversation 🙂

> [@qml.DiagonalQubitUnitary and qml.QubitUnitary cannot be trained?](https://discuss.pennylane.ai/t/qml-diagonalqubitunitary-and-qml-qubitunitary-cannot-be-trained/3521):
>
> [https://docs.pennylane.ai/en/stable/code/api/pennylane.DiagonalQubitUnitary.html](https://docs.pennylane.ai/en/stable/code/api/pennylane.DiagonalQubitUnitary.html) While checking the documentation, I found that DiagonalQubitUnitary doesn’t even have a gradient recipe? How can I make a custom Unitary matrix with variable parameters for training? def my\_func(feat, para): qml.MottonenStatePreparation(state\_vector=feat, wires=wires) diag = [] for i in range(dim1): diag.append(np.sign(para[i])) #print(len(diag)) diag = np.array(diag, requires\_grad=True…
