# Density Matrix of 3 Qubit Quantum Circuit (Qiskit Density Matrix)

If I have a one gate circuit like in the following image:

I can calculate the density matrix by following steps:

However, I want to do it for the Toffoli gate circuit:

and I am confused with the initial density matrix. How to write an initial density matrix for 3 qubits?

I used qiskit to compute it and I used the following code:

qc_AB = QuantumCircuit(3)
qc_AB.h(2)
qc_AB.cx(1,2)
qc_AB.tdg(2)
qc_AB.cx(0,2)
qc_AB.t(2)
qc_AB.cx(1,2)
qc_AB.tdg(2)
qc_AB.cx(0,2)
qc_AB.cx(0,1)
qc_AB.h(2)
qc_AB.t(0)
qc_AB.tdg(1)
qc_AB.cx(0,1)
qc_AB.draw()
rho_AB = qi.DensityMatrix.from_instruction(qc_AB)
rho_AB.draw('latex', prefix='\\rho_{AB} = ')

According the qiskit my initial density matrix is here:

How to calculate it by hand? I am not sure this initial density matrix is correct or not...

Note that to add Toffoli gate to your circuit you can call toffoli method:

qc_AB.toffoli(0, 1, 2)
• great, thank you very much for your help and yes I guess now I found what I wanted !. I cannot use toffoli gate directly because of some experimental part of the study ...:/ Jan 4 at 12:57

"How to calculate it by hand? I am not sure this initial density matrix is correct or not..."

If you initialize your states to $$|000\rangle = |0\rangle\otimes |0\rangle \otimes|0\rangle$$, and each $$|0\rangle$$ has a vector representation (in the "computational basis") of:

$$\tag{1} |0\rangle \equiv\begin{pmatrix} 1 \\ 0 \end{pmatrix},$$

then the vector representation of your initial three qubits will be an 8-element vector where the first one is a 1 and the rest are all 0.

Now you need to do the outer product of this vector with itself. This will give you an 8x8 matrix in which the first element (row 1, column 1) is a 1 and the rest are all 0s.

I think this is even easier to verify with octave-online than with Python:

Zero = [1 ; 0];ZeroZeroZero = kron(kron(Zero,Zero),Zero)
ZeroZeroZero =

1
0
0
0
0
0
0
0

rho = ZeroZeroZero*ZeroZeroZero'
ans =

1   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0
• amazing thanks. I now know how to do it. UNfortunatel I cannot give 2 tick for different answer, the forum is not allow me to do it. thanks : )) Jan 4 at 12:58