# How do you compute the compiled unitary of a quantum circuit comprised of different $n$-input gates?

Given a quantum circuit consisting of two qubits, how is the compiled unitary of the circuit computed when we have different input type gates? (X-gate, H-gate are single-input gates, CNOT is a 2-input gate).

qc = QuantumCircuit(2)
qc.x(1)
qc.h(0)
qc.cnot(0,1)
usim = Aer.get_backend('unitary_simulator')
qobj = assemble(qc)
unitary = usim.run(qobj).result().get_unitary()
array_to_latex(unitary, pretext="\\text{Circuit = } ")


Section 2.2 of Qiskit shows how the simultaneous operations of two single-input gates are represented by computing the tensor product of the two gates, and that when we apply a gate to one bit at a time, we represent the unitary as the tensor product with the identity matrix. For the circuit above, how do we compile the circuit's unitary for the $$4\times 4$$ CNOT matrix,

$$\text{CNOT} = \begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 1\\ 0 & 0 & 1 & 0\\ 0 & 1 & 0 & 0\\ \end{pmatrix},$$

and the $$2\times 2$$ $$X$$ and $$H$$ matrices?

Your circuit is looking like this: $$U = CNOT \cdot \big( H \otimes X \big)$$

where

\begin{align} H \otimes X &= \dfrac{1}{\sqrt{2}} \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} \otimes \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} = \dfrac{1}{\sqrt{2}} \begin{pmatrix} 1 \cdot \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} & 1 \cdot \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} \\ 1 \cdot \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} & -1 \cdot \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} \end{pmatrix} \\ &= \dfrac{1}{\sqrt{2}} \begin{pmatrix} 0 & 1 & 0 & 1 \\ 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & -1\\ 1 & 0 & -1 & 0 \\ \end{pmatrix} \end{align}

and CNOT (textbook convention) is

$$CNOT = \begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \\ \end{pmatrix}$$

If you us little endian (like qiskit does) then instead of $$H \otimes X$$, you have to do $$X \otimes H$$. You have to read the circuit in reverse.

$$U = CNOT \cdot \big( X \otimes H \big)$$

so here you have

\begin{align} X \otimes H &= \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} \otimes \dfrac{1}{\sqrt{2}} \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} = \begin{pmatrix} 0 \cdot \dfrac{1}{\sqrt{2}} \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} & 1 \cdot \dfrac{1}{\sqrt{2}} \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} \\ 1 \cdot \dfrac{1}{\sqrt{2}} \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} & 0 \cdot \dfrac{1}{\sqrt{2}} \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} \end{pmatrix} \\ &= \dfrac{1}{\sqrt{2}} \begin{pmatrix} 0 & 0 & 1 & 1 \\ 0 & 0 & 1 & -1 \\ 1 & 1 & 0 & 0 \\ 1 & -1 & 0 & 0 \\ \end{pmatrix} \end{align}

and in this convention, we have

$$\text{CNOT} = \begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 1\\ 0 & 0 & 1 & 0\\ 0 & 1 & 0 & 0\\ \end{pmatrix},$$

• I think the question has $H$ on $q_0$ and $X$ on $q_1$? May 7, 2021 at 20:22
• @AdamZalcman Thanks! :) I didn't read the circuit correctly at fist. May 7, 2021 at 20:34
• Thank you so much!!!:) Learning Qiskit and trying out the exercises. Yes, because it is little endian, qc.x(1), qc.h(0) corresponds to the gate matrix tensor product in (X⊗H)|q1q0>. Then, because the control quit is q0 when applying CNOT qc.cnot(0,1), we would use the CNOT matrix stated in the question otherwise the textbook convention one stated in your answer. The unitary is then U = CNOT•(X⊗H), where • is matrix multiplication. The circuit generates the bell state 1/√2(|01> + |10>). If we test the product of U with |00>, we get the corresponding state vector for the entangled state. May 7, 2021 at 22:27