FAILURE MAP
← Case archive

FA-91281 / Quantum circuit simulation / Open access

Rotation merger keeps a merged identity rotation · case 01

rz(3pi/4) followed by rz(5pi/4) leaves an rz(0) in the output.

Verified by executionVariant 1 · 7 checks per implementationDownload source bundle ↓JSON ↗

ROOT CAUSE

After merging, a zero angle is written back instead of removing the gate.

VERIFIED REPAIR

Pop the merged gate when the normalized angle is 0.

Unsuccessful approach: The attempted repair compares the normalized angle to 2, which normalization never produces.

Case contract

Input a gate list: ["rz", q, angle] (angle in units of pi as a fraction string), ["x", q], ["h", q], ["cx", c, t]. Scan left to right; a new gate looks back to the last op touching its qubit, except that rz may look past a cx whose control is that qubit. rz merges into a found rz, angles normalized to (-1, 1] and zero rotations dropped; x or h cancels a found identical gate. Return the optimized list with angles as fraction strings.

Why this case matters

Rotation merging and cancellation are the cheapest transpiler wins; commutation or normalization slips change circuit semantics.

1 / The failure

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(x):
    ops = x
    out = []
    def touches(op, q):
        if op[0] == 'cx':
            return q in (op[1], op[2])
        return op[1] == q
    def norm(a):
        a = a % 2
        if a > 1:
            a -= 2
        return a
    for op in ops:
        name = op[0]
        if name == 'cx':
            out.append(['cx', op[1], op[2]])
            continue
        q = op[1]
        j = len(out) - 1
        while j >= 0:
            prev = out[j]
            if not touches(prev, q):
                j -= 1
                continue
            if name == 'rz' and prev[0] == 'cx' and prev[1] == q:
                j -= 1
                continue
            break
        if name == 'rz':
            ang = Fraction(op[2])
            if j >= 0 and out[j][0] == 'rz':
                merged = norm(out[j][2] + ang)
                out[j][2] = merged
            else:
                ang = norm(ang)
                if ang != 0:
                    out.append(['rz', q, ang])
        elif name in ('x', 'h') and j >= 0 and out[j][0] == name:
            out.pop(j)
        else:
            out.append([name, q])
    return [[o[0], o[1], str(o[2])] if o[0] == 'rz' else o for o in out]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: merge cancels to zero', [['rz', 0, '3/4'], ['rz', 0, '5/4']], []], ['regression: random peephole 0', [['rz', 0, '1/2'], ['rz', 0, '-1/2'], ['rz', 1, '3/2'], ['rz', 2, '0'], ['rz', 1, '2'], ['x', 2], ['cx', 1, 0], ['x', 2]], [['rz', 1, '-1/2'], ['cx', 1, 0]]], ['regression: random peephole 7', [['rz', 1, '3/2'], ['cx', 2, 0], ['rz', 0, '0'], ['rz', 0, '3/2'], ['cx', 0, 1], ['rz', 0, '3'], ['rz', 2, '-1'], ['rz', 0, '3/2']], [['rz', 1, '-1/2'], ['cx', 2, 0], ['cx', 0, 1], ['rz', 2, '1']]], ['control: rz merges to pi', [['rz', 0, '1/2'], ['rz', 0, '1/2']], [['rz', 0, '1']]], ['control: rz commutes through cx control', [['rz', 0, '1/4'], ['cx', 0, 1], ['rz', 0, '1/4']], [['rz', 0, '1/2'], ['cx', 0, 1]]], ['control: rz blocked by cx target', [['rz', 1, '1/4'], ['cx', 0, 1], ['rz', 1, '1/4']], [['rz', 1, '1/4'], ['cx', 0, 1], ['rz', 1, '1/4']]], ['control: unnormalized single rz', [['rz', 2, '5/2']], [['rz', 2, '1/2']]]], [['regression: random peephole 8', [['rz', 2, '1/4'], ['rz', 2, '3/4'], ['rz', 2, '1'], ['x', 0], ['rz', 2, '3/4']], [['x', 0], ['rz', 2, '3/4']]], ['regression: random peephole 43', [['h', 0], ['h', 0], ['rz', 1, '7/4'], ['rz', 1, '1/4'], ['rz', 0, '3/2'], ['cx', 2, 1], ['cx', 2, 0]], [['rz', 0, '-1/2'], ['cx', 2, 1], ['cx', 2, 0]]], ['regression: random peephole 7', [['rz', 1, '3/2'], ['cx', 2, 0], ['rz', 0, '0'], ['rz', 0, '3/2'], ['cx', 0, 1], ['rz', 0, '3'], ['rz', 2, '-1'], ['rz', 0, '3/2']], [['rz', 1, '-1/2'], ['cx', 2, 0], ['cx', 0, 1], ['rz', 2, '1']]], ['control: rz of pi alone', [['rz', 0, '1']], [['rz', 0, '1']]], ['control: x on different qubits', [['x', 0], ['x', 1]], [['x', 0], ['x', 1]]], ['control: x cancels across other qubit', [['x', 0], ['h', 1], ['x', 0]], [['h', 1]]], ['control: h blocked by rz', [['h', 0], ['rz', 0, '1/2'], ['h', 0]], [['h', 0], ['rz', 0, '1/2'], ['h', 0]]]], [['regression: random peephole 50', [['rz', 1, '-1/4'], ['rz', 1, '1/4'], ['h', 1]], [['h', 1]]], ['regression: merge cancels to zero', [['rz', 0, '3/4'], ['rz', 0, '5/4']], []], ['regression: random peephole 43', [['h', 0], ['h', 0], ['rz', 1, '7/4'], ['rz', 1, '1/4'], ['rz', 0, '3/2'], ['cx', 2, 1], ['cx', 2, 0]], [['rz', 0, '-1/2'], ['cx', 2, 1], ['cx', 2, 0]]], ['control: random peephole 1', [['cx', 2, 0], ['x', 0]], [['cx', 2, 0], ['x', 0]]], ['control: random peephole 2', [['h', 1], ['h', 0], ['h', 1]], [['h', 0]]], ['control: random peephole 3', [['x', 0], ['rz', 1, '1/2'], ['rz', 1, '1/4'], ['rz', 0, '-1'], ['cx', 2, 0], ['rz', 0, '0'], ['h', 0], ['cx', 1, 2]], [['x', 0], ['rz', 1, '3/4'], ['rz', 0, '1'], ['cx', 2, 0], ['h', 0], ['cx', 1, 2]]], ['control: random peephole 4', [['rz', 2, '5/4'], ['rz', 2, '-1/2'], ['rz', 1, '1/4'], ['rz', 1, '3'], ['cx', 1, 0], ['cx', 2, 0]], [['rz', 2, '3/4'], ['rz', 1, '-3/4'], ['cx', 1, 0], ['cx', 2, 0]]]], [['regression: random peephole 7', [['rz', 1, '3/2'], ['cx', 2, 0], ['rz', 0, '0'], ['rz', 0, '3/2'], ['cx', 0, 1], ['rz', 0, '3'], ['rz', 2, '-1'], ['rz', 0, '3/2']], [['rz', 1, '-1/2'], ['cx', 2, 0], ['cx', 0, 1], ['rz', 2, '1']]], ['regression: random peephole 8', [['rz', 2, '1/4'], ['rz', 2, '3/4'], ['rz', 2, '1'], ['x', 0], ['rz', 2, '3/4']], [['x', 0], ['rz', 2, '3/4']]], ['regression: random peephole 50', [['rz', 1, '-1/4'], ['rz', 1, '1/4'], ['h', 1]], [['h', 1]]], ['control: random peephole 5', [['x', 2], ['rz', 2, '3/2'], ['rz', 2, '1'], ['x', 1]], [['x', 2], ['rz', 2, '1/2'], ['x', 1]]], ['control: random peephole 6', [['rz', 1, '-1/4'], ['rz', 0, '1'], ['x', 0], ['x', 1], ['cx', 1, 0], ['rz', 0, '1'], ['rz', 2, '1/4'], ['x', 0]], [['rz', 1, '-1/4'], ['rz', 0, '1'], ['x', 0], ['x', 1], ['cx', 1, 0], ['rz', 0, '1'], ['rz', 2, '1/4'], ['x', 0]]], ['control: random peephole 9', [['cx', 2, 1], ['rz', 1, '-1/4'], ['rz', 0, '-1'], ['rz', 2, '0'], ['x', 0], ['rz', 1, '-1/2'], ['cx', 2, 1]], [['cx', 2, 1], ['rz', 1, '-3/4'], ['rz', 0, '1'], ['x', 0], ['cx', 2, 1]]], ['control: random peephole 10', [['cx', 1, 2], ['rz', 0, '5/4'], ['rz', 2, '7/4'], ['rz', 0, '-1/4'], ['h', 1], ['rz', 0, '3/2'], ['rz', 0, '7/4'], ['x', 2]], [['cx', 1, 2], ['rz', 0, '1/4'], ['rz', 2, '-1/4'], ['h', 1], ['x', 2]]]], [['regression: random peephole 48', [['rz', 0, '3'], ['rz', 0, '5/4'], ['h', 2], ['cx', 2, 1], ['h', 2], ['rz', 1, '1/2'], ['rz', 0, '-1/4']], [['h', 2], ['cx', 2, 1], ['h', 2], ['rz', 1, '1/2']]], ['regression: random peephole 50', [['rz', 1, '-1/4'], ['rz', 1, '1/4'], ['h', 1]], [['h', 1]]], ['regression: random peephole 0', [['rz', 0, '1/2'], ['rz', 0, '-1/2'], ['rz', 1, '3/2'], ['rz', 2, '0'], ['rz', 1, '2'], ['x', 2], ['cx', 1, 0], ['x', 2]], [['rz', 1, '-1/2'], ['cx', 1, 0]]], ['control: random peephole 11', [['rz', 0, '-1/2'], ['h', 1], ['x', 2], ['cx', 0, 1], ['rz', 1, '3/2'], ['h', 1], ['cx', 2, 0], ['cx', 1, 0]], [['rz', 0, '-1/2'], ['h', 1], ['x', 2], ['cx', 0, 1], ['rz', 1, '-1/2'], ['h', 1], ['cx', 2, 0], ['cx', 1, 0]]], ['control: random peephole 12', [['x', 2], ['h', 0]], [['x', 2], ['h', 0]]], ['control: random peephole 13', [['h', 1], ['rz', 0, '1/2'], ['rz', 2, '5/4']], [['h', 1], ['rz', 0, '1/2'], ['rz', 2, '-3/4']]], ['control: random peephole 14', [['rz', 2, '7/4'], ['cx', 0, 2], ['h', 2], ['h', 0], ['h', 0], ['rz', 0, '1/2'], ['cx', 2, 1], ['rz', 1, '1/2']], [['rz', 2, '-1/4'], ['cx', 0, 2], ['h', 2], ['rz', 0, '1/2'], ['cx', 2, 1], ['rz', 1, '1/2']]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
regression: merge cancels to zero[['rz', 0, '0']][]Failed
regression: random peephole 0[['rz', 0, '0'], ['rz', 1, '-1/2'], ['cx', 1, 0]][['rz', 1, '-1/2'], ['cx', 1, 0]]Failed
regression: random peephole 7[['rz', 1, '-1/2'], ['cx', 2, 0], ['rz', 0, '0'], ['cx', 0, 1], ['rz', 2, '1']][['rz', 1, '-1/2'], ['cx', 2, 0], ['cx', 0, 1], ['rz', 2, '1']]Failed
control: rz merges to pi[['rz', 0, '1']][['rz', 0, '1']]Passed
control: rz commutes through cx control[['rz', 0, '1/2'], ['cx', 0, 1]][['rz', 0, '1/2'], ['cx', 0, 1]]Passed
control: rz blocked by cx target[['rz', 1, '1/4'], ['cx', 0, 1], ['rz', 1, '1/4']][['rz', 1, '1/4'], ['cx', 0, 1], ['rz', 1, '1/4']]Passed
control: unnormalized single rz[['rz', 2, '1/2']][['rz', 2, '1/2']]Passed

SHA-256 / 819a7e5bee35828b67f7c4585cf554d525052a80a0fdced8fb1879813680a1b5

2 / The unsuccessful fix

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(x):
    ops = x
    out = []
    def touches(op, q):
        if op[0] == 'cx':
            return q in (op[1], op[2])
        return op[1] == q
    def norm(a):
        a = a % 2
        if a > 1:
            a -= 2
        return a
    for op in ops:
        name = op[0]
        if name == 'cx':
            out.append(['cx', op[1], op[2]])
            continue
        q = op[1]
        j = len(out) - 1
        while j >= 0:
            prev = out[j]
            if not touches(prev, q):
                j -= 1
                continue
            if name == 'rz' and prev[0] == 'cx' and prev[1] == q:
                j -= 1
                continue
            break
        if name == 'rz':
            ang = Fraction(op[2])
            if j >= 0 and out[j][0] == 'rz':
                merged = norm(out[j][2] + ang)
                if merged == 2:
                    out.pop(j)
                else:
                    out[j][2] = merged
            else:
                ang = norm(ang)
                if ang != 0:
                    out.append(['rz', q, ang])
        elif name in ('x', 'h') and j >= 0 and out[j][0] == name:
            out.pop(j)
        else:
            out.append([name, q])
    return [[o[0], o[1], str(o[2])] if o[0] == 'rz' else o for o in out]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: merge cancels to zero', [['rz', 0, '3/4'], ['rz', 0, '5/4']], []], ['regression: random peephole 0', [['rz', 0, '1/2'], ['rz', 0, '-1/2'], ['rz', 1, '3/2'], ['rz', 2, '0'], ['rz', 1, '2'], ['x', 2], ['cx', 1, 0], ['x', 2]], [['rz', 1, '-1/2'], ['cx', 1, 0]]], ['regression: random peephole 7', [['rz', 1, '3/2'], ['cx', 2, 0], ['rz', 0, '0'], ['rz', 0, '3/2'], ['cx', 0, 1], ['rz', 0, '3'], ['rz', 2, '-1'], ['rz', 0, '3/2']], [['rz', 1, '-1/2'], ['cx', 2, 0], ['cx', 0, 1], ['rz', 2, '1']]], ['control: rz merges to pi', [['rz', 0, '1/2'], ['rz', 0, '1/2']], [['rz', 0, '1']]], ['control: rz commutes through cx control', [['rz', 0, '1/4'], ['cx', 0, 1], ['rz', 0, '1/4']], [['rz', 0, '1/2'], ['cx', 0, 1]]], ['control: rz blocked by cx target', [['rz', 1, '1/4'], ['cx', 0, 1], ['rz', 1, '1/4']], [['rz', 1, '1/4'], ['cx', 0, 1], ['rz', 1, '1/4']]], ['control: unnormalized single rz', [['rz', 2, '5/2']], [['rz', 2, '1/2']]]], [['regression: random peephole 8', [['rz', 2, '1/4'], ['rz', 2, '3/4'], ['rz', 2, '1'], ['x', 0], ['rz', 2, '3/4']], [['x', 0], ['rz', 2, '3/4']]], ['regression: random peephole 43', [['h', 0], ['h', 0], ['rz', 1, '7/4'], ['rz', 1, '1/4'], ['rz', 0, '3/2'], ['cx', 2, 1], ['cx', 2, 0]], [['rz', 0, '-1/2'], ['cx', 2, 1], ['cx', 2, 0]]], ['regression: random peephole 7', [['rz', 1, '3/2'], ['cx', 2, 0], ['rz', 0, '0'], ['rz', 0, '3/2'], ['cx', 0, 1], ['rz', 0, '3'], ['rz', 2, '-1'], ['rz', 0, '3/2']], [['rz', 1, '-1/2'], ['cx', 2, 0], ['cx', 0, 1], ['rz', 2, '1']]], ['control: rz of pi alone', [['rz', 0, '1']], [['rz', 0, '1']]], ['control: x on different qubits', [['x', 0], ['x', 1]], [['x', 0], ['x', 1]]], ['control: x cancels across other qubit', [['x', 0], ['h', 1], ['x', 0]], [['h', 1]]], ['control: h blocked by rz', [['h', 0], ['rz', 0, '1/2'], ['h', 0]], [['h', 0], ['rz', 0, '1/2'], ['h', 0]]]], [['regression: random peephole 50', [['rz', 1, '-1/4'], ['rz', 1, '1/4'], ['h', 1]], [['h', 1]]], ['regression: merge cancels to zero', [['rz', 0, '3/4'], ['rz', 0, '5/4']], []], ['regression: random peephole 43', [['h', 0], ['h', 0], ['rz', 1, '7/4'], ['rz', 1, '1/4'], ['rz', 0, '3/2'], ['cx', 2, 1], ['cx', 2, 0]], [['rz', 0, '-1/2'], ['cx', 2, 1], ['cx', 2, 0]]], ['control: random peephole 1', [['cx', 2, 0], ['x', 0]], [['cx', 2, 0], ['x', 0]]], ['control: random peephole 2', [['h', 1], ['h', 0], ['h', 1]], [['h', 0]]], ['control: random peephole 3', [['x', 0], ['rz', 1, '1/2'], ['rz', 1, '1/4'], ['rz', 0, '-1'], ['cx', 2, 0], ['rz', 0, '0'], ['h', 0], ['cx', 1, 2]], [['x', 0], ['rz', 1, '3/4'], ['rz', 0, '1'], ['cx', 2, 0], ['h', 0], ['cx', 1, 2]]], ['control: random peephole 4', [['rz', 2, '5/4'], ['rz', 2, '-1/2'], ['rz', 1, '1/4'], ['rz', 1, '3'], ['cx', 1, 0], ['cx', 2, 0]], [['rz', 2, '3/4'], ['rz', 1, '-3/4'], ['cx', 1, 0], ['cx', 2, 0]]]], [['regression: random peephole 7', [['rz', 1, '3/2'], ['cx', 2, 0], ['rz', 0, '0'], ['rz', 0, '3/2'], ['cx', 0, 1], ['rz', 0, '3'], ['rz', 2, '-1'], ['rz', 0, '3/2']], [['rz', 1, '-1/2'], ['cx', 2, 0], ['cx', 0, 1], ['rz', 2, '1']]], ['regression: random peephole 8', [['rz', 2, '1/4'], ['rz', 2, '3/4'], ['rz', 2, '1'], ['x', 0], ['rz', 2, '3/4']], [['x', 0], ['rz', 2, '3/4']]], ['regression: random peephole 50', [['rz', 1, '-1/4'], ['rz', 1, '1/4'], ['h', 1]], [['h', 1]]], ['control: random peephole 5', [['x', 2], ['rz', 2, '3/2'], ['rz', 2, '1'], ['x', 1]], [['x', 2], ['rz', 2, '1/2'], ['x', 1]]], ['control: random peephole 6', [['rz', 1, '-1/4'], ['rz', 0, '1'], ['x', 0], ['x', 1], ['cx', 1, 0], ['rz', 0, '1'], ['rz', 2, '1/4'], ['x', 0]], [['rz', 1, '-1/4'], ['rz', 0, '1'], ['x', 0], ['x', 1], ['cx', 1, 0], ['rz', 0, '1'], ['rz', 2, '1/4'], ['x', 0]]], ['control: random peephole 9', [['cx', 2, 1], ['rz', 1, '-1/4'], ['rz', 0, '-1'], ['rz', 2, '0'], ['x', 0], ['rz', 1, '-1/2'], ['cx', 2, 1]], [['cx', 2, 1], ['rz', 1, '-3/4'], ['rz', 0, '1'], ['x', 0], ['cx', 2, 1]]], ['control: random peephole 10', [['cx', 1, 2], ['rz', 0, '5/4'], ['rz', 2, '7/4'], ['rz', 0, '-1/4'], ['h', 1], ['rz', 0, '3/2'], ['rz', 0, '7/4'], ['x', 2]], [['cx', 1, 2], ['rz', 0, '1/4'], ['rz', 2, '-1/4'], ['h', 1], ['x', 2]]]], [['regression: random peephole 48', [['rz', 0, '3'], ['rz', 0, '5/4'], ['h', 2], ['cx', 2, 1], ['h', 2], ['rz', 1, '1/2'], ['rz', 0, '-1/4']], [['h', 2], ['cx', 2, 1], ['h', 2], ['rz', 1, '1/2']]], ['regression: random peephole 50', [['rz', 1, '-1/4'], ['rz', 1, '1/4'], ['h', 1]], [['h', 1]]], ['regression: random peephole 0', [['rz', 0, '1/2'], ['rz', 0, '-1/2'], ['rz', 1, '3/2'], ['rz', 2, '0'], ['rz', 1, '2'], ['x', 2], ['cx', 1, 0], ['x', 2]], [['rz', 1, '-1/2'], ['cx', 1, 0]]], ['control: random peephole 11', [['rz', 0, '-1/2'], ['h', 1], ['x', 2], ['cx', 0, 1], ['rz', 1, '3/2'], ['h', 1], ['cx', 2, 0], ['cx', 1, 0]], [['rz', 0, '-1/2'], ['h', 1], ['x', 2], ['cx', 0, 1], ['rz', 1, '-1/2'], ['h', 1], ['cx', 2, 0], ['cx', 1, 0]]], ['control: random peephole 12', [['x', 2], ['h', 0]], [['x', 2], ['h', 0]]], ['control: random peephole 13', [['h', 1], ['rz', 0, '1/2'], ['rz', 2, '5/4']], [['h', 1], ['rz', 0, '1/2'], ['rz', 2, '-3/4']]], ['control: random peephole 14', [['rz', 2, '7/4'], ['cx', 0, 2], ['h', 2], ['h', 0], ['h', 0], ['rz', 0, '1/2'], ['cx', 2, 1], ['rz', 1, '1/2']], [['rz', 2, '-1/4'], ['cx', 0, 2], ['h', 2], ['rz', 0, '1/2'], ['cx', 2, 1], ['rz', 1, '1/2']]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
regression: merge cancels to zero[['rz', 0, '0']][]Failed
regression: random peephole 0[['rz', 0, '0'], ['rz', 1, '-1/2'], ['cx', 1, 0]][['rz', 1, '-1/2'], ['cx', 1, 0]]Failed
regression: random peephole 7[['rz', 1, '-1/2'], ['cx', 2, 0], ['rz', 0, '0'], ['cx', 0, 1], ['rz', 2, '1']][['rz', 1, '-1/2'], ['cx', 2, 0], ['cx', 0, 1], ['rz', 2, '1']]Failed
control: rz merges to pi[['rz', 0, '1']][['rz', 0, '1']]Passed
control: rz commutes through cx control[['rz', 0, '1/2'], ['cx', 0, 1]][['rz', 0, '1/2'], ['cx', 0, 1]]Passed
control: rz blocked by cx target[['rz', 1, '1/4'], ['cx', 0, 1], ['rz', 1, '1/4']][['rz', 1, '1/4'], ['cx', 0, 1], ['rz', 1, '1/4']]Passed
control: unnormalized single rz[['rz', 2, '1/2']][['rz', 2, '1/2']]Passed

SHA-256 / a270712c782ba119ee0c41ada3c92403c143be7eec0235e7b006d22a219a3a2f

3 / The verified repair

Exit 0
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(x):
    ops = x
    out = []
    def touches(op, q):
        if op[0] == 'cx':
            return q in (op[1], op[2])
        return op[1] == q
    def norm(a):
        a = a % 2
        if a > 1:
            a -= 2
        return a
    for op in ops:
        name = op[0]
        if name == 'cx':
            out.append(['cx', op[1], op[2]])
            continue
        q = op[1]
        j = len(out) - 1
        while j >= 0:
            prev = out[j]
            if not touches(prev, q):
                j -= 1
                continue
            if name == 'rz' and prev[0] == 'cx' and prev[1] == q:
                j -= 1
                continue
            break
        if name == 'rz':
            ang = Fraction(op[2])
            if j >= 0 and out[j][0] == 'rz':
                merged = norm(out[j][2] + ang)
                if merged == 0:
                    out.pop(j)
                else:
                    out[j][2] = merged
            else:
                ang = norm(ang)
                if ang != 0:
                    out.append(['rz', q, ang])
        elif name in ('x', 'h') and j >= 0 and out[j][0] == name:
            out.pop(j)
        else:
            out.append([name, q])
    return [[o[0], o[1], str(o[2])] if o[0] == 'rz' else o for o in out]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: merge cancels to zero', [['rz', 0, '3/4'], ['rz', 0, '5/4']], []], ['regression: random peephole 0', [['rz', 0, '1/2'], ['rz', 0, '-1/2'], ['rz', 1, '3/2'], ['rz', 2, '0'], ['rz', 1, '2'], ['x', 2], ['cx', 1, 0], ['x', 2]], [['rz', 1, '-1/2'], ['cx', 1, 0]]], ['regression: random peephole 7', [['rz', 1, '3/2'], ['cx', 2, 0], ['rz', 0, '0'], ['rz', 0, '3/2'], ['cx', 0, 1], ['rz', 0, '3'], ['rz', 2, '-1'], ['rz', 0, '3/2']], [['rz', 1, '-1/2'], ['cx', 2, 0], ['cx', 0, 1], ['rz', 2, '1']]], ['control: rz merges to pi', [['rz', 0, '1/2'], ['rz', 0, '1/2']], [['rz', 0, '1']]], ['control: rz commutes through cx control', [['rz', 0, '1/4'], ['cx', 0, 1], ['rz', 0, '1/4']], [['rz', 0, '1/2'], ['cx', 0, 1]]], ['control: rz blocked by cx target', [['rz', 1, '1/4'], ['cx', 0, 1], ['rz', 1, '1/4']], [['rz', 1, '1/4'], ['cx', 0, 1], ['rz', 1, '1/4']]], ['control: unnormalized single rz', [['rz', 2, '5/2']], [['rz', 2, '1/2']]]], [['regression: random peephole 8', [['rz', 2, '1/4'], ['rz', 2, '3/4'], ['rz', 2, '1'], ['x', 0], ['rz', 2, '3/4']], [['x', 0], ['rz', 2, '3/4']]], ['regression: random peephole 43', [['h', 0], ['h', 0], ['rz', 1, '7/4'], ['rz', 1, '1/4'], ['rz', 0, '3/2'], ['cx', 2, 1], ['cx', 2, 0]], [['rz', 0, '-1/2'], ['cx', 2, 1], ['cx', 2, 0]]], ['regression: random peephole 7', [['rz', 1, '3/2'], ['cx', 2, 0], ['rz', 0, '0'], ['rz', 0, '3/2'], ['cx', 0, 1], ['rz', 0, '3'], ['rz', 2, '-1'], ['rz', 0, '3/2']], [['rz', 1, '-1/2'], ['cx', 2, 0], ['cx', 0, 1], ['rz', 2, '1']]], ['control: rz of pi alone', [['rz', 0, '1']], [['rz', 0, '1']]], ['control: x on different qubits', [['x', 0], ['x', 1]], [['x', 0], ['x', 1]]], ['control: x cancels across other qubit', [['x', 0], ['h', 1], ['x', 0]], [['h', 1]]], ['control: h blocked by rz', [['h', 0], ['rz', 0, '1/2'], ['h', 0]], [['h', 0], ['rz', 0, '1/2'], ['h', 0]]]], [['regression: random peephole 50', [['rz', 1, '-1/4'], ['rz', 1, '1/4'], ['h', 1]], [['h', 1]]], ['regression: merge cancels to zero', [['rz', 0, '3/4'], ['rz', 0, '5/4']], []], ['regression: random peephole 43', [['h', 0], ['h', 0], ['rz', 1, '7/4'], ['rz', 1, '1/4'], ['rz', 0, '3/2'], ['cx', 2, 1], ['cx', 2, 0]], [['rz', 0, '-1/2'], ['cx', 2, 1], ['cx', 2, 0]]], ['control: random peephole 1', [['cx', 2, 0], ['x', 0]], [['cx', 2, 0], ['x', 0]]], ['control: random peephole 2', [['h', 1], ['h', 0], ['h', 1]], [['h', 0]]], ['control: random peephole 3', [['x', 0], ['rz', 1, '1/2'], ['rz', 1, '1/4'], ['rz', 0, '-1'], ['cx', 2, 0], ['rz', 0, '0'], ['h', 0], ['cx', 1, 2]], [['x', 0], ['rz', 1, '3/4'], ['rz', 0, '1'], ['cx', 2, 0], ['h', 0], ['cx', 1, 2]]], ['control: random peephole 4', [['rz', 2, '5/4'], ['rz', 2, '-1/2'], ['rz', 1, '1/4'], ['rz', 1, '3'], ['cx', 1, 0], ['cx', 2, 0]], [['rz', 2, '3/4'], ['rz', 1, '-3/4'], ['cx', 1, 0], ['cx', 2, 0]]]], [['regression: random peephole 7', [['rz', 1, '3/2'], ['cx', 2, 0], ['rz', 0, '0'], ['rz', 0, '3/2'], ['cx', 0, 1], ['rz', 0, '3'], ['rz', 2, '-1'], ['rz', 0, '3/2']], [['rz', 1, '-1/2'], ['cx', 2, 0], ['cx', 0, 1], ['rz', 2, '1']]], ['regression: random peephole 8', [['rz', 2, '1/4'], ['rz', 2, '3/4'], ['rz', 2, '1'], ['x', 0], ['rz', 2, '3/4']], [['x', 0], ['rz', 2, '3/4']]], ['regression: random peephole 50', [['rz', 1, '-1/4'], ['rz', 1, '1/4'], ['h', 1]], [['h', 1]]], ['control: random peephole 5', [['x', 2], ['rz', 2, '3/2'], ['rz', 2, '1'], ['x', 1]], [['x', 2], ['rz', 2, '1/2'], ['x', 1]]], ['control: random peephole 6', [['rz', 1, '-1/4'], ['rz', 0, '1'], ['x', 0], ['x', 1], ['cx', 1, 0], ['rz', 0, '1'], ['rz', 2, '1/4'], ['x', 0]], [['rz', 1, '-1/4'], ['rz', 0, '1'], ['x', 0], ['x', 1], ['cx', 1, 0], ['rz', 0, '1'], ['rz', 2, '1/4'], ['x', 0]]], ['control: random peephole 9', [['cx', 2, 1], ['rz', 1, '-1/4'], ['rz', 0, '-1'], ['rz', 2, '0'], ['x', 0], ['rz', 1, '-1/2'], ['cx', 2, 1]], [['cx', 2, 1], ['rz', 1, '-3/4'], ['rz', 0, '1'], ['x', 0], ['cx', 2, 1]]], ['control: random peephole 10', [['cx', 1, 2], ['rz', 0, '5/4'], ['rz', 2, '7/4'], ['rz', 0, '-1/4'], ['h', 1], ['rz', 0, '3/2'], ['rz', 0, '7/4'], ['x', 2]], [['cx', 1, 2], ['rz', 0, '1/4'], ['rz', 2, '-1/4'], ['h', 1], ['x', 2]]]], [['regression: random peephole 48', [['rz', 0, '3'], ['rz', 0, '5/4'], ['h', 2], ['cx', 2, 1], ['h', 2], ['rz', 1, '1/2'], ['rz', 0, '-1/4']], [['h', 2], ['cx', 2, 1], ['h', 2], ['rz', 1, '1/2']]], ['regression: random peephole 50', [['rz', 1, '-1/4'], ['rz', 1, '1/4'], ['h', 1]], [['h', 1]]], ['regression: random peephole 0', [['rz', 0, '1/2'], ['rz', 0, '-1/2'], ['rz', 1, '3/2'], ['rz', 2, '0'], ['rz', 1, '2'], ['x', 2], ['cx', 1, 0], ['x', 2]], [['rz', 1, '-1/2'], ['cx', 1, 0]]], ['control: random peephole 11', [['rz', 0, '-1/2'], ['h', 1], ['x', 2], ['cx', 0, 1], ['rz', 1, '3/2'], ['h', 1], ['cx', 2, 0], ['cx', 1, 0]], [['rz', 0, '-1/2'], ['h', 1], ['x', 2], ['cx', 0, 1], ['rz', 1, '-1/2'], ['h', 1], ['cx', 2, 0], ['cx', 1, 0]]], ['control: random peephole 12', [['x', 2], ['h', 0]], [['x', 2], ['h', 0]]], ['control: random peephole 13', [['h', 1], ['rz', 0, '1/2'], ['rz', 2, '5/4']], [['h', 1], ['rz', 0, '1/2'], ['rz', 2, '-3/4']]], ['control: random peephole 14', [['rz', 2, '7/4'], ['cx', 0, 2], ['h', 2], ['h', 0], ['h', 0], ['rz', 0, '1/2'], ['cx', 2, 1], ['rz', 1, '1/2']], [['rz', 2, '-1/4'], ['cx', 0, 2], ['h', 2], ['rz', 0, '1/2'], ['cx', 2, 1], ['rz', 1, '1/2']]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
regression: merge cancels to zero[][]Passed
regression: random peephole 0[['rz', 1, '-1/2'], ['cx', 1, 0]][['rz', 1, '-1/2'], ['cx', 1, 0]]Passed
regression: random peephole 7[['rz', 1, '-1/2'], ['cx', 2, 0], ['cx', 0, 1], ['rz', 2, '1']][['rz', 1, '-1/2'], ['cx', 2, 0], ['cx', 0, 1], ['rz', 2, '1']]Passed
control: rz merges to pi[['rz', 0, '1']][['rz', 0, '1']]Passed
control: rz commutes through cx control[['rz', 0, '1/2'], ['cx', 0, 1]][['rz', 0, '1/2'], ['cx', 0, 1]]Passed
control: rz blocked by cx target[['rz', 1, '1/4'], ['cx', 0, 1], ['rz', 1, '1/4']][['rz', 1, '1/4'], ['cx', 0, 1], ['rz', 1, '1/4']]Passed
control: unnormalized single rz[['rz', 2, '1/2']][['rz', 2, '1/2']]Passed

SHA-256 / 39de298ce98aeee09cc22e48e2ecf1bcb2c8eaeb75d1a157acbb6870ac98664a

Verification & scope

A deterministic bounded teaching model with a stipulated toy contract; amplitudes are rounded to fixed decimals for strict JSON output. It is not a production quantum SDK and claims no standards conformance. This reproducer isolates one failure mechanism. Results cover the supplied fixtures. Variants within a family share a test contract and should remain grouped when constructing evaluation splits. Related mechanisms with a shared evaluation_group must also remain together; these controlled models are not independent production incidents.

Observations recorded using Python 3.12.14 at 2026-09-29T14:51:34.427965+00:00.

Case digest / ebec10b2bd8c5695e3bf838560477ba38669cb383dd42556ff84b3b75e4cb658