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

infinite distnace --> undefined (np.nan) distance for trivial (dimension-0) codes #68

Merged
merged 3 commits into from
Apr 2, 2024
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
20 changes: 8 additions & 12 deletions qldpc/codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ def get_distance_exact(
words = self.words() - self.field(vector)[np.newaxis, :]
return np.min(np.count_nonzero(words.view(np.ndarray), axis=1))

# by convention, trivial (dimension-0) codes have infinite distance
# the distance of trivial (dimension-0) codes is undefined
if self.dimension == 0:
return np.inf
return np.nan

# if we know the exact code distance, return it
if self._exact_distance is not None:
Expand Down Expand Up @@ -387,10 +387,6 @@ def get_one_distance_bound(
Additional arguments, if applicable, are passed to a decoder.
"""
if vector is not None:
if self.dimension == 0:
# only all-0 is a valid code word
return np.count_nonzero(vector)

# find the distance of the given vector from a code word
correction = qldpc.decoder.decode(
self.matrix,
Expand All @@ -399,9 +395,9 @@ def get_one_distance_bound(
)
return int(np.count_nonzero(correction))

# by convention, trivial (dimension-0) codes have infinite distance
# the distance of trivial (dimension-0) codes is undefined
if self.dimension == 0:
return np.inf
return np.nan

# effective syndrome: a trivial "actual" syndrome, and a nonzero overlap with a random word
effective_syndrome = np.zeros(self.num_checks + 1, dtype=int)
Expand Down Expand Up @@ -968,9 +964,9 @@ def get_distance_exact(
"""
assert pauli is None or pauli in PAULIS_XZ

# by convention, trivial (dimension-0) codes have infinite distance
# the distance of trivial (dimension-0) codes is undefined
if self.dimension == 0:
return np.inf
return np.nan

if pauli is None:
return min(
Expand Down Expand Up @@ -1069,9 +1065,9 @@ def get_one_distance_bound(
"""
assert pauli is None or pauli in PAULIS_XZ

# by convention, trivial (dimension-0) codes have infinite distance
# the distance of trivial (dimension-0) codes is undefined
if self.dimension == 0:
return np.inf
return np.nan

# define code_z and pauli_z as if we are computing X-distance
pauli = pauli or random.choice(PAULIS_XZ)
Expand Down
29 changes: 10 additions & 19 deletions qldpc/codes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,13 @@ def test_distance_from_classical_code(bits: int = 3) -> None:
assert dist_brute <= dist_bound
assert rep_code.get_distance(brute=False) == bits


def test_infinite_distance() -> None:
"""The distance of zero-dimensional codes is infinite."""
code: codes.AbstractCode

code = codes.ClassicalCode([[1, 0], [1, 1]])
random_vector = np.random.randint(2, size=code.num_bits)
assert code.dimension == 0
assert code.get_distance_exact() is np.inf
assert code.get_distance_bound() is np.inf
assert code.get_distance_exact(vector=random_vector) == np.count_nonzero(random_vector)
assert code.get_distance_bound(vector=random_vector) == np.count_nonzero(random_vector)

code = codes.HGPCode(code)
assert code.dimension == 0
assert code.get_distance(bound=False) is np.inf
assert code.get_distance(bound=True) is np.inf
trivial_code = codes.ClassicalCode([[1, 0], [1, 1]])
random_vector = np.random.randint(2, size=trivial_code.num_bits)
assert trivial_code.dimension == 0
assert trivial_code.get_distance_exact() is np.nan
assert trivial_code.get_distance_bound() is np.nan
assert trivial_code.get_distance_exact(vector=random_vector) == np.count_nonzero(random_vector)
assert trivial_code.get_distance_bound(vector=random_vector) == np.count_nonzero(random_vector)


def test_qubit_code(num_qubits: int = 5, num_checks: int = 3) -> None:
Expand Down Expand Up @@ -237,7 +227,7 @@ def test_qudit_stabilizers(field: int, bits: int = 5, checks: int = 3) -> None:


def test_quantum_distance() -> None:
"""Distance calculations for qudit codes."""
"""Distance calculations for CSS codes."""
code = codes.HGPCode(codes.RepetitionCode(2))
assert code.get_distance() == 2

Expand All @@ -249,7 +239,8 @@ def test_quantum_distance() -> None:
trivial_code = codes.ClassicalCode([[1, 0], [1, 1]])
code = codes.HGPCode(trivial_code)
assert code.dimension == 0
assert code.get_distance() is np.inf
assert code.get_distance(bound=False) is np.nan
assert code.get_distance(bound=True) is np.nan


def test_quantum_code_string() -> None:
Expand Down