Skip to content

Commit

Permalink
Update the definition of the Hellinger fidelity (bp #5595) (#5703)
Browse files Browse the repository at this point in the history
* Update the definition of the Hellinger fidelity (#5595)

* Update the definition of the Hellinger fidelity

* Updates based on review

* add explanation

* Update releasenotes/notes/fix_hellinger_fidelity_def-7f60d53e3577fdaf.yaml

Co-authored-by: Christopher J. Wood <cjwood@us.ibm.com>

Co-authored-by: Christopher J. Wood <cjwood@us.ibm.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit fc77b19)

* Make hellinger_distance() private and update releasenotes

In #5595 the hellinger_fidelity() method was split into a function to
calculate the hellinger distance and the fidelity function was update to
use that internally. However, we don't want to backport a new feature
in a backport/bugfix release so this commit marks it as private and
updates the release notes to not mention the new function. We will
introduce the new api function in the 0.17.0 release.

Co-authored-by: Paul Nation <nonhermitian@gmail.com>
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
  • Loading branch information
3 people authored Jan 25, 2021
1 parent 29e0737 commit df53e34
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 26 deletions.
77 changes: 51 additions & 26 deletions qiskit/quantum_info/analysis/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,55 @@
import numpy as np


def _hellinger_distance(dist_p, dist_q):
"""Computes the Hellinger distance between
two counts distributions.
Parameters:
dist_p (dict): First dict of counts.
dist_q (dict): Second dict of counts.
Returns:
float: Distance
References:
`Hellinger Distance @ wikipedia <https://en.wikipedia.org/wiki/Hellinger_distance>`_
"""
p_sum = sum(dist_p.values())
q_sum = sum(dist_q.values())

p_normed = {}
for key, val in dist_p.items():
p_normed[key] = val/p_sum

q_normed = {}
for key, val in dist_q.items():
q_normed[key] = val/q_sum

total = 0
for key, val in p_normed.items():
if key in q_normed.keys():
total += (np.sqrt(val) - np.sqrt(q_normed[key]))**2
del q_normed[key]
else:
total += val
total += sum(q_normed.values())

dist = np.sqrt(total)/np.sqrt(2)

return dist


def hellinger_fidelity(dist_p, dist_q):
"""Computes the Hellinger fidelity between
two counts distributions.
The fidelity is defined as 1-H where H is the
Hellinger distance. This value is bounded
in the range [0, 1].
The fidelity is defined as :math:`\\left(1-H^{2}\\right)^{2}` where H is the
Hellinger distance. This value is bounded in the range [0, 1].
This is equivalent to the standard classical fidelity
:math:`F(Q,P)=\\left(\\sum_{i}\\sqrt{p_{i}q_{i}}\\right)^{2}` that in turn
is equal to the quantum state fidelity for diagonal density matrices.
Parameters:
dist_p (dict): First dict of counts.
Expand Down Expand Up @@ -49,27 +91,10 @@ def hellinger_fidelity(dist_p, dist_q):
res2 = execute(qc, sim).result()
hellinger_fidelity(res1.get_counts(), res2.get_counts())
"""
p_sum = sum(dist_p.values())
q_sum = sum(dist_q.values())

p_normed = {}
for key, val in dist_p.items():
p_normed[key] = val/p_sum

q_normed = {}
for key, val in dist_q.items():
q_normed[key] = val/q_sum

total = 0
for key, val in p_normed.items():
if key in q_normed.keys():
total += (np.sqrt(val) - np.sqrt(q_normed[key]))**2
del q_normed[key]
else:
total += val
total += sum(q_normed.values())
dist = np.sqrt(total)/np.sqrt(2)

return 1-dist
References:
`Quantum Fidelity @ wikipedia <https://en.wikipedia.org/wiki/Fidelity_of_quantum_states>`_
`Hellinger Distance @ wikipedia <https://en.wikipedia.org/wiki/Hellinger_distance>`_
"""
dist = _hellinger_distance(dist_p, dist_q)
return (1-dist**2)**2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
The definition of the Hellinger fidelity from has been corrected from the
previous defition of :math`1-H(P,Q)` to :math:`[1-H(P,Q)**2]**2` so that it
is equal to the quantum state fidelity of P, Q as diagonal density matrices.

0 comments on commit df53e34

Please sign in to comment.