# How to create a unitary matrix from a circuit

I've been trying to find a way to analytically find the unitary matrix of a circuit, but I cant find the resources to do so. How can I do so?

• mathematically, or programmatically? If the latter, using which package? Apr 7, 2023 at 16:30
• Mathematically. Apr 7, 2023 at 16:57

Suppose a quantum circuit of $$n$$ qubits has $$g$$ unitary gates. Label these gates by $$U_1, U_2, \dots, U_g$$, in the order they occur in the circuit (if some gates occur simultaneously, it doesn't matter how you order those ones). Each of these gates has a representation as a $$2^n \times 2^n$$ unitary. To get $$U$$, just multiply these in reverse order: $$U = U_g U_{g-1} \dots U_1$$. The reversal comes from the way functions are ordered in standard math notation.
So how do you get the matrices $$U_i$$ in the first place? If it's a single-qubit gate, you could take the tensor product of the matrix for the single qubit with the identity on the rest of them, minding the ordering. Or, for any gate you could looking at how it acts on computational basis states.