broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    n, gates = x
    xs = [[0] * n for _ in range(n)]
    zs = [[1 if i == j else 0 for j in range(n)] for i in range(n)]
    rs = [0] * n
    for g in gates:
        op = g[0]
        for row in range(n):
            X, Z = xs[row], zs[row]
            if op == 'h':
                a = g[1]
                rs[row] ^= X[a] & Z[a]
                X[a], Z[a] = Z[a], X[a]
            elif op == 's':
                a = g[1]
                rs[row] ^= X[a] & Z[a]
                Z[a] ^= X[a]
            elif op == 'x':
                rs[row] ^= Z[g[1]]
            elif op == 'z':
                rs[row] ^= X[g[1]]
            elif op == 'cx':
                a, b = g[1], g[2]
                rs[row] ^= X[a] & Z[b] & (X[b] ^ Z[a] ^ 1)
                X[b] ^= X[a]
                Z[b] ^= Z[a]
    letters = {(0, 0): 'I', (1, 0): 'X', (0, 1): 'Z', (1, 1): 'Y'}
    return [('-' if rs[r] else '+') + ''.join(letters[(xs[r][q], zs[r][q])] for q in range(n)) for r in range(n)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: bell preparation', [2, [['h', 0], ['cx', 0, 1]]], ['+XX', '+ZZ']], ['regression: cx on excited control', [2, [['x', 0], ['cx', 0, 1]]], ['-ZI', '+ZZ']], ['regression: ghz', [3, [['h', 0], ['cx', 0, 1], ['cx', 0, 2]]], ['+XXX', '+ZZI', '+ZIZ']], ['control: h then s', [1, [['h', 0], ['s', 0]]], ['+Y']], ['control: s twice after h', [1, [['h', 0], ['s', 0], ['s', 0]]], ['-X']], ['control: x flips z sign', [1, [['x', 0]]], ['-Z']], ['control: z on plus', [1, [['h', 0], ['z', 0]]], ['-X']]], [['regression: random clifford 0', [3, [['cx', 1, 2]]], ['+ZII', '+IZI', '+IZZ']], ['regression: random clifford 2', [3, [['cx', 0, 2], ['z', 2], ['s', 2], ['h', 0], ['cx', 2, 1], ['x', 1], ['cx', 1, 0], ['x', 1], ['x', 1], ['z', 0], ['cx', 2, 1], ['h', 1], ['h', 0]]], ['-ZII', '-IXI', '-ZIZ']], ['regression: ghz', [3, [['h', 0], ['cx', 0, 1], ['cx', 0, 2]]], ['+XXX', '+ZZI', '+ZIZ']], ['control: h on y eigenstate', [1, [['h', 0], ['s', 0], ['h', 0]]], ['-Y']], ['control: random clifford 1', [1, [['z', 0], ['s', 0], ['x', 0], ['x', 0], ['x', 0], ['z', 0], ['z', 0], ['z', 0]]], ['-Z']], ['control: random clifford 3', [1, [['s', 0], ['h', 0], ['h', 0], ['x', 0], ['h', 0], ['h', 0], ['s', 0], ['s', 0]]], ['-Z']], ['control: random clifford 5', [1, [['h', 0], ['h', 0], ['h', 0], ['h', 0], ['z', 0], ['s', 0], ['s', 0], ['s', 0], ['x', 0], ['h', 0], ['x', 0], ['h', 0]]], ['-Z']]], [['regression: random clifford 7', [3, [['cx', 0, 2], ['cx', 2, 1], ['s', 0], ['cx', 0, 2], ['cx', 2, 1], ['z', 1], ['z', 0], ['h', 1], ['x', 1]]], ['+ZII', '+ZXI', '+IIZ']], ['regression: random clifford 8', [3, [['cx', 0, 1], ['h', 2], ['h', 2], ['h', 0], ['cx', 2, 1], ['h', 2], ['z', 0], ['cx', 1, 2]]], ['-XII', '-XZX', '+IIX']], ['regression: random clifford 2', [3, [['cx', 0, 2], ['z', 2], ['s', 2], ['h', 0], ['cx', 2, 1], ['x', 1], ['cx', 1, 0], ['x', 1], ['x', 1], ['z', 0], ['cx', 2, 1], ['h', 1], ['h', 0]]], ['-ZII', '-IXI', '-ZIZ']], ['control: random clifford 6', [1, [['h', 0], ['z', 0], ['h', 0], ['z', 0], ['x', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['s', 0], ['x', 0]]], ['-Z']], ['control: random clifford 10', [1, [['z', 0], ['z', 0], ['s', 0], ['z', 0], ['h', 0], ['h', 0], ['s', 0]]], ['+Z']], ['control: random clifford 12', [1, [['h', 0], ['s', 0], ['h', 0], ['x', 0], ['s', 0], ['x', 0], ['x', 0], ['z', 0], ['h', 0], ['s', 0], ['s', 0]]], ['+Z']], ['control: random clifford 13', [1, [['z', 0], ['x', 0], ['z', 0], ['z', 0], ['s', 0], ['x', 0], ['h', 0], ['h', 0], ['z', 0], ['h', 0]]], ['+X']]], [['regression: random clifford 11', [2, [['s', 1], ['h', 0], ['cx', 0, 1], ['s', 0], ['x', 0]]], ['-YX', '-ZZ']], ['regression: random clifford 15', [2, [['z', 1], ['cx', 0, 1], ['x', 1], ['h', 1]]], ['+ZI', '-ZX']], ['regression: random clifford 7', [3, [['cx', 0, 2], ['cx', 2, 1], ['s', 0], ['cx', 0, 2], ['cx', 2, 1], ['z', 1], ['z', 0], ['h', 1], ['x', 1]]], ['+ZII', '+ZXI', '+IIZ']], ['control: random clifford 14', [1, [['z', 0], ['s', 0], ['h', 0], ['z', 0], ['h', 0], ['x', 0], ['x', 0], ['z', 0], ['h', 0], ['h', 0], ['x', 0], ['x', 0], ['z', 0], ['h', 0]]], ['-X']], ['control: random clifford 16', [1, [['x', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['z', 0], ['z', 0], ['h', 0], ['x', 0], ['h', 0]]], ['-Z']], ['control: random clifford 17', [1, [['h', 0], ['s', 0], ['h', 0], ['h', 0], ['x', 0], ['s', 0], ['s', 0]]], ['+Y']], ['control: random clifford 24', [1, [['x', 0]]], ['-Z']]], [['regression: random clifford 19', [2, [['h', 0], ['cx', 1, 0], ['cx', 1, 0], ['cx', 0, 1], ['h', 0], ['cx', 1, 0], ['cx', 1, 0], ['cx', 0, 1], ['cx', 0, 1], ['cx', 1, 0], ['cx', 0, 1]]], ['+XZ', '-YY']], ['regression: random clifford 20', [3, [['cx', 2, 0], ['h', 2], ['z', 0], ['cx', 2, 1], ['cx', 0, 2], ['cx', 2, 1]]], ['+ZIX', '+ZZI', '+IIX']], ['regression: random clifford 9', [2, [['cx', 1, 0], ['cx', 1, 0], ['x', 0], ['s', 0], ['cx', 1, 0], ['h', 1], ['x', 0], ['z', 1], ['cx', 0, 1]]], ['-ZX', '-IX']], ['control: random clifford 25', [2, [['cx', 1, 0], ['cx', 1, 0]]], ['+ZI', '+IZ']], ['control: random clifford 26', [1, [['x', 0], ['h', 0], ['x', 0], ['h', 0], ['s', 0], ['h', 0], ['s', 0], ['h', 0], ['s', 0], ['s', 0], ['x', 0]]], ['+Y']], ['control: random clifford 29', [3, [['h', 2], ['s', 0], ['z', 1], ['h', 0], ['s', 0], ['z', 2], ['x', 1], ['z', 0], ['h', 2]]], ['-YII', '-IZI', '-IIZ']], ['control: random clifford 30', [1, [['x', 0], ['z', 0], ['x', 0], ['s', 0], ['z', 0], ['z', 0], ['h', 0]]], ['+X']]]]
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

N = 1
observations = []
def solve(x):
    n, gates = x
    xs = [[0] * n for _ in range(n)]
    zs = [[1 if i == j else 0 for j in range(n)] for i in range(n)]
    rs = [0] * n
    for g in gates:
        op = g[0]
        for row in range(n):
            X, Z = xs[row], zs[row]
            if op == 'h':
                a = g[1]
                rs[row] ^= X[a] & Z[a]
                X[a], Z[a] = Z[a], X[a]
            elif op == 's':
                a = g[1]
                rs[row] ^= X[a] & Z[a]
                Z[a] ^= X[a]
            elif op == 'x':
                rs[row] ^= Z[g[1]]
            elif op == 'z':
                rs[row] ^= X[g[1]]
            elif op == 'cx':
                a, b = g[1], g[2]
                rs[row] ^= X[a] & Z[b] & (X[b] ^ Z[a] ^ 1)
                X[b] ^= X[a]
                Z[a] ^= X[b]
    letters = {(0, 0): 'I', (1, 0): 'X', (0, 1): 'Z', (1, 1): 'Y'}
    return [('-' if rs[r] else '+') + ''.join(letters[(xs[r][q], zs[r][q])] for q in range(n)) for r in range(n)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: bell preparation', [2, [['h', 0], ['cx', 0, 1]]], ['+XX', '+ZZ']], ['regression: cx on excited control', [2, [['x', 0], ['cx', 0, 1]]], ['-ZI', '+ZZ']], ['regression: ghz', [3, [['h', 0], ['cx', 0, 1], ['cx', 0, 2]]], ['+XXX', '+ZZI', '+ZIZ']], ['control: h then s', [1, [['h', 0], ['s', 0]]], ['+Y']], ['control: s twice after h', [1, [['h', 0], ['s', 0], ['s', 0]]], ['-X']], ['control: x flips z sign', [1, [['x', 0]]], ['-Z']], ['control: z on plus', [1, [['h', 0], ['z', 0]]], ['-X']]], [['regression: random clifford 0', [3, [['cx', 1, 2]]], ['+ZII', '+IZI', '+IZZ']], ['regression: random clifford 2', [3, [['cx', 0, 2], ['z', 2], ['s', 2], ['h', 0], ['cx', 2, 1], ['x', 1], ['cx', 1, 0], ['x', 1], ['x', 1], ['z', 0], ['cx', 2, 1], ['h', 1], ['h', 0]]], ['-ZII', '-IXI', '-ZIZ']], ['regression: ghz', [3, [['h', 0], ['cx', 0, 1], ['cx', 0, 2]]], ['+XXX', '+ZZI', '+ZIZ']], ['control: h on y eigenstate', [1, [['h', 0], ['s', 0], ['h', 0]]], ['-Y']], ['control: random clifford 1', [1, [['z', 0], ['s', 0], ['x', 0], ['x', 0], ['x', 0], ['z', 0], ['z', 0], ['z', 0]]], ['-Z']], ['control: random clifford 3', [1, [['s', 0], ['h', 0], ['h', 0], ['x', 0], ['h', 0], ['h', 0], ['s', 0], ['s', 0]]], ['-Z']], ['control: random clifford 5', [1, [['h', 0], ['h', 0], ['h', 0], ['h', 0], ['z', 0], ['s', 0], ['s', 0], ['s', 0], ['x', 0], ['h', 0], ['x', 0], ['h', 0]]], ['-Z']]], [['regression: random clifford 7', [3, [['cx', 0, 2], ['cx', 2, 1], ['s', 0], ['cx', 0, 2], ['cx', 2, 1], ['z', 1], ['z', 0], ['h', 1], ['x', 1]]], ['+ZII', '+ZXI', '+IIZ']], ['regression: random clifford 8', [3, [['cx', 0, 1], ['h', 2], ['h', 2], ['h', 0], ['cx', 2, 1], ['h', 2], ['z', 0], ['cx', 1, 2]]], ['-XII', '-XZX', '+IIX']], ['regression: random clifford 2', [3, [['cx', 0, 2], ['z', 2], ['s', 2], ['h', 0], ['cx', 2, 1], ['x', 1], ['cx', 1, 0], ['x', 1], ['x', 1], ['z', 0], ['cx', 2, 1], ['h', 1], ['h', 0]]], ['-ZII', '-IXI', '-ZIZ']], ['control: random clifford 6', [1, [['h', 0], ['z', 0], ['h', 0], ['z', 0], ['x', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['s', 0], ['x', 0]]], ['-Z']], ['control: random clifford 10', [1, [['z', 0], ['z', 0], ['s', 0], ['z', 0], ['h', 0], ['h', 0], ['s', 0]]], ['+Z']], ['control: random clifford 12', [1, [['h', 0], ['s', 0], ['h', 0], ['x', 0], ['s', 0], ['x', 0], ['x', 0], ['z', 0], ['h', 0], ['s', 0], ['s', 0]]], ['+Z']], ['control: random clifford 13', [1, [['z', 0], ['x', 0], ['z', 0], ['z', 0], ['s', 0], ['x', 0], ['h', 0], ['h', 0], ['z', 0], ['h', 0]]], ['+X']]], [['regression: random clifford 11', [2, [['s', 1], ['h', 0], ['cx', 0, 1], ['s', 0], ['x', 0]]], ['-YX', '-ZZ']], ['regression: random clifford 15', [2, [['z', 1], ['cx', 0, 1], ['x', 1], ['h', 1]]], ['+ZI', '-ZX']], ['regression: random clifford 7', [3, [['cx', 0, 2], ['cx', 2, 1], ['s', 0], ['cx', 0, 2], ['cx', 2, 1], ['z', 1], ['z', 0], ['h', 1], ['x', 1]]], ['+ZII', '+ZXI', '+IIZ']], ['control: random clifford 14', [1, [['z', 0], ['s', 0], ['h', 0], ['z', 0], ['h', 0], ['x', 0], ['x', 0], ['z', 0], ['h', 0], ['h', 0], ['x', 0], ['x', 0], ['z', 0], ['h', 0]]], ['-X']], ['control: random clifford 16', [1, [['x', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['z', 0], ['z', 0], ['h', 0], ['x', 0], ['h', 0]]], ['-Z']], ['control: random clifford 17', [1, [['h', 0], ['s', 0], ['h', 0], ['h', 0], ['x', 0], ['s', 0], ['s', 0]]], ['+Y']], ['control: random clifford 24', [1, [['x', 0]]], ['-Z']]], [['regression: random clifford 19', [2, [['h', 0], ['cx', 1, 0], ['cx', 1, 0], ['cx', 0, 1], ['h', 0], ['cx', 1, 0], ['cx', 1, 0], ['cx', 0, 1], ['cx', 0, 1], ['cx', 1, 0], ['cx', 0, 1]]], ['+XZ', '-YY']], ['regression: random clifford 20', [3, [['cx', 2, 0], ['h', 2], ['z', 0], ['cx', 2, 1], ['cx', 0, 2], ['cx', 2, 1]]], ['+ZIX', '+ZZI', '+IIX']], ['regression: random clifford 9', [2, [['cx', 1, 0], ['cx', 1, 0], ['x', 0], ['s', 0], ['cx', 1, 0], ['h', 1], ['x', 0], ['z', 1], ['cx', 0, 1]]], ['-ZX', '-IX']], ['control: random clifford 25', [2, [['cx', 1, 0], ['cx', 1, 0]]], ['+ZI', '+IZ']], ['control: random clifford 26', [1, [['x', 0], ['h', 0], ['x', 0], ['h', 0], ['s', 0], ['h', 0], ['s', 0], ['h', 0], ['s', 0], ['s', 0], ['x', 0]]], ['+Y']], ['control: random clifford 29', [3, [['h', 2], ['s', 0], ['z', 1], ['h', 0], ['s', 0], ['z', 2], ['x', 1], ['z', 0], ['h', 2]]], ['-YII', '-IZI', '-IIZ']], ['control: random clifford 30', [1, [['x', 0], ['z', 0], ['x', 0], ['s', 0], ['z', 0], ['z', 0], ['h', 0]]], ['+X']]]]
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 Z stabilizer on the target is not copied onto the control, so a Bell state reports +XX, +ZI instead of +XX, +ZZ.

Input [n, gates] with gates h, s, x, z (["g", q]) and cx (["cx", c, t]). Track the n stabilizer generators of |0...0> (initially Z on each qubit) under the Aaronson-Gottesman update rules and return them as signed strings such as "+XX", character q describing qubit q.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    n, gates = x
    xs = [[0] * n for _ in range(n)]
    zs = [[1 if i == j else 0 for j in range(n)] for i in range(n)]
    rs = [0] * n
    for g in gates:
        op = g[0]
        for row in range(n):
            X, Z = xs[row], zs[row]
            if op == 'h':
                a = g[1]
                rs[row] ^= X[a] & Z[a]
                X[a], Z[a] = Z[a], X[a]
            elif op == 's':
                a = g[1]
                rs[row] ^= X[a] & Z[a]
                Z[a] ^= X[a]
            elif op == 'x':
                rs[row] ^= Z[g[1]]
            elif op == 'z':
                rs[row] ^= X[g[1]]
            elif op == 'cx':
                a, b = g[1], g[2]
                rs[row] ^= X[a] & Z[b] & (X[b] ^ Z[a] ^ 1)
                X[a] ^= X[b]
                Z[a] ^= Z[b]
    letters = {(0, 0): 'I', (1, 0): 'X', (0, 1): 'Z', (1, 1): 'Y'}
    return [('-' if rs[r] else '+') + ''.join(letters[(xs[r][q], zs[r][q])] for q in range(n)) for r in range(n)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: bell preparation', [2, [['h', 0], ['cx', 0, 1]]], ['+XX', '+ZZ']], ['regression: ghz', [3, [['h', 0], ['cx', 0, 1], ['cx', 0, 2]]], ['+XXX', '+ZZI', '+ZIZ']], ['regression: random clifford 2', [3, [['cx', 0, 2], ['z', 2], ['s', 2], ['h', 0], ['cx', 2, 1], ['x', 1], ['cx', 1, 0], ['x', 1], ['x', 1], ['z', 0], ['cx', 2, 1], ['h', 1], ['h', 0]]], ['-ZII', '-IXI', '-ZIZ']], ['control: h then s', [1, [['h', 0], ['s', 0]]], ['+Y']], ['control: s twice after h', [1, [['h', 0], ['s', 0], ['s', 0]]], ['-X']], ['control: x flips z sign', [1, [['x', 0]]], ['-Z']], ['control: z on plus', [1, [['h', 0], ['z', 0]]], ['-X']]], [['regression: random clifford 4', [3, [['cx', 1, 2], ['h', 1], ['cx', 1, 0], ['s', 1], ['x', 2], ['cx', 1, 2], ['cx', 1, 2], ['h', 0], ['cx', 0, 2], ['z', 0], ['s', 2], ['h', 2], ['z', 0]]], ['-XZY', '+ZYI', '-IYX']], ['regression: random clifford 8', [3, [['cx', 0, 1], ['h', 2], ['h', 2], ['h', 0], ['cx', 2, 1], ['h', 2], ['z', 0], ['cx', 1, 2]]], ['-XII', '-XZX', '+IIX']], ['regression: random clifford 9', [2, [['cx', 1, 0], ['cx', 1, 0], ['x', 0], ['s', 0], ['cx', 1, 0], ['h', 1], ['x', 0], ['z', 1], ['cx', 0, 1]]], ['-ZX', '-IX']], ['control: cx on excited control', [2, [['x', 0], ['cx', 0, 1]]], ['-ZI', '+ZZ']], ['control: h on y eigenstate', [1, [['h', 0], ['s', 0], ['h', 0]]], ['-Y']], ['control: random clifford 0', [3, [['cx', 1, 2]]], ['+ZII', '+IZI', '+IZZ']], ['control: random clifford 1', [1, [['z', 0], ['s', 0], ['x', 0], ['x', 0], ['x', 0], ['z', 0], ['z', 0], ['z', 0]]], ['-Z']]], [['regression: random clifford 11', [2, [['s', 1], ['h', 0], ['cx', 0, 1], ['s', 0], ['x', 0]]], ['-YX', '-ZZ']], ['regression: random clifford 18', [3, [['cx', 1, 0], ['z', 0], ['z', 1], ['x', 2], ['h', 0], ['s', 2], ['z', 1], ['z', 2], ['cx', 0, 1], ['z', 1]]], ['+YYI', '+ZZI', '-IIZ']], ['regression: random clifford 20', [3, [['cx', 2, 0], ['h', 2], ['z', 0], ['cx', 2, 1], ['cx', 0, 2], ['cx', 2, 1]]], ['+ZIX', '+ZZI', '+IIX']], ['control: random clifford 3', [1, [['s', 0], ['h', 0], ['h', 0], ['x', 0], ['h', 0], ['h', 0], ['s', 0], ['s', 0]]], ['-Z']], ['control: random clifford 5', [1, [['h', 0], ['h', 0], ['h', 0], ['h', 0], ['z', 0], ['s', 0], ['s', 0], ['s', 0], ['x', 0], ['h', 0], ['x', 0], ['h', 0]]], ['-Z']], ['control: random clifford 6', [1, [['h', 0], ['z', 0], ['h', 0], ['z', 0], ['x', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['s', 0], ['x', 0]]], ['-Z']], ['control: random clifford 7', [3, [['cx', 0, 2], ['cx', 2, 1], ['s', 0], ['cx', 0, 2], ['cx', 2, 1], ['z', 1], ['z', 0], ['h', 1], ['x', 1]]], ['+ZII', '+ZXI', '+IIZ']]], [['regression: random clifford 20', [3, [['cx', 2, 0], ['h', 2], ['z', 0], ['cx', 2, 1], ['cx', 0, 2], ['cx', 2, 1]]], ['+ZIX', '+ZZI', '+IIX']], ['regression: random clifford 22', [3, [['h', 2], ['h', 0], ['cx', 1, 0], ['s', 1], ['cx', 0, 1], ['cx', 1, 0], ['s', 0], ['cx', 2, 1]]], ['+IXI', '+ZII', '+IXX']], ['regression: random clifford 23', [3, [['s', 0], ['z', 0], ['cx', 1, 0], ['z', 1], ['x', 2], ['z', 2], ['cx', 0, 1], ['z', 0], ['s', 2], ['h', 1], ['h', 2], ['cx', 2, 0], ['cx', 2, 1], ['z', 0]]], ['+IXI', '+ZXZ', '+XXX']], ['control: random clifford 10', [1, [['z', 0], ['z', 0], ['s', 0], ['z', 0], ['h', 0], ['h', 0], ['s', 0]]], ['+Z']], ['control: random clifford 12', [1, [['h', 0], ['s', 0], ['h', 0], ['x', 0], ['s', 0], ['x', 0], ['x', 0], ['z', 0], ['h', 0], ['s', 0], ['s', 0]]], ['+Z']], ['control: random clifford 13', [1, [['z', 0], ['x', 0], ['z', 0], ['z', 0], ['s', 0], ['x', 0], ['h', 0], ['h', 0], ['z', 0], ['h', 0]]], ['+X']], ['control: random clifford 14', [1, [['z', 0], ['s', 0], ['h', 0], ['z', 0], ['h', 0], ['x', 0], ['x', 0], ['z', 0], ['h', 0], ['h', 0], ['x', 0], ['x', 0], ['z', 0], ['h', 0]]], ['-X']]], [['regression: random clifford 27', [2, [['cx', 1, 0], ['x', 0], ['h', 0], ['cx', 1, 0], ['cx', 1, 0], ['cx', 1, 0], ['h', 0], ['cx', 0, 1], ['cx', 0, 1], ['h', 0], ['z', 1], ['h', 1]]], ['-XX', '+IX']], ['regression: random clifford 28', [3, [['h', 1], ['cx', 0, 1], ['h', 0], ['cx', 2, 1], ['z', 0], ['x', 0], ['cx', 2, 1], ['h', 1], ['h', 2], ['s', 0], ['h', 0], ['cx', 1, 2]]], ['+YII', '+IZI', '+IIX']], ['regression: random clifford 35', [2, [['z', 1], ['s', 1], ['s', 1], ['cx', 0, 1], ['cx', 0, 1], ['cx', 1, 0], ['z', 0], ['x', 0], ['cx', 0, 1], ['z', 0], ['h', 1], ['h', 0], ['cx', 1, 0], ['cx', 0, 1]]], ['-XI', '+IX']], ['control: random clifford 15', [2, [['z', 1], ['cx', 0, 1], ['x', 1], ['h', 1]]], ['+ZI', '-ZX']], ['control: random clifford 16', [1, [['x', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['z', 0], ['z', 0], ['h', 0], ['x', 0], ['h', 0]]], ['-Z']], ['control: random clifford 17', [1, [['h', 0], ['s', 0], ['h', 0], ['h', 0], ['x', 0], ['s', 0], ['s', 0]]], ['+Y']], ['control: random clifford 21', [3, [['h', 0], ['s', 2], ['s', 1], ['h', 0], ['cx', 0, 2], ['cx', 0, 2], ['cx', 1, 2], ['h', 1], ['h', 1], ['cx', 2, 1]]], ['+ZII', '+IZZ', '+IZI']]]]
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)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    n, gates = x
    xs = [[0] * n for _ in range(n)]
    zs = [[1 if i == j else 0 for j in range(n)] for i in range(n)]
    rs = [0] * n
    for g in gates:
        op = g[0]
        for row in range(n):
            X, Z = xs[row], zs[row]
            if op == 'h':
                a = g[1]
                rs[row] ^= X[a] & Z[a]
                X[a], Z[a] = Z[a], X[a]
            elif op == 's':
                a = g[1]
                rs[row] ^= X[a] & Z[a]
                Z[a] ^= X[a]
            elif op == 'x':
                rs[row] ^= Z[g[1]]
            elif op == 'z':
                rs[row] ^= X[g[1]]
            elif op == 'cx':
                a, b = g[1], g[2]
                rs[row] ^= X[a] & Z[b] & (X[b] ^ Z[a] ^ 1)
                X[b] = X[a]
                Z[a] ^= Z[b]
    letters = {(0, 0): 'I', (1, 0): 'X', (0, 1): 'Z', (1, 1): 'Y'}
    return [('-' if rs[r] else '+') + ''.join(letters[(xs[r][q], zs[r][q])] for q in range(n)) for r in range(n)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: bell preparation', [2, [['h', 0], ['cx', 0, 1]]], ['+XX', '+ZZ']], ['regression: ghz', [3, [['h', 0], ['cx', 0, 1], ['cx', 0, 2]]], ['+XXX', '+ZZI', '+ZIZ']], ['regression: random clifford 2', [3, [['cx', 0, 2], ['z', 2], ['s', 2], ['h', 0], ['cx', 2, 1], ['x', 1], ['cx', 1, 0], ['x', 1], ['x', 1], ['z', 0], ['cx', 2, 1], ['h', 1], ['h', 0]]], ['-ZII', '-IXI', '-ZIZ']], ['control: h then s', [1, [['h', 0], ['s', 0]]], ['+Y']], ['control: s twice after h', [1, [['h', 0], ['s', 0], ['s', 0]]], ['-X']], ['control: x flips z sign', [1, [['x', 0]]], ['-Z']], ['control: z on plus', [1, [['h', 0], ['z', 0]]], ['-X']]], [['regression: random clifford 4', [3, [['cx', 1, 2], ['h', 1], ['cx', 1, 0], ['s', 1], ['x', 2], ['cx', 1, 2], ['cx', 1, 2], ['h', 0], ['cx', 0, 2], ['z', 0], ['s', 2], ['h', 2], ['z', 0]]], ['-XZY', '+ZYI', '-IYX']], ['regression: random clifford 8', [3, [['cx', 0, 1], ['h', 2], ['h', 2], ['h', 0], ['cx', 2, 1], ['h', 2], ['z', 0], ['cx', 1, 2]]], ['-XII', '-XZX', '+IIX']], ['regression: random clifford 9', [2, [['cx', 1, 0], ['cx', 1, 0], ['x', 0], ['s', 0], ['cx', 1, 0], ['h', 1], ['x', 0], ['z', 1], ['cx', 0, 1]]], ['-ZX', '-IX']], ['control: cx on excited control', [2, [['x', 0], ['cx', 0, 1]]], ['-ZI', '+ZZ']], ['control: h on y eigenstate', [1, [['h', 0], ['s', 0], ['h', 0]]], ['-Y']], ['control: random clifford 0', [3, [['cx', 1, 2]]], ['+ZII', '+IZI', '+IZZ']], ['control: random clifford 1', [1, [['z', 0], ['s', 0], ['x', 0], ['x', 0], ['x', 0], ['z', 0], ['z', 0], ['z', 0]]], ['-Z']]], [['regression: random clifford 11', [2, [['s', 1], ['h', 0], ['cx', 0, 1], ['s', 0], ['x', 0]]], ['-YX', '-ZZ']], ['regression: random clifford 18', [3, [['cx', 1, 0], ['z', 0], ['z', 1], ['x', 2], ['h', 0], ['s', 2], ['z', 1], ['z', 2], ['cx', 0, 1], ['z', 1]]], ['+YYI', '+ZZI', '-IIZ']], ['regression: random clifford 20', [3, [['cx', 2, 0], ['h', 2], ['z', 0], ['cx', 2, 1], ['cx', 0, 2], ['cx', 2, 1]]], ['+ZIX', '+ZZI', '+IIX']], ['control: random clifford 3', [1, [['s', 0], ['h', 0], ['h', 0], ['x', 0], ['h', 0], ['h', 0], ['s', 0], ['s', 0]]], ['-Z']], ['control: random clifford 5', [1, [['h', 0], ['h', 0], ['h', 0], ['h', 0], ['z', 0], ['s', 0], ['s', 0], ['s', 0], ['x', 0], ['h', 0], ['x', 0], ['h', 0]]], ['-Z']], ['control: random clifford 6', [1, [['h', 0], ['z', 0], ['h', 0], ['z', 0], ['x', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['s', 0], ['x', 0]]], ['-Z']], ['control: random clifford 7', [3, [['cx', 0, 2], ['cx', 2, 1], ['s', 0], ['cx', 0, 2], ['cx', 2, 1], ['z', 1], ['z', 0], ['h', 1], ['x', 1]]], ['+ZII', '+ZXI', '+IIZ']]], [['regression: random clifford 20', [3, [['cx', 2, 0], ['h', 2], ['z', 0], ['cx', 2, 1], ['cx', 0, 2], ['cx', 2, 1]]], ['+ZIX', '+ZZI', '+IIX']], ['regression: random clifford 22', [3, [['h', 2], ['h', 0], ['cx', 1, 0], ['s', 1], ['cx', 0, 1], ['cx', 1, 0], ['s', 0], ['cx', 2, 1]]], ['+IXI', '+ZII', '+IXX']], ['regression: random clifford 23', [3, [['s', 0], ['z', 0], ['cx', 1, 0], ['z', 1], ['x', 2], ['z', 2], ['cx', 0, 1], ['z', 0], ['s', 2], ['h', 1], ['h', 2], ['cx', 2, 0], ['cx', 2, 1], ['z', 0]]], ['+IXI', '+ZXZ', '+XXX']], ['control: random clifford 10', [1, [['z', 0], ['z', 0], ['s', 0], ['z', 0], ['h', 0], ['h', 0], ['s', 0]]], ['+Z']], ['control: random clifford 12', [1, [['h', 0], ['s', 0], ['h', 0], ['x', 0], ['s', 0], ['x', 0], ['x', 0], ['z', 0], ['h', 0], ['s', 0], ['s', 0]]], ['+Z']], ['control: random clifford 13', [1, [['z', 0], ['x', 0], ['z', 0], ['z', 0], ['s', 0], ['x', 0], ['h', 0], ['h', 0], ['z', 0], ['h', 0]]], ['+X']], ['control: random clifford 14', [1, [['z', 0], ['s', 0], ['h', 0], ['z', 0], ['h', 0], ['x', 0], ['x', 0], ['z', 0], ['h', 0], ['h', 0], ['x', 0], ['x', 0], ['z', 0], ['h', 0]]], ['-X']]], [['regression: random clifford 27', [2, [['cx', 1, 0], ['x', 0], ['h', 0], ['cx', 1, 0], ['cx', 1, 0], ['cx', 1, 0], ['h', 0], ['cx', 0, 1], ['cx', 0, 1], ['h', 0], ['z', 1], ['h', 1]]], ['-XX', '+IX']], ['regression: random clifford 28', [3, [['h', 1], ['cx', 0, 1], ['h', 0], ['cx', 2, 1], ['z', 0], ['x', 0], ['cx', 2, 1], ['h', 1], ['h', 2], ['s', 0], ['h', 0], ['cx', 1, 2]]], ['+YII', '+IZI', '+IIX']], ['regression: random clifford 35', [2, [['z', 1], ['s', 1], ['s', 1], ['cx', 0, 1], ['cx', 0, 1], ['cx', 1, 0], ['z', 0], ['x', 0], ['cx', 0, 1], ['z', 0], ['h', 1], ['h', 0], ['cx', 1, 0], ['cx', 0, 1]]], ['-XI', '+IX']], ['control: random clifford 15', [2, [['z', 1], ['cx', 0, 1], ['x', 1], ['h', 1]]], ['+ZI', '-ZX']], ['control: random clifford 16', [1, [['x', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['z', 0], ['z', 0], ['h', 0], ['x', 0], ['h', 0]]], ['-Z']], ['control: random clifford 17', [1, [['h', 0], ['s', 0], ['h', 0], ['h', 0], ['x', 0], ['s', 0], ['s', 0]]], ['+Y']], ['control: random clifford 21', [3, [['h', 0], ['s', 2], ['s', 1], ['h', 0], ['cx', 0, 2], ['cx', 0, 2], ['cx', 1, 2], ['h', 1], ['h', 1], ['cx', 2, 1]]], ['+ZII', '+IZZ', '+IZI']]]]
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:
After H on qubit 0 and CNOT(0,1) the generator is reported as +XI instead of +XX.

Input [n, gates] with gates h, s, x, z (["g", q]) and cx (["cx", c, t]). Track the n stabilizer generators of |0...0> (initially Z on each qubit) under the Aaronson-Gottesman update rules and return them as signed strings such as "+XX", character q describing qubit q.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    n, gates = x
    xs = [[0] * n for _ in range(n)]
    zs = [[1 if i == j else 0 for j in range(n)] for i in range(n)]
    rs = [0] * n
    for g in gates:
        op = g[0]
        for row in range(n):
            X, Z = xs[row], zs[row]
            if op == 'h':
                a = g[1]
                rs[row] ^= X[a] & Z[a]
                X[a], Z[a] = Z[a], X[a]
            elif op == 's':
                a = g[1]
                rs[row] ^= X[a] & Z[a]
                Z[a] ^= X[a]
            elif op == 'x':
                rs[row] ^= X[g[1]]
            elif op == 'z':
                rs[row] ^= X[g[1]]
            elif op == 'cx':
                a, b = g[1], g[2]
                rs[row] ^= X[a] & Z[b] & (X[b] ^ Z[a] ^ 1)
                X[b] ^= X[a]
                Z[a] ^= Z[b]
    letters = {(0, 0): 'I', (1, 0): 'X', (0, 1): 'Z', (1, 1): 'Y'}
    return [('-' if rs[r] else '+') + ''.join(letters[(xs[r][q], zs[r][q])] for q in range(n)) for r in range(n)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: x flips z sign', [1, [['x', 0]]], ['-Z']], ['regression: cx on excited control', [2, [['x', 0], ['cx', 0, 1]]], ['-ZI', '+ZZ']], ['repair check: random clifford 5', [1, [['h', 0], ['h', 0], ['h', 0], ['h', 0], ['z', 0], ['s', 0], ['s', 0], ['s', 0], ['x', 0], ['h', 0], ['x', 0], ['h', 0]]], ['-Z']], ['control: bell preparation', [2, [['h', 0], ['cx', 0, 1]]], ['+XX', '+ZZ']], ['control: h then s', [1, [['h', 0], ['s', 0]]], ['+Y']], ['control: s twice after h', [1, [['h', 0], ['s', 0], ['s', 0]]], ['-X']], ['control: z on plus', [1, [['h', 0], ['z', 0]]], ['-X']]], [['regression: random clifford 2', [3, [['cx', 0, 2], ['z', 2], ['s', 2], ['h', 0], ['cx', 2, 1], ['x', 1], ['cx', 1, 0], ['x', 1], ['x', 1], ['z', 0], ['cx', 2, 1], ['h', 1], ['h', 0]]], ['-ZII', '-IXI', '-ZIZ']], ['regression: random clifford 3', [1, [['s', 0], ['h', 0], ['h', 0], ['x', 0], ['h', 0], ['h', 0], ['s', 0], ['s', 0]]], ['-Z']], ['regression: random clifford 11', [2, [['s', 1], ['h', 0], ['cx', 0, 1], ['s', 0], ['x', 0]]], ['-YX', '-ZZ']], ['control: ghz', [3, [['h', 0], ['cx', 0, 1], ['cx', 0, 2]]], ['+XXX', '+ZZI', '+ZIZ']], ['control: h on y eigenstate', [1, [['h', 0], ['s', 0], ['h', 0]]], ['-Y']], ['control: random clifford 0', [3, [['cx', 1, 2]]], ['+ZII', '+IZI', '+IZZ']], ['control: random clifford 6', [1, [['h', 0], ['z', 0], ['h', 0], ['z', 0], ['x', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['s', 0], ['x', 0]]], ['-Z']]], [['regression: random clifford 7', [3, [['cx', 0, 2], ['cx', 2, 1], ['s', 0], ['cx', 0, 2], ['cx', 2, 1], ['z', 1], ['z', 0], ['h', 1], ['x', 1]]], ['+ZII', '+ZXI', '+IIZ']], ['regression: random clifford 11', [2, [['s', 1], ['h', 0], ['cx', 0, 1], ['s', 0], ['x', 0]]], ['-YX', '-ZZ']], ['repair check: random clifford 16', [1, [['x', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['z', 0], ['z', 0], ['h', 0], ['x', 0], ['h', 0]]], ['-Z']], ['control: random clifford 8', [3, [['cx', 0, 1], ['h', 2], ['h', 2], ['h', 0], ['cx', 2, 1], ['h', 2], ['z', 0], ['cx', 1, 2]]], ['-XII', '-XZX', '+IIX']], ['control: random clifford 9', [2, [['cx', 1, 0], ['cx', 1, 0], ['x', 0], ['s', 0], ['cx', 1, 0], ['h', 1], ['x', 0], ['z', 1], ['cx', 0, 1]]], ['-ZX', '-IX']], ['control: random clifford 10', [1, [['z', 0], ['z', 0], ['s', 0], ['z', 0], ['h', 0], ['h', 0], ['s', 0]]], ['+Z']], ['control: random clifford 13', [1, [['z', 0], ['x', 0], ['z', 0], ['z', 0], ['s', 0], ['x', 0], ['h', 0], ['h', 0], ['z', 0], ['h', 0]]], ['+X']]], [['regression: random clifford 18', [3, [['cx', 1, 0], ['z', 0], ['z', 1], ['x', 2], ['h', 0], ['s', 2], ['z', 1], ['z', 2], ['cx', 0, 1], ['z', 1]]], ['+YYI', '+ZZI', '-IIZ']], ['regression: random clifford 23', [3, [['s', 0], ['z', 0], ['cx', 1, 0], ['z', 1], ['x', 2], ['z', 2], ['cx', 0, 1], ['z', 0], ['s', 2], ['h', 1], ['h', 2], ['cx', 2, 0], ['cx', 2, 1], ['z', 0]]], ['+IXI', '+ZXZ', '+XXX']], ['regression: random clifford 28', [3, [['h', 1], ['cx', 0, 1], ['h', 0], ['cx', 2, 1], ['z', 0], ['x', 0], ['cx', 2, 1], ['h', 1], ['h', 2], ['s', 0], ['h', 0], ['cx', 1, 2]]], ['+YII', '+IZI', '+IIX']], ['control: random clifford 14', [1, [['z', 0], ['s', 0], ['h', 0], ['z', 0], ['h', 0], ['x', 0], ['x', 0], ['z', 0], ['h', 0], ['h', 0], ['x', 0], ['x', 0], ['z', 0], ['h', 0]]], ['-X']], ['control: random clifford 19', [2, [['h', 0], ['cx', 1, 0], ['cx', 1, 0], ['cx', 0, 1], ['h', 0], ['cx', 1, 0], ['cx', 1, 0], ['cx', 0, 1], ['cx', 0, 1], ['cx', 1, 0], ['cx', 0, 1]]], ['+XZ', '-YY']], ['control: random clifford 20', [3, [['cx', 2, 0], ['h', 2], ['z', 0], ['cx', 2, 1], ['cx', 0, 2], ['cx', 2, 1]]], ['+ZIX', '+ZZI', '+IIX']], ['control: random clifford 21', [3, [['h', 0], ['s', 2], ['s', 1], ['h', 0], ['cx', 0, 2], ['cx', 0, 2], ['cx', 1, 2], ['h', 1], ['h', 1], ['cx', 2, 1]]], ['+ZII', '+IZZ', '+IZI']]], [['regression: random clifford 27', [2, [['cx', 1, 0], ['x', 0], ['h', 0], ['cx', 1, 0], ['cx', 1, 0], ['cx', 1, 0], ['h', 0], ['cx', 0, 1], ['cx', 0, 1], ['h', 0], ['z', 1], ['h', 1]]], ['-XX', '+IX']], ['regression: random clifford 28', [3, [['h', 1], ['cx', 0, 1], ['h', 0], ['cx', 2, 1], ['z', 0], ['x', 0], ['cx', 2, 1], ['h', 1], ['h', 2], ['s', 0], ['h', 0], ['cx', 1, 2]]], ['+YII', '+IZI', '+IIX']], ['regression: random clifford 43', [1, [['h', 0], ['x', 0], ['h', 0], ['z', 0], ['x', 0], ['x', 0], ['s', 0]]], ['+Z']], ['control: random clifford 22', [3, [['h', 2], ['h', 0], ['cx', 1, 0], ['s', 1], ['cx', 0, 1], ['cx', 1, 0], ['s', 0], ['cx', 2, 1]]], ['+IXI', '+ZII', '+IXX']], ['control: random clifford 25', [2, [['cx', 1, 0], ['cx', 1, 0]]], ['+ZI', '+IZ']], ['control: random clifford 26', [1, [['x', 0], ['h', 0], ['x', 0], ['h', 0], ['s', 0], ['h', 0], ['s', 0], ['h', 0], ['s', 0], ['s', 0], ['x', 0]]], ['+Y']], ['control: random clifford 30', [1, [['x', 0], ['z', 0], ['x', 0], ['s', 0], ['z', 0], ['z', 0], ['h', 0]]], ['+X']]]]
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

N = 1
observations = []
def solve(x):
    n, gates = x
    xs = [[0] * n for _ in range(n)]
    zs = [[1 if i == j else 0 for j in range(n)] for i in range(n)]
    rs = [0] * n
    for g in gates:
        op = g[0]
        for row in range(n):
            X, Z = xs[row], zs[row]
            if op == 'h':
                a = g[1]
                rs[row] ^= X[a] & Z[a]
                X[a], Z[a] = Z[a], X[a]
            elif op == 's':
                a = g[1]
                rs[row] ^= X[a] & Z[a]
                Z[a] ^= X[a]
            elif op == 'x':
                rs[row] ^= Z[g[1]] ^ X[g[1]]
            elif op == 'z':
                rs[row] ^= X[g[1]]
            elif op == 'cx':
                a, b = g[1], g[2]
                rs[row] ^= X[a] & Z[b] & (X[b] ^ Z[a] ^ 1)
                X[b] ^= X[a]
                Z[a] ^= Z[b]
    letters = {(0, 0): 'I', (1, 0): 'X', (0, 1): 'Z', (1, 1): 'Y'}
    return [('-' if rs[r] else '+') + ''.join(letters[(xs[r][q], zs[r][q])] for q in range(n)) for r in range(n)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: x flips z sign', [1, [['x', 0]]], ['-Z']], ['regression: cx on excited control', [2, [['x', 0], ['cx', 0, 1]]], ['-ZI', '+ZZ']], ['repair check: random clifford 5', [1, [['h', 0], ['h', 0], ['h', 0], ['h', 0], ['z', 0], ['s', 0], ['s', 0], ['s', 0], ['x', 0], ['h', 0], ['x', 0], ['h', 0]]], ['-Z']], ['control: bell preparation', [2, [['h', 0], ['cx', 0, 1]]], ['+XX', '+ZZ']], ['control: h then s', [1, [['h', 0], ['s', 0]]], ['+Y']], ['control: s twice after h', [1, [['h', 0], ['s', 0], ['s', 0]]], ['-X']], ['control: z on plus', [1, [['h', 0], ['z', 0]]], ['-X']]], [['regression: random clifford 2', [3, [['cx', 0, 2], ['z', 2], ['s', 2], ['h', 0], ['cx', 2, 1], ['x', 1], ['cx', 1, 0], ['x', 1], ['x', 1], ['z', 0], ['cx', 2, 1], ['h', 1], ['h', 0]]], ['-ZII', '-IXI', '-ZIZ']], ['regression: random clifford 3', [1, [['s', 0], ['h', 0], ['h', 0], ['x', 0], ['h', 0], ['h', 0], ['s', 0], ['s', 0]]], ['-Z']], ['regression: random clifford 11', [2, [['s', 1], ['h', 0], ['cx', 0, 1], ['s', 0], ['x', 0]]], ['-YX', '-ZZ']], ['control: ghz', [3, [['h', 0], ['cx', 0, 1], ['cx', 0, 2]]], ['+XXX', '+ZZI', '+ZIZ']], ['control: h on y eigenstate', [1, [['h', 0], ['s', 0], ['h', 0]]], ['-Y']], ['control: random clifford 0', [3, [['cx', 1, 2]]], ['+ZII', '+IZI', '+IZZ']], ['control: random clifford 6', [1, [['h', 0], ['z', 0], ['h', 0], ['z', 0], ['x', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['s', 0], ['x', 0]]], ['-Z']]], [['regression: random clifford 7', [3, [['cx', 0, 2], ['cx', 2, 1], ['s', 0], ['cx', 0, 2], ['cx', 2, 1], ['z', 1], ['z', 0], ['h', 1], ['x', 1]]], ['+ZII', '+ZXI', '+IIZ']], ['regression: random clifford 11', [2, [['s', 1], ['h', 0], ['cx', 0, 1], ['s', 0], ['x', 0]]], ['-YX', '-ZZ']], ['repair check: random clifford 16', [1, [['x', 0], ['h', 0], ['h', 0], ['h', 0], ['h', 0], ['z', 0], ['z', 0], ['h', 0], ['x', 0], ['h', 0]]], ['-Z']], ['control: random clifford 8', [3, [['cx', 0, 1], ['h', 2], ['h', 2], ['h', 0], ['cx', 2, 1], ['h', 2], ['z', 0], ['cx', 1, 2]]], ['-XII', '-XZX', '+IIX']], ['control: random clifford 9', [2, [['cx', 1, 0], ['cx', 1, 0], ['x', 0], ['s', 0], ['cx', 1, 0], ['h', 1], ['x', 0], ['z', 1], ['cx', 0, 1]]], ['-ZX', '-IX']], ['control: random clifford 10', [1, [['z', 0], ['z', 0], ['s', 0], ['z', 0], ['h', 0], ['h', 0], ['s', 0]]], ['+Z']], ['control: random clifford 13', [1, [['z', 0], ['x', 0], ['z', 0], ['z', 0], ['s', 0], ['x', 0], ['h', 0], ['h', 0], ['z', 0], ['h', 0]]], ['+X']]], [['regression: random clifford 18', [3, [['cx', 1, 0], ['z', 0], ['z', 1], ['x', 2], ['h', 0], ['s', 2], ['z', 1], ['z', 2], ['cx', 0, 1], ['z', 1]]], ['+YYI', '+ZZI', '-IIZ']], ['regression: random clifford 23', [3, [['s', 0], ['z', 0], ['cx', 1, 0], ['z', 1], ['x', 2], ['z', 2], ['cx', 0, 1], ['z', 0], ['s', 2], ['h', 1], ['h', 2], ['cx', 2, 0], ['cx', 2, 1], ['z', 0]]], ['+IXI', '+ZXZ', '+XXX']], ['regression: random clifford 28', [3, [['h', 1], ['cx', 0, 1], ['h', 0], ['cx', 2, 1], ['z', 0], ['x', 0], ['cx', 2, 1], ['h', 1], ['h', 2], ['s', 0], ['h', 0], ['cx', 1, 2]]], ['+YII', '+IZI', '+IIX']], ['control: random clifford 14', [1, [['z', 0], ['s', 0], ['h', 0], ['z', 0], ['h', 0], ['x', 0], ['x', 0], ['z', 0], ['h', 0], ['h', 0], ['x', 0], ['x', 0], ['z', 0], ['h', 0]]], ['-X']], ['control: random clifford 19', [2, [['h', 0], ['cx', 1, 0], ['cx', 1, 0], ['cx', 0, 1], ['h', 0], ['cx', 1, 0], ['cx', 1, 0], ['cx', 0, 1], ['cx', 0, 1], ['cx', 1, 0], ['cx', 0, 1]]], ['+XZ', '-YY']], ['control: random clifford 20', [3, [['cx', 2, 0], ['h', 2], ['z', 0], ['cx', 2, 1], ['cx', 0, 2], ['cx', 2, 1]]], ['+ZIX', '+ZZI', '+IIX']], ['control: random clifford 21', [3, [['h', 0], ['s', 2], ['s', 1], ['h', 0], ['cx', 0, 2], ['cx', 0, 2], ['cx', 1, 2], ['h', 1], ['h', 1], ['cx', 2, 1]]], ['+ZII', '+IZZ', '+IZI']]], [['regression: random clifford 27', [2, [['cx', 1, 0], ['x', 0], ['h', 0], ['cx', 1, 0], ['cx', 1, 0], ['cx', 1, 0], ['h', 0], ['cx', 0, 1], ['cx', 0, 1], ['h', 0], ['z', 1], ['h', 1]]], ['-XX', '+IX']], ['regression: random clifford 28', [3, [['h', 1], ['cx', 0, 1], ['h', 0], ['cx', 2, 1], ['z', 0], ['x', 0], ['cx', 2, 1], ['h', 1], ['h', 2], ['s', 0], ['h', 0], ['cx', 1, 2]]], ['+YII', '+IZI', '+IIX']], ['regression: random clifford 43', [1, [['h', 0], ['x', 0], ['h', 0], ['z', 0], ['x', 0], ['x', 0], ['s', 0]]], ['+Z']], ['control: random clifford 22', [3, [['h', 2], ['h', 0], ['cx', 1, 0], ['s', 1], ['cx', 0, 1], ['cx', 1, 0], ['s', 0], ['cx', 2, 1]]], ['+IXI', '+ZII', '+IXX']], ['control: random clifford 25', [2, [['cx', 1, 0], ['cx', 1, 0]]], ['+ZI', '+IZ']], ['control: random clifford 26', [1, [['x', 0], ['h', 0], ['x', 0], ['h', 0], ['s', 0], ['h', 0], ['s', 0], ['h', 0], ['s', 0], ['s', 0], ['x', 0]]], ['+Y']], ['control: random clifford 30', [1, [['x', 0], ['z', 0], ['x', 0], ['s', 0], ['z', 0], ['z', 0], ['h', 0]]], ['+X']]]]
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 |0> leaves the stabilizer +Z instead of -Z.

Input [n, gates] with gates h, s, x, z (["g", q]) and cx (["cx", c, t]). Track the n stabilizer generators of |0...0> (initially Z on each qubit) under the Aaronson-Gottesman update rules and return them as signed strings such as "+XX", character q describing qubit q.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    nq, nc, ops = x
    ql = [0] * nq
    cl = [0] * nc
    for op in ops:
        name, qs = op[0], op[1]
        if name == 'barrier':
            if not qs:
                continue
            top = max(ql[q] for q in qs)
            for q in qs:
                ql[q] = top
            continue
        if name == 'id':
            continue
        cbits = []
        if name == 'measure':
            cbits = op[2]
        elif len(op) > 2 and op[2] == 'if':
            cbits = [op[3]]
        level = 1 + max([ql[q] for q in qs] + [cl[c] for c in cbits])
        for q in qs:
            ql[q] = level
        for c in cbits:
            cl[c] = level
    return [max(ql + cl + [0]), ql]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: barrier aligns everything', [3, 1, [['h', [0]], ['h', [0]], ['barrier', []], ['x', [2]]]], [3, [2, 2, 3]]], ['regression: random schedule 37', [3, 2, [['rz', [2]], ['rz', [0]], ['h', [0]], ['h', [2]], ['rz', [2]], ['barrier', []], ['x', [0], 'if', 0]]], [4, [4, 3, 3]]], ['regression: random schedule 47', [4, 1, [['ccx', [1, 0, 3]], ['barrier', []], ['measure', [0], [0]], ['x', [3]], ['ccx', [1, 3, 0]], ['cx', [0, 1]], ['x', [2]], ['id', [0]]]], [4, [4, 4, 2, 3]]], ['control: partial barrier', [3, 1, [['h', [0]], ['barrier', [0, 1]], ['x', [1]], ['x', [2]]]], [2, [1, 2, 1]]], ['control: toffoli waits on all', [3, 0, [['h', [2]], ['h', [2]], ['ccx', [0, 1, 2]], ['x', [0]]]], [4, [4, 3, 3]]], ['control: cx waits on target', [2, 0, [['h', [1]], ['h', [1]], ['cx', [0, 1]]]], [3, [3, 3]]], ['control: identity is free', [1, 0, [['id', [0]], ['id', [0]], ['x', [0]]]], [1, [1]]]], [['regression: barrier aligns everything', [3, 1, [['h', [0]], ['h', [0]], ['barrier', []], ['x', [2]]]], [3, [2, 2, 3]]], ['regression: random schedule 37', [3, 2, [['rz', [2]], ['rz', [0]], ['h', [0]], ['h', [2]], ['rz', [2]], ['barrier', []], ['x', [0], 'if', 0]]], [4, [4, 3, 3]]], ['regression: random schedule 47', [4, 1, [['ccx', [1, 0, 3]], ['barrier', []], ['measure', [0], [0]], ['x', [3]], ['ccx', [1, 3, 0]], ['cx', [0, 1]], ['x', [2]], ['id', [0]]]], [4, [4, 4, 2, 3]]], ['control: conditioned gate after measure', [2, 1, [['h', [0]], ['measure', [0], [0]], ['x', [1], 'if', 0]]], [3, [2, 3]]], ['control: measure after conditioned read', [2, 1, [['h', [0]], ['h', [0]], ['h', [0]], ['x', [1], 'if', 0], ['measure', [1], [0]]]], [3, [3, 2]]], ['control: empty circuit', [2, 1, []], [0, [0, 0]]], ['control: late read then early-qubit measure', [2, 1, [['h', [1]], ['h', [1]], ['h', [1]], ['x', [1], 'if', 0], ['measure', [0], [0]]]], [5, [5, 4]]]], [['regression: barrier aligns everything', [3, 1, [['h', [0]], ['h', [0]], ['barrier', []], ['x', [2]]]], [3, [2, 2, 3]]], ['regression: random schedule 37', [3, 2, [['rz', [2]], ['rz', [0]], ['h', [0]], ['h', [2]], ['rz', [2]], ['barrier', []], ['x', [0], 'if', 0]]], [4, [4, 3, 3]]], ['regression: random schedule 47', [4, 1, [['ccx', [1, 0, 3]], ['barrier', []], ['measure', [0], [0]], ['x', [3]], ['ccx', [1, 3, 0]], ['cx', [0, 1]], ['x', [2]], ['id', [0]]]], [4, [4, 4, 2, 3]]], ['control: read then measure other clbit writer', [3, 2, [['h', [2]], ['h', [2]], ['z', [2], 'if', 1], ['measure', [0], [1]], ['x', [1], 'if', 1]]], [5, [4, 5, 3]]], ['control: random schedule 0', [1, 2, [['x', [0]], ['rz', [0]], ['measure', [0], [0]], ['x', [0]], ['barrier', [0]], ['x', [0], 'if', 1], ['x', [0], 'if', 1], ['x', [0]], ['measure', [0], [0]]]], [8, [8]]], ['control: random schedule 1', [4, 2, [['ccx', [1, 0, 3]], ['ccx', [2, 1, 3]], ['barrier', [0, 1, 2, 3]], ['cx', [1, 0]], ['id', [1]], ['cx', [1, 2]], ['cx', [0, 2]]]], [5, [5, 4, 5, 2]]], ['control: random schedule 2', [1, 1, [['h', [0]], ['rz', [0]], ['x', [0]], ['x', [0], 'if', 0], ['x', [0]], ['x', [0]], ['x', [0], 'if', 0], ['id', [0]]]], [7, [7]]]], [['regression: barrier aligns everything', [3, 1, [['h', [0]], ['h', [0]], ['barrier', []], ['x', [2]]]], [3, [2, 2, 3]]], ['regression: random schedule 37', [3, 2, [['rz', [2]], ['rz', [0]], ['h', [0]], ['h', [2]], ['rz', [2]], ['barrier', []], ['x', [0], 'if', 0]]], [4, [4, 3, 3]]], ['regression: random schedule 47', [4, 1, [['ccx', [1, 0, 3]], ['barrier', []], ['measure', [0], [0]], ['x', [3]], ['ccx', [1, 3, 0]], ['cx', [0, 1]], ['x', [2]], ['id', [0]]]], [4, [4, 4, 2, 3]]], ['control: random schedule 3', [1, 2, [['h', [0]], ['h', [0]]]], [2, [2]]], ['control: random schedule 4', [4, 1, [['measure', [3], [0]], ['ccx', [2, 1, 0]], ['ccx', [0, 2, 3]]]], [2, [2, 1, 2, 2]]], ['control: random schedule 5', [3, 2, [['ccx', [0, 1, 2]], ['ccx', [2, 1, 0]], ['barrier', [0, 1, 2]], ['x', [1]]]], [3, [2, 3, 2]]], ['control: random schedule 6', [4, 2, [['barrier', [2]], ['cx', [1, 2]], ['ccx', [2, 3, 0]]]], [2, [2, 1, 2, 2]]]], [['regression: barrier aligns everything', [3, 1, [['h', [0]], ['h', [0]], ['barrier', []], ['x', [2]]]], [3, [2, 2, 3]]], ['regression: random schedule 37', [3, 2, [['rz', [2]], ['rz', [0]], ['h', [0]], ['h', [2]], ['rz', [2]], ['barrier', []], ['x', [0], 'if', 0]]], [4, [4, 3, 3]]], ['regression: random schedule 47', [4, 1, [['ccx', [1, 0, 3]], ['barrier', []], ['measure', [0], [0]], ['x', [3]], ['ccx', [1, 3, 0]], ['cx', [0, 1]], ['x', [2]], ['id', [0]]]], [4, [4, 4, 2, 3]]], ['control: random schedule 7', [4, 2, [['x', [2], 'if', 1], ['x', [1]], ['barrier', [2]], ['cx', [0, 1]], ['h', [2]]]], [2, [2, 2, 2, 0]]], ['control: random schedule 8', [1, 1, [['x', [0], 'if', 0], ['rz', [0]], ['rz', [0]], ['h', [0]], ['rz', [0]], ['h', [0]], ['measure', [0], [0]], ['rz', [0]]]], [8, [8]]], ['control: random schedule 9', [3, 1, [['cx', [2, 1]], ['h', [1]], ['barrier', [1, 2]]]], [2, [0, 2, 2]]], ['control: random schedule 10', [4, 1, [['id', [2]]]], [0, [0, 0, 0, 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

N = 1
observations = []
def solve(x):
    nq, nc, ops = x
    ql = [0] * nq
    cl = [0] * nc
    for op in ops:
        name, qs = op[0], op[1]
        if name == 'barrier':
            qs = qs or [q for q in range(nq) if ql[q]]
            top = max(ql[q] for q in qs)
            for q in qs:
                ql[q] = top
            continue
        if name == 'id':
            continue
        cbits = []
        if name == 'measure':
            cbits = op[2]
        elif len(op) > 2 and op[2] == 'if':
            cbits = [op[3]]
        level = 1 + max([ql[q] for q in qs] + [cl[c] for c in cbits])
        for q in qs:
            ql[q] = level
        for c in cbits:
            cl[c] = level
    return [max(ql + cl + [0]), ql]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: barrier aligns everything', [3, 1, [['h', [0]], ['h', [0]], ['barrier', []], ['x', [2]]]], [3, [2, 2, 3]]], ['regression: random schedule 37', [3, 2, [['rz', [2]], ['rz', [0]], ['h', [0]], ['h', [2]], ['rz', [2]], ['barrier', []], ['x', [0], 'if', 0]]], [4, [4, 3, 3]]], ['regression: random schedule 47', [4, 1, [['ccx', [1, 0, 3]], ['barrier', []], ['measure', [0], [0]], ['x', [3]], ['ccx', [1, 3, 0]], ['cx', [0, 1]], ['x', [2]], ['id', [0]]]], [4, [4, 4, 2, 3]]], ['control: partial barrier', [3, 1, [['h', [0]], ['barrier', [0, 1]], ['x', [1]], ['x', [2]]]], [2, [1, 2, 1]]], ['control: toffoli waits on all', [3, 0, [['h', [2]], ['h', [2]], ['ccx', [0, 1, 2]], ['x', [0]]]], [4, [4, 3, 3]]], ['control: cx waits on target', [2, 0, [['h', [1]], ['h', [1]], ['cx', [0, 1]]]], [3, [3, 3]]], ['control: identity is free', [1, 0, [['id', [0]], ['id', [0]], ['x', [0]]]], [1, [1]]]], [['regression: barrier aligns everything', [3, 1, [['h', [0]], ['h', [0]], ['barrier', []], ['x', [2]]]], [3, [2, 2, 3]]], ['regression: random schedule 37', [3, 2, [['rz', [2]], ['rz', [0]], ['h', [0]], ['h', [2]], ['rz', [2]], ['barrier', []], ['x', [0], 'if', 0]]], [4, [4, 3, 3]]], ['regression: random schedule 47', [4, 1, [['ccx', [1, 0, 3]], ['barrier', []], ['measure', [0], [0]], ['x', [3]], ['ccx', [1, 3, 0]], ['cx', [0, 1]], ['x', [2]], ['id', [0]]]], [4, [4, 4, 2, 3]]], ['control: conditioned gate after measure', [2, 1, [['h', [0]], ['measure', [0], [0]], ['x', [1], 'if', 0]]], [3, [2, 3]]], ['control: measure after conditioned read', [2, 1, [['h', [0]], ['h', [0]], ['h', [0]], ['x', [1], 'if', 0], ['measure', [1], [0]]]], [3, [3, 2]]], ['control: empty circuit', [2, 1, []], [0, [0, 0]]], ['control: late read then early-qubit measure', [2, 1, [['h', [1]], ['h', [1]], ['h', [1]], ['x', [1], 'if', 0], ['measure', [0], [0]]]], [5, [5, 4]]]], [['regression: barrier aligns everything', [3, 1, [['h', [0]], ['h', [0]], ['barrier', []], ['x', [2]]]], [3, [2, 2, 3]]], ['regression: random schedule 37', [3, 2, [['rz', [2]], ['rz', [0]], ['h', [0]], ['h', [2]], ['rz', [2]], ['barrier', []], ['x', [0], 'if', 0]]], [4, [4, 3, 3]]], ['regression: random schedule 47', [4, 1, [['ccx', [1, 0, 3]], ['barrier', []], ['measure', [0], [0]], ['x', [3]], ['ccx', [1, 3, 0]], ['cx', [0, 1]], ['x', [2]], ['id', [0]]]], [4, [4, 4, 2, 3]]], ['control: read then measure other clbit writer', [3, 2, [['h', [2]], ['h', [2]], ['z', [2], 'if', 1], ['measure', [0], [1]], ['x', [1], 'if', 1]]], [5, [4, 5, 3]]], ['control: random schedule 0', [1, 2, [['x', [0]], ['rz', [0]], ['measure', [0], [0]], ['x', [0]], ['barrier', [0]], ['x', [0], 'if', 1], ['x', [0], 'if', 1], ['x', [0]], ['measure', [0], [0]]]], [8, [8]]], ['control: random schedule 1', [4, 2, [['ccx', [1, 0, 3]], ['ccx', [2, 1, 3]], ['barrier', [0, 1, 2, 3]], ['cx', [1, 0]], ['id', [1]], ['cx', [1, 2]], ['cx', [0, 2]]]], [5, [5, 4, 5, 2]]], ['control: random schedule 2', [1, 1, [['h', [0]], ['rz', [0]], ['x', [0]], ['x', [0], 'if', 0], ['x', [0]], ['x', [0]], ['x', [0], 'if', 0], ['id', [0]]]], [7, [7]]]], [['regression: barrier aligns everything', [3, 1, [['h', [0]], ['h', [0]], ['barrier', []], ['x', [2]]]], [3, [2, 2, 3]]], ['regression: random schedule 37', [3, 2, [['rz', [2]], ['rz', [0]], ['h', [0]], ['h', [2]], ['rz', [2]], ['barrier', []], ['x', [0], 'if', 0]]], [4, [4, 3, 3]]], ['regression: random schedule 47', [4, 1, [['ccx', [1, 0, 3]], ['barrier', []], ['measure', [0], [0]], ['x', [3]], ['ccx', [1, 3, 0]], ['cx', [0, 1]], ['x', [2]], ['id', [0]]]], [4, [4, 4, 2, 3]]], ['control: random schedule 3', [1, 2, [['h', [0]], ['h', [0]]]], [2, [2]]], ['control: random schedule 4', [4, 1, [['measure', [3], [0]], ['ccx', [2, 1, 0]], ['ccx', [0, 2, 3]]]], [2, [2, 1, 2, 2]]], ['control: random schedule 5', [3, 2, [['ccx', [0, 1, 2]], ['ccx', [2, 1, 0]], ['barrier', [0, 1, 2]], ['x', [1]]]], [3, [2, 3, 2]]], ['control: random schedule 6', [4, 2, [['barrier', [2]], ['cx', [1, 2]], ['ccx', [2, 3, 0]]]], [2, [2, 1, 2, 2]]]], [['regression: barrier aligns everything', [3, 1, [['h', [0]], ['h', [0]], ['barrier', []], ['x', [2]]]], [3, [2, 2, 3]]], ['regression: random schedule 37', [3, 2, [['rz', [2]], ['rz', [0]], ['h', [0]], ['h', [2]], ['rz', [2]], ['barrier', []], ['x', [0], 'if', 0]]], [4, [4, 3, 3]]], ['regression: random schedule 47', [4, 1, [['ccx', [1, 0, 3]], ['barrier', []], ['measure', [0], [0]], ['x', [3]], ['ccx', [1, 3, 0]], ['cx', [0, 1]], ['x', [2]], ['id', [0]]]], [4, [4, 4, 2, 3]]], ['control: random schedule 7', [4, 2, [['x', [2], 'if', 1], ['x', [1]], ['barrier', [2]], ['cx', [0, 1]], ['h', [2]]]], [2, [2, 2, 2, 0]]], ['control: random schedule 8', [1, 1, [['x', [0], 'if', 0], ['rz', [0]], ['rz', [0]], ['h', [0]], ['rz', [0]], ['h', [0]], ['measure', [0], [0]], ['rz', [0]]]], [8, [8]]], ['control: random schedule 9', [3, 1, [['cx', [2, 1]], ['h', [1]], ['barrier', [1, 2]]]], [2, [0, 2, 2]]], ['control: random schedule 10', [4, 1, [['id', [2]]]], [0, [0, 0, 0, 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:
A bare barrier after deep work on qubit 0 does not delay a following gate on qubit 2.

Input [nq, nc, ops]; ops are [name, qubits], ["measure", [q], [c]], ["g", [q], "if", c] (classically conditioned), ["barrier", qubits] (empty list = all qubits) and ["id", [q]]. ASAP layering: each op lands one layer after the latest of its qubits and any clbit it reads or writes, and sets those levels; barriers align the listed qubits to their maximum without adding a layer; id costs nothing. Return [depth, qubit levels].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    nq, nc, ops = x
    ql = [0] * nq
    cl = [0] * nc
    for op in ops:
        name, qs = op[0], op[1]
        if name == 'barrier':
            qs = qs or list(range(nq))
            top = max(ql[q] for q in qs)
            for q in qs:
                ql[q] = top
            continue
        if name == 'id':
            continue
        cbits = []
        if name == 'measure':
            cbits = op[2]
        elif len(op) > 2 and op[2] == 'if':
            cbits = [op[3]]
        level = 1 + max([ql[qs[0]]] + [cl[c] for c in cbits])
        for q in qs:
            ql[q] = level
        for c in cbits:
            cl[c] = level
    return [max(ql + cl + [0]), ql]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: toffoli waits on all', [3, 0, [['h', [2]], ['h', [2]], ['ccx', [0, 1, 2]], ['x', [0]]]], [4, [4, 3, 3]]], ['regression: cx waits on target', [2, 0, [['h', [1]], ['h', [1]], ['cx', [0, 1]]]], [3, [3, 3]]], ['regression: random schedule 51', [3, 2, [['x', [1], 'if', 1], ['x', [1], 'if', 1], ['measure', [1], [0]], ['x', [1]], ['measure', [2], [1]], ['ccx', [2, 1, 0]]]], [5, [5, 5, 5]]], ['control: barrier aligns everything', [3, 1, [['h', [0]], ['h', [0]], ['barrier', []], ['x', [2]]]], [3, [2, 2, 3]]], ['control: partial barrier', [3, 1, [['h', [0]], ['barrier', [0, 1]], ['x', [1]], ['x', [2]]]], [2, [1, 2, 1]]], ['control: identity is free', [1, 0, [['id', [0]], ['id', [0]], ['x', [0]]]], [1, [1]]], ['control: conditioned gate after measure', [2, 1, [['h', [0]], ['measure', [0], [0]], ['x', [1], 'if', 0]]], [3, [2, 3]]]], [['regression: random schedule 7', [4, 2, [['x', [2], 'if', 1], ['x', [1]], ['barrier', [2]], ['cx', [0, 1]], ['h', [2]]]], [2, [2, 2, 2, 0]]], ['regression: random schedule 14', [4, 1, [['cx', [0, 3]], ['x', [0]], ['cx', [0, 2]], ['cx', [2, 3]], ['x', [1], 'if', 0], ['cx', [3, 0]], ['cx', [1, 2]], ['id', [0]], ['barrier', [2]]]], [5, [5, 5, 5, 5]]], ['regression: random schedule 51', [3, 2, [['x', [1], 'if', 1], ['x', [1], 'if', 1], ['measure', [1], [0]], ['x', [1]], ['measure', [2], [1]], ['ccx', [2, 1, 0]]]], [5, [5, 5, 5]]], ['control: measure after conditioned read', [2, 1, [['h', [0]], ['h', [0]], ['h', [0]], ['x', [1], 'if', 0], ['measure', [1], [0]]]], [3, [3, 2]]], ['control: empty circuit', [2, 1, []], [0, [0, 0]]], ['control: late read then early-qubit measure', [2, 1, [['h', [1]], ['h', [1]], ['h', [1]], ['x', [1], 'if', 0], ['measure', [0], [0]]]], [5, [5, 4]]], ['control: read then measure other clbit writer', [3, 2, [['h', [2]], ['h', [2]], ['z', [2], 'if', 1], ['measure', [0], [1]], ['x', [1], 'if', 1]]], [5, [4, 5, 3]]]], [['regression: random schedule 17', [2, 1, [['barrier', [1]], ['rz', [1]], ['cx', [1, 0]], ['cx', [1, 0]], ['x', [1]], ['cx', [0, 1]], ['measure', [1], [0]], ['h', [0]], ['barrier', [0, 1]]]], [6, [6, 6]]], ['regression: random schedule 23', [4, 1, [['barrier', [0, 1, 2, 3]], ['x', [3], 'if', 0], ['rz', [2]], ['rz', [1]], ['cx', [0, 3]], ['x', [3], 'if', 0], ['rz', [2]], ['barrier', [0, 2, 3]]]], [3, [3, 1, 3, 3]]], ['regression: random schedule 51', [3, 2, [['x', [1], 'if', 1], ['x', [1], 'if', 1], ['measure', [1], [0]], ['x', [1]], ['measure', [2], [1]], ['ccx', [2, 1, 0]]]], [5, [5, 5, 5]]], ['control: random schedule 0', [1, 2, [['x', [0]], ['rz', [0]], ['measure', [0], [0]], ['x', [0]], ['barrier', [0]], ['x', [0], 'if', 1], ['x', [0], 'if', 1], ['x', [0]], ['measure', [0], [0]]]], [8, [8]]], ['control: random schedule 2', [1, 1, [['h', [0]], ['rz', [0]], ['x', [0]], ['x', [0], 'if', 0], ['x', [0]], ['x', [0]], ['x', [0], 'if', 0], ['id', [0]]]], [7, [7]]], ['control: random schedule 3', [1, 2, [['h', [0]], ['h', [0]]]], [2, [2]]], ['control: random schedule 4', [4, 1, [['measure', [3], [0]], ['ccx', [2, 1, 0]], ['ccx', [0, 2, 3]]]], [2, [2, 1, 2, 2]]]], [['regression: random schedule 31', [3, 2, [['ccx', [2, 1, 0]], ['measure', [0], [1]], ['x', [1]], ['cx', [2, 0]], ['ccx', [1, 0, 2]], ['cx', [1, 0]]]], [5, [5, 5, 4]]], ['regression: random schedule 32', [3, 2, [['cx', [1, 0]], ['cx', [1, 0]], ['rz', [0]], ['ccx', [2, 1, 0]], ['cx', [0, 2]], ['id', [0]], ['h', [0]], ['h', [0]], ['cx', [1, 2]]]], [7, [7, 6, 6]]], ['regression: random schedule 51', [3, 2, [['x', [1], 'if', 1], ['x', [1], 'if', 1], ['measure', [1], [0]], ['x', [1]], ['measure', [2], [1]], ['ccx', [2, 1, 0]]]], [5, [5, 5, 5]]], ['control: random schedule 5', [3, 2, [['ccx', [0, 1, 2]], ['ccx', [2, 1, 0]], ['barrier', [0, 1, 2]], ['x', [1]]]], [3, [2, 3, 2]]], ['control: random schedule 6', [4, 2, [['barrier', [2]], ['cx', [1, 2]], ['ccx', [2, 3, 0]]]], [2, [2, 1, 2, 2]]], ['control: random schedule 8', [1, 1, [['x', [0], 'if', 0], ['rz', [0]], ['rz', [0]], ['h', [0]], ['rz', [0]], ['h', [0]], ['measure', [0], [0]], ['rz', [0]]]], [8, [8]]], ['control: random schedule 9', [3, 1, [['cx', [2, 1]], ['h', [1]], ['barrier', [1, 2]]]], [2, [0, 2, 2]]]], [['regression: random schedule 39', [2, 1, [['barrier', [1]], ['barrier', []], ['cx', [0, 1]], ['rz', [1]], ['id', [0]], ['barrier', [0]], ['h', [1]], ['cx', [0, 1]]]], [4, [4, 4]]], ['regression: random schedule 41', [3, 2, [['barrier', [0, 1, 2]], ['h', [0]], ['x', [0], 'if', 0], ['cx', [1, 0]], ['x', [0]]]], [4, [4, 3, 0]]], ['regression: random schedule 51', [3, 2, [['x', [1], 'if', 1], ['x', [1], 'if', 1], ['measure', [1], [0]], ['x', [1]], ['measure', [2], [1]], ['ccx', [2, 1, 0]]]], [5, [5, 5, 5]]], ['control: random schedule 10', [4, 1, [['id', [2]]]], [0, [0, 0, 0, 0]]], ['control: random schedule 11', [2, 2, [['barrier', [0]], ['barrier', [1]], ['h', [1]], ['h', [0]], ['id', [0]], ['cx', [0, 1]], ['x', [1]], ['rz', [0]]]], [3, [3, 3]]], ['control: random schedule 12', [1, 2, [['rz', [0]], ['rz', [0]], ['barrier', []], ['rz', [0]], ['id', [0]], ['barrier', [0]], ['x', [0]], ['h', [0]]]], [5, [5]]], ['control: random schedule 13', [4, 2, [['cx', [3, 2]], ['x', [3]], ['ccx', [3, 1, 0]], ['ccx', [1, 0, 2]], ['h', [0]]]], [5, [5, 4, 4, 3]]]]]
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

N = 1
observations = []
def solve(x):
    nq, nc, ops = x
    ql = [0] * nq
    cl = [0] * nc
    for op in ops:
        name, qs = op[0], op[1]
        if name == 'barrier':
            qs = qs or list(range(nq))
            top = max(ql[q] for q in qs)
            for q in qs:
                ql[q] = top
            continue
        if name == 'id':
            continue
        cbits = []
        if name == 'measure':
            cbits = op[2]
        elif len(op) > 2 and op[2] == 'if':
            cbits = [op[3]]
        level = 1 + max([ql[qs[0]], ql[qs[-1]]] + [cl[c] for c in cbits])
        for q in qs:
            ql[q] = level
        for c in cbits:
            cl[c] = level
    return [max(ql + cl + [0]), ql]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: toffoli waits on all', [3, 0, [['h', [2]], ['h', [2]], ['ccx', [0, 1, 2]], ['x', [0]]]], [4, [4, 3, 3]]], ['regression: cx waits on target', [2, 0, [['h', [1]], ['h', [1]], ['cx', [0, 1]]]], [3, [3, 3]]], ['regression: random schedule 51', [3, 2, [['x', [1], 'if', 1], ['x', [1], 'if', 1], ['measure', [1], [0]], ['x', [1]], ['measure', [2], [1]], ['ccx', [2, 1, 0]]]], [5, [5, 5, 5]]], ['control: barrier aligns everything', [3, 1, [['h', [0]], ['h', [0]], ['barrier', []], ['x', [2]]]], [3, [2, 2, 3]]], ['control: partial barrier', [3, 1, [['h', [0]], ['barrier', [0, 1]], ['x', [1]], ['x', [2]]]], [2, [1, 2, 1]]], ['control: identity is free', [1, 0, [['id', [0]], ['id', [0]], ['x', [0]]]], [1, [1]]], ['control: conditioned gate after measure', [2, 1, [['h', [0]], ['measure', [0], [0]], ['x', [1], 'if', 0]]], [3, [2, 3]]]], [['regression: random schedule 7', [4, 2, [['x', [2], 'if', 1], ['x', [1]], ['barrier', [2]], ['cx', [0, 1]], ['h', [2]]]], [2, [2, 2, 2, 0]]], ['regression: random schedule 14', [4, 1, [['cx', [0, 3]], ['x', [0]], ['cx', [0, 2]], ['cx', [2, 3]], ['x', [1], 'if', 0], ['cx', [3, 0]], ['cx', [1, 2]], ['id', [0]], ['barrier', [2]]]], [5, [5, 5, 5, 5]]], ['regression: random schedule 51', [3, 2, [['x', [1], 'if', 1], ['x', [1], 'if', 1], ['measure', [1], [0]], ['x', [1]], ['measure', [2], [1]], ['ccx', [2, 1, 0]]]], [5, [5, 5, 5]]], ['control: measure after conditioned read', [2, 1, [['h', [0]], ['h', [0]], ['h', [0]], ['x', [1], 'if', 0], ['measure', [1], [0]]]], [3, [3, 2]]], ['control: empty circuit', [2, 1, []], [0, [0, 0]]], ['control: late read then early-qubit measure', [2, 1, [['h', [1]], ['h', [1]], ['h', [1]], ['x', [1], 'if', 0], ['measure', [0], [0]]]], [5, [5, 4]]], ['control: read then measure other clbit writer', [3, 2, [['h', [2]], ['h', [2]], ['z', [2], 'if', 1], ['measure', [0], [1]], ['x', [1], 'if', 1]]], [5, [4, 5, 3]]]], [['regression: random schedule 17', [2, 1, [['barrier', [1]], ['rz', [1]], ['cx', [1, 0]], ['cx', [1, 0]], ['x', [1]], ['cx', [0, 1]], ['measure', [1], [0]], ['h', [0]], ['barrier', [0, 1]]]], [6, [6, 6]]], ['regression: random schedule 23', [4, 1, [['barrier', [0, 1, 2, 3]], ['x', [3], 'if', 0], ['rz', [2]], ['rz', [1]], ['cx', [0, 3]], ['x', [3], 'if', 0], ['rz', [2]], ['barrier', [0, 2, 3]]]], [3, [3, 1, 3, 3]]], ['regression: random schedule 51', [3, 2, [['x', [1], 'if', 1], ['x', [1], 'if', 1], ['measure', [1], [0]], ['x', [1]], ['measure', [2], [1]], ['ccx', [2, 1, 0]]]], [5, [5, 5, 5]]], ['control: random schedule 0', [1, 2, [['x', [0]], ['rz', [0]], ['measure', [0], [0]], ['x', [0]], ['barrier', [0]], ['x', [0], 'if', 1], ['x', [0], 'if', 1], ['x', [0]], ['measure', [0], [0]]]], [8, [8]]], ['control: random schedule 2', [1, 1, [['h', [0]], ['rz', [0]], ['x', [0]], ['x', [0], 'if', 0], ['x', [0]], ['x', [0]], ['x', [0], 'if', 0], ['id', [0]]]], [7, [7]]], ['control: random schedule 3', [1, 2, [['h', [0]], ['h', [0]]]], [2, [2]]], ['control: random schedule 4', [4, 1, [['measure', [3], [0]], ['ccx', [2, 1, 0]], ['ccx', [0, 2, 3]]]], [2, [2, 1, 2, 2]]]], [['regression: random schedule 31', [3, 2, [['ccx', [2, 1, 0]], ['measure', [0], [1]], ['x', [1]], ['cx', [2, 0]], ['ccx', [1, 0, 2]], ['cx', [1, 0]]]], [5, [5, 5, 4]]], ['regression: random schedule 32', [3, 2, [['cx', [1, 0]], ['cx', [1, 0]], ['rz', [0]], ['ccx', [2, 1, 0]], ['cx', [0, 2]], ['id', [0]], ['h', [0]], ['h', [0]], ['cx', [1, 2]]]], [7, [7, 6, 6]]], ['regression: random schedule 51', [3, 2, [['x', [1], 'if', 1], ['x', [1], 'if', 1], ['measure', [1], [0]], ['x', [1]], ['measure', [2], [1]], ['ccx', [2, 1, 0]]]], [5, [5, 5, 5]]], ['control: random schedule 5', [3, 2, [['ccx', [0, 1, 2]], ['ccx', [2, 1, 0]], ['barrier', [0, 1, 2]], ['x', [1]]]], [3, [2, 3, 2]]], ['control: random schedule 6', [4, 2, [['barrier', [2]], ['cx', [1, 2]], ['ccx', [2, 3, 0]]]], [2, [2, 1, 2, 2]]], ['control: random schedule 8', [1, 1, [['x', [0], 'if', 0], ['rz', [0]], ['rz', [0]], ['h', [0]], ['rz', [0]], ['h', [0]], ['measure', [0], [0]], ['rz', [0]]]], [8, [8]]], ['control: random schedule 9', [3, 1, [['cx', [2, 1]], ['h', [1]], ['barrier', [1, 2]]]], [2, [0, 2, 2]]]], [['regression: random schedule 39', [2, 1, [['barrier', [1]], ['barrier', []], ['cx', [0, 1]], ['rz', [1]], ['id', [0]], ['barrier', [0]], ['h', [1]], ['cx', [0, 1]]]], [4, [4, 4]]], ['regression: random schedule 41', [3, 2, [['barrier', [0, 1, 2]], ['h', [0]], ['x', [0], 'if', 0], ['cx', [1, 0]], ['x', [0]]]], [4, [4, 3, 0]]], ['regression: random schedule 51', [3, 2, [['x', [1], 'if', 1], ['x', [1], 'if', 1], ['measure', [1], [0]], ['x', [1]], ['measure', [2], [1]], ['ccx', [2, 1, 0]]]], [5, [5, 5, 5]]], ['control: random schedule 10', [4, 1, [['id', [2]]]], [0, [0, 0, 0, 0]]], ['control: random schedule 11', [2, 2, [['barrier', [0]], ['barrier', [1]], ['h', [1]], ['h', [0]], ['id', [0]], ['cx', [0, 1]], ['x', [1]], ['rz', [0]]]], [3, [3, 3]]], ['control: random schedule 12', [1, 2, [['rz', [0]], ['rz', [0]], ['barrier', []], ['rz', [0]], ['id', [0]], ['barrier', [0]], ['x', [0]], ['h', [0]]]], [5, [5]]], ['control: random schedule 13', [4, 2, [['cx', [3, 2]], ['x', [3]], ['ccx', [3, 1, 0]], ['ccx', [1, 0, 2]], ['h', [0]]]], [5, [5, 4, 4, 3]]]]]
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 CNOT whose target is still busy is placed in the same layer as the target work.

Input [nq, nc, ops]; ops are [name, qubits], ["measure", [q], [c]], ["g", [q], "if", c] (classically conditioned), ["barrier", qubits] (empty list = all qubits) and ["id", [q]]. ASAP layering: each op lands one layer after the latest of its qubits and any clbit it reads or writes, and sets those levels; barriers align the listed qubits to their maximum without adding a layer; id costs nothing. Return [depth, qubit levels].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    nq, nc, ops = x
    ql = [0] * nq
    cl = [0] * nc
    for op in ops:
        name, qs = op[0], op[1]
        if name == 'barrier':
            qs = qs or list(range(nq))
            top = max(ql[q] for q in qs)
            for q in qs:
                ql[q] = top
            continue
        pass
        cbits = []
        if name == 'measure':
            cbits = op[2]
        elif len(op) > 2 and op[2] == 'if':
            cbits = [op[3]]
        level = 1 + max([ql[q] for q in qs] + [cl[c] for c in cbits])
        for q in qs:
            ql[q] = level
        for c in cbits:
            cl[c] = level
    return [max(ql + cl + [0]), ql]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: identity is free', [1, 0, [['id', [0]], ['id', [0]], ['x', [0]]]], [1, [1]]], ['regression: random schedule 1', [4, 2, [['ccx', [1, 0, 3]], ['ccx', [2, 1, 3]], ['barrier', [0, 1, 2, 3]], ['cx', [1, 0]], ['id', [1]], ['cx', [1, 2]], ['cx', [0, 2]]]], [5, [5, 4, 5, 2]]], ['regression: random schedule 2', [1, 1, [['h', [0]], ['rz', [0]], ['x', [0]], ['x', [0], 'if', 0], ['x', [0]], ['x', [0]], ['x', [0], 'if', 0], ['id', [0]]]], [7, [7]]], ['control: barrier aligns everything', [3, 1, [['h', [0]], ['h', [0]], ['barrier', []], ['x', [2]]]], [3, [2, 2, 3]]], ['control: partial barrier', [3, 1, [['h', [0]], ['barrier', [0, 1]], ['x', [1]], ['x', [2]]]], [2, [1, 2, 1]]], ['control: toffoli waits on all', [3, 0, [['h', [2]], ['h', [2]], ['ccx', [0, 1, 2]], ['x', [0]]]], [4, [4, 3, 3]]], ['control: cx waits on target', [2, 0, [['h', [1]], ['h', [1]], ['cx', [0, 1]]]], [3, [3, 3]]]], [['regression: random schedule 10', [4, 1, [['id', [2]]]], [0, [0, 0, 0, 0]]], ['regression: random schedule 11', [2, 2, [['barrier', [0]], ['barrier', [1]], ['h', [1]], ['h', [0]], ['id', [0]], ['cx', [0, 1]], ['x', [1]], ['rz', [0]]]], [3, [3, 3]]], ['regression: random schedule 2', [1, 1, [['h', [0]], ['rz', [0]], ['x', [0]], ['x', [0], 'if', 0], ['x', [0]], ['x', [0]], ['x', [0], 'if', 0], ['id', [0]]]], [7, [7]]], ['control: conditioned gate after measure', [2, 1, [['h', [0]], ['measure', [0], [0]], ['x', [1], 'if', 0]]], [3, [2, 3]]], ['control: measure after conditioned read', [2, 1, [['h', [0]], ['h', [0]], ['h', [0]], ['x', [1], 'if', 0], ['measure', [1], [0]]]], [3, [3, 2]]], ['control: empty circuit', [2, 1, []], [0, [0, 0]]], ['control: late read then early-qubit measure', [2, 1, [['h', [1]], ['h', [1]], ['h', [1]], ['x', [1], 'if', 0], ['measure', [0], [0]]]], [5, [5, 4]]]], [['regression: random schedule 14', [4, 1, [['cx', [0, 3]], ['x', [0]], ['cx', [0, 2]], ['cx', [2, 3]], ['x', [1], 'if', 0], ['cx', [3, 0]], ['cx', [1, 2]], ['id', [0]], ['barrier', [2]]]], [5, [5, 5, 5, 5]]], ['regression: random schedule 28', [2, 1, [['x', [1]], ['id', [0]], ['x', [0]], ['cx', [1, 0]], ['id', [1]], ['h', [1]]]], [3, [2, 3]]], ['regression: random schedule 11', [2, 2, [['barrier', [0]], ['barrier', [1]], ['h', [1]], ['h', [0]], ['id', [0]], ['cx', [0, 1]], ['x', [1]], ['rz', [0]]]], [3, [3, 3]]], ['control: read then measure other clbit writer', [3, 2, [['h', [2]], ['h', [2]], ['z', [2], 'if', 1], ['measure', [0], [1]], ['x', [1], 'if', 1]]], [5, [4, 5, 3]]], ['control: random schedule 0', [1, 2, [['x', [0]], ['rz', [0]], ['measure', [0], [0]], ['x', [0]], ['barrier', [0]], ['x', [0], 'if', 1], ['x', [0], 'if', 1], ['x', [0]], ['measure', [0], [0]]]], [8, [8]]], ['control: random schedule 3', [1, 2, [['h', [0]], ['h', [0]]]], [2, [2]]], ['control: random schedule 4', [4, 1, [['measure', [3], [0]], ['ccx', [2, 1, 0]], ['ccx', [0, 2, 3]]]], [2, [2, 1, 2, 2]]]], [['regression: random schedule 32', [3, 2, [['cx', [1, 0]], ['cx', [1, 0]], ['rz', [0]], ['ccx', [2, 1, 0]], ['cx', [0, 2]], ['id', [0]], ['h', [0]], ['h', [0]], ['cx', [1, 2]]]], [7, [7, 6, 6]]], ['regression: random schedule 33', [3, 2, [['rz', [2]], ['cx', [0, 1]], ['x', [0], 'if', 0], ['barrier', [0, 1, 2]], ['x', [0], 'if', 0], ['x', [0]], ['rz', [0]], ['rz', [0]], ['id', [0]]]], [6, [6, 2, 2]]], ['regression: random schedule 14', [4, 1, [['cx', [0, 3]], ['x', [0]], ['cx', [0, 2]], ['cx', [2, 3]], ['x', [1], 'if', 0], ['cx', [3, 0]], ['cx', [1, 2]], ['id', [0]], ['barrier', [2]]]], [5, [5, 5, 5, 5]]], ['control: random schedule 5', [3, 2, [['ccx', [0, 1, 2]], ['ccx', [2, 1, 0]], ['barrier', [0, 1, 2]], ['x', [1]]]], [3, [2, 3, 2]]], ['control: random schedule 6', [4, 2, [['barrier', [2]], ['cx', [1, 2]], ['ccx', [2, 3, 0]]]], [2, [2, 1, 2, 2]]], ['control: random schedule 7', [4, 2, [['x', [2], 'if', 1], ['x', [1]], ['barrier', [2]], ['cx', [0, 1]], ['h', [2]]]], [2, [2, 2, 2, 0]]], ['control: random schedule 8', [1, 1, [['x', [0], 'if', 0], ['rz', [0]], ['rz', [0]], ['h', [0]], ['rz', [0]], ['h', [0]], ['measure', [0], [0]], ['rz', [0]]]], [8, [8]]]], [['regression: random schedule 44', [4, 2, [['id', [0]], ['cx', [3, 1]], ['x', [0]], ['ccx', [3, 0, 1]], ['cx', [2, 1]], ['cx', [2, 0]]]], [4, [4, 3, 4, 2]]], ['regression: random schedule 47', [4, 1, [['ccx', [1, 0, 3]], ['barrier', []], ['measure', [0], [0]], ['x', [3]], ['ccx', [1, 3, 0]], ['cx', [0, 1]], ['x', [2]], ['id', [0]]]], [4, [4, 4, 2, 3]]], ['regression: random schedule 29', [4, 1, [['x', [0]], ['h', [0]], ['id', [1]]]], [2, [2, 0, 0, 0]]], ['control: random schedule 9', [3, 1, [['cx', [2, 1]], ['h', [1]], ['barrier', [1, 2]]]], [2, [0, 2, 2]]], ['control: random schedule 13', [4, 2, [['cx', [3, 2]], ['x', [3]], ['ccx', [3, 1, 0]], ['ccx', [1, 0, 2]], ['h', [0]]]], [5, [5, 4, 4, 3]]], ['control: random schedule 15', [1, 2, [['barrier', [0]], ['rz', [0]], ['rz', [0]], ['x', [0], 'if', 0], ['rz', [0]], ['x', [0], 'if', 0], ['x', [0]], ['measure', [0], [1]]]], [7, [7]]], ['control: random schedule 16', [3, 1, [['x', [2], 'if', 0], ['barrier', [0, 1, 2]], ['cx', [1, 2]], ['cx', [0, 1]], ['h', [0]], ['measure', [1], [0]], ['measure', [2], [0]]]], [5, [4, 4, 5]]]]]
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

N = 1
observations = []
def solve(x):
    nq, nc, ops = x
    ql = [0] * nq
    cl = [0] * nc
    for op in ops:
        name, qs = op[0], op[1]
        if name == 'barrier':
            qs = qs or list(range(nq))
            top = max(ql[q] for q in qs)
            for q in qs:
                ql[q] = top
            continue
        if name == 'I':
            continue
        cbits = []
        if name == 'measure':
            cbits = op[2]
        elif len(op) > 2 and op[2] == 'if':
            cbits = [op[3]]
        level = 1 + max([ql[q] for q in qs] + [cl[c] for c in cbits])
        for q in qs:
            ql[q] = level
        for c in cbits:
            cl[c] = level
    return [max(ql + cl + [0]), ql]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: identity is free', [1, 0, [['id', [0]], ['id', [0]], ['x', [0]]]], [1, [1]]], ['regression: random schedule 1', [4, 2, [['ccx', [1, 0, 3]], ['ccx', [2, 1, 3]], ['barrier', [0, 1, 2, 3]], ['cx', [1, 0]], ['id', [1]], ['cx', [1, 2]], ['cx', [0, 2]]]], [5, [5, 4, 5, 2]]], ['regression: random schedule 2', [1, 1, [['h', [0]], ['rz', [0]], ['x', [0]], ['x', [0], 'if', 0], ['x', [0]], ['x', [0]], ['x', [0], 'if', 0], ['id', [0]]]], [7, [7]]], ['control: barrier aligns everything', [3, 1, [['h', [0]], ['h', [0]], ['barrier', []], ['x', [2]]]], [3, [2, 2, 3]]], ['control: partial barrier', [3, 1, [['h', [0]], ['barrier', [0, 1]], ['x', [1]], ['x', [2]]]], [2, [1, 2, 1]]], ['control: toffoli waits on all', [3, 0, [['h', [2]], ['h', [2]], ['ccx', [0, 1, 2]], ['x', [0]]]], [4, [4, 3, 3]]], ['control: cx waits on target', [2, 0, [['h', [1]], ['h', [1]], ['cx', [0, 1]]]], [3, [3, 3]]]], [['regression: random schedule 10', [4, 1, [['id', [2]]]], [0, [0, 0, 0, 0]]], ['regression: random schedule 11', [2, 2, [['barrier', [0]], ['barrier', [1]], ['h', [1]], ['h', [0]], ['id', [0]], ['cx', [0, 1]], ['x', [1]], ['rz', [0]]]], [3, [3, 3]]], ['regression: random schedule 2', [1, 1, [['h', [0]], ['rz', [0]], ['x', [0]], ['x', [0], 'if', 0], ['x', [0]], ['x', [0]], ['x', [0], 'if', 0], ['id', [0]]]], [7, [7]]], ['control: conditioned gate after measure', [2, 1, [['h', [0]], ['measure', [0], [0]], ['x', [1], 'if', 0]]], [3, [2, 3]]], ['control: measure after conditioned read', [2, 1, [['h', [0]], ['h', [0]], ['h', [0]], ['x', [1], 'if', 0], ['measure', [1], [0]]]], [3, [3, 2]]], ['control: empty circuit', [2, 1, []], [0, [0, 0]]], ['control: late read then early-qubit measure', [2, 1, [['h', [1]], ['h', [1]], ['h', [1]], ['x', [1], 'if', 0], ['measure', [0], [0]]]], [5, [5, 4]]]], [['regression: random schedule 14', [4, 1, [['cx', [0, 3]], ['x', [0]], ['cx', [0, 2]], ['cx', [2, 3]], ['x', [1], 'if', 0], ['cx', [3, 0]], ['cx', [1, 2]], ['id', [0]], ['barrier', [2]]]], [5, [5, 5, 5, 5]]], ['regression: random schedule 28', [2, 1, [['x', [1]], ['id', [0]], ['x', [0]], ['cx', [1, 0]], ['id', [1]], ['h', [1]]]], [3, [2, 3]]], ['regression: random schedule 11', [2, 2, [['barrier', [0]], ['barrier', [1]], ['h', [1]], ['h', [0]], ['id', [0]], ['cx', [0, 1]], ['x', [1]], ['rz', [0]]]], [3, [3, 3]]], ['control: read then measure other clbit writer', [3, 2, [['h', [2]], ['h', [2]], ['z', [2], 'if', 1], ['measure', [0], [1]], ['x', [1], 'if', 1]]], [5, [4, 5, 3]]], ['control: random schedule 0', [1, 2, [['x', [0]], ['rz', [0]], ['measure', [0], [0]], ['x', [0]], ['barrier', [0]], ['x', [0], 'if', 1], ['x', [0], 'if', 1], ['x', [0]], ['measure', [0], [0]]]], [8, [8]]], ['control: random schedule 3', [1, 2, [['h', [0]], ['h', [0]]]], [2, [2]]], ['control: random schedule 4', [4, 1, [['measure', [3], [0]], ['ccx', [2, 1, 0]], ['ccx', [0, 2, 3]]]], [2, [2, 1, 2, 2]]]], [['regression: random schedule 32', [3, 2, [['cx', [1, 0]], ['cx', [1, 0]], ['rz', [0]], ['ccx', [2, 1, 0]], ['cx', [0, 2]], ['id', [0]], ['h', [0]], ['h', [0]], ['cx', [1, 2]]]], [7, [7, 6, 6]]], ['regression: random schedule 33', [3, 2, [['rz', [2]], ['cx', [0, 1]], ['x', [0], 'if', 0], ['barrier', [0, 1, 2]], ['x', [0], 'if', 0], ['x', [0]], ['rz', [0]], ['rz', [0]], ['id', [0]]]], [6, [6, 2, 2]]], ['regression: random schedule 14', [4, 1, [['cx', [0, 3]], ['x', [0]], ['cx', [0, 2]], ['cx', [2, 3]], ['x', [1], 'if', 0], ['cx', [3, 0]], ['cx', [1, 2]], ['id', [0]], ['barrier', [2]]]], [5, [5, 5, 5, 5]]], ['control: random schedule 5', [3, 2, [['ccx', [0, 1, 2]], ['ccx', [2, 1, 0]], ['barrier', [0, 1, 2]], ['x', [1]]]], [3, [2, 3, 2]]], ['control: random schedule 6', [4, 2, [['barrier', [2]], ['cx', [1, 2]], ['ccx', [2, 3, 0]]]], [2, [2, 1, 2, 2]]], ['control: random schedule 7', [4, 2, [['x', [2], 'if', 1], ['x', [1]], ['barrier', [2]], ['cx', [0, 1]], ['h', [2]]]], [2, [2, 2, 2, 0]]], ['control: random schedule 8', [1, 1, [['x', [0], 'if', 0], ['rz', [0]], ['rz', [0]], ['h', [0]], ['rz', [0]], ['h', [0]], ['measure', [0], [0]], ['rz', [0]]]], [8, [8]]]], [['regression: random schedule 44', [4, 2, [['id', [0]], ['cx', [3, 1]], ['x', [0]], ['ccx', [3, 0, 1]], ['cx', [2, 1]], ['cx', [2, 0]]]], [4, [4, 3, 4, 2]]], ['regression: random schedule 47', [4, 1, [['ccx', [1, 0, 3]], ['barrier', []], ['measure', [0], [0]], ['x', [3]], ['ccx', [1, 3, 0]], ['cx', [0, 1]], ['x', [2]], ['id', [0]]]], [4, [4, 4, 2, 3]]], ['regression: random schedule 29', [4, 1, [['x', [0]], ['h', [0]], ['id', [1]]]], [2, [2, 0, 0, 0]]], ['control: random schedule 9', [3, 1, [['cx', [2, 1]], ['h', [1]], ['barrier', [1, 2]]]], [2, [0, 2, 2]]], ['control: random schedule 13', [4, 2, [['cx', [3, 2]], ['x', [3]], ['ccx', [3, 1, 0]], ['ccx', [1, 0, 2]], ['h', [0]]]], [5, [5, 4, 4, 3]]], ['control: random schedule 15', [1, 2, [['barrier', [0]], ['rz', [0]], ['rz', [0]], ['x', [0], 'if', 0], ['rz', [0]], ['x', [0], 'if', 0], ['x', [0]], ['measure', [0], [1]]]], [7, [7]]], ['control: random schedule 16', [3, 1, [['x', [2], 'if', 0], ['barrier', [0, 1, 2]], ['cx', [1, 2]], ['cx', [0, 1]], ['h', [0]], ['measure', [1], [0]], ['measure', [2], [0]]]], [5, [4, 4, 5]]]]]
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:
Two id gates on a qubit add two layers of depth.

Input [nq, nc, ops]; ops are [name, qubits], ["measure", [q], [c]], ["g", [q], "if", c] (classically conditioned), ["barrier", qubits] (empty list = all qubits) and ["id", [q]]. ASAP layering: each op lands one layer after the latest of its qubits and any clbit it reads or writes, and sets those levels; barriers align the listed qubits to their maximum without adding a layer; id costs nothing. Return [depth, qubit levels].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    nq, nc, ops = x
    ql = [0] * nq
    cl = [0] * nc
    for op in ops:
        name, qs = op[0], op[1]
        if name == 'barrier':
            qs = qs or list(range(nq))
            top = max(ql[q] for q in qs)
            for q in qs:
                ql[q] = top
            continue
        if name == 'id':
            continue
        cbits = []
        if name == 'measure':
            cbits = op[2]
        elif len(op) > 2 and op[2] == 'if':
            cbits = []
        level = 1 + max([ql[q] for q in qs] + [cl[c] for c in cbits])
        for q in qs:
            ql[q] = level
        for c in cbits:
            cl[c] = level
    return [max(ql + cl + [0]), ql]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: conditioned gate after measure', [2, 1, [['h', [0]], ['measure', [0], [0]], ['x', [1], 'if', 0]]], [3, [2, 3]]], ['regression: late read then early-qubit measure', [2, 1, [['h', [1]], ['h', [1]], ['h', [1]], ['x', [1], 'if', 0], ['measure', [0], [0]]]], [5, [5, 4]]], ['regression: read then measure other clbit writer', [3, 2, [['h', [2]], ['h', [2]], ['z', [2], 'if', 1], ['measure', [0], [1]], ['x', [1], 'if', 1]]], [5, [4, 5, 3]]], ['control: barrier aligns everything', [3, 1, [['h', [0]], ['h', [0]], ['barrier', []], ['x', [2]]]], [3, [2, 2, 3]]], ['control: partial barrier', [3, 1, [['h', [0]], ['barrier', [0, 1]], ['x', [1]], ['x', [2]]]], [2, [1, 2, 1]]], ['control: toffoli waits on all', [3, 0, [['h', [2]], ['h', [2]], ['ccx', [0, 1, 2]], ['x', [0]]]], [4, [4, 3, 3]]], ['control: cx waits on target', [2, 0, [['h', [1]], ['h', [1]], ['cx', [0, 1]]]], [3, [3, 3]]]], [['regression: random schedule 26', [2, 2, [['measure', [0], [0]], ['x', [1], 'if', 0], ['measure', [0], [0]], ['cx', [0, 1]], ['h', [1]], ['rz', [1]]]], [6, [4, 6]]], ['regression: conditioned gate after measure', [2, 1, [['h', [0]], ['measure', [0], [0]], ['x', [1], 'if', 0]]], [3, [2, 3]]], ['regression: late read then early-qubit measure', [2, 1, [['h', [1]], ['h', [1]], ['h', [1]], ['x', [1], 'if', 0], ['measure', [0], [0]]]], [5, [5, 4]]], ['control: identity is free', [1, 0, [['id', [0]], ['id', [0]], ['x', [0]]]], [1, [1]]], ['control: measure after conditioned read', [2, 1, [['h', [0]], ['h', [0]], ['h', [0]], ['x', [1], 'if', 0], ['measure', [1], [0]]]], [3, [3, 2]]], ['control: empty circuit', [2, 1, []], [0, [0, 0]]], ['control: random schedule 0', [1, 2, [['x', [0]], ['rz', [0]], ['measure', [0], [0]], ['x', [0]], ['barrier', [0]], ['x', [0], 'if', 1], ['x', [0], 'if', 1], ['x', [0]], ['measure', [0], [0]]]], [8, [8]]]], [['regression: read then measure other clbit writer', [3, 2, [['h', [2]], ['h', [2]], ['z', [2], 'if', 1], ['measure', [0], [1]], ['x', [1], 'if', 1]]], [5, [4, 5, 3]]], ['regression: random schedule 26', [2, 2, [['measure', [0], [0]], ['x', [1], 'if', 0], ['measure', [0], [0]], ['cx', [0, 1]], ['h', [1]], ['rz', [1]]]], [6, [4, 6]]], ['regression: late read then early-qubit measure', [2, 1, [['h', [1]], ['h', [1]], ['h', [1]], ['x', [1], 'if', 0], ['measure', [0], [0]]]], [5, [5, 4]]], ['control: random schedule 1', [4, 2, [['ccx', [1, 0, 3]], ['ccx', [2, 1, 3]], ['barrier', [0, 1, 2, 3]], ['cx', [1, 0]], ['id', [1]], ['cx', [1, 2]], ['cx', [0, 2]]]], [5, [5, 4, 5, 2]]], ['control: random schedule 2', [1, 1, [['h', [0]], ['rz', [0]], ['x', [0]], ['x', [0], 'if', 0], ['x', [0]], ['x', [0]], ['x', [0], 'if', 0], ['id', [0]]]], [7, [7]]], ['control: random schedule 3', [1, 2, [['h', [0]], ['h', [0]]]], [2, [2]]], ['control: random schedule 4', [4, 1, [['measure', [3], [0]], ['ccx', [2, 1, 0]], ['ccx', [0, 2, 3]]]], [2, [2, 1, 2, 2]]]], [['regression: late read then early-qubit measure', [2, 1, [['h', [1]], ['h', [1]], ['h', [1]], ['x', [1], 'if', 0], ['measure', [0], [0]]]], [5, [5, 4]]], ['regression: read then measure other clbit writer', [3, 2, [['h', [2]], ['h', [2]], ['z', [2], 'if', 1], ['measure', [0], [1]], ['x', [1], 'if', 1]]], [5, [4, 5, 3]]], ['control: random schedule 5', [3, 2, [['ccx', [0, 1, 2]], ['ccx', [2, 1, 0]], ['barrier', [0, 1, 2]], ['x', [1]]]], [3, [2, 3, 2]]], ['control: random schedule 6', [4, 2, [['barrier', [2]], ['cx', [1, 2]], ['ccx', [2, 3, 0]]]], [2, [2, 1, 2, 2]]], ['control: random schedule 7', [4, 2, [['x', [2], 'if', 1], ['x', [1]], ['barrier', [2]], ['cx', [0, 1]], ['h', [2]]]], [2, [2, 2, 2, 0]]], ['control: random schedule 8', [1, 1, [['x', [0], 'if', 0], ['rz', [0]], ['rz', [0]], ['h', [0]], ['rz', [0]], ['h', [0]], ['measure', [0], [0]], ['rz', [0]]]], [8, [8]]], ['regression: conditioned gate after measure', [2, 1, [['h', [0]], ['measure', [0], [0]], ['x', [1], 'if', 0]]], [3, [2, 3]]]], [['regression: conditioned gate after measure', [2, 1, [['h', [0]], ['measure', [0], [0]], ['x', [1], 'if', 0]]], [3, [2, 3]]], ['regression: late read then early-qubit measure', [2, 1, [['h', [1]], ['h', [1]], ['h', [1]], ['x', [1], 'if', 0], ['measure', [0], [0]]]], [5, [5, 4]]], ['regression: read then measure other clbit writer', [3, 2, [['h', [2]], ['h', [2]], ['z', [2], 'if', 1], ['measure', [0], [1]], ['x', [1], 'if', 1]]], [5, [4, 5, 3]]], ['control: random schedule 9', [3, 1, [['cx', [2, 1]], ['h', [1]], ['barrier', [1, 2]]]], [2, [0, 2, 2]]], ['control: random schedule 10', [4, 1, [['id', [2]]]], [0, [0, 0, 0, 0]]], ['control: random schedule 11', [2, 2, [['barrier', [0]], ['barrier', [1]], ['h', [1]], ['h', [0]], ['id', [0]], ['cx', [0, 1]], ['x', [1]], ['rz', [0]]]], [3, [3, 3]]], ['control: random schedule 12', [1, 2, [['rz', [0]], ['rz', [0]], ['barrier', []], ['rz', [0]], ['id', [0]], ['barrier', [0]], ['x', [0]], ['h', [0]]]], [5, [5]]]]]
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

N = 1
observations = []
def solve(x):
    nq, nc, ops = x
    ql = [0] * nq
    cl = [0] * nc
    for op in ops:
        name, qs = op[0], op[1]
        if name == 'barrier':
            qs = qs or list(range(nq))
            top = max(ql[q] for q in qs)
            for q in qs:
                ql[q] = top
            continue
        if name == 'id':
            continue
        cbits = []
        if name == 'measure':
            cbits = op[2]
        elif len(op) > 2 and op[2] == 'if':
            cbits = [op[3]] if cl[op[3]] else []
        level = 1 + max([ql[q] for q in qs] + [cl[c] for c in cbits])
        for q in qs:
            ql[q] = level
        for c in cbits:
            cl[c] = level
    return [max(ql + cl + [0]), ql]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: conditioned gate after measure', [2, 1, [['h', [0]], ['measure', [0], [0]], ['x', [1], 'if', 0]]], [3, [2, 3]]], ['regression: late read then early-qubit measure', [2, 1, [['h', [1]], ['h', [1]], ['h', [1]], ['x', [1], 'if', 0], ['measure', [0], [0]]]], [5, [5, 4]]], ['regression: read then measure other clbit writer', [3, 2, [['h', [2]], ['h', [2]], ['z', [2], 'if', 1], ['measure', [0], [1]], ['x', [1], 'if', 1]]], [5, [4, 5, 3]]], ['control: barrier aligns everything', [3, 1, [['h', [0]], ['h', [0]], ['barrier', []], ['x', [2]]]], [3, [2, 2, 3]]], ['control: partial barrier', [3, 1, [['h', [0]], ['barrier', [0, 1]], ['x', [1]], ['x', [2]]]], [2, [1, 2, 1]]], ['control: toffoli waits on all', [3, 0, [['h', [2]], ['h', [2]], ['ccx', [0, 1, 2]], ['x', [0]]]], [4, [4, 3, 3]]], ['control: cx waits on target', [2, 0, [['h', [1]], ['h', [1]], ['cx', [0, 1]]]], [3, [3, 3]]]], [['regression: random schedule 26', [2, 2, [['measure', [0], [0]], ['x', [1], 'if', 0], ['measure', [0], [0]], ['cx', [0, 1]], ['h', [1]], ['rz', [1]]]], [6, [4, 6]]], ['regression: conditioned gate after measure', [2, 1, [['h', [0]], ['measure', [0], [0]], ['x', [1], 'if', 0]]], [3, [2, 3]]], ['regression: late read then early-qubit measure', [2, 1, [['h', [1]], ['h', [1]], ['h', [1]], ['x', [1], 'if', 0], ['measure', [0], [0]]]], [5, [5, 4]]], ['control: identity is free', [1, 0, [['id', [0]], ['id', [0]], ['x', [0]]]], [1, [1]]], ['control: measure after conditioned read', [2, 1, [['h', [0]], ['h', [0]], ['h', [0]], ['x', [1], 'if', 0], ['measure', [1], [0]]]], [3, [3, 2]]], ['control: empty circuit', [2, 1, []], [0, [0, 0]]], ['control: random schedule 0', [1, 2, [['x', [0]], ['rz', [0]], ['measure', [0], [0]], ['x', [0]], ['barrier', [0]], ['x', [0], 'if', 1], ['x', [0], 'if', 1], ['x', [0]], ['measure', [0], [0]]]], [8, [8]]]], [['regression: read then measure other clbit writer', [3, 2, [['h', [2]], ['h', [2]], ['z', [2], 'if', 1], ['measure', [0], [1]], ['x', [1], 'if', 1]]], [5, [4, 5, 3]]], ['regression: random schedule 26', [2, 2, [['measure', [0], [0]], ['x', [1], 'if', 0], ['measure', [0], [0]], ['cx', [0, 1]], ['h', [1]], ['rz', [1]]]], [6, [4, 6]]], ['regression: late read then early-qubit measure', [2, 1, [['h', [1]], ['h', [1]], ['h', [1]], ['x', [1], 'if', 0], ['measure', [0], [0]]]], [5, [5, 4]]], ['control: random schedule 1', [4, 2, [['ccx', [1, 0, 3]], ['ccx', [2, 1, 3]], ['barrier', [0, 1, 2, 3]], ['cx', [1, 0]], ['id', [1]], ['cx', [1, 2]], ['cx', [0, 2]]]], [5, [5, 4, 5, 2]]], ['control: random schedule 2', [1, 1, [['h', [0]], ['rz', [0]], ['x', [0]], ['x', [0], 'if', 0], ['x', [0]], ['x', [0]], ['x', [0], 'if', 0], ['id', [0]]]], [7, [7]]], ['control: random schedule 3', [1, 2, [['h', [0]], ['h', [0]]]], [2, [2]]], ['control: random schedule 4', [4, 1, [['measure', [3], [0]], ['ccx', [2, 1, 0]], ['ccx', [0, 2, 3]]]], [2, [2, 1, 2, 2]]]], [['regression: late read then early-qubit measure', [2, 1, [['h', [1]], ['h', [1]], ['h', [1]], ['x', [1], 'if', 0], ['measure', [0], [0]]]], [5, [5, 4]]], ['regression: read then measure other clbit writer', [3, 2, [['h', [2]], ['h', [2]], ['z', [2], 'if', 1], ['measure', [0], [1]], ['x', [1], 'if', 1]]], [5, [4, 5, 3]]], ['control: random schedule 5', [3, 2, [['ccx', [0, 1, 2]], ['ccx', [2, 1, 0]], ['barrier', [0, 1, 2]], ['x', [1]]]], [3, [2, 3, 2]]], ['control: random schedule 6', [4, 2, [['barrier', [2]], ['cx', [1, 2]], ['ccx', [2, 3, 0]]]], [2, [2, 1, 2, 2]]], ['control: random schedule 7', [4, 2, [['x', [2], 'if', 1], ['x', [1]], ['barrier', [2]], ['cx', [0, 1]], ['h', [2]]]], [2, [2, 2, 2, 0]]], ['control: random schedule 8', [1, 1, [['x', [0], 'if', 0], ['rz', [0]], ['rz', [0]], ['h', [0]], ['rz', [0]], ['h', [0]], ['measure', [0], [0]], ['rz', [0]]]], [8, [8]]], ['regression: conditioned gate after measure', [2, 1, [['h', [0]], ['measure', [0], [0]], ['x', [1], 'if', 0]]], [3, [2, 3]]]], [['regression: conditioned gate after measure', [2, 1, [['h', [0]], ['measure', [0], [0]], ['x', [1], 'if', 0]]], [3, [2, 3]]], ['regression: late read then early-qubit measure', [2, 1, [['h', [1]], ['h', [1]], ['h', [1]], ['x', [1], 'if', 0], ['measure', [0], [0]]]], [5, [5, 4]]], ['regression: read then measure other clbit writer', [3, 2, [['h', [2]], ['h', [2]], ['z', [2], 'if', 1], ['measure', [0], [1]], ['x', [1], 'if', 1]]], [5, [4, 5, 3]]], ['control: random schedule 9', [3, 1, [['cx', [2, 1]], ['h', [1]], ['barrier', [1, 2]]]], [2, [0, 2, 2]]], ['control: random schedule 10', [4, 1, [['id', [2]]]], [0, [0, 0, 0, 0]]], ['control: random schedule 11', [2, 2, [['barrier', [0]], ['barrier', [1]], ['h', [1]], ['h', [0]], ['id', [0]], ['cx', [0, 1]], ['x', [1]], ['rz', [0]]]], [3, [3, 3]]], ['control: random schedule 12', [1, 2, [['rz', [0]], ['rz', [0]], ['barrier', []], ['rz', [0]], ['id', [0]], ['barrier', [0]], ['x', [0]], ['h', [0]]]], [5, [5]]]]]
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 gate conditioned on c0 is scheduled in parallel with the measurement that writes c0.

Input [nq, nc, ops]; ops are [name, qubits], ["measure", [q], [c]], ["g", [q], "if", c] (classically conditioned), ["barrier", qubits] (empty list = all qubits) and ["id", [q]]. ASAP layering: each op lands one layer after the latest of its qubits and any clbit it reads or writes, and sets those levels; barriers align the listed qubits to their maximum without adding a layer; id costs nothing. Return [depth, qubit levels].

split: open-access