How to output the unitary matrix for a given quantum circuit using pennylane?

The problem is described in the title. I am trying to obtain a unitary matrix for a specific quantum circuit using pennylane, but I can’t find the available qml. function to get it. Thanks for the help!

Hi @cyrie_wang! If you are using the latest GitHub version of PennyLane, installable using

pip install git+https://github.com/PennyLaneAI/pennylane.git#egg=pennylane

then you can access the new qml.transforms.get_unitary_matrix() function:

def circuit(theta):
    qml.RX(theta, wires=1)
    qml.PauliZ(wires=0)

We can use get_unitary_matrix() to generate a new function that returns the unitary matrix corresponding to the function circuit:

>>> get_matrix = get_unitary_matrix(circuit)
>>> theta = np.pi/4
>>> get_matrix(theta)
3 Likes

wonderful! thank you so much.