Skip to content

qec/lib.py

Dutta3

Dutta's 3-qubit code, which is a permutation-invariant code designed to be the smallest code that can correct a single AD error. The codewords are:

|0L=13(|001+|010+|100)

py
def Dutta3() -> Code
Implementation
python
def Dutta3() -> Code:
    cw0 = pt.zeros(8)
    cw1 = pt.zeros(8)
    cw0[1] = 1
    cw0[2] = 1
    cw0[4] = 1
    cw1[7] = 1
    code = pt.stack([cw0 / pt.linalg.norm(cw0), cw1 / pt.linalg.norm(cw1)])
    return Code(code)

Leung

Leung's 4-qubit code, which is a standard code that can correct a single AD error. The codewords are:

$ |0_L\rangle = \frac{1}{\sqrt{2}}(|0000\rangle + |1111\rangle) \ |1_L\rangle = \frac{1}{\sqrt{2}}(|0011\rangle + |1100\rangle)

py
def Leung() -> Code
Implementation
python
def Leung() -> Code:
    leung_0 = pt.zeros(16)
    leung_1 = pt.zeros(16)
    leung_0[0] = 1
    leung_0[-1] = 1
    leung_1[3] = 1
    leung_1[12] = 1
    code = pt.stack([leung_0 / pt.linalg.norm(leung_0), leung_1 / pt.linalg.norm(leung_1)])
    return Code(code)

Perfect

The [[5,1,3]] Perfect code, which is the smallest code that can correct an arbitrary single-qubit error. The codewords are:

|0L=14(|00000+|11000+|01100+|00110+|00011|10001|01001|00101|00010|10000|01000|00100|11110|11101|11011|10111)

|1L=X5|0L

py
def Perfect() -> Code
Implementation
python
def Perfect() -> Code:
    _0L = pt.zeros(32)
    _1L = pt.zeros(32)
    k0 = [0, 18, 9, 20, 10, -27, -6, -24, -29, -3, -30, -15, -17, -12, -23, 5]
    k1 = [31, 13, 22, 11, 21, -4, -25, -7, -2, -28, -1, -16, -14, -19, -8, 26]
    for i in range(len(k0)):
        _0key = k0[i]
        _1key = k1[i]
        _0L[abs(_0key)] = 1.0 if _0key >= 0 else -1.0
        _1L[abs(_1key)] = 1.0 if _1key >= 0 else -1.0
    _0L[0] = 1
    return Code(pt.stack([_0L / pt.linalg.norm(_0L), _1L / pt.linalg.norm(_1L)]))

Qutrit3

3-qutrit repetition code: three codewords |0L=|000, |1L=|111, |2L=|222.

Encodes one logical qutrit in three physical qutrits (dn=33=27 dimensional space). Detects (and with measurement corrects) single qutrit shift (X3) errors.

py
def Qutrit3() -> Code
Implementation
python
def Qutrit3() -> Code:
    cw = pt.zeros(3, 27)
    cw[0, 0] = 1.0
    cw[1, 13] = 1.0
    cw[2, 26] = 1.0
    return Code(cw, d=3)

GottesmanD

Gottesman-type CSS code for prime d: encodes one logical d-level qudit in d physical qudits.

Codewords: |jL=1da=0d1|a,a+j,a+2j,(modd)

For d=2: |0L=(|00+|11)/2, |1L=(|01+|10)/2. For d=3: three orthonormal codewords on 3 physical qutrits (27-dimensional space).

py
def GottesmanD(d: int) -> Code
Implementation
python
def GottesmanD(d: int=2) -> Code:
    n_phys = d
    dim_total = d ** n_phys
    cws = []
    norm = 1.0 / math.sqrt(d)
    for j in range(d):
        vec = pt.zeros(dim_total, dtype=pt.complex64)
        for a in range(d):
            idx = 0
            for pos in range(n_phys):
                digit = (a + pos * j) % d
                idx = idx * d + digit
            vec[idx] += norm
        cws.append(vec)
    return Code(pt.stack(cws), d=d)

Surface

Build an m×n surface code and return a Code spanning the stabilizer space.

py
def Surface(m: int, n: int, d: int, edge: str, start: str) -> Code
Implementation
python
def Surface(m: int, n: int, d: int=2, edge: str='even', start: str='X') -> Code:
    stabs = Stabilisers(m, n, edge=edge, start=start)
    return Code.fromStabilizers(stabs, d=d)