source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
N = 1
observations = []
def solve(x):
    bx, by, bz = [float(v) for v in x[0]]
    for ch in x[1]:
        kind = ch[0]
        if kind == 'depol':
            p = float(ch[1])
            bx, by, bz = (1 - p) * bx, (1 - p) * by, (1 - p) * bz
        elif kind == 'dephase':
            p = float(ch[1])
            bx, by = (1 - 2 * p) * bx, (1 - 2 * p) * by
        elif kind == 'bitflip':
            p = float(ch[1])
            by, bz = (1 - 2 * p) * by, (1 - 2 * p) * bz
        elif kind == 'ampdamp':
            g = float(ch[1])
            s = math.sqrt(1 - g)
            bx, by, bz = s * bx, (1 - g) * by, g + (1 - g) * bz
        elif kind == 'h':
            bx, by, bz = bz, -by, bx
        elif kind == 's':
            bx, by = -by, bx
        elif kind == 'x':
            by, bz = -by, -bz
    return [round(v, 6) + 0.0 for v in (bx, by, bz)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: random channel chain 1', [['0.2317', '0.4662', '-0.3393'], [['h'], ['ampdamp', '0.1'], ['h'], ['h']]], [-0.321888, -0.442276, 0.30853]], ['regression: random channel chain 7', [['0.2531', '-0.7095', '0.5685'], [['ampdamp', '0.2'], ['bitflip', '0.1'], ['dephase', '0.2'], ['x']]], [0.135828, 0.304606, -0.52384]], ['regression: random channel chain 9', [['0.2932', '0.3716', '-0.2949'], [['x'], ['bitflip', '0.2'], ['ampdamp', '0.3']]], [0.245309, -0.186542, 0.423858]], ['control: amplitude damping from |1>', [['0', '0', '-1'], [['ampdamp', '0.5']]], [0.0, 0.0, 0.0]], ['control: dephasing plus state', [['1', '0', '0'], [['dephase', '0.25']]], [0.5, 0.0, 0.0]], ['control: bit flip on |0>', [['0', '0', '1'], [['bitflip', '0.25']]], [0.0, 0.0, 0.5]], ['control: hadamard on y', [['0', '1', '0'], [['h']]], [0.0, -1.0, 0.0]]], [['regression: random channel chain 10', [['0.0325', '0.65', '0.2184'], [['depol', '0.2'], ['ampdamp', '0.05'], ['bitflip', '0.05'], ['h']]], [0.194386, -0.45615, 0.025342]], ['regression: random channel chain 15', [['0.2915', '0.4987', '-0.2812'], [['ampdamp', '0.1'], ['s'], ['x']]], [-0.473108, -0.276541, 0.15308]], ['regression: random channel chain 9', [['0.2932', '0.3716', '-0.2949'], [['x'], ['bitflip', '0.2'], ['ampdamp', '0.3']]], [0.245309, -0.186542, 0.423858]], ['control: s on x', [['1', '0', '0'], [['s']]], [0.0, 1.0, 0.0]], ['control: x on |+i>', [['0', '1', '0'], [['x']]], [0.0, -1.0, 0.0]], ['control: depolarize', [['0.6', '0', '0.8'], [['depol', '0.1']]], [0.54, 0.0, 0.72]], ['control: random channel chain 0', [['0.0691', '-0.4004', '0.0791'], [['dephase', '0.2'], ['bitflip', '0.3'], ['s']]], [0.096096, 0.04146, 0.03164]]], [['regression: random channel chain 26', [['-0.4382', '0.0441', '-0.3433'], [['depol', '0.1'], ['bitflip', '0.5'], ['ampdamp', '0.2']]], [-0.352744, 0.0, 0.2]], ['regression: random channel chain 27', [['-0.2675', '0.148', '0.3339'], [['s'], ['dephase', '0.1'], ['dephase', '0.05'], ['ampdamp', '0.1']]], [-0.101092, -0.182716, 0.40051]], ['regression: random channel chain 15', [['0.2915', '0.4987', '-0.2812'], [['ampdamp', '0.1'], ['s'], ['x']]], [-0.473108, -0.276541, 0.15308]], ['control: random channel chain 2', [['0.0766', '-0.6718', '0.0357'], [['s']]], [0.6718, 0.0766, 0.0357]], ['control: random channel chain 3', [['0.2389', '-0.1722', '0.2639'], [['dephase', '0.1'], ['bitflip', '0.2'], ['x']]], [0.19112, 0.082656, -0.15834]], ['control: random channel chain 4', [['-0.0477', '-0.2431', '-0.2391'], [['x'], ['depol', '0.5'], ['x'], ['bitflip', '0.1']]], [-0.02385, -0.09724, -0.09564]], ['control: random channel chain 5', [['-0.4729', '-0.0797', '-0.3692'], [['h'], ['s'], ['dephase', '0.1']]], [-0.06376, -0.29536, -0.4729]]], [['regression: random channel chain 32', [['0.089', '-0.4706', '-0.3715'], [['ampdamp', '0.2'], ['bitflip', '0.3'], ['ampdamp', '0.3']]], [0.066602, -0.140866, 0.272784]], ['regression: random channel chain 33', [['-0.1736', '-0.2281', '-0.3032'], [['ampdamp', '0.1'], ['h']]], [-0.17288, 0.216395, -0.164691]], ['regression: random channel chain 27', [['-0.2675', '0.148', '0.3339'], [['s'], ['dephase', '0.1'], ['dephase', '0.05'], ['ampdamp', '0.1']]], [-0.101092, -0.182716, 0.40051]], ['control: random channel chain 6', [['0.394', '0.1792', '-0.6952'], [['x']]], [0.394, -0.1792, 0.6952]], ['control: random channel chain 8', [['0.255', '-0.4128', '-0.2664'], [['x'], ['dephase', '0.05'], ['h'], ['depol', '0.05']]], [0.25308, -0.352944, 0.218025]], ['control: random channel chain 11', [['0.2608', '0.1361', '-0.2576'], [['x'], ['depol', '0.05']]], [0.24776, -0.129295, 0.24472]], ['control: random channel chain 12', [['0.3838', '0.2434', '0.1995'], [['x'], ['s'], ['depol', '0.5'], ['dephase', '0.05']]], [0.10953, 0.17271, -0.09975]]], [['regression: random channel chain 42', [['-0.1465', '0.5217', '-0.7391'], [['bitflip', '0.2'], ['x'], ['s'], ['ampdamp', '0.05']]], [0.305094, -0.142791, 0.471287]], ['regression: random channel chain 44', [['-0.8194', '0.3314', '0.4424'], [['h'], ['ampdamp', '0.5'], ['h'], ['depol', '0.05']]], [0.085785, 0.222618, 0.297183]], ['regression: random channel chain 32', [['0.089', '-0.4706', '-0.3715'], [['ampdamp', '0.2'], ['bitflip', '0.3'], ['ampdamp', '0.3']]], [0.066602, -0.140866, 0.272784]], ['control: random channel chain 13', [['-0.3842', '-0.4838', '0.2088'], [['h'], ['depol', '0.05']]], [0.19836, 0.45961, -0.36499]], ['control: random channel chain 14', [['-0.1159', '-0.3227', '0.1975'], [['bitflip', '0.3'], ['h'], ['bitflip', '0.1'], ['dephase', '0.3']]], [0.0316, 0.041306, -0.09272]], ['control: random channel chain 17', [['0.1445', '-0.5422', '-0.0485'], [['s'], ['s'], ['dephase', '0.2'], ['dephase', '0.05']]], [-0.07803, 0.292788, -0.0485]], ['control: random channel chain 18', [['-0.3894', '0.6131', '-0.5953'], [['bitflip', '0.05']]], [-0.3894, 0.55179, -0.53577]]]]
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)

prompt:
Coherences decay as (1-g) instead of sqrt(1-g), so T2 appears equal to T1/2 everywhere.

Input [[x, y, z], channels] (decimal strings). Apply in order: depol p (r -> (1-p) r), dephase p (x, y scaled by 1-2p), bitflip p (y, z scaled by 1-2p), ampdamp g (x, y scaled by sqrt(1-g), z -> g + (1-g) z toward |0> at z=+1), h ((x,y,z) -> (z,-y,x)), s ((x,y) -> (-y,x)), x ((y,z) -> (-y,-z)). Return the final vector rounded to 6 decimals.

split: open-access


meaning: Fixture-count band, not measured model difficulty

deps: stdlib

prompt:
A phase-flip probability of 0.5 leaves half the coherence instead of erasing it.

Input [[x, y, z], channels] (decimal strings). Apply in order: depol p (r -> (1-p) r), dephase p (x, y scaled by 1-2p), bitflip p (y, z scaled by 1-2p), ampdamp g (x, y scaled by sqrt(1-g), z -> g + (1-g) z toward |0> at z=+1), h ((x,y,z) -> (z,-y,x)), s ((x,y) -> (-y,x)), x ((y,z) -> (-y,-z)). Return the final vector rounded to 6 decimals.

split: open-access


prompt:
A bit flip leaves |0> at z = 1 while shrinking x.

Input [[x, y, z], channels] (decimal strings). Apply in order: depol p (r -> (1-p) r), dephase p (x, y scaled by 1-2p), bitflip p (y, z scaled by 1-2p), ampdamp g (x, y scaled by sqrt(1-g), z -> g + (1-g) z toward |0> at z=+1), h ((x,y,z) -> (z,-y,x)), s ((x,y) -> (-y,x)), x ((y,z) -> (-y,-z)). Return the final vector rounded to 6 decimals.

split: open-access


prompt:
H applied to |+i> stays at y = +1 instead of moving to y = -1.

Input [[x, y, z], channels] (decimal strings). Apply in order: depol p (r -> (1-p) r), dephase p (x, y scaled by 1-2p), bitflip p (y, z scaled by 1-2p), ampdamp g (x, y scaled by sqrt(1-g), z -> g + (1-g) z toward |0> at z=+1), h ((x,y,z) -> (z,-y,x)), s ((x,y) -> (-y,x)), x ((y,z) -> (-y,-z)). Return the final vector rounded to 6 decimals.

split: open-access


prompt:
S applied to |+> lands at y = -1 instead of y = +1.

Input [[x, y, z], channels] (decimal strings). Apply in order: depol p (r -> (1-p) r), dephase p (x, y scaled by 1-2p), bitflip p (y, z scaled by 1-2p), ampdamp g (x, y scaled by sqrt(1-g), z -> g + (1-g) z toward |0> at z=+1), h ((x,y,z) -> (z,-y,x)), s ((x,y) -> (-y,x)), x ((y,z) -> (-y,-z)). Return the final vector rounded to 6 decimals.

split: open-access


broken_source:
"""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: rz merges to pi', [['rz', 0, '1/2'], ['rz', 0, '1/2']], [['rz', 0, '1']]], ['regression: rz of pi alone', [['rz', 0, '1']], [['rz', 0, '1']]], ['repair check: 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: 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: merge cancels to zero', [['rz', 0, '3/4'], ['rz', 0, '5/4']], []], ['control: unnormalized single rz', [['rz', 2, '5/2']], [['rz', 2, '1/2']]]], [['regression: 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]]], ['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']]], ['repair check: 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: 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]]], ['control: random peephole 1', [['cx', 2, 0], ['x', 0]], [['cx', 2, 0], ['x', 0]]]], [['regression: random peephole 17', [['cx', 0, 2], ['x', 2], ['rz', 0, '1'], ['h', 0]], [['cx', 0, 2], ['x', 2], ['rz', 0, '1'], ['h', 0]]], ['regression: random peephole 20', [['rz', 1, '3'], ['cx', 0, 2], ['rz', 1, '2'], ['cx', 2, 0], ['rz', 1, '0'], ['cx', 1, 2], ['h', 0]], [['rz', 1, '1'], ['cx', 0, 2], ['cx', 2, 0], ['cx', 1, 2], ['h', 0]]], ['repair check: 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]]], ['control: random peephole 2', [['h', 1], ['h', 0], ['h', 1]], [['h', 0]]], ['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 8', [['rz', 2, '1/4'], ['rz', 2, '3/4'], ['rz', 2, '1'], ['x', 0], ['rz', 2, '3/4']], [['x', 0], ['rz', 2, '3/4']]], ['control: random peephole 12', [['x', 2], ['h', 0]], [['x', 2], ['h', 0]]]], [['regression: random peephole 22', [['rz', 2, '-1'], ['cx', 0, 1], ['h', 0], ['h', 2], ['cx', 2, 1], ['rz', 2, '-1/2']], [['rz', 2, '1'], ['cx', 0, 1], ['h', 0], ['h', 2], ['cx', 2, 1], ['rz', 2, '-1/2']]], ['regression: random peephole 30', [['rz', 0, '1'], ['x', 1], ['rz', 1, '3'], ['cx', 1, 2], ['rz', 0, '-1/2']], [['rz', 0, '1/2'], ['x', 1], ['rz', 1, '1'], ['cx', 1, 2]]], ['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: random peephole 18', [['h', 1], ['cx', 0, 2]], [['h', 1], ['cx', 0, 2]]], ['control: random peephole 24', [['h', 2], ['cx', 1, 0], ['h', 2], ['cx', 2, 1]], [['cx', 1, 0], ['cx', 2, 1]]], ['control: random peephole 26', [['cx', 1, 2], ['rz', 0, '1/2'], ['x', 1]], [['cx', 1, 2], ['rz', 0, '1/2'], ['x', 1]]], ['control: random peephole 29', [['cx', 0, 1], ['rz', 1, '5/4'], ['rz', 0, '1/4'], ['rz', 1, '-1/2'], ['rz', 1, '3/2'], ['h', 2]], [['cx', 0, 1], ['rz', 1, '1/4'], ['rz', 0, '1/4'], ['h', 2]]]], [['regression: random peephole 34', [['x', 1], ['rz', 2, '3/2'], ['x', 1], ['rz', 1, '-1']], [['rz', 2, '-1/2'], ['rz', 1, '1']]], ['regression: random peephole 46', [['x', 2], ['cx', 0, 2], ['cx', 0, 2], ['rz', 1, '-1']], [['x', 2], ['cx', 0, 2], ['cx', 0, 2], ['rz', 1, '1']]], ['repair check: 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]]], ['control: random peephole 33', [['rz', 0, '3/4'], ['rz', 1, '1/2']], [['rz', 0, '3/4'], ['rz', 1, '1/2']]], ['control: random peephole 36', [['cx', 2, 0], ['x', 1], ['cx', 2, 0]], [['cx', 2, 0], ['x', 1], ['cx', 2, 0]]], ['control: random peephole 37', [['cx', 0, 1], ['cx', 0, 1]], [['cx', 0, 1], ['cx', 0, 1]]], ['control: random peephole 38', [['x', 1], ['rz', 0, '3/4'], ['rz', 2, '1/2'], ['x', 1], ['rz', 2, '1/4']], [['rz', 0, '3/4'], ['rz', 2, '3/4']]]]]
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)

source:
"""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 % 1
        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: rz merges to pi', [['rz', 0, '1/2'], ['rz', 0, '1/2']], [['rz', 0, '1']]], ['regression: rz of pi alone', [['rz', 0, '1']], [['rz', 0, '1']]], ['repair check: 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: 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: merge cancels to zero', [['rz', 0, '3/4'], ['rz', 0, '5/4']], []], ['control: unnormalized single rz', [['rz', 2, '5/2']], [['rz', 2, '1/2']]]], [['regression: 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]]], ['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']]], ['repair check: 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: 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]]], ['control: random peephole 1', [['cx', 2, 0], ['x', 0]], [['cx', 2, 0], ['x', 0]]]], [['regression: random peephole 17', [['cx', 0, 2], ['x', 2], ['rz', 0, '1'], ['h', 0]], [['cx', 0, 2], ['x', 2], ['rz', 0, '1'], ['h', 0]]], ['regression: random peephole 20', [['rz', 1, '3'], ['cx', 0, 2], ['rz', 1, '2'], ['cx', 2, 0], ['rz', 1, '0'], ['cx', 1, 2], ['h', 0]], [['rz', 1, '1'], ['cx', 0, 2], ['cx', 2, 0], ['cx', 1, 2], ['h', 0]]], ['repair check: 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]]], ['control: random peephole 2', [['h', 1], ['h', 0], ['h', 1]], [['h', 0]]], ['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 8', [['rz', 2, '1/4'], ['rz', 2, '3/4'], ['rz', 2, '1'], ['x', 0], ['rz', 2, '3/4']], [['x', 0], ['rz', 2, '3/4']]], ['control: random peephole 12', [['x', 2], ['h', 0]], [['x', 2], ['h', 0]]]], [['regression: random peephole 22', [['rz', 2, '-1'], ['cx', 0, 1], ['h', 0], ['h', 2], ['cx', 2, 1], ['rz', 2, '-1/2']], [['rz', 2, '1'], ['cx', 0, 1], ['h', 0], ['h', 2], ['cx', 2, 1], ['rz', 2, '-1/2']]], ['regression: random peephole 30', [['rz', 0, '1'], ['x', 1], ['rz', 1, '3'], ['cx', 1, 2], ['rz', 0, '-1/2']], [['rz', 0, '1/2'], ['x', 1], ['rz', 1, '1'], ['cx', 1, 2]]], ['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: random peephole 18', [['h', 1], ['cx', 0, 2]], [['h', 1], ['cx', 0, 2]]], ['control: random peephole 24', [['h', 2], ['cx', 1, 0], ['h', 2], ['cx', 2, 1]], [['cx', 1, 0], ['cx', 2, 1]]], ['control: random peephole 26', [['cx', 1, 2], ['rz', 0, '1/2'], ['x', 1]], [['cx', 1, 2], ['rz', 0, '1/2'], ['x', 1]]], ['control: random peephole 29', [['cx', 0, 1], ['rz', 1, '5/4'], ['rz', 0, '1/4'], ['rz', 1, '-1/2'], ['rz', 1, '3/2'], ['h', 2]], [['cx', 0, 1], ['rz', 1, '1/4'], ['rz', 0, '1/4'], ['h', 2]]]], [['regression: random peephole 34', [['x', 1], ['rz', 2, '3/2'], ['x', 1], ['rz', 1, '-1']], [['rz', 2, '-1/2'], ['rz', 1, '1']]], ['regression: random peephole 46', [['x', 2], ['cx', 0, 2], ['cx', 0, 2], ['rz', 1, '-1']], [['x', 2], ['cx', 0, 2], ['cx', 0, 2], ['rz', 1, '1']]], ['repair check: 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]]], ['control: random peephole 33', [['rz', 0, '3/4'], ['rz', 1, '1/2']], [['rz', 0, '3/4'], ['rz', 1, '1/2']]], ['control: random peephole 36', [['cx', 2, 0], ['x', 1], ['cx', 2, 0]], [['cx', 2, 0], ['x', 1], ['cx', 2, 0]]], ['control: random peephole 37', [['cx', 0, 1], ['cx', 0, 1]], [['cx', 0, 1], ['cx', 0, 1]]], ['control: random peephole 38', [['x', 1], ['rz', 0, '3/4'], ['rz', 2, '1/2'], ['x', 1], ['rz', 2, '1/4']], [['rz', 0, '3/4'], ['rz', 2, '3/4']]]]]
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)

prompt:
rz(pi/2) followed by rz(pi/2) is emitted as rz(-1) instead of rz(1).

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.

split: open-access


broken_source:
"""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[2] == 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: rz commutes through cx control', [['rz', 0, '1/4'], ['cx', 0, 1], ['rz', 0, '1/4']], [['rz', 0, '1/2'], ['cx', 0, 1]]], ['regression: 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']]], ['regression: random peephole 16', [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '5/4'], ['cx', 0, 1], ['rz', 1, '3/2']], [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '-3/4'], ['cx', 0, 1], ['rz', 1, '-1/2']]], ['control: rz merges to pi', [['rz', 0, '1/2'], ['rz', 0, '1/2']], [['rz', 0, '1']]], ['control: merge cancels to zero', [['rz', 0, '3/4'], ['rz', 0, '5/4']], []], ['control: unnormalized single rz', [['rz', 2, '5/2']], [['rz', 2, '1/2']]], ['control: rz of pi alone', [['rz', 0, '1']], [['rz', 0, '1']]]], [['regression: random peephole 16', [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '5/4'], ['cx', 0, 1], ['rz', 1, '3/2']], [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '-3/4'], ['cx', 0, 1], ['rz', 1, '-1/2']]], ['regression: random peephole 31', [['rz', 2, '7/4'], ['rz', 2, '-1/4'], ['cx', 2, 0], ['rz', 2, '7/4'], ['h', 1]], [['rz', 2, '-3/4'], ['cx', 2, 0], ['h', 1]]], ['regression: 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: 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]]], ['control: 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: 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']]], ['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 16', [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '5/4'], ['cx', 0, 1], ['rz', 1, '3/2']], [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '-3/4'], ['cx', 0, 1], ['rz', 1, '-1/2']]], ['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 31', [['rz', 2, '7/4'], ['rz', 2, '-1/4'], ['cx', 2, 0], ['rz', 2, '7/4'], ['h', 1]], [['rz', 2, '-3/4'], ['cx', 2, 0], ['h', 1]]], ['regression: rz commutes through cx control', [['rz', 0, '1/4'], ['cx', 0, 1], ['rz', 0, '1/4']], [['rz', 0, '1/2'], ['cx', 0, 1]]], ['regression: 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: 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 8', [['rz', 2, '1/4'], ['rz', 2, '3/4'], ['rz', 2, '1'], ['x', 0], ['rz', 2, '3/4']], [['x', 0], ['rz', 2, '3/4']]], ['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]]]], [['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 16', [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '5/4'], ['cx', 0, 1], ['rz', 1, '3/2']], [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '-3/4'], ['cx', 0, 1], ['rz', 1, '-1/2']]], ['regression: 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: 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]]], ['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']]]]]
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)

source:
"""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 q in (prev[1], prev[2]):
                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: rz commutes through cx control', [['rz', 0, '1/4'], ['cx', 0, 1], ['rz', 0, '1/4']], [['rz', 0, '1/2'], ['cx', 0, 1]]], ['regression: 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']]], ['regression: random peephole 16', [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '5/4'], ['cx', 0, 1], ['rz', 1, '3/2']], [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '-3/4'], ['cx', 0, 1], ['rz', 1, '-1/2']]], ['control: rz merges to pi', [['rz', 0, '1/2'], ['rz', 0, '1/2']], [['rz', 0, '1']]], ['control: merge cancels to zero', [['rz', 0, '3/4'], ['rz', 0, '5/4']], []], ['control: unnormalized single rz', [['rz', 2, '5/2']], [['rz', 2, '1/2']]], ['control: rz of pi alone', [['rz', 0, '1']], [['rz', 0, '1']]]], [['regression: random peephole 16', [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '5/4'], ['cx', 0, 1], ['rz', 1, '3/2']], [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '-3/4'], ['cx', 0, 1], ['rz', 1, '-1/2']]], ['regression: random peephole 31', [['rz', 2, '7/4'], ['rz', 2, '-1/4'], ['cx', 2, 0], ['rz', 2, '7/4'], ['h', 1]], [['rz', 2, '-3/4'], ['cx', 2, 0], ['h', 1]]], ['regression: 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: 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]]], ['control: 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: 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']]], ['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 16', [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '5/4'], ['cx', 0, 1], ['rz', 1, '3/2']], [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '-3/4'], ['cx', 0, 1], ['rz', 1, '-1/2']]], ['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 31', [['rz', 2, '7/4'], ['rz', 2, '-1/4'], ['cx', 2, 0], ['rz', 2, '7/4'], ['h', 1]], [['rz', 2, '-3/4'], ['cx', 2, 0], ['h', 1]]], ['regression: rz commutes through cx control', [['rz', 0, '1/4'], ['cx', 0, 1], ['rz', 0, '1/4']], [['rz', 0, '1/2'], ['cx', 0, 1]]], ['regression: 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: 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 8', [['rz', 2, '1/4'], ['rz', 2, '3/4'], ['rz', 2, '1'], ['x', 0], ['rz', 2, '3/4']], [['x', 0], ['rz', 2, '3/4']]], ['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]]]], [['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 16', [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '5/4'], ['cx', 0, 1], ['rz', 1, '3/2']], [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '-3/4'], ['cx', 0, 1], ['rz', 1, '-1/2']]], ['regression: 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: 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]]], ['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']]]]]
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)

prompt:
rz on qubit 1 before and after cx(0,1) are merged although they do not commute.

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.

split: open-access


broken_source:
"""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)

source:
"""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)

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

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.

split: open-access


broken_source:
"""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:
                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: unnormalized single rz', [['rz', 2, '5/2']], [['rz', 2, '1/2']]], ['regression: 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]]], ['repair check: 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: 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: merge cancels to zero', [['rz', 0, '3/4'], ['rz', 0, '5/4']], []]], [['regression: 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]]], ['regression: 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 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: 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 13', [['h', 1], ['rz', 0, '1/2'], ['rz', 2, '5/4']], [['h', 1], ['rz', 0, '1/2'], ['rz', 2, '-3/4']]], ['regression: 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']]], ['regression: random peephole 15', [['rz', 0, '3/2'], ['cx', 1, 0], ['rz', 2, '-1'], ['rz', 1, '0'], ['rz', 2, '5/4'], ['rz', 2, '2']], [['rz', 0, '-1/2'], ['cx', 1, 0], ['rz', 2, '1/4']]], ['control: 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 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 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 16', [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '5/4'], ['cx', 0, 1], ['rz', 1, '3/2']], [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '-3/4'], ['cx', 0, 1], ['rz', 1, '-1/2']]], ['regression: random peephole 19', [['h', 1], ['cx', 2, 0], ['rz', 1, '3/2']], [['h', 1], ['cx', 2, 0], ['rz', 1, '-1/2']]], ['regression: random peephole 15', [['rz', 0, '3/2'], ['cx', 1, 0], ['rz', 2, '-1'], ['rz', 1, '0'], ['rz', 2, '5/4'], ['rz', 2, '2']], [['rz', 0, '-1/2'], ['cx', 1, 0], ['rz', 2, '1/4']]], ['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 8', [['rz', 2, '1/4'], ['rz', 2, '3/4'], ['rz', 2, '1'], ['x', 0], ['rz', 2, '3/4']], [['x', 0], ['rz', 2, '3/4']]], ['control: random peephole 12', [['x', 2], ['h', 0]], [['x', 2], ['h', 0]]], ['control: random peephole 17', [['cx', 0, 2], ['x', 2], ['rz', 0, '1'], ['h', 0]], [['cx', 0, 2], ['x', 2], ['rz', 0, '1'], ['h', 0]]]], [['regression: random peephole 23', [['h', 0], ['cx', 1, 2], ['rz', 0, '1/4'], ['rz', 1, '7/4'], ['x', 1]], [['h', 0], ['cx', 1, 2], ['rz', 0, '1/4'], ['rz', 1, '-1/4'], ['x', 1]]], ['regression: random peephole 27', [['x', 2], ['cx', 0, 1], ['rz', 0, '0'], ['rz', 0, '5/4'], ['h', 2], ['h', 2], ['h', 0], ['x', 2]], [['cx', 0, 1], ['rz', 0, '-3/4'], ['h', 0]]], ['regression: random peephole 19', [['h', 1], ['cx', 2, 0], ['rz', 1, '3/2']], [['h', 1], ['cx', 2, 0], ['rz', 1, '-1/2']]], ['control: random peephole 18', [['h', 1], ['cx', 0, 2]], [['h', 1], ['cx', 0, 2]]], ['control: random peephole 20', [['rz', 1, '3'], ['cx', 0, 2], ['rz', 1, '2'], ['cx', 2, 0], ['rz', 1, '0'], ['cx', 1, 2], ['h', 0]], [['rz', 1, '1'], ['cx', 0, 2], ['cx', 2, 0], ['cx', 1, 2], ['h', 0]]], ['control: random peephole 21', [['rz', 0, '1'], ['x', 2]], [['rz', 0, '1'], ['x', 2]]], ['control: random peephole 24', [['h', 2], ['cx', 1, 0], ['h', 2], ['cx', 2, 1]], [['cx', 1, 0], ['cx', 2, 1]]]]]
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)

source:
"""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 = ang % 2
                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: unnormalized single rz', [['rz', 2, '5/2']], [['rz', 2, '1/2']]], ['regression: 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]]], ['repair check: 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: 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: merge cancels to zero', [['rz', 0, '3/4'], ['rz', 0, '5/4']], []]], [['regression: 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]]], ['regression: 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 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: 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 13', [['h', 1], ['rz', 0, '1/2'], ['rz', 2, '5/4']], [['h', 1], ['rz', 0, '1/2'], ['rz', 2, '-3/4']]], ['regression: 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']]], ['regression: random peephole 15', [['rz', 0, '3/2'], ['cx', 1, 0], ['rz', 2, '-1'], ['rz', 1, '0'], ['rz', 2, '5/4'], ['rz', 2, '2']], [['rz', 0, '-1/2'], ['cx', 1, 0], ['rz', 2, '1/4']]], ['control: 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 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 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 16', [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '5/4'], ['cx', 0, 1], ['rz', 1, '3/2']], [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '-3/4'], ['cx', 0, 1], ['rz', 1, '-1/2']]], ['regression: random peephole 19', [['h', 1], ['cx', 2, 0], ['rz', 1, '3/2']], [['h', 1], ['cx', 2, 0], ['rz', 1, '-1/2']]], ['regression: random peephole 15', [['rz', 0, '3/2'], ['cx', 1, 0], ['rz', 2, '-1'], ['rz', 1, '0'], ['rz', 2, '5/4'], ['rz', 2, '2']], [['rz', 0, '-1/2'], ['cx', 1, 0], ['rz', 2, '1/4']]], ['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 8', [['rz', 2, '1/4'], ['rz', 2, '3/4'], ['rz', 2, '1'], ['x', 0], ['rz', 2, '3/4']], [['x', 0], ['rz', 2, '3/4']]], ['control: random peephole 12', [['x', 2], ['h', 0]], [['x', 2], ['h', 0]]], ['control: random peephole 17', [['cx', 0, 2], ['x', 2], ['rz', 0, '1'], ['h', 0]], [['cx', 0, 2], ['x', 2], ['rz', 0, '1'], ['h', 0]]]], [['regression: random peephole 23', [['h', 0], ['cx', 1, 2], ['rz', 0, '1/4'], ['rz', 1, '7/4'], ['x', 1]], [['h', 0], ['cx', 1, 2], ['rz', 0, '1/4'], ['rz', 1, '-1/4'], ['x', 1]]], ['regression: random peephole 27', [['x', 2], ['cx', 0, 1], ['rz', 0, '0'], ['rz', 0, '5/4'], ['h', 2], ['h', 2], ['h', 0], ['x', 2]], [['cx', 0, 1], ['rz', 0, '-3/4'], ['h', 0]]], ['regression: random peephole 19', [['h', 1], ['cx', 2, 0], ['rz', 1, '3/2']], [['h', 1], ['cx', 2, 0], ['rz', 1, '-1/2']]], ['control: random peephole 18', [['h', 1], ['cx', 0, 2]], [['h', 1], ['cx', 0, 2]]], ['control: random peephole 20', [['rz', 1, '3'], ['cx', 0, 2], ['rz', 1, '2'], ['cx', 2, 0], ['rz', 1, '0'], ['cx', 1, 2], ['h', 0]], [['rz', 1, '1'], ['cx', 0, 2], ['cx', 2, 0], ['cx', 1, 2], ['h', 0]]], ['control: random peephole 21', [['rz', 0, '1'], ['x', 2]], [['rz', 0, '1'], ['x', 2]]], ['control: random peephole 24', [['h', 2], ['cx', 1, 0], ['h', 2], ['cx', 2, 1]], [['cx', 1, 0], ['cx', 2, 1]]]]]
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)

prompt:
A lone rz(5pi/2) is emitted as "5/2" instead of "1/2".

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.

split: open-access


broken_source:
"""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 out and out[-1][0] == name:
            out.pop()
        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: x on different qubits', [['x', 0], ['x', 1]], [['x', 0], ['x', 1]]], ['regression: x cancels across other qubit', [['x', 0], ['h', 1], ['x', 0]], [['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: 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: merge cancels to zero', [['rz', 0, '3/4'], ['rz', 0, '5/4']], []]], [['regression: random peephole 2', [['h', 1], ['h', 0], ['h', 1]], [['h', 0]]], ['regression: 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]]], ['regression: random peephole 24', [['h', 2], ['cx', 1, 0], ['h', 2], ['cx', 2, 1]], [['cx', 1, 0], ['cx', 2, 1]]], ['control: unnormalized single rz', [['rz', 2, '5/2']], [['rz', 2, '1/2']]], ['control: rz of pi alone', [['rz', 0, '1']], [['rz', 0, '1']]], ['control: h blocked by rz', [['h', 0], ['rz', 0, '1/2'], ['h', 0]], [['h', 0], ['rz', 0, '1/2'], ['h', 0]]], ['control: random peephole 1', [['cx', 2, 0], ['x', 0]], [['cx', 2, 0], ['x', 0]]]], [['regression: random peephole 22', [['rz', 2, '-1'], ['cx', 0, 1], ['h', 0], ['h', 2], ['cx', 2, 1], ['rz', 2, '-1/2']], [['rz', 2, '1'], ['cx', 0, 1], ['h', 0], ['h', 2], ['cx', 2, 1], ['rz', 2, '-1/2']]], ['regression: random peephole 24', [['h', 2], ['cx', 1, 0], ['h', 2], ['cx', 2, 1]], [['cx', 1, 0], ['cx', 2, 1]]], ['regression: random peephole 25', [['rz', 1, '1/2'], ['h', 0], ['h', 2], ['h', 0], ['rz', 0, '-1/4']], [['rz', 1, '1/2'], ['h', 2], ['rz', 0, '-1/4']]], ['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]]], ['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 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 27', [['x', 2], ['cx', 0, 1], ['rz', 0, '0'], ['rz', 0, '5/4'], ['h', 2], ['h', 2], ['h', 0], ['x', 2]], [['cx', 0, 1], ['rz', 0, '-3/4'], ['h', 0]]], ['regression: random peephole 34', [['x', 1], ['rz', 2, '3/2'], ['x', 1], ['rz', 1, '-1']], [['rz', 2, '-1/2'], ['rz', 1, '1']]], ['regression: random peephole 38', [['x', 1], ['rz', 0, '3/4'], ['rz', 2, '1/2'], ['x', 1], ['rz', 2, '1/4']], [['rz', 0, '3/4'], ['rz', 2, '3/4']]], ['control: 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']]], ['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]]], ['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]]]], [['regression: random peephole 39', [['rz', 2, '-1/2'], ['h', 2], ['rz', 0, '2'], ['x', 0], ['rz', 2, '5/4'], ['x', 0], ['h', 2]], [['rz', 2, '-1/2'], ['h', 2], ['rz', 2, '-3/4'], ['h', 2]]], ['regression: random peephole 44', [['x', 1], ['x', 2]], [['x', 1], ['x', 2]]], ['regression: random peephole 49', [['h', 0], ['x', 1], ['rz', 0, '0'], ['x', 2], ['x', 1], ['cx', 0, 1]], [['h', 0], ['x', 2], ['cx', 0, 1]]], ['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 15', [['rz', 0, '3/2'], ['cx', 1, 0], ['rz', 2, '-1'], ['rz', 1, '0'], ['rz', 2, '5/4'], ['rz', 2, '2']], [['rz', 0, '-1/2'], ['cx', 1, 0], ['rz', 2, '1/4']]], ['control: random peephole 16', [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '5/4'], ['cx', 0, 1], ['rz', 1, '3/2']], [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '-3/4'], ['cx', 0, 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)

source:
"""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 out and out[-1] == [name, q]:
            out.pop()
        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: x on different qubits', [['x', 0], ['x', 1]], [['x', 0], ['x', 1]]], ['regression: x cancels across other qubit', [['x', 0], ['h', 1], ['x', 0]], [['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: 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: merge cancels to zero', [['rz', 0, '3/4'], ['rz', 0, '5/4']], []]], [['regression: random peephole 2', [['h', 1], ['h', 0], ['h', 1]], [['h', 0]]], ['regression: 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]]], ['regression: random peephole 24', [['h', 2], ['cx', 1, 0], ['h', 2], ['cx', 2, 1]], [['cx', 1, 0], ['cx', 2, 1]]], ['control: unnormalized single rz', [['rz', 2, '5/2']], [['rz', 2, '1/2']]], ['control: rz of pi alone', [['rz', 0, '1']], [['rz', 0, '1']]], ['control: h blocked by rz', [['h', 0], ['rz', 0, '1/2'], ['h', 0]], [['h', 0], ['rz', 0, '1/2'], ['h', 0]]], ['control: random peephole 1', [['cx', 2, 0], ['x', 0]], [['cx', 2, 0], ['x', 0]]]], [['regression: random peephole 22', [['rz', 2, '-1'], ['cx', 0, 1], ['h', 0], ['h', 2], ['cx', 2, 1], ['rz', 2, '-1/2']], [['rz', 2, '1'], ['cx', 0, 1], ['h', 0], ['h', 2], ['cx', 2, 1], ['rz', 2, '-1/2']]], ['regression: random peephole 24', [['h', 2], ['cx', 1, 0], ['h', 2], ['cx', 2, 1]], [['cx', 1, 0], ['cx', 2, 1]]], ['regression: random peephole 25', [['rz', 1, '1/2'], ['h', 0], ['h', 2], ['h', 0], ['rz', 0, '-1/4']], [['rz', 1, '1/2'], ['h', 2], ['rz', 0, '-1/4']]], ['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]]], ['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 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 27', [['x', 2], ['cx', 0, 1], ['rz', 0, '0'], ['rz', 0, '5/4'], ['h', 2], ['h', 2], ['h', 0], ['x', 2]], [['cx', 0, 1], ['rz', 0, '-3/4'], ['h', 0]]], ['regression: random peephole 34', [['x', 1], ['rz', 2, '3/2'], ['x', 1], ['rz', 1, '-1']], [['rz', 2, '-1/2'], ['rz', 1, '1']]], ['regression: random peephole 38', [['x', 1], ['rz', 0, '3/4'], ['rz', 2, '1/2'], ['x', 1], ['rz', 2, '1/4']], [['rz', 0, '3/4'], ['rz', 2, '3/4']]], ['control: 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']]], ['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]]], ['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]]]], [['regression: random peephole 39', [['rz', 2, '-1/2'], ['h', 2], ['rz', 0, '2'], ['x', 0], ['rz', 2, '5/4'], ['x', 0], ['h', 2]], [['rz', 2, '-1/2'], ['h', 2], ['rz', 2, '-3/4'], ['h', 2]]], ['regression: random peephole 44', [['x', 1], ['x', 2]], [['x', 1], ['x', 2]]], ['regression: random peephole 49', [['h', 0], ['x', 1], ['rz', 0, '0'], ['x', 2], ['x', 1], ['cx', 0, 1]], [['h', 0], ['x', 2], ['cx', 0, 1]]], ['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 15', [['rz', 0, '3/2'], ['cx', 1, 0], ['rz', 2, '-1'], ['rz', 1, '0'], ['rz', 2, '5/4'], ['rz', 2, '2']], [['rz', 0, '-1/2'], ['cx', 1, 0], ['rz', 2, '1/4']]], ['control: random peephole 16', [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '5/4'], ['cx', 0, 1], ['rz', 1, '3/2']], [['cx', 2, 0], ['h', 0], ['cx', 2, 0], ['cx', 1, 0], ['rz', 1, '-3/4'], ['cx', 0, 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)

prompt:
x on qubit 0 followed by x on qubit 1 cancels both.

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.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import re
N = 1
observations = []
def solve(x):
    qregs, cregs = {}, {}
    nq = nc = 0
    out = []
    def ref(tok, regs):
        m = re.fullmatch(r'([a-z]\w*)(?:\[(\d+)\])?', tok.strip())
        name, idx = m.group(1), m.group(2)
        if name not in regs:
            raise KeyError('unknown-register')
        off, size = regs[name]
        if idx is None:
            return [off + i for i in range(size)]
        if int(idx) >= size:
            raise KeyError('index-out-of-range')
        return [off + int(idx)]
    for no, stmt in enumerate(x):
        s = stmt.strip().rstrip(';').strip()
        head, _, rest = s.partition(' ')
        try:
            if head in ('qreg', 'creg'):
                m = re.fullmatch(r'([a-z]\w*)\[(\d+)\]', rest.strip())
                size = int(m.group(2))
                if head == 'qreg':
                    qregs[m.group(1)] = (nq, size)
                    nq += size
                else:
                    cregs[m.group(1)] = (nq, size)
                    nq += size
            elif head == 'measure':
                src, dst = rest.split('->')
                qs, cs = ref(src, qregs), ref(dst, cregs)
                if len(qs) != len(cs):
                    raise KeyError('size-mismatch')
                out += [['measure', a, b] for a, b in zip(qs, cs)]
            else:
                args = [ref(t, qregs) for t in rest.split(',')]
                width = max(len(a) for a in args)
                if any(len(a) not in (1, width) for a in args):
                    raise KeyError('size-mismatch')
                for i in range(width):
                    out.append([head, [a[0] if len(a) == 1 else a[i] for a in args]])
        except KeyError as e:
            return [e.args[0], no]
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: two registers broadcast', ['qreg q[2];', 'qreg r[2];', 'creg c[2];', 'cx q,r;', 'measure r -> c;'], [['cx', [0, 2]], ['cx', [1, 3]], ['measure', 2, 0], ['measure', 3, 1]]], ['regression: creg declared between qregs', ['qreg a[1];', 'creg c[2];', 'qreg b[2];', 'h b[1];'], [['h', [2]]]], ['regression: random program 5', ['qreg q[1];', 'creg c[3];', 'qreg r[1];', 'creg d[3];', 'cx r[0],q[0];', 'measure r -> d[0];', 'cx q[0],q[0];', 'cx r[0],r;'], [['cx', [1, 0]], ['measure', 1, 3], ['cx', [0, 0]], ['cx', [1, 1]]]], ['control: index equals size', ['qreg q[2];', 'h q[2];'], ['index-out-of-range', 1]], ['control: second register overflow', ['qreg q[2];', 'qreg r[2];', 'x r[3];'], ['index-out-of-range', 2]], ['control: single control broadcast target', ['qreg q[1];', 'qreg r[3];', 'cx q[0],r;'], [['cx', [0, 1]], ['cx', [0, 2]], ['cx', [0, 3]]]], ['control: register control single target', ['qreg q[3];', 'qreg r[1];', 'cx q,r[0];'], [['cx', [0, 3]], ['cx', [1, 3]], ['cx', [2, 3]]]]], [['regression: random program 5', ['qreg q[1];', 'creg c[3];', 'qreg r[1];', 'creg d[3];', 'cx r[0],q[0];', 'measure r -> d[0];', 'cx q[0],q[0];', 'cx r[0],r;'], [['cx', [1, 0]], ['measure', 1, 3], ['cx', [0, 0]], ['cx', [1, 1]]]], ['regression: random program 8', ['qreg q[2];', 'qreg r[1];', 'qreg s[2];', 'creg c[2];', 'cx r[0],s[1];', 'h s;', 'measure q[1] -> c[1];', 'h s;'], [['cx', [2, 4]], ['h', [3]], ['h', [4]], ['measure', 1, 1], ['h', [3]], ['h', [4]]]], ['regression: random program 12', ['qreg q[3];', 'creg c[1];', 'qreg r[2];', 'creg d[3];', 'x q;', 'h r;', 'x r[1];'], [['x', [0]], ['x', [1]], ['x', [2]], ['h', [3]], ['h', [4]], ['x', [4]]]], ['control: measure size mismatch', ['qreg q[3];', 'creg c[2];', 'measure q -> c;'], ['size-mismatch', 2]], ['control: measure into qreg name', ['qreg q[1];', 'qreg r[1];', 'measure q[0] -> r[0];'], ['unknown-register', 2]], ['control: unknown register', ['qreg q[1];', 'h z[0];'], ['unknown-register', 1]], ['control: random program 1', ['qreg q[3];', 'creg c[2];', 'h q;', 'measure q[0] -> c;'], ['size-mismatch', 3]]], [['regression: random program 14', ['qreg q[3];', 'creg c[2];', 'qreg r[2];', 'creg d[3];', 'qreg s[2];', 'x r[1];', 'x q;', 'x r[0];', 'cx s[0],r;'], [['x', [4]], ['x', [0]], ['x', [1]], ['x', [2]], ['x', [3]], ['cx', [5, 3]], ['cx', [5, 4]]]], ['regression: random program 16', ['qreg q[2];', 'creg c[2];', 'qreg r[1];', 'qreg s[2];', 'creg d[1];', 'cx s,q;', 'x r[0];'], [['cx', [3, 0]], ['cx', [4, 1]], ['x', [2]]]], ['regression: random program 17', ['qreg q[2];', 'qreg r[3];', 'creg c[3];', 'qreg s[2];', 'measure q[1] -> c[2];', 'cx s,r[2];', 'measure q[0] -> c[2];', 'cx s[0],r;'], [['measure', 1, 2], ['cx', [5, 4]], ['cx', [6, 4]], ['measure', 0, 2], ['cx', [5, 2]], ['cx', [5, 3]], ['cx', [5, 4]]]], ['control: random program 2', ['qreg q[3];', 'creg c[3];', 'qreg r[1];', 'creg d[1];', 'h q[1];'], [['h', [1]]]], ['control: random program 3', ['qreg q[2];', 'creg c[2];', 'h q[1];'], [['h', [1]]]], ['control: random program 4', ['qreg q[3];', 'creg c[3];', 'qreg r[1];', 'creg d[3];', 'qreg s[1];', 'creg e[2];', 'x s;', 'measure s[0] -> e;', 'cx q,r[0];'], ['size-mismatch', 7]], ['control: random program 6', ['qreg q[3];', 'creg c[2];', 'cx q[2],q;'], [['cx', [2, 0]], ['cx', [2, 1]], ['cx', [2, 2]]]]], [['regression: random program 18', ['qreg q[1];', 'creg c[2];', 'qreg r[1];', 'creg d[2];', 'qreg s[2];', 'cx q,r[0];'], [['cx', [0, 1]]]], ['regression: random program 20', ['qreg q[1];', 'qreg r[3];', 'creg c[3];', 'qreg s[2];', 'creg d[1];', 'measure s[0] -> c[2];', 'measure r[0] -> d[0];', 'measure q[0] -> d;'], [['measure', 4, 2], ['measure', 1, 3], ['measure', 0, 3]]], ['regression: random program 22', ['qreg q[1];', 'qreg r[3];', 'creg c[2];', 'qreg s[3];', 'creg d[1];', 'cx q[0],s[1];'], [['cx', [0, 5]]]], ['control: random program 7', ['qreg q[1];', 'creg c[3];', 'x q;', 'cx q,q[1];', 'cx q[0],q[0];', 'cx q[0],q;'], ['index-out-of-range', 3]], ['control: random program 9', ['qreg q[1];', 'qreg r[1];', 'qreg s[1];', 'creg c[2];', 'h q[0];'], [['h', [0]]]], ['control: random program 10', ['qreg q[2];', 'qreg r[2];', 'creg c[2];', 'h q[0];', 'x q;', 'measure r[0] -> c;'], ['size-mismatch', 5]], ['control: random program 11', ['qreg q[2];', 'creg c[1];', 'qreg r[1];', 'creg d[3];', 'measure q -> d[2];', 'x r[0];', 'x q;', 'measure q[0] -> d[0];'], ['size-mismatch', 4]]], [['regression: random program 22', ['qreg q[1];', 'qreg r[3];', 'creg c[2];', 'qreg s[3];', 'creg d[1];', 'cx q[0],s[1];'], [['cx', [0, 5]]]], ['regression: random program 25', ['qreg q[1];', 'qreg r[3];', 'creg c[2];', 'cx r[1],q[0];', 'measure r[2] -> c[0];'], [['cx', [2, 0]], ['measure', 3, 0]]], ['regression: random program 26', ['qreg q[2];', 'creg c[3];', 'qreg r[1];', 'creg d[1];', 'h r[0];'], [['h', [2]]]], ['control: random program 13', ['qreg q[2];', 'qreg r[2];', 'qreg s[1];', 'creg c[2];', 'cx r[0],q;', 'measure q[0] -> c[1];', 'cx r[1],r;', 'measure r[0] -> c;'], ['size-mismatch', 7]], ['control: random program 15', ['qreg q[1];', 'qreg r[2];', 'qreg s[3];', 'creg c[2];', 'x q;', 'h r[1];', 'cx s[2],r[1];', 'cx r[1],q[0];'], [['x', [0]], ['h', [2]], ['cx', [5, 2]], ['cx', [2, 0]]]], ['control: random program 19', ['qreg q[1];', 'qreg r[1];', 'qreg s[2];', 'creg c[3];', 'measure s[0] -> c;', 'h q[0];'], ['size-mismatch', 4]], ['control: random program 23', ['qreg q[3];', 'creg c[1];', 'x q[0];'], [['x', [0]]]]]]
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)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import re
N = 1
observations = []
def solve(x):
    qregs, cregs = {}, {}
    nq = nc = 0
    out = []
    def ref(tok, regs):
        m = re.fullmatch(r'([a-z]\w*)(?:\[(\d+)\])?', tok.strip())
        name, idx = m.group(1), m.group(2)
        if name not in regs:
            raise KeyError('unknown-register')
        off, size = regs[name]
        if idx is None:
            return [off + i for i in range(size)]
        if int(idx) >= size:
            raise KeyError('index-out-of-range')
        return [off + int(idx)]
    for no, stmt in enumerate(x):
        s = stmt.strip().rstrip(';').strip()
        head, _, rest = s.partition(' ')
        try:
            if head in ('qreg', 'creg'):
                m = re.fullmatch(r'([a-z]\w*)\[(\d+)\]', rest.strip())
                size = int(m.group(2))
                if head == 'qreg':
                    qregs[m.group(1)] = (nq, size)
                    nq += size
                else:
                    cregs[m.group(1)] = (nc, size)
                    nq += size
            elif head == 'measure':
                src, dst = rest.split('->')
                qs, cs = ref(src, qregs), ref(dst, cregs)
                if len(qs) != len(cs):
                    raise KeyError('size-mismatch')
                out += [['measure', a, b] for a, b in zip(qs, cs)]
            else:
                args = [ref(t, qregs) for t in rest.split(',')]
                width = max(len(a) for a in args)
                if any(len(a) not in (1, width) for a in args):
                    raise KeyError('size-mismatch')
                for i in range(width):
                    out.append([head, [a[0] if len(a) == 1 else a[i] for a in args]])
        except KeyError as e:
            return [e.args[0], no]
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: two registers broadcast', ['qreg q[2];', 'qreg r[2];', 'creg c[2];', 'cx q,r;', 'measure r -> c;'], [['cx', [0, 2]], ['cx', [1, 3]], ['measure', 2, 0], ['measure', 3, 1]]], ['regression: creg declared between qregs', ['qreg a[1];', 'creg c[2];', 'qreg b[2];', 'h b[1];'], [['h', [2]]]], ['regression: random program 5', ['qreg q[1];', 'creg c[3];', 'qreg r[1];', 'creg d[3];', 'cx r[0],q[0];', 'measure r -> d[0];', 'cx q[0],q[0];', 'cx r[0],r;'], [['cx', [1, 0]], ['measure', 1, 3], ['cx', [0, 0]], ['cx', [1, 1]]]], ['control: index equals size', ['qreg q[2];', 'h q[2];'], ['index-out-of-range', 1]], ['control: second register overflow', ['qreg q[2];', 'qreg r[2];', 'x r[3];'], ['index-out-of-range', 2]], ['control: single control broadcast target', ['qreg q[1];', 'qreg r[3];', 'cx q[0],r;'], [['cx', [0, 1]], ['cx', [0, 2]], ['cx', [0, 3]]]], ['control: register control single target', ['qreg q[3];', 'qreg r[1];', 'cx q,r[0];'], [['cx', [0, 3]], ['cx', [1, 3]], ['cx', [2, 3]]]]], [['regression: random program 5', ['qreg q[1];', 'creg c[3];', 'qreg r[1];', 'creg d[3];', 'cx r[0],q[0];', 'measure r -> d[0];', 'cx q[0],q[0];', 'cx r[0],r;'], [['cx', [1, 0]], ['measure', 1, 3], ['cx', [0, 0]], ['cx', [1, 1]]]], ['regression: random program 8', ['qreg q[2];', 'qreg r[1];', 'qreg s[2];', 'creg c[2];', 'cx r[0],s[1];', 'h s;', 'measure q[1] -> c[1];', 'h s;'], [['cx', [2, 4]], ['h', [3]], ['h', [4]], ['measure', 1, 1], ['h', [3]], ['h', [4]]]], ['regression: random program 12', ['qreg q[3];', 'creg c[1];', 'qreg r[2];', 'creg d[3];', 'x q;', 'h r;', 'x r[1];'], [['x', [0]], ['x', [1]], ['x', [2]], ['h', [3]], ['h', [4]], ['x', [4]]]], ['control: measure size mismatch', ['qreg q[3];', 'creg c[2];', 'measure q -> c;'], ['size-mismatch', 2]], ['control: measure into qreg name', ['qreg q[1];', 'qreg r[1];', 'measure q[0] -> r[0];'], ['unknown-register', 2]], ['control: unknown register', ['qreg q[1];', 'h z[0];'], ['unknown-register', 1]], ['control: random program 1', ['qreg q[3];', 'creg c[2];', 'h q;', 'measure q[0] -> c;'], ['size-mismatch', 3]]], [['regression: random program 14', ['qreg q[3];', 'creg c[2];', 'qreg r[2];', 'creg d[3];', 'qreg s[2];', 'x r[1];', 'x q;', 'x r[0];', 'cx s[0],r;'], [['x', [4]], ['x', [0]], ['x', [1]], ['x', [2]], ['x', [3]], ['cx', [5, 3]], ['cx', [5, 4]]]], ['regression: random program 16', ['qreg q[2];', 'creg c[2];', 'qreg r[1];', 'qreg s[2];', 'creg d[1];', 'cx s,q;', 'x r[0];'], [['cx', [3, 0]], ['cx', [4, 1]], ['x', [2]]]], ['regression: random program 17', ['qreg q[2];', 'qreg r[3];', 'creg c[3];', 'qreg s[2];', 'measure q[1] -> c[2];', 'cx s,r[2];', 'measure q[0] -> c[2];', 'cx s[0],r;'], [['measure', 1, 2], ['cx', [5, 4]], ['cx', [6, 4]], ['measure', 0, 2], ['cx', [5, 2]], ['cx', [5, 3]], ['cx', [5, 4]]]], ['control: random program 2', ['qreg q[3];', 'creg c[3];', 'qreg r[1];', 'creg d[1];', 'h q[1];'], [['h', [1]]]], ['control: random program 3', ['qreg q[2];', 'creg c[2];', 'h q[1];'], [['h', [1]]]], ['control: random program 4', ['qreg q[3];', 'creg c[3];', 'qreg r[1];', 'creg d[3];', 'qreg s[1];', 'creg e[2];', 'x s;', 'measure s[0] -> e;', 'cx q,r[0];'], ['size-mismatch', 7]], ['control: random program 6', ['qreg q[3];', 'creg c[2];', 'cx q[2],q;'], [['cx', [2, 0]], ['cx', [2, 1]], ['cx', [2, 2]]]]], [['regression: random program 18', ['qreg q[1];', 'creg c[2];', 'qreg r[1];', 'creg d[2];', 'qreg s[2];', 'cx q,r[0];'], [['cx', [0, 1]]]], ['regression: random program 20', ['qreg q[1];', 'qreg r[3];', 'creg c[3];', 'qreg s[2];', 'creg d[1];', 'measure s[0] -> c[2];', 'measure r[0] -> d[0];', 'measure q[0] -> d;'], [['measure', 4, 2], ['measure', 1, 3], ['measure', 0, 3]]], ['regression: random program 22', ['qreg q[1];', 'qreg r[3];', 'creg c[2];', 'qreg s[3];', 'creg d[1];', 'cx q[0],s[1];'], [['cx', [0, 5]]]], ['control: random program 7', ['qreg q[1];', 'creg c[3];', 'x q;', 'cx q,q[1];', 'cx q[0],q[0];', 'cx q[0],q;'], ['index-out-of-range', 3]], ['control: random program 9', ['qreg q[1];', 'qreg r[1];', 'qreg s[1];', 'creg c[2];', 'h q[0];'], [['h', [0]]]], ['control: random program 10', ['qreg q[2];', 'qreg r[2];', 'creg c[2];', 'h q[0];', 'x q;', 'measure r[0] -> c;'], ['size-mismatch', 5]], ['control: random program 11', ['qreg q[2];', 'creg c[1];', 'qreg r[1];', 'creg d[3];', 'measure q -> d[2];', 'x r[0];', 'x q;', 'measure q[0] -> d[0];'], ['size-mismatch', 4]]], [['regression: random program 22', ['qreg q[1];', 'qreg r[3];', 'creg c[2];', 'qreg s[3];', 'creg d[1];', 'cx q[0],s[1];'], [['cx', [0, 5]]]], ['regression: random program 25', ['qreg q[1];', 'qreg r[3];', 'creg c[2];', 'cx r[1],q[0];', 'measure r[2] -> c[0];'], [['cx', [2, 0]], ['measure', 3, 0]]], ['regression: random program 26', ['qreg q[2];', 'creg c[3];', 'qreg r[1];', 'creg d[1];', 'h r[0];'], [['h', [2]]]], ['control: random program 13', ['qreg q[2];', 'qreg r[2];', 'qreg s[1];', 'creg c[2];', 'cx r[0],q;', 'measure q[0] -> c[1];', 'cx r[1],r;', 'measure r[0] -> c;'], ['size-mismatch', 7]], ['control: random program 15', ['qreg q[1];', 'qreg r[2];', 'qreg s[3];', 'creg c[2];', 'x q;', 'h r[1];', 'cx s[2],r[1];', 'cx r[1],q[0];'], [['x', [0]], ['h', [2]], ['cx', [5, 2]], ['cx', [2, 0]]]], ['control: random program 19', ['qreg q[1];', 'qreg r[1];', 'qreg s[2];', 'creg c[3];', 'measure s[0] -> c;', 'h q[0];'], ['size-mismatch', 4]], ['control: random program 23', ['qreg q[3];', 'creg c[1];', 'x q[0];'], [['x', [0]]]]]]
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)

prompt:
Declaring a creg between two qregs shifts every qubit of the second qreg by the creg size.

Input a list of OpenQASM-2-like statements: "qreg n[k];", "creg n[k];", gate statements "g a,b;" and "measure a -> b;" where operands are whole registers or "n[i]". Registers are flattened in declaration order per kind (qubits and clbits have separate counters). Whole-register operands broadcast; a single qubit operand is reused across the broadcast. Return [[g, [flat qubits]] or ["measure", q, c]...] or [error, statement index] with errors "unknown-register", "index-out-of-range", "size-mismatch".

split: open-access