Feature/bures distance [unitaryHACK]#60
Conversation
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
| 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)) |
There was a problem hiding this comment.
Can simplify these lines by just returning the calculation--that is:
return np.sqrt(2.0 * (1.0 - round_fidelity))| 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) |
| 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) ) ) |
There was a problem hiding this comment.
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.
|
|
||
| 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 |
There was a problem hiding this comment.
This is one of the lines the liner is complaining about being too long I think.
| @@ -0,0 +1,61 @@ | |||
| """Tests for bures distance.""" | |||
There was a problem hiding this comment.
bures distance -> bures_distance.
|
|
||
| from toqito.state_metrics import fidelity | ||
|
|
||
| def bures_distance(rho_1: np.ndarray, rho_2: np.ndarray ) -> float: |
There was a problem hiding this comment.
Extra space between np.ndarray and ).
|
|
||
| def bures_distance(rho_1: np.ndarray, rho_2: np.ndarray ) -> float: | ||
| r""" | ||
| Compute the bures distance of two density matrices [WikFid]_. |
|
|
||
| def bures_distance(rho_1: np.ndarray, rho_2: np.ndarray ) -> float: | ||
| r""" | ||
| Compute the bures distance of two density matrices [WikFid]_. |
There was a problem hiding this comment.
[WikFid]_ should probably have something different like [WikBures]_.
There was a problem hiding this comment.
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.
| r""" | ||
| Compute the bures distance of two density matrices [WikFid]_. | ||
|
|
||
| Calculate the bures distance between the two density matrices: |
| 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: |
There was a problem hiding this comment.
Should be ":code:`rho_1`" and ":code:`rho_2`".
|
|
||
| :param rho_1: Density operator. | ||
| :param rho_2: Density operator. | ||
| :return: The Bures distance between :code:`rho_1` and :code:`sigma_2`. |
There was a problem hiding this comment.
The Bures distance between :code:`rho_1` and :code:`rho_2`, right?
| :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)): |
There was a problem hiding this comment.
Redundant parens around (rho_2.shape)?
| 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) )) |
There was a problem hiding this comment.
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.""" | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
In this case, I used the qutip function bures_dist to compare the result.
|
You'll also want to make sure that the |
| np.testing.assert_equal(np.isclose(res, 0), True) | ||
|
|
||
|
|
||
| def test_bures_distance_cvx(): |
There was a problem hiding this comment.
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 | ||
|
|
There was a problem hiding this comment.
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).
| 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) )) |
There was a problem hiding this comment.
Extra space between the ) and )) closing parens.
| r""" | ||
| Compute the Bures distance of two density matrices [WikBures]_. | ||
|
|
||
| Calculate the Bures distance between the two density matrices: |
There was a problem hiding this comment.
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:
| \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 |
There was a problem hiding this comment.
You should alter the instances of \sqrt(2) to \sqrt{2} (note the ( and ) vs. the { and }.
| \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. |
There was a problem hiding this comment.
There is a missing space between the "." and the word "This"
| 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)}, |
There was a problem hiding this comment.
Looks like there's a missing closing parens here.
Description
This function takes as input two density matrices and performs the following calculation:
where
rho_1andrho_2are density operators and where thefidelityfunction comes fromtoqito.state_metrics.fidelity.Todos
rho_1 = rho_2,fidelity(rho_1, rho_2) = 1.0000000000000002. This will create an error in thesqrt. I added theroundfunction to the 10 decimal to avoid it.tests/test_state_metrics/test_bures_distance.pyfrom toqito.state_metrics.bures_distance import bures_distancetotoqito/state_metrics/__init__.pyQuestions
fidelityfunction for the caserho_1 = rho_2and avoid the numeric error1.0000000000000002. That2may cause problems somewhere else.Status