# Implementation of tomography on IBM Q

I wanted to ask how do you implement a circuit that finds the non-diagonal values of the density matrix of a quantum state on IBM Q?

Basically you add measurements in different bases by applying gates before the (Z-basis) measurement.

See here the standard implementation:

https://github.com/Qiskit/qiskit-ignis/blob/3c59f82c11e87c071bc7e84240b50e2aa995281f/qiskit/ignis/verification/tomography/basis/paulibasis.py#L31

Density matrix of single qubit state can be estimated based on this formula

$$$$\rho = \frac{\text{tr}(\rho)I+\text{tr}(X\rho)X+\text{tr}(Y\rho)Y+\text{tr}(Z\rho)Z}{2},$$$$

where $$X$$, $$Y$$, $$Z$$ are Pauli matrices.

Obviously $$\mathrm{tr}(\rho) = 1$$.

Terms $$\mathrm{tr}(X\rho)$$, $$\mathrm{tr}(Y\rho)$$ and $$\mathrm{tr}(Z\rho)$$ can be estimated by measuring a quantum state in different bases:

• In $$z$$ basis you simply measure the state
• In $$x$$ basis Hadamard gate has to be applied before measurement
• In $$y$$ basis $$S^\dagger$$ gate followed by Hadamard gate have to be applied before measurement

Value of $$\mathrm{tr}(A\rho)$$, where $$A \in \{X,Y,Z\}$$, is given by

$$\mathrm{tr}(A\rho) = \frac{1}{m}\sum_{i=1}^{m}\lambda_{i},$$

where $$m$$ is number of measurements (i.e. shots on IBM Q) and $$\lambda_{i}$$ is eigenvalue respective to measured state. Since Pauli matrices are Hermitian and unitary, their eigenvalues are -1 and +1. Moreover, eigenvalue -1 is assigned to such eigenstate that after measurement it is mapped to state $$|1\rangle$$, eigenvalue +1 is mapped to state $$|0\rangle$$.

Note: You can check this if you calculate eigenvectors and eigenvalues of Pauli matrices and then apply above mentioned gates. Eigenstates should be mapped to $$|0\rangle$$ and $$|1\rangle$$.

For practical purposes formula for calculation $$\mathrm{tr}(A\rho)$$ can be rewritten in terms of measured states probability distribution followingly

$$\mathrm{tr}(A\rho) = P(|0\rangle) - P(|1\rangle),$$

because +1 is equivalent to measuring $$|0\rangle$$, hence $$\frac{1}{m}\sum_{\lambda = 1}\lambda_i = \frac{\#(\lambda = 1)}{m} = P(\lambda = 1) = P(|0\rangle)$$. Similarly for eigenvalue -1.

Note: probabilities are expressed as decimal number, not percentage!

To sum up how to do quantum tomography on IBM Q:

1. Prepare a qubit in some state
2. Measure it in $$x$$, $$y$$ and $$z$$ bases
3. Use probabilities of measuring $$|0\rangle$$ and $$|1\rangle$$ for estimation of $$\mathrm{tr}(A\rho)$$
4. Calculate density matrix $$\rho$$ (the first formula in this answer)