-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add DensityMatrix.to_statevector() for pure states #4433
add DensityMatrix.to_statevector() for pure states #4433
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should take an atol
value for comparing small eigenvalues close to zero. You could also check that the current density matrix is hermitian before computing the eigen decomposition since you can fail early if its not (DensityMatrix objects could be in an "invalid" quantum state before calling this function).
After this as Jay says you should check the number of eigenvalues rather than the purity. I would check that there is only 1 non-zero eigenvalue up to the absolute tolerance. Something like this:
def to_statevector(self, atol=None, rtol=None):
if atol is None:
atol = self.atol
if rtol is None:
rtol = self.rtol
if not is_hermitian_matrix(self._data, atol=atol, rtol=rtol):
raise QiskitError("Not a valid density matrix")
evals, evecs = np.linalg.eigh(self._data)
if len(evals[abs(evals) > atol]) != 1:
raise QiskitError("Density matrix is not a pure state")
return Statevector(np.sqrt(np.max(evals)) * evals[:, np.argmax(evals)])
The last line allows for the case where the density matrix is a rank-1 projector, but not normalized to trace 1. It can still be converted into a vector, but that vector will not be normalized.
53dd9f5
to
a9d5f1a
Compare
@chriseclectic @jaygambetta addressed your comments. |
* add DensityMatrix.to_statevector() * lint * updates * review * nit * docs * .. Co-authored-by: Christopher J. Wood <cjwood@us.ibm.com>
This is handy when you have a circuit in a product state and you want the reduced state as a statevector (e.g. discarding an uncomputed ancilla).