Skip to content

noise/index.py

class Error

A Kraus/error operator represented as a NumPy ndarray subclass.

Conceptually, an error operator E acts on a state as ρEρE, and a noise channel is typically a collection {Ek}.

nametypedefault
correctableboolFalse
dintNone
namestrNone
paramsdict[str, Any]None

class Channel

A quantum channel represented in Kraus form.

For Kraus operators {Ek}, the channel acts as Φ(ρ)=kEkρEk.

nametypedefault
correctableslist[Union[int, list[int]]]None
dintNone
opslist[Error]None

init

No Definition provided

py
def __init__(self: Any, ops: list[Error]) -> Any
Implementation
python
def __init__(self, ops: list[Error]):
    assert isinstance(ops, list) and len(ops) > 0, 'ops must be List[ops]'
    self.ops = unnull(ops)
    self.d = ops[0].d if isinstance(ops[0], Error) else int(ops[0].shape[0])
    self.correctables = []

run

Apply the channel in Kraus form: ρkEkρEk.

py
def run(self: Any, rho: Union[State, np.ndarray]) -> np.ndarray
Implementation
python
def run(self, rho: Union[State, np.ndarray]) -> np.ndarray:
    rho_arr: Any = getattr(rho, 'tensor', rho)
    result = [O @ rho_arr @ O.conj().T for O in self.ops]
    return np.sum(result, axis=0)

correctable

Return the subset(s) of Kraus operators marked as correctable (if any).

py
def correctable(self: Any) -> Any
Implementation
python
def correctable(self) -> Any:
    if not self.correctables:
        return []
    c0 = self.correctables[0]
    if isinstance(c0, int):
        idxs = [i for i in self.correctables if isinstance(i, int)]
        return [self.ops[i] for i in idxs]
    if isinstance(c0, list):
        Ek: list[list[Error]] = []
        sets = [s for s in self.correctables if isinstance(s, list)]
        for s in sets:
            Ek.append([self.ops[i] for i in s])
        return Ek
    return Exception("Please don't change correctables")

isTP

Check trace-preservation (TP) via kEkEk=I (approx.).

py
def isTP(self: Any) -> bool
Implementation
python
def isTP(self) -> bool:
    ti = [np.trace(O.conj().T @ O) for O in self.ops]
    return bool(np.isclose(sum(ti), 1.0))

isCP

Check complete-positivity (CP) by verifying Choi matrix is PSD.

py
def isCP(self: Any) -> bool
Implementation
python
def isCP(self) -> bool:
    J = self.toChoi()
    eig = np.linalg.eigvalsh(J)
    return bool(np.all(eig >= -1e-08))

isCPTP

Check if the channel is CPTP (completely positive and trace preserving).

py
def isCPTP(self: Any) -> bool
Implementation
python
def isCPTP(self) -> bool:
    return self.isCP and self.isTP

toChoi

Compute the Choi matrix J(Φ)=i,j|ij|Φ(|ij|).

py
def toChoi(self: Any) -> np.ndarray
Implementation
python
def toChoi(self) -> np.ndarray:
    d = self.d
    J = np.zeros((d * d, d * d), dtype=complex)
    basis = np.eye(d, dtype=complex)
    for i in range(d):
        for j in range(d):
            Eij = np.outer(basis[:, i], basis[:, j].conj())
            PhiE = self.run(Eij)
            J += np.kron(Eij, PhiE)
    return J

toSuperop

Compute the superoperator S such that vec(Φ(ρ))=Svec(ρ).

py
def toSuperop(self: Any) -> np.ndarray
Implementation
python
def toSuperop(self) -> np.ndarray:
    d = self.d
    S = np.zeros((d * d, d * d), dtype=complex)
    I = np.eye(d, dtype=complex)
    for k in range(d * d):
        ek = I.flatten()[k]
        E = ek.reshape(d, d)
        vecPhi = self.run(E).flatten()
        S[:, k] = vecPhi
    return S

toStinespring

Compute a Stinespring isometry V by stacking Kraus operators.

Constructs V:CdCdCr with V=k|kEk, represented as a (dr)×d matrix.

py
def toStinespring(self: Any) -> np.ndarray
Implementation
python
def toStinespring(self) -> np.ndarray:
    K = len(self.ops)
    d = self.d
    r = K
    V = np.zeros((d * r, d), dtype=complex)
    for n, O in enumerate(self.ops):
        V[n * d:(n + 1) * d, :] = O
    return V

Ak

Alias for the Kraus list {Ek}

py
def Ak(self: Any) -> list[Error]
Implementation
python
def Ak(self) -> list[Error]:
    return self.ops

unnull

Filter out (approximately) zero operators.

Drops any matrix A with A0 (elementwise up to tolerance), useful to clean Kraus lists.

py
def unnull(lst: List[np.ndarray]) -> List[np.ndarray]
Implementation
python
def unnull(lst: List[np.ndarray]) -> List[np.ndarray]:
    return [matrix for matrix in lst if not np.all(np.isclose(matrix, 0, atol=1e-08))]