Skip to content

Feature/bures distance [unitaryHACK]#60

Merged
vprusso merged 20 commits intovprusso:masterfrom
victor-onofre:feature/bures_distance
May 17, 2021
Merged

Feature/bures distance [unitaryHACK]#60
vprusso merged 20 commits intovprusso:masterfrom
victor-onofre:feature/bures_distance

Conversation

@victor-onofre
Copy link
Copy Markdown
Contributor

Description

This function takes as input two density matrices and performs the following calculation:

from toqito.state_metrics import fidelity

np.sqrt(2.0 * (1.0 - fidelity(rho_1, rho_2)))

where rho_1 and rho_2 are density operators and where the fidelity function comes from toqito.state_metrics.fidelity.

Todos

  • Compute the bures distance
  • When rho_1 = rho_2, fidelity(rho_1, rho_2) = 1.0000000000000002. This will create an error in the sqrt. I added the round function to the 10 decimal to avoid it.
  • I added the tests for the function in tests/test_state_metrics/test_bures_distance.py
  • I added from toqito.state_metrics.bures_distance import bures_distance to toqito/state_metrics/__init__.py

Questions

  • Perhaps add something in the fidelity function for the case rho_1 = rho_2 and avoid the numeric error 1.0000000000000002. That 2 may cause problems somewhere else.

Status

  • Ready to go

@codecov
Copy link
Copy Markdown

codecov bot commented May 15, 2021

Codecov Report

Merging #60 (937018e) into master (57bd2ef) will increase coverage by 0.3%.
The diff coverage is 100.0%.

Impacted file tree graph

@@           Coverage Diff            @@
##           master     #60     +/-   ##
========================================
+ Coverage    98.3%   98.7%   +0.3%     
========================================
  Files         116     117      +1     
  Lines        2142    2159     +17     
  Branches      494     499      +5     
========================================
+ Hits         2107    2131     +24     
+ Misses         18      12      -6     
+ Partials       17      16      -1     
Impacted Files Coverage Δ
toqito/state_metrics/bures_distance.py 100.0% <100.0%> (ø)
toqito/state_ops/schmidt_decomposition.py 92.5% <0.0%> (+7.7%) ⬆️
toqito/state_props/is_product_vector.py 96.9% <0.0%> (+9.0%) ⬆️
toqito/state_props/schmidt_rank.py 96.6% <0.0%> (+12.0%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 57bd2ef...937018e. Read the comment docs.

Comment thread toqito/state_metrics/bures_distance.py Outdated
if round_fidelity > 1 :
raise ValueError("Numeric Error: fidelity(rho_1, rho_2) greater than 1")

distance = np.sqrt(2.0 * (1.0 - round_fidelity))
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can simplify these lines by just returning the calculation--that is:

return np.sqrt(2.0 * (1.0 - round_fidelity))

Comment thread toqito/state_metrics/bures_distance.py Outdated
raise ValueError("InvalidDim: `rho_1` and `rho_2` must be matrices of the same size.")

# Round fidelity to only 10 decimals to avoid error when rho_1 = rho_2
round_fidelity = round(fidelity(rho_1, rho_2),10)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space between , and 10.

Comment thread toqito/state_metrics/bures_distance.py Outdated
Calculate the bures distance between the two density matrices : rho_1 and rho_2, defined
by:

D = sqrt(2 * (1 - fidelity(rho_1, rho_2) ) )
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, this should be formatted using LaTeX such as:

.. math::
    \sqrt{2 (1 - F(\rho_1, \rho_2)}

where :math:F(\cdot) is the fidelity function and where :math:\rho_1 and :math:\rho_2 are density operators.

Comment thread toqito/state_metrics/bures_distance.py Outdated

def bures_distance(rho_1: np.ndarray, rho_2: np.ndarray ) -> float:
r"""
Calculate the bures distance between the two density matrices : rho_1 and rho_2, defined
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of the lines the liner is complaining about being too long I think.

@@ -0,0 +1,61 @@
"""Tests for bures distance."""
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bures distance -> bures_distance.

Comment thread toqito/state_metrics/bures_distance.py Outdated

from toqito.state_metrics import fidelity

def bures_distance(rho_1: np.ndarray, rho_2: np.ndarray ) -> float:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space between np.ndarray and ).

Comment thread toqito/state_metrics/bures_distance.py Outdated

def bures_distance(rho_1: np.ndarray, rho_2: np.ndarray ) -> float:
r"""
Compute the bures distance of two density matrices [WikFid]_.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bures -> Bures.

Comment thread toqito/state_metrics/bures_distance.py Outdated

def bures_distance(rho_1: np.ndarray, rho_2: np.ndarray ) -> float:
r"""
Compute the bures distance of two density matrices [WikFid]_.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[WikFid]_ should probably have something different like [WikBures]_.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm so sorry. I used the fidelity function as a template. I didn't notice the error. Next time I will write the docs from scratch to avoid this type of mistake.

Comment thread toqito/state_metrics/bures_distance.py Outdated
r"""
Compute the bures distance of two density matrices [WikFid]_.

Calculate the bures distance between the two density matrices:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bures -> Bures.

Comment thread toqito/state_metrics/bures_distance.py Outdated
Compute the bures distance of two density matrices [WikFid]_.

Calculate the bures distance between the two density matrices:
`rho_1` and `rho_2`, defined by:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be ":code:`rho_1`" and ":code:`rho_2`".

Comment thread toqito/state_metrics/bures_distance.py Outdated

:param rho_1: Density operator.
:param rho_2: Density operator.
:return: The Bures distance between :code:`rho_1` and :code:`sigma_2`.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Bures distance between :code:`rho_1` and :code:`rho_2`, right?

Comment thread toqito/state_metrics/bures_distance.py Outdated
:return: The Bures distance between :code:`rho_1` and :code:`sigma_2`.
"""
# Perform some error checking.
if not np.all(rho_1.shape == (rho_2.shape)):
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant parens around (rho_2.shape)?

Comment thread toqito/state_metrics/bures_distance.py Outdated
if not np.all(rho_1.shape == (rho_2.shape)):
raise ValueError("InvalidDim: `rho_1` and `rho_2` must be matrices of the same size.")
# Round fidelity to only 10 decimals to avoid error when rho_1 = rho_2
return np.sqrt(2.0 * (1.0 - round(fidelity(rho_1, rho_2), 10) ))
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using np.around() as opposed to round(). Also, should use an optional parameter for decimal precision instead of something hardcoded. Refer to functions like is_hermitian for reference on how to do that.

@@ -0,0 +1,61 @@
"""Tests for bures_distance."""
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious if there are any specific examples in the literature, academic course notes, etc. where the Bures distance is calculated between two different specific density matrices with a known result. That might be nice to add as a test case for a sanity check. One could also look at the unit tests for say the qutip package that also has a function for the bures_distance.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the test in qutip but they don't have any specific test for the bures_dist. They only check an inequality, here is the test: https://github.com/qutip/qutip/blob/master/qutip/tests/test_metrics.py.

I have added an example for pure states shown in this book: Elements of Quantum Computation and Quantum Communication, the example is the 3.42 in the book. I couldn't find more examples but I will keep searching.

e_0, e_1 = basis(2, 0), basis(2, 1)
rho = 3 / 4 * e_0 * e_0.conj().T + 1 / 4 * e_1 * e_1.conj().T
sigma = 1 / 8 * e_0 * e_0.conj().T + 7 / 8 * e_1 * e_1.conj().T
np.testing.assert_equal(np.isclose(bures_distance(rho, sigma), 0.6724, rtol=1e-03), True)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to have cases like this, but I suppose aside from this value being arbitrarily the value that you see when you run is, is there any other way to verify that this value is indeed correct?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, I used the qutip function bures_dist to compare the result.

@vprusso
Copy link
Copy Markdown
Owner

vprusso commented May 16, 2021

You'll also want to make sure that the bures_distance function gets picked up in the documentation generation. So in docs/states.rst make sure you add a line toqito.state_metrics.bures_distance under Distance Metrics for Quantum States.

np.testing.assert_equal(np.isclose(res, 0), True)


def test_bures_distance_cvx():
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running this test yields a failure. I don't believe you need to account for the case when the inputs are CVX-type objects, so I think you can safely remove this test so long as doing so does not decrease your overall testing coverage.

import numpy as np

from toqito.state_metrics import fidelity

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be an extra blank line between from toqito.state_metrics import fidelity and def bures_distance(rho_1: np.ndarray, rho_2: np.ndarray, decimals: int = 10) -> float: (PEP-8 requirement).

Comment thread toqito/state_metrics/bures_distance.py Outdated
if not np.all(rho_1.shape == rho_2.shape):
raise ValueError("InvalidDim: `rho_1` and `rho_2` must be matrices of the same size.")
# Round fidelity to only 10 decimals to avoid error when rho_1 = rho_2
return np.sqrt(2.0 * (1.0 - np.round(fidelity(rho_1, rho_2), decimals) ))
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space between the ) and )) closing parens.

Comment thread toqito/state_metrics/bures_distance.py Outdated
r"""
Compute the Bures distance of two density matrices [WikBures]_.

Calculate the Bures distance between the two density matrices:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running the doc generation seems to yield an unexpected indentation warning. This paragraph should be formatted as:

    Calculate the Bures distance between the two density matrices: :code:`rho_1` and
    :code:`rho_2`, defined by:

Comment thread toqito/state_metrics/bures_distance.py Outdated
\sqrt{2 (1 - F(\rho_1, \rho_2)},

where :math:`F(\cdot)` denotes the fidelity between :math:`\rho_1` and :math:`\rho_2`.
The return is a value between :math:`0` and :math:`\sqrt(2)`,with
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should alter the instances of \sqrt(2) to \sqrt{2} (note the ( and ) vs. the { and }.

Comment thread toqito/state_metrics/bures_distance.py Outdated
\end{pmatrix} \in \text{D}(\mathcal{X}).

In the event where we calculate the Bures distance between states that are identical, we
should obtain the value of :math:`0`.This can be observed in :code:`toqito` as follows.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a missing space between the "." and the word "This"

Comment thread toqito/state_metrics/bures_distance.py Outdated
Calculate the Bures distance between the two density matrices:
":code:`rho_1` and ":code:`rho_2`, defined by:
.. math::
\sqrt{2 (1 - F(\rho_1, \rho_2)},
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's a missing closing parens here.

@vprusso vprusso merged commit 17b272e into vprusso:master May 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants