Skip to content
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

Merged
merged 8 commits into from
May 13, 2020

Conversation

ajavadia
Copy link
Member

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).

Copy link
Member

@chriseclectic chriseclectic left a 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.

@ajavadia ajavadia force-pushed the density-matrix-to-statevector branch from 53dd9f5 to a9d5f1a Compare May 12, 2020 18:50
@ajavadia
Copy link
Member Author

@chriseclectic @jaygambetta addressed your comments.
Chris I restricted it to trace-1 density matrices for now as that is the is_valid definition currently.

@mergify mergify bot merged commit 948671e into Qiskit:master May 13, 2020
@ajavadia ajavadia added the Changelog: New Feature Include in the "Added" section of the changelog label Aug 3, 2020
faisaldebouni pushed a commit to faisaldebouni/qiskit-terra that referenced this pull request Aug 5, 2020
* add DensityMatrix.to_statevector()

* lint

* updates

* review

* nit

* docs

* ..

Co-authored-by: Christopher J. Wood <cjwood@us.ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants