# How do I write a tensor product of conditional gates in matrix form?

I am writing a program where I need to find the eigenstates of an operator that is a Kronecker product of conditional quantum gates. I am wondering how I would compute this product in matrix form as the conditional gates act on different qubits.

For example: Say I want to find the Kronecker product of some Pauli-X gate X_k that acts on qubit k and conditional phase gates CZ_i,j that have a control qubit i and target j:

X_1 ⊗ CZ_2,3 ⊗ CZ_3,4 ⊗ CZ_2,4


How would I compute this product so the result is a matrix? (Or in some form where I can find the eigenstates) For context, I am using Python and NumPy.

• Your indices overlap. Should make disjoint or clarify the order of terms. – AHusain Jun 11 '19 at 22:20
• @AHusain: actually, the overlapping terms happen to commute. Still, the question shows a bit of confusion about what the tensor product means. Perhaps the OP would benefit from a solution to a simpler example. – Niel de Beaudrap Jun 12 '19 at 0:13
• @NieldeBeaudrap Yes the overlapping terms are correct and thank you for the reply. Would you care to elaborate? How I understand it, in a tensor product the first term acts on the first qubit, the second term acts on the second qubit and so forth. My lack on understanding is clear for I am unsure of how to compute the tensor product when gates act on multiple qubits. Can you clarify this? – meelszz Jun 12 '19 at 0:25
• @NieldeBeaudrap so they do. Could still make disjoint so that this question can address how to compute tensor product using Python and Numpy. Could address the underlying confusion of what tensor product means separately. – AHusain Jun 12 '19 at 0:27
• @AHusain Ahh I see. So I must turn the conditional phase gates into disjoint single qubit operators and compute from there? – meelszz Jun 12 '19 at 0:32

You can always write your operation as one gate at a time, and take the product. So, in your case, you'd be calculating a product $$U=U_1U_2U_3U_4$$ where, for example, $$U_1=X_1=X\otimes 1\otimes 1\otimes 1$$, $$U_2=CZ_{2,3}=I\otimes I\otimes I\otimes I-I\otimes |1\rangle\langle 1|\otimes(Z-I)\otimes I,$$ and so on. In particular, you can start to use the formulation I've given you for $$U_2$$ to work out how to apply CZ on any pair of qubits. For instance, $$U_4=CP_{2,4}=I\otimes I\otimes I\otimes I-I\otimes |1\rangle\langle 1|\otimes I\otimes(Z-I).$$

pauli_X=[[0,1],[1,0]] cz=[[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,-1]] np.kron(np.kron(np.kron(pauli_X,cz),cz),cz)

This would compute $$X_1 \otimes CZ_{2,3} \otimes CZ_{4,5} \otimes CZ_{6,7}$$.

Addressing the confusion about what tensor product means. $$A$$ operating on $$d_1$$ qubits $$(\mathbb{C}^2)^{\otimes d_1}$$ and $$B$$ operating on $$d_2$$ qubits $$(\mathbb{C}^2)^{\otimes d_2}$$ means that $$A \otimes B$$ will operate on $$(\mathbb{C}^2)^{\otimes d_1} \otimes (\mathbb{C}^2)^{\otimes d_2}$$ which is $$d_1 + d_2$$ qubits. No overlapping indices between the $$d_1$$ and the $$d_2$$.

In this case there are 7 qubits so the total Hilbert space is $$(\mathbb{C}^2)^{\otimes 7} \simeq \mathbb{C}^{2^7}$$ with basis indexed by the $$2^7$$ bit strings of length 7. From 0000000, 0000001, 0000010, etc all the way to 1111111.