# Printing the matrix of a circuit during optimization

**URL:** https://discuss.pennylane.ai/t/printing-the-matrix-of-a-circuit-during-optimization/758
**Category:** PennyLane Help
**Created:** [January 8, 2021, 6:40pm UTC](https://discuss.pennylane.ai/t/printing-the-matrix-of-a-circuit-during-optimization/758 "2021-01-08T18:40:29Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![andrew1](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/andrew1/32/528_2.png) [@andrew1](https://discuss.pennylane.ai/u/andrew1)
#### Post date: [January 8, 2021, 6:40pm UTC](https://discuss.pennylane.ai/t/printing-the-matrix-of-a-circuit-during-optimization/758/1 "2021-01-08T18:40:29Z")

</div>

Let’s say I have a circuit like this:

```
@qml.qnode(dev)
def circuit(weights):
    qml.RZ(weights, wires=0)
    return qml.expval(qml.PauliZ(0))

```

I can print the circuit diagram like this at each step of optimization:

```
print(circuit.draw())

```

But I would like the print the unitary matrix corresponding to this circuit. Is there any way I can achieve this?

---

<div class="post-metadata">

### Author: ![Tom\_Bromley](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/tom_bromley/32/129_2.png) [@Tom\_Bromley](https://discuss.pennylane.ai/u/Tom_Bromley)
#### Post date: [January 8, 2021, 9:46pm UTC](https://discuss.pennylane.ai/t/printing-the-matrix-of-a-circuit-during-optimization/758/2 "2021-01-08T21:46:04Z")

</div>

Hi @andrew1, welcome to the forum!

Good question! This isn’t something that’s done by default in PennyLane due to the potential overhead of calculating and storing the unitary as the number of qubits increases. However, it’s possible to put together some code to recover the unitary, for example:

```python
import pennylane as qml
from pennylane import numpy as np

qml.enable_tape()

wires = 2
dev = qml.device("default.qubit", wires=wires)

def template(x):
    qml.RX(x, wires=0)
    qml.RY(0.3, wires=1)
    qml.CNOT(wires=[0, 1])

def find_unitary(template, *args, **kwargs):

    @qml.qnode(dev)
    def find_output_state(input_state):
        qml.QubitStateVector(input_state, wires=range(wires))
        template(*args, **kwargs)
        return qml.state()
    
    input_states = np.eye(2 ** wires)
    
    return np.stack([find_output_state(state) for state in input_states]).T

U = find_unitary(template, 0.4)

print(U)

```

In the example above, you can update your circuit by changing the contents of `template()` (and the value of `wires` accordingly).

---

<div class="post-metadata">

### Author: ![andrew1](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/andrew1/32/528_2.png) [@andrew1](https://discuss.pennylane.ai/u/andrew1)
#### Post date: [January 9, 2021, 6:17pm UTC](https://discuss.pennylane.ai/t/printing-the-matrix-of-a-circuit-during-optimization/758/3 "2021-01-09T18:17:14Z")

</div>

@Tom_Bromley Thank you! This works when I use the “default.qubit” device, but what if I wanted to use the “default.mixed” device? In this case, it returns an array with shape (32, 32, 32). (where I have wires=5).

---

<div class="post-metadata">

### Author: ![Nicolas\_Quesada](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nicolas_quesada/32/118_2.png) [@Nicolas\_Quesada](https://discuss.pennylane.ai/u/Nicolas_Quesada)
#### Post date: [January 11, 2021, 3:02pm UTC](https://discuss.pennylane.ai/t/printing-the-matrix-of-a-circuit-during-optimization/758/4 "2021-01-11T15:02:57Z")

</div>

Hi @andrew1 — If you are interested in getting the unitary of a circuit then you are automatically thinking about pure unitary evolution, thus you don’t need to use the mixed state simulator.

---

<div class="post-metadata">

### Author: ![andrew1](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/andrew1/32/528_2.png) [@andrew1](https://discuss.pennylane.ai/u/andrew1)
#### Post date: [January 11, 2021, 6:05pm UTC](https://discuss.pennylane.ai/t/printing-the-matrix-of-a-circuit-during-optimization/758/5 "2021-01-11T18:05:58Z")

</div>

@Nicolas_Quesada right you are… thanks!

---

<div class="post-metadata">

### Author: ![Tom\_Bromley](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/tom_bromley/32/129_2.png) [@Tom\_Bromley](https://discuss.pennylane.ai/u/Tom_Bromley)
#### Post date: [January 11, 2021, 6:34pm UTC](https://discuss.pennylane.ai/t/printing-the-matrix-of-a-circuit-during-optimization/758/6 "2021-01-11T18:34:25Z")

</div>

@andrew1, please let us know if there’s anything else we can help with!
