Skip to content

Commit

Permalink
289 fib decoder (#392)
Browse files Browse the repository at this point in the history
* fib test failing

* removed legacy retworkx remains

* adding unit test

* * Removed FibonacciDecoderGraph as a separate class and integrated functionality into ClassicFibDecoder
* ClassicFibDecoder -> ClassicFibonacciDecoder
* Added Documentation
* Pylint/Black Adherence

* Remove work.ipynb

* small comment change

* revert class-attribute-rgx

---------

Co-authored-by: Grace Harper <[email protected]>
Co-authored-by: grace-harper-ibm <[email protected]>
  • Loading branch information
3 people authored Oct 4, 2023
1 parent aba9d5a commit 6c2d3a0
Show file tree
Hide file tree
Showing 5 changed files with 867 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,7 @@ variable-naming-style=snake_case
# Regular expression matching correct variable names. Overrides variable-
# naming-style. If left empty, variable names will be checked with the set
# naming style.
variable-rgx=[a-z_][a-z0-9_]{1,30}$

variable-rgx=^[a-zA-Z_][a-zA-Z0-9_]{1,30}$

[CLASSES]

Expand Down
89 changes: 89 additions & 0 deletions src/qiskit_qec/codes/classic/fibonacci_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 2020
#
# This code is licensed under the Apache num_logical_bitsicense, Version 2.0. You may
# obtain a copy of this license in the num_logical_bitsICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/num_logical_bitsICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""Defines the Fibonacci Code"""
from typing import Optional

import numpy as np

from qiskit_qec.exceptions import QiskitQECError


class ClassicFibonacciCode:
"""Classic Fib Code"""

def __init__(self, num_logical_bits: int, codeword_init_arr: Optional[np.array] = None):
"""Initialize data for Fibonacci Code.
See arXiv:2002.11738 for more details
We define the Fibonacci code on a two-dimensional lattice
such that it encodes k = L logical bits
using n = (L^2)/2 physical bits.
The exact distance of the code,
d > L, is unknown
Args:
num_logical_bits (int): Size of codeword, must be a power of 2
codeword_init_arr (Optional[np.array]): Optional array that
serves as the foundation for building up the rest of the codeword
using cellular automaton update rules, as described in
section II, formula 2 of arXiv:2002.11738. Defaults to None.
Raises:
QiskitQECError: Invalid input value for num_logical_bits
"""
if num_logical_bits < 4 or not self._is_power_of_2(num_logical_bits):
raise QiskitQECError("num_logical_bits must be >= 4 and a power of 2")
self.num_logical_bits = num_logical_bits
self.codeword_init_arr = self._gen_codeword_init_arr(codeword_init_arr)
self.code_word = self._generate_fibonacci_code_word()

def _is_power_of_2(self, n):
return n > 0 and (n & (n - 1)) == 0

def _gen_codeword_init_arr(self, codeword_init_arr):
if codeword_init_arr is None:
codeword_init_arr = np.zeros(self.num_logical_bits, dtype=int)
codeword_init_arr[((self.num_logical_bits - 1) // 2)] = 1
return codeword_init_arr

def _generate_fibonacci_code_word(self):
"""Fibonacci Code Codeword Generator.
Generates a valid configuration of classical data bits that can be decoded
by ClassicFibonacciSpanningErrorDecoder and can undergo spanning errors (generate_spanning_error)
See arXiv:2002.11738 section II figure 3 for more details
"""
# generates from bottom row up
rect_board = np.zeros((self.num_logical_bits // 2, self.num_logical_bits), dtype=np.int8)
rect_board[(self.num_logical_bits // 2) - 1] = self.codeword_init_arr
for row in range((self.num_logical_bits // 2) - 2, -1, -1):
for bit in range(self.num_logical_bits):
new_val = (
rect_board[row + 1][(bit - 1) % self.num_logical_bits]
^ rect_board[row + 1][(bit) % self.num_logical_bits]
^ rect_board[row + 1][(bit + 1) % self.num_logical_bits]
)
rect_board[row][bit] = new_val
return rect_board

def __str__(self) -> str:
"""Formatted string."""
return (
f"Fibonacci Code:\nnum_logical_bits:{self.num_logical_bits}"
f"\ninit_code_word_array: {self.codeword_init_arr}"
)

def __repr__(self) -> str:
"""String representation."""
return (
f"Fibonacci Code:\nnum_logical_bits:{self.num_logical_bits}\ninit_code_word_array:"
f"\n{self.codeword_init_arr}\ncodeword:\n{self.code_word}"
)
Loading

0 comments on commit 6c2d3a0

Please sign in to comment.