Metadata-Version: 2.5
Name: py-quantum-verifier
Version: 0.1.0
Summary: Deterministic pre-flight verification for quantum circuits: catches silent transpiler blowups, topology traps and noise-only 'signals' before you spend hardware time.
License: MIT
License-File: LICENSE
Keywords: noise,qiskit,quantum,transpiler,verification
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Python: >=3.10
Requires-Dist: numpy
Requires-Dist: qiskit-aer
Requires-Dist: qiskit>=2.0
Provides-Extra: all
Requires-Dist: cudaq; extra == 'all'
Requires-Dist: pennylane>=0.38; extra == 'all'
Requires-Dist: pytest>=7; extra == 'all'
Requires-Dist: pytket; extra == 'all'
Requires-Dist: pytket-qiskit; extra == 'all'
Provides-Extra: cuda
Requires-Dist: cudaq; extra == 'cuda'
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == 'dev'
Provides-Extra: pennylane
Requires-Dist: pennylane>=0.38; extra == 'pennylane'
Provides-Extra: tket
Requires-Dist: pytket; extra == 'tket'
Requires-Dist: pytket-qiskit; extra == 'tket'
Description-Content-Type: text/markdown

# py-quantum-verifier

Deterministic, offline pre-flight checks for quantum circuits. Run them before you spend hardware time.

```python
from py_quantum_verifier import verify_experiment, falsify_claim
from qiskit import QuantumCircuit
qc = QuantumCircuit(2, 2); qc.h(0); qc.cx(0, 1); qc.measure([0, 1], [0, 1])
print(verify_experiment(qc, "ionq-forte-1"))                 # GO (ionq-forte-1): All checks passed.
print(falsify_claim(qc, "ionq-forte-1", ["00", "11"]).verdict)  # SUPPORTED
```

No API tokens, no network, no GPU. Everything runs on Qiskit Aer with built-in device models.

## Install

```bash
pip install py-quantum-verifier                  # core: qiskit>=2.0, qiskit-aer, numpy
pip install "py-quantum-verifier[pennylane]"     # + analyze_circuit_gradients
pip install "py-quantum-verifier[cuda]"          # + cudaq (Linux/macOS with CUDA-Q wheels)
pip install "py-quantum-verifier[tket]"          # + Qiskit-vs-TKET diffing
pip install "py-quantum-verifier[all]"           # everything, incl. pytest
```

Optional integrations are imported lazily inside `try/except ImportError`, so the core
install is small and identical on macOS, Windows and Linux. `optional_features()` reports
which extras are importable.

## Real bugs this catches

**Silent gate-synthesis blowup (`rzz` on IonQ-style native gates).** IonQ's native `ZZ`
gate is tunable, so a logical `rzz(θ)` should compile to exactly **one** native gate:
`RZZ(θ) == ZZ(θ/2π)`. If the compiler has no direct `rzz → zz` equivalence it silently falls
back to a generic `cx` decomposition: **2** native two-qubit gates plus a pile of single-qubit
gates. The circuit still runs and still "looks right"; it just carries double the two-qubit
error. `gate_synthesis_check` compares logical and native gate counts and blocks it:

```python
from py_quantum_verifier import gate_synthesis_check, get_device, verify_experiment

device = get_device("ionq-forte-1").with_native_equivalences(direct_rzz=False)  # unpatched compiler
verify_experiment(qc_with_rzz, device)
# BLOCK: 1 logical two-qubit gate(s) became 2 native two-qubit gate(s) (2.0x, limit 1.5x for tunable ...)
```

Limits are device-aware: on CZ/CX/ECR-native hardware an arbitrary-angle `rzz` legitimately
needs 2 gates, so that is not flagged there (limit 2.5x).

**Topology traps.** On heavy-hex devices a qubit with more than 3 interaction partners forces
SWAP routing that multiplies two-qubit gate count. `topology_check` flags it before transpiling.
All-to-all devices report "not applicable" rather than silently passing.

**Noise passed off as a signal.** A forgotten `h` before a `cx` gives `00` at 99%, which looks
like a great Bell-state result. `falsify_claim` builds a control with the entangling gates removed,
runs both under the same noise model, and compares: readout bias and SPAM hit both arms equally
and cancel. If the control reproduces the signal, the claim is `NOT_SUPPORTED`.

**Claims the shot budget can't support.** `verify_experiment` computes the shots needed to separate
a claimed amplification from uniform noise and blocks under-powered runs up front.

**Fragile winners.** `find_robust_circuit` ranks candidates by mean minus variance under the device
noise model and re-validates the winner on a seed it was never scored on, so a circuit that wins
only in ideal simulation loses.

## Benchmark (synthetic suite)

These numbers describe this 30-circuit test suite, not real hardware.

30 circuits (Bell, GHZ incl. star fan-out, QAOA, hardware-efficient ansätze, `rzz` pairs/chains, plus
deliberately broken and buggy-compiler variants) × 2 device presets, 4096 shots, fixed seeds,
produced by `benchmarks/run_benchmark.py`:

| Metric | ibm-heron | ionq-forte-1 |
|---|---|---|
| Circuits run | 30 | 30 |
| `verify_experiment` GO | 28 (93.3%) | 22 (73.3%) |
| `verify_experiment` BLOCK | 2 (6.7%) | 8 (26.7%) |
| &nbsp;&nbsp;of which gate-synthesis inflation | 0 | 8 |
| &nbsp;&nbsp;of which topology (routing) | 2 | 0 |
| &nbsp;&nbsp;of which ground-truth claim failed | 0 | 0 |
| Buggy-compiler `rzz` variants caught | 0/8 (n/a: cz-native, 2 cz is the minimum) | 8/8 |
| Clean-compiler `rzz` circuits falsely blocked | 0/9 | 0/9 |
| Fake-entanglement circuits (missing `h`) passed by `verify_experiment` | 3/3 | 3/3 |
| Fake-entanglement circuits rejected by `falsify_claim` | 3/3 | 3/3 |
| Non-broken circuits rejected by `falsify_claim` | 2/27 | 2/27 |

Takeaways:
- The 8 IonQ blocks are exactly the 8 buggy-compiler variants; none of the 9 clean `rzz` circuits is flagged.
- The fake-entanglement circuits (a `cx` on `|0>`, or an all-zero-parameter ansatz) show a ~99% "Bell/GHZ signal", so
  `verify_experiment` passes them. Only the control-circuit test in `falsify_claim` rejects them, which is why both exist.
- The 2 non-broken `falsify_claim` rejections are the `rzz(0.3)` pair and its buggy-compiler duplicate: a rotation that
  small can't be separated from noise.

Reproduce:

```bash
pip install -e ".[dev]" && python benchmarks/run_benchmark.py --json benchmarks/results.json
```

## API

| Function | What it does |
|---|---|
| `verify_experiment(circuit, device, *, shots, expected_marked_bitstrings, expected_amplification, tolerance)` | Parse → semantic → topology → shot-budget → ideal sim → native translation + synthesis check → noisy sim → ground truth. Returns a `Verdict`. |
| `falsify_claim(circuit, device, marked_bitstrings=None)` | Control-circuit test. With marked bitstrings: isolated effect size + one-sided z-test. Without: TVD vs. the sampling-noise floor. |
| `find_robust_circuit(candidates, device, marked_bitstrings)` | Noise-aware candidate ranking with held-out validation. |
| `gate_synthesis_check(circuit, device)` | Native-gate inflation check; can also audit your own `transpiled=` circuit. |
| `analyze_circuit_gradients(build_fn, params, n_qubits)` | Barren-plateau / dead-parameter check via PennyLane autodiff. Raises an install hint if PennyLane is missing. |
| `diff_compilers(circuit, device)` | Qiskit vs. TKET onto the device's native basis; each output is checked by exact unitary equivalence (≤12 qubits). Degrades to Qiskit-only without TKET. |

`circuit` is a `QuantumCircuit` or an OpenQASM 2/3 string. `Verdict` has `.status` (`"GO"`/`"BLOCK"`),
`.reason`, `.checks` (every stage's raw result) and `.metrics`.

### Devices and offline noise

Presets: `ionq-forte-1` (all-to-all, native `zz`), `ibm-heron` (heavy-hex degree 3, native `cz`), `ideal`.
Noise rates are representative order-of-magnitude figures, **not live calibration data**. Supply your own:

```python
from py_quantum_verifier import get_device
dev = get_device("ibm-heron").with_noise(p1=3e-4, p2=5e-3, readout=2e-2)
```

The native-gate models are offline approximations (single-qubit gates compile to `rz`/`sx`/`x`, with `rz`
treated as a free frame change). They reproduce the structural failure modes above, not vendor-exact
pulse counts. Treat a `GO` as "passed these checks", not as a hardware guarantee.

## Limitations

- The "buggy compiler" is emulated (`Device.with_native_equivalences(direct_rzz=False)`), not a run of IonQ's real software.
- Noise rates are representative presets, not live calibration data.
- A `GO` means the circuit passed these checks, not that it will work on hardware.

## Development

```bash
pip install -e ".[dev]"
pytest
```

MIT licensed.
