Metadata-Version: 2.1
Name: qzsim
Version: 0.2.0
Summary: MPS trajectory quantum circuit simulator with optional surface-code QEC and zero-noise extrapolation
License: MIT
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE

# qzsim

A standalone MPS (matrix product state) quantum circuit simulator using stochastic
noise trajectories, with optional rotated-surface-code QEC and zero-noise
extrapolation (ZNE), plus a one-call `qec_zne` pipeline.

## Components

| Component | Module |
|---|---|
| Circuit API (gates, arbitrary 1/2-qubit unitaries, measure, reset, classical control) | `qzsim.circuit` |
| MPS backend, bond-dimension cap, truncation tracker | `qzsim.mps`, `qzsim.Simulator` |
| Trajectory noise: coherent over-rotation, depolarizing, amplitude damping, readout/reset flips | `qzsim.noise` |
| ZNE: global per-segment unitary folding, 5 extrapolators | `qzsim.zne` |
| Rotated surface-code memory (d odd >= 3, no default distance) | `qzsim.qec` |
| Matching decoder (PyMatching, with built-in exact fallback) | `qzsim.decode` |
| QEC + ZNE in one call | `qzsim.qec_zne` |

## Quick start

Plain ZNE on any circuit:

```python
from qzsim import Circuit, NoiseModel
from qzsim.zne import zne

c = Circuit(1)
for _ in range(4):
    c.i(0)
noise = NoiseModel(coherent_angle_1q=0.025, coherent_axis="X")
print(zne(c, "Z", scale_factors=[1, 3, 5], noise=noise, extrapolator="richardson").summary())
```

QEC + ZNE (surface-code memory, residual logical error mitigated by ZNE):

```python
from qzsim import NoiseModel
from qzsim.qec_zne import qec_zne

noise = NoiseModel(depolarizing_1q=2e-3, depolarizing_2q=2e-3, measurement_flip=2e-3)
r = qec_zne(distance=3, rounds=3, noise=noise,
            scale_factors=[1, 3, 5], trajectories=[200, 400, 800],
            extrapolator="power_law",      # exponent defaults to ceil(d/2)
            chi_max=64, n_jobs=4, seed=1)
print(r.summary())
```

`qec_zne(circuit, observable, distance=None)` is plain ZNE, so it is a superset of `zne`.

## Design notes

* **Trajectories only.** Pure-state MPS; each trajectory is one noisy run. Error bars are
  standard errors across trajectories. Each trajectory is one shot for measured/decoded
  quantities.
* **Noise is fixed per run.** `NoiseModel` is frozen and reused at every scale factor.
  Scale factor lambda only changes circuit depth.
* **Folding is global, applied per unitary segment.** Measurements, resets and classically
  controlled gates are never folded. They split the circuit into segments, and each is folded
  by lambda. lambda must be >= 1. Odd integers fold exactly; other values partially fold the
  tail of each segment, are flagged approximate, and the fit uses the achieved gate-count ratio.
* **Readout and reset noise are not amplified** by folding.
* **ZNE scales physical noise only.** The fitted quantity is the decoded logical observable vs
  lambda. With `extrapolator="power_law"` the exponent defaults to ceil(d/2) in `qec_zne`.
* **Error sources are reported separately:** sampling error, truncation error
  (1 - cumulative fidelity, worst cut, max bond) per lambda, and fit error.
  Logical error rate per lambda is in `result.points[i].extras`.
* **Surface code:** rotated layout with ancilla reuse, X/Z memory experiments, optional logical
  |1>, row-major qubit order for the MPS line. The encoded path is a memory experiment (logical
  identity); compiling arbitrary logical circuits is not supported.
* **Decoder:** uniform-weight matching over the space-time graph of the stored basis' checks.
  Coherent noise is not Pauli, so matching treats it as an effective Pauli channel.
* Bitstrings put qubit 0 first. Gates act on 1 or 2 qubits (non-adjacent pairs are SWAP-routed).
  There is no idle noise: noise attaches to gates, measurements and resets.

## Cost

d=3 (17 qubits) runs in roughly 0.06 s per trajectory per round-set at lambda=1 and about 0.3 s at
lambda=5. d=5 (49 qubits) is about 1.8 s per trajectory at chi_max=64 with truncation reported.
Use `n_jobs` to parallelise trajectories.

## Tests

```
python -m unittest discover -s tests
```
