2
$\begingroup$

I am trying to wrap my head around he quantum_info module on qiskit, since most of the functions on qiskit.tools are going to be deprecated, but I am very confused with the Statevector and DensityMatrix objects. For example, with this snippet of code:

simulator = BasicAer.get_backend('statevector_simulator') # the device to run on
result6 = execute(circuit6, simulator).result()
outputstate6 = result6.get_statevector(circuit6, decimals=3)
probability = np.abs(np.array(outputstate6))**2
outstatevector=quantum_info.states.Statevector(outputstate6)
print(type(outstatevector))
print(type(outputstate6))
print(outputstate6)
print(quantum_info.entropy(outputstate6))

I get:

<class 'qiskit.quantum_info.states.statevector.Statevector'>
<class 'numpy.ndarray'>
[0.447+0.j 0.   +0.j 0.632+0.j 0.632+0.j]

as expected but then I get the error on quantum_info.entropy(outputstate6):

QiskitError: 'Input quantum state is not a valid'

How can I solve this?

$\endgroup$
  • $\begingroup$ Is it because you're giving it quantum_info.entropy(outputstate6), rather than quantum_info.entropy(outstatevector)? It looks like there's a check to see if it's a statevector rather than a numpy array - qiskit.org/documentation/_modules/qiskit/quantum_info/states/… $\endgroup$ – snulty Mar 8 at 15:19
  • $\begingroup$ I tried with the outstatevector and I still get the same error, that was the motivation to post this question here. I honestly don't know what is wrong. The same happened with the density matrix methods, some of them worked, some of them didn't $\endgroup$ – Bidon Mar 8 at 15:24
2
$\begingroup$

Short answer

You are getting that error because your example does not use a valid (normalized) statevector. If you remove the decimals=3 kwarg where you call result.get_statevector it will work.

Long Answer

The Von-Neuman entropy function in the qiskit.quantum_info works with either Statevector or DensityMatrix object inputs, or inputs that can be implicitly converted to those objects (ie a list or np.array for a vector or a square matrix). So you can do any of the following for example:

import numpy as np
from qiskit.quantum_info import entropy, Statevector, DensityMatrix

# Pure state entropy (Note this is always 0)
# The following are equivalent:

s1 = entropy([1, 0, 0, 0])  # Statevector as list
s2 = entropy(np.array([1, 0, 0, 0])) # Statevector as array
s3 = entropy(Statevector([1, 0, 0, 0]))  # Statevector object

print(s1, s2, s3)

# Mixed state entropy
# The following are equivalent

s1 = entropy([[0.75, 0], [0, 0.25]])  # Density matrix as list
s2 = entropy(np.array([[0.75, 0], [0, 0.25]])) # density matrix as array
s3 = entropy(DensityMatrix([[0.75, 0], [0, 0.25]]))  # Density matrix object

print(s1, s2, s3)

As for your specific issue: the entropy is only well-defined for a valid quantum state, so the function checks the input state is valid. In the case of a statevector this is checking it has norm 1, in the case of a density matrix that it is trace 1 and postive-semidefinite.

Your example does not have a norm-1 input state because you truncated the decimals when you got the output state from the simulator. You can check this using the statevector object for example:

# Returns True:
Statevector([1 / np.sqrt(2), 1 / np.sqrt(2)]).is_valid()

# Returns False:
Statevector([0.707, 0.707]).is_valid()

Entropy base: Another thing you should keep in mind if you didn't already notice is that the qiskit.quantum_info.entropy function takes logarithms in base 2 by default (and you can use a different base using the base kwarg). The deprecated qiskit.tools.qi.entropy function was always taken in log base e:

import numpy as np
from qiskit.quantum_info import entropy
from qiskit.tools.qi import entropy as old_entropy

rho = [[0.75, 0], [0, 0.25]]

s1 = entropy(rho)  # base-2
s2 = entropy(rho, base=np.e)  # base-e
s3 = old_entropy(rho)  # base-e

print(s1, s2, s3, s2 == s3)
```
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.