# Qiskit state_fidelity not accepting my Density Matrices

I'm attempting to use qiskit's state_fidelity(state1, state2, validate=True) but keep getting the following error: QiskitError: 'Input quantum state is not a valid'

In order to use this form I had already read the documentation to see that state1 and state2 must be either density matrices or statevectors.

As a result I used the qiskit.quantum_info module to generate a density matrix for state1.

For state2 I created the array using numpy importing numbers from state tomography. (Shown below)

#Turn this into a the density matrix
state2_matrix = (1/2)*np.array([[1+expectation_vals, expectation_vals-1j*expectation_vals], [expectation_vals+1j*expectation_vals, 1-expectation_vals]])
state2=DensityMatrix(state2_matrix)


where expectation_vals=[0.004, 1.0, -0.04] comes from my single qubit tomography.

Edit This matrix, despite now being created using the Density Matrix class, is still giving me the error that the Input quantum state is not valid. This leads me to believe that there is some property of density matrices that is not being fulfilled by this matrix.
$$\begin{bmatrix} 0.502 & \tfrac{1}{2} + 0.02i \\ \tfrac{1}{2} - 0.02i & 0.498 \\ \end{bmatrix}$$

I can see it has trace=1, and I found the eigenvalues, using numpy, to be (array([ 1.00040384e+00+0.j, -4.03836916e-04+0.j]).

I now assume that this second eigenvalue is the reason this is not working since Density matrices must be positive semidefinite. If this is the reason state_fidelity() is not working, then how can this be avoided when using tomography on a single qubit?

Edit 2.0 In the end I was able to leave validate = True, but I had to renormalize my expectation values in order to do so. (Pure State)

Thank you all for your help!

When validate=True, the first thing that the state_fidelity function does is checking whether both state1 and state2 are valid quantum states. In particular:

• if you pass a statevector $$| \psi \rangle$$, it checks that $$\langle \psi | \psi \rangle = 1$$ (unit norm)
• if you pass a density matrix $$\rho$$, it checks that $$tr(\rho) = 1$$ and that $$\rho$$ is positive semidefinite

As you already figured out, in your case, state2 is a DensityMatrix instance but it's not positive semidefinite and that's why state_fidelity fails. However, you can compute the fidelity with no errors setting validate=False. Qiskit allows this because, in some cases like maybe yours, you may want to get the fidelity between objects that are not necessarily valid quantum states (e.g. affected by some form of noise).

EDIT: also, note that state1 and state2 can simply be numpy arrays or Python lists. So, creating the DensityMatrix from your array is an unnecessary step because the conversion occurs anyway inside the state_fidelity function.

• This worked and I was able to use the fidelity. I still need to figure out why I have an impossible density matrix, but at least me code is running again! Jan 16 at 16:24

You have to pass the np.array as a parameter to the densityMatrix constructor:

import qiskit.quantum_info as qi
state2_matrix = (1/2)*np.array([[1+expectation_vals, expectation_vals-1j*expectation_vals], [expectation_vals+1j*expectation_vals, 1-expectation_vals]])
state2 = qi.DensityMatrix(state2_matrix)

• Tried this tdoay. This was exactly the answer to the question I thought I was asking, but I still get the same error. Something about my expectation values is perhaps preventing it from being a valid density operator. I will edit my question accordingly. Jan 16 at 14:34