LocalFoldPass¶
- class LocalFoldPass(scale_factor, selection='random', exclude=None, rng=None)[source]¶
Bases:
TransformationPassPer-gate folding with fractional scale-factor support.
Each unitary gate
Gis replaced byG · (G† · G)^k. For a target scale factorson a circuit withdunitary gates:k = (s - 1) // 2 # base folds applied to every gate remainder = s - (1 + 2k) n = round(remainder · d / 2) # gates receiving one extra fold
ngates are then selected for the extra fold according toselection, yielding a post-transform gate count ofd · (1 + 2k) + 2nand an effective scale factor of1 + 2k + 2n/d. Non-unitary instructions (measure,reset,barrier) are skipped. Mirrors Mitiq’sfold_gates_from_left/_from_right/_at_randomselection strategies.Excluded gates (see
exclude) are removed from the candidate pool before thek/narithmetic. A gate is excluded if its op name matches any entry inexcludeor its arity matches one of the shorthands ("single","double","triple").The requested
scale_factortherefore applies to the foldable subset only: excluded gates appear once in the output and the circuit-wide noise scale is effectively lower than requested. This matches Mitiq’sfold_allexcludesemantics.The achievable scales form a grid of granularity
2/d— for smalldthe effective scale may differ from the request. Useeffective_scale()to query the realised value. Example, for a 4-gate circuit:requested s | k | n | size | effective s ---------------------------------------- 1.5 | 0 | 1 | 6 | 1.5 2.0 | 0 | 2 | 8 | 2.0 3.0 | 1 | 0 | 12 | 3.0 3.5 | 1 | 1 | 14 | 3.5
Complements
GlobalFoldPassfor fine-grained noise scaling on deep circuits where global folding is too coarse. UnlikeGlobalFoldPass, this pass introduces randomness at fractional scales (whichngates receive the extra fold) — pass a fixedrngor useselection="from_left"/"from_right"for deterministic output.- Parameters:
scale_factor (
float) – Real number ≥ 1.1.0is a pass-through.selection (
Literal['random','from_left','from_right']) – Whichngates receive the extra fold:"random"(default) — uniformly sampled without replacement;"from_left"— the firstngates in topological order;"from_right"— the lastngates in topological order.exclude (
set[str] |None) – Optional set of op names ("h","cx", …) and/or arity shorthands ("single","double","triple") to skip during folding. Unknown names match nothing (harmless).rng (
Random|None) – Optionalrandom.Randomfor reproducible selection whenselection="random". Ignored otherwise. WhenNone, uses a freshrandom.Random.
- Raises:
ValueError – If
scale_factor< 1 orselectionis unknown.
Methods Summary
effective_scale(dag)Scale factor actually realised on
dag(may differ from the requested value when the foldable pool is too small for the fractional part to round cleanly).run(dag)Fold
dagin place and return the mutated DAG.Methods Documentation
- effective_scale(dag)[source]¶
Scale factor actually realised on
dag(may differ from the requested value when the foldable pool is too small for the fractional part to round cleanly).- Return type:
- run(dag)[source]¶
Fold
dagin place and return the mutated DAG.The input is consumed — callers that need the original should deepcopy before invoking the pass. Matches the standard Qiskit
TransformationPasscontract.- Return type:
- Returns:
The same
dagobject, mutated to contain the folded circuit.