> ## Documentation Index
> Fetch the complete documentation index at: https://docs.haiqu.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Targeted Noise in QEC Runs

> Choose which parts of a QEC-compiled circuit the specified noise model is applied to.

In simulations, it can often be useful to apply noise to only some parts of the circuit. In
particular, in (early) fault-tolerant circuits, it is useful to apply noise only on certain logical
operations or to exclude some of them from the noise application. For example, one may want to
study only the effect of noise on the error-correction cycle (syndrome measurements), or keep an
operation that is not implemented fault-tolerantly noise-free. Targeted noise enables that.

By default [qec\_run](https://docs.haiqu.ai/reference/run/qec_run.md) applies noise everywhere.
This can be controlled by setting the `toggles` argument described on this page.
The noise itself is determined by the noise model given in the `noise` argument (see
[Noise model](https://docs.haiqu.ai/reference/run/qec_run.md#noise-model)). The `toggles`
decide where that noise is applied; they do not change the noise model itself.

# How a compiled circuit is structured

The QEC compiler wraps every instruction it emits for a logical operation in a box carrying a label
that names that operation. Boxes are the unit of selection: noise is either applied to a box as a
whole (meaning, to all of the gates and measurements contained in the box alike), or not applied at
all (to that box). Six kinds of boxes can occur in a compiled circuit:

| Logical operation | Appears when | Label builder |
| - | - | - |
| Logical state preparation | every logical qubit, at the start of the circuit | `logical_state_preparation_box_label(state)` |
| Error-correction cycle | after every `ec_cycle_frequency` layers of logical gates, an option of `haiqu.qec_compile` and `haiqu.qec_run` (see [Error-correction cycles](https://docs.haiqu.ai/reference/run/qec_compile.md#error-correction-cycles)) | `ec_cycle_box_label()` |
| Logical gate | for each logical gate of the compiled circuit | `logical_gate_box_label(gate_name)` |
| Logical reset | a logical qubit is reset | `logical_reset_box_label(state)` |
| Data measurement | the data qubits of a logical qubit are read out in a basis | `data_measurement_box_label(basis)` |
| Logical measurement | a logical Pauli observable is measured, for codes that support it | `logical_measurement_box_label(observable)` |

It is strongly recommended to create labels with the builders in [`haiqu.sdk.qec_labels`](#module-haiqu.sdk.qec_labels)
rather than writing the label strings by hand.

# Selecting where noise applies

`toggles` is a plain dictionary with up to three keys. Omit a key to keep its default.

| Key | Default | Meaning |
| - | - | - |
| `"noisy_box_labels"` | `None` | A list of box labels (strings) defining the boxes that receive noise. `None` selects every box; an empty list (`[]`) selects no box. |
| `"apply_noise_outside_boxes"` | `True` | Whether instructions outside every box receive noise. |
| `"qubit_gate_overrides"` | `None` | Overrides in which boxes gate noise is applied to a particular logical qubit's data qubits. A dictionary whose keys are logical qubit indices in the circuit and whose values are lists of box labels, as in `"noisy_box_labels"`. For that qubit, the list replaces `"noisy_box_labels"` for gates only; measurements always follow `"noisy_box_labels"`. Several logical qubits can be given as separate keys. |

Together the `toggles` decide for every gate and measurement of the compiled circuit whether it
is noisy.

#### NOTE

A two-qubit gate is noisy if it is noisy for either of its qubits. Since an override names only
a logical qubit's data qubits, the two-qubit gates between data and syndrome qubits inside an
error-correction cycle keep following `"noisy_box_labels"` for the syndrome qubit.

# Examples

All examples start from a compiled circuit and a noise model, and decode with `"tesseract"`, the
most accurate decoder (see [Decoders](https://docs.haiqu.ai/reference/run/qec_run.md#decoders)):

```python theme={null}
from qiskit import QuantumCircuit
from haiqu.sdk import haiqu

haiqu.login()
haiqu.init("qec-noise-docs-examples")

circuit = QuantumCircuit(1, 1)
circuit.h(0)
circuit.h(0)
circuit.measure(0, 0)

compiled = haiqu.qec_compile(circuit, code="color_488_d3", ec_cycle_frequency=1).result()
noise = {"p1q": 1e-3, "p2q": 1e-3, "p_meas": 1e-3}
```

**Noise only in the error-correction cycles.** State preparation, the logical gates and the final
readout are noise-free, so the logical error rate measures the cycles alone. This is the setting of
a threshold study of the error-correction cycle.

```python theme={null}
from haiqu.sdk.qec_labels import ec_cycle_box_label

job = haiqu.qec_run(
    compiled,
    noise=noise,
    decoder="tesseract",
    toggles={
        "noisy_box_labels": [ec_cycle_box_label()],
        "apply_noise_outside_boxes": False,
    },
)
```

**Everything noisy except the final readout.** Every box is named except the data measurement, so
the gates and the syndrome measurements inside the cycles stay noisy while the terminating data
readout is noise-free.

```python theme={null}
from haiqu.sdk.qec_labels import (
    ec_cycle_box_label,
    logical_gate_box_label,
    logical_state_preparation_box_label,
)

job = haiqu.qec_run(
    compiled,
    noise=noise,
    decoder="tesseract",
    toggles={
        "noisy_box_labels": [
            logical_state_preparation_box_label("0"),
            logical_gate_box_label("h"),
            ec_cycle_box_label(),
        ],
    },
)
```

To switch off measurement noise everywhere instead, including inside the cycles, set `p_meas`
to zero in `noise`: box selection places gate and measurement noise together.

**One bad logical qubit.** Only the gates on the data qubits of logical qubit 0, inside the
logical Hadamard boxes, are noisy. No box is selected globally and there is no measurement noise.

```python theme={null}
from haiqu.sdk.qec_labels import logical_gate_box_label

job = haiqu.qec_run(
    compiled,
    noise=noise,
    decoder="tesseract",
    toggles={
        "noisy_box_labels": [],
        "apply_noise_outside_boxes": False,
        "qubit_gate_overrides": {0: [logical_gate_box_label("h")]},
    },
)
```

# Things to check

* **A label must match a box of your compiled circuit.** Labels name the gates as compiled, not
  as written: a code whose native logical gates are H, S and CX compiles a logical X into H and S
  boxes, so `logical_gate_box_label("x")` selects nothing in that circuit. A label that matches
  no box is not an error; the part of the circuit you meant to select simply stays noise-free.
* **None and an empty list are opposites.** `"noisy_box_labels": None` (or omitting the key)
  selects every box; `"noisy_box_labels": []` selects none.
* **Overrides govern gates only.** Selecting a box with `"noisy_box_labels"` makes its gates and
  its measurements noisy; naming it in `"qubit_gate_overrides"` makes only the gates on that
  logical qubit's data qubits noisy.

# Label builders

Haiqu SDK. QEC box labels.

The QEC compiler wraps each logical operation of a compiled circuit in a labeled box. This module
builds those labels, so that they do not have to be typed by hand when selecting boxes in the
`toggles` argument of `haiqu.qec_run`, either in `"noisy_box_labels"` or in
`"qubit_gate_overrides"`.

Every label is the shared prefix, a fixed operation term, and, for a parameterized operation, a
single space and one `key=value` tail:

```default theme={null}
__qec_compiler__:ec_cycle
__qec_compiler__:logical_state_preparation state=0
__qec_compiler__:logical_gate name=x
__qec_compiler__:data_measurement basis=Z
__qec_compiler__:logical_reset state=0
__qec_compiler__:logical_measurement observable=Z
```

### haiqu.sdk.qec\_labels.QEC\_COMPILER\_BOX\_LABEL\_PREFIX *= '*\_qec\_compiler*\_:'*

Prefix shared by every label the compiler puts on a box.

### haiqu.sdk.qec\_labels.data\_measurement\_box\_label(basis)

Label on the box wrapping a data measurement (e.g. basis='Z').

* **Parameters:**
  **basis** (*str*)
* **Return type:**
  str

### haiqu.sdk.qec\_labels.ec\_cycle\_box\_label()

Label on the box wrapping each error-correction cycle.

* **Return type:**
  str

### haiqu.sdk.qec\_labels.logical\_gate\_box\_label(gate\_name)

Label on the box wrapping a logical gate (e.g. gate\_name='x').

* **Parameters:**
  **gate\_name** (*str*)
* **Return type:**
  str

### haiqu.sdk.qec\_labels.logical\_measurement\_box\_label(observable)

Label on the box wrapping a logical measurement (e.g. observable='Z').

* **Parameters:**
  **observable** (*str*)
* **Return type:**
  str

### haiqu.sdk.qec\_labels.logical\_reset\_box\_label(state)

Label on the box wrapping a logical reset (e.g. state='0').

* **Parameters:**
  **state** (*str*)
* **Return type:**
  str

### haiqu.sdk.qec\_labels.logical\_state\_preparation\_box\_label(state)

Label on the box wrapping a logical state preparation (e.g. state='0').

* **Parameters:**
  **state** (*str*)
* **Return type:**
  str


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.