Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions netrd/distance/quantum_jsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import numpy as np
from scipy.linalg import expm
from .base import BaseDistance
from ..utilities import ensure_undirected


class QuantumJSD(BaseDistance):
Expand Down Expand Up @@ -110,6 +111,9 @@ def dist(self, G1, G2, beta=0.1, q=None):
if q and q >= 2:
warnings.warn("JSD is only a metric for 0 ≤ q < 2.", RuntimeWarning)

G1 = ensure_undirected(G1)
G2 = ensure_undirected(G2)

def density_matrix(A, beta):
"""
Create the density matrix encoding probabilities for entropies.
Expand Down
14 changes: 7 additions & 7 deletions tests/test_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_different_graphs():
## networks may have the same eigenvalues, thus a method that compares
## their eigenvalues would result in distance 0. However, this is very
## unlikely in the constructed case, so we rely on it for now.
G1 = nx.fast_gnp_random_graph(100, 0.1)
G1 = nx.fast_gnp_random_graph(100, 0.3)
G2 = nx.barabasi_albert_graph(100, 5)

for obj in distance.__dict__.values():
Expand All @@ -41,7 +41,7 @@ def test_different_graphs():
def test_symmetry():
"""The distance between two graphs must be symmetric."""
G1 = nx.barabasi_albert_graph(100, 4)
G2 = nx.fast_gnp_random_graph(100, 0.1)
G2 = nx.fast_gnp_random_graph(100, 0.3)

for label, obj in distance.__dict__.items():
if isinstance(obj, type) and BaseDistance in obj.__bases__:
Expand All @@ -63,13 +63,13 @@ def test_quantum_jsd():
dist = JSD.dist(G, G, beta=0.1, q=2)
assert np.isclose(dist, 0.0)

G1 = nx.fast_gnp_random_graph(100, 0.1)
G1 = nx.fast_gnp_random_graph(100, 0.3)
G2 = nx.barabasi_albert_graph(100, 5)
dist = JSD.dist(G1, G2, beta=0.1, q=2)
assert dist > 0.0

G1 = nx.barabasi_albert_graph(100, 4)
G2 = nx.fast_gnp_random_graph(100, 0.1)
G2 = nx.fast_gnp_random_graph(100, 0.3)
dist1 = JSD.dist(G1, G2, beta=0.1, q=2)
dist2 = JSD.dist(G2, G1, beta=0.1, q=2)
assert np.isclose(dist1, dist2)
Expand All @@ -80,15 +80,15 @@ def test_directed_input():
warnings.filterwarnings(
"ignore", message="Coercing directed graph to undirected."
)
G = nx.fast_gnp_random_graph(100, 0.1, directed=True)
G = nx.fast_gnp_random_graph(100, 0.3, directed=True)

for label, obj in distance.__dict__.items():
if isinstance(obj, type) and BaseDistance in obj.__bases__:
dist = obj().dist(G, G)
assert np.isclose(dist, 0.0)

G1 = nx.fast_gnp_random_graph(100, 0.1, directed=True)
G2 = nx.fast_gnp_random_graph(100, 0.1, directed=True)
G1 = nx.fast_gnp_random_graph(100, 0.3, directed=True)
G2 = nx.fast_gnp_random_graph(100, 0.3, directed=True)

for label, obj in distance.__dict__.items():
if isinstance(obj, type) and BaseDistance in obj.__bases__:
Expand Down