Add ErrorChannel - Noro-Official/OQS GitHub Wiki

Adding a New ErrorChannel

Error channels in OpenQStack are built on a common interface that allows Kraus-based noise simulation.


Step 1: Inherit from ErrorChannel

Each new channel should subclass ErrorChannel and define its Kraus operators:

from openqstack.qec import ErrorChannel, tensor
import numpy as np

class CustomXFlip(ErrorChannel):
    def __init__(self, p, n_qubits=1, target_qubit=0):
        super().__init__(n_qubits)
        self.p = p
        self.target = target_qubit

    def kraus_operators(self):
        I = np.eye(2)
        X = np.array([0, 1], [1, 0](/Noro-Official/OQS/wiki/0,-1],-[1,-0))
        K0 = np.sqrt(1 - self.p) * I
        K1 = np.sqrt(self.p) * X
        return [
            (self._expand(K0), 1 - self.p),
            (self._expand(K1), self.p)
        ]

    def _expand(self, op):
        ops = [np.eye(2)] * self.n_qubits
        ops[self.target] = op
        return tensor(*ops)

Step 2: Use the Channel

You can now apply it to any quantum state:

channel = CustomXFlip(p=0.2, n_qubits=3, target_qubit=1)
noisy_state = channel(state)

Step 3: Optional Enhancements

  • Add "average" mode support for density matrices
  • Add unit tests to test_channels.py
  • Add visualization (e.g. plot_error_map())

Contribute Your Channel

If your channel is general-purpose, submit it via pull request under qec.py or a new noise/ module.