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)] = (nc, size)
                    nc += 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: index equals size', ['qreg q[2];', 'h q[2];'], ['index-out-of-range', 1]], ['regression: 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]], ['repair check: second register overflow', ['qreg q[2];', 'qreg r[2];', 'x r[3];'], ['index-out-of-range', 2]], ['control: 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]]], ['control: creg declared between qregs', ['qreg a[1];', 'creg c[2];', 'qreg b[2];', 'h b[1];'], [['h', [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 59', ['qreg q[1];', 'qreg r[3];', 'creg c[2];', 'x r[1];', 'measure r[3] -> c;', 'h q[0];', 'h q;'], ['index-out-of-range', 4]], ['regression: index equals size', ['qreg q[2];', 'h q[2];'], ['index-out-of-range', 1]], ['repair check: second register overflow', ['qreg q[2];', 'qreg r[2];', 'x r[3];'], ['index-out-of-range', 2]], ['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 0', ['qreg q[2];', 'creg c[2];', 'h q[0];', 'measure q[1] -> c[0];'], [['h', [0]], ['measure', 1, 0]]]], [['regression: random program 46', ['qreg q[1];', 'creg c[1];', 'qreg r[2];', 'creg d[3];', 'qreg s[3];', 'creg e[2];', 'measure r[1] -> d[0];', 'cx s,s[3];'], ['index-out-of-range', 7]], ['regression: random program 59', ['qreg q[1];', 'qreg r[3];', 'creg c[2];', 'x r[1];', 'measure r[3] -> c;', 'h q[0];', 'h q;'], ['index-out-of-range', 4]], ['repair check: second register overflow', ['qreg q[2];', 'qreg r[2];', 'x r[3];'], ['index-out-of-range', 2]], ['control: random program 1', ['qreg q[3];', 'creg c[2];', 'h q;', 'measure q[0] -> c;'], ['size-mismatch', 3]], ['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]]], [['regression: 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]], ['regression: random program 46', ['qreg q[1];', 'creg c[1];', 'qreg r[2];', 'creg d[3];', 'qreg s[3];', 'creg e[2];', 'measure r[1] -> d[0];', 'cx s,s[3];'], ['index-out-of-range', 7]], ['repair check: second register overflow', ['qreg q[2];', 'qreg r[2];', 'x r[3];'], ['index-out-of-range', 2]], ['control: 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: random program 6', ['qreg q[3];', 'creg c[2];', 'cx q[2],q;'], [['cx', [2, 0]], ['cx', [2, 1]], ['cx', [2, 2]]]], ['control: 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]]]], ['control: random program 9', ['qreg q[1];', 'qreg r[1];', 'qreg s[1];', 'creg c[2];', 'h q[0];'], [['h', [0]]]]], [['regression: index equals size', ['qreg q[2];', 'h q[2];'], ['index-out-of-range', 1]], ['regression: 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]], ['regression: random program 59', ['qreg q[1];', 'qreg r[3];', 'creg c[2];', 'x r[1];', 'measure r[3] -> c;', 'h q[0];', 'h q;'], ['index-out-of-range', 4]], ['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]], ['control: 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: 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]]]]
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 + off:
            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)
                    nc += 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: index equals size', ['qreg q[2];', 'h q[2];'], ['index-out-of-range', 1]], ['regression: 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]], ['repair check: second register overflow', ['qreg q[2];', 'qreg r[2];', 'x r[3];'], ['index-out-of-range', 2]], ['control: 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]]], ['control: creg declared between qregs', ['qreg a[1];', 'creg c[2];', 'qreg b[2];', 'h b[1];'], [['h', [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 59', ['qreg q[1];', 'qreg r[3];', 'creg c[2];', 'x r[1];', 'measure r[3] -> c;', 'h q[0];', 'h q;'], ['index-out-of-range', 4]], ['regression: index equals size', ['qreg q[2];', 'h q[2];'], ['index-out-of-range', 1]], ['repair check: second register overflow', ['qreg q[2];', 'qreg r[2];', 'x r[3];'], ['index-out-of-range', 2]], ['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 0', ['qreg q[2];', 'creg c[2];', 'h q[0];', 'measure q[1] -> c[0];'], [['h', [0]], ['measure', 1, 0]]]], [['regression: random program 46', ['qreg q[1];', 'creg c[1];', 'qreg r[2];', 'creg d[3];', 'qreg s[3];', 'creg e[2];', 'measure r[1] -> d[0];', 'cx s,s[3];'], ['index-out-of-range', 7]], ['regression: random program 59', ['qreg q[1];', 'qreg r[3];', 'creg c[2];', 'x r[1];', 'measure r[3] -> c;', 'h q[0];', 'h q;'], ['index-out-of-range', 4]], ['repair check: second register overflow', ['qreg q[2];', 'qreg r[2];', 'x r[3];'], ['index-out-of-range', 2]], ['control: random program 1', ['qreg q[3];', 'creg c[2];', 'h q;', 'measure q[0] -> c;'], ['size-mismatch', 3]], ['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]]], [['regression: 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]], ['regression: random program 46', ['qreg q[1];', 'creg c[1];', 'qreg r[2];', 'creg d[3];', 'qreg s[3];', 'creg e[2];', 'measure r[1] -> d[0];', 'cx s,s[3];'], ['index-out-of-range', 7]], ['repair check: second register overflow', ['qreg q[2];', 'qreg r[2];', 'x r[3];'], ['index-out-of-range', 2]], ['control: 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: random program 6', ['qreg q[3];', 'creg c[2];', 'cx q[2],q;'], [['cx', [2, 0]], ['cx', [2, 1]], ['cx', [2, 2]]]], ['control: 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]]]], ['control: random program 9', ['qreg q[1];', 'qreg r[1];', 'qreg s[1];', 'creg c[2];', 'h q[0];'], [['h', [0]]]]], [['regression: index equals size', ['qreg q[2];', 'h q[2];'], ['index-out-of-range', 1]], ['regression: 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]], ['regression: random program 59', ['qreg q[1];', 'qreg r[3];', 'creg c[2];', 'x r[1];', 'measure r[3] -> c;', 'h q[0];', 'h q;'], ['index-out-of-range', 4]], ['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]], ['control: 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: 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]]]]
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:
h q[2] on a two-qubit register maps to the first qubit of the next register (or a phantom qubit).

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


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)] = (nc, size)
                    nc += size
            elif head == 'measure':
                src, dst = rest.split('->')
                qs, cs = ref(src, qregs), ref(dst, cregs)
                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: measure size mismatch', ['qreg q[3];', 'creg c[2];', 'measure q -> c;'], ['size-mismatch', 2]], ['regression: random program 1', ['qreg q[3];', 'creg c[2];', 'h q;', 'measure q[0] -> c;'], ['size-mismatch', 3]], ['regression: 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: 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]]], ['control: creg declared between qregs', ['qreg a[1];', 'creg c[2];', 'qreg b[2];', 'h b[1];'], [['h', [2]]]], ['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]]], [['regression: random program 10', ['qreg q[2];', 'qreg r[2];', 'creg c[2];', 'h q[0];', 'x q;', 'measure r[0] -> c;'], ['size-mismatch', 5]], ['regression: 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 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: 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]]]], ['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]]], [['regression: 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]], ['regression: random program 24', ['qreg q[3];', 'creg c[2];', 'h q[0];', 'measure q[2] -> c;'], ['size-mismatch', 3]], ['regression: random program 28', ['qreg q[3];', 'creg c[2];', 'h q[0];', 'cx q[1],q[0];', 'measure q[1] -> c;'], ['size-mismatch', 4]], ['control: random program 0', ['qreg q[2];', 'creg c[2];', 'h q[0];', 'measure q[1] -> c[0];'], [['h', [0]], ['measure', 1, 0]]], ['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 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 38', ['qreg q[3];', 'creg c[1];', 'measure q -> c[0];', 'h q[0];', 'measure q[3] -> c[0];', 'cx q[2],q[0];'], ['size-mismatch', 2]], ['regression: random program 40', ['qreg q[1];', 'qreg r[1];', 'creg c[3];', 'measure r -> c;'], ['size-mismatch', 3]], ['regression: random program 28', ['qreg q[3];', 'creg c[2];', 'h q[0];', 'cx q[1],q[0];', 'measure q[1] -> c;'], ['size-mismatch', 4]], ['control: random program 6', ['qreg q[3];', 'creg c[2];', 'cx q[2],q;'], [['cx', [2, 0]], ['cx', [2, 1]], ['cx', [2, 2]]]], ['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 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]]]], ['control: random program 9', ['qreg q[1];', 'qreg r[1];', 'qreg s[1];', 'creg c[2];', 'h q[0];'], [['h', [0]]]]], [['regression: random program 43', ['qreg q[3];', 'creg c[2];', 'measure q[0] -> c;'], ['size-mismatch', 2]], ['regression: random program 48', ['qreg q[2];', 'creg c[3];', 'qreg r[2];', 'measure q[0] -> c[2];', 'measure q -> c;'], ['size-mismatch', 4]], ['regression: random program 53', ['qreg q[2];', 'creg c[3];', 'qreg r[2];', 'qreg s[3];', 'measure r -> c;', 'x q[1];', 'cx q[1],q[0];'], ['size-mismatch', 4]], ['control: 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: 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]]]], ['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 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]]]]]]
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
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)
                    nc += 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: measure size mismatch', ['qreg q[3];', 'creg c[2];', 'measure q -> c;'], ['size-mismatch', 2]], ['regression: random program 1', ['qreg q[3];', 'creg c[2];', 'h q;', 'measure q[0] -> c;'], ['size-mismatch', 3]], ['regression: 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: 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]]], ['control: creg declared between qregs', ['qreg a[1];', 'creg c[2];', 'qreg b[2];', 'h b[1];'], [['h', [2]]]], ['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]]], [['regression: random program 10', ['qreg q[2];', 'qreg r[2];', 'creg c[2];', 'h q[0];', 'x q;', 'measure r[0] -> c;'], ['size-mismatch', 5]], ['regression: 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 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: 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]]]], ['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]]], [['regression: 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]], ['regression: random program 24', ['qreg q[3];', 'creg c[2];', 'h q[0];', 'measure q[2] -> c;'], ['size-mismatch', 3]], ['regression: random program 28', ['qreg q[3];', 'creg c[2];', 'h q[0];', 'cx q[1],q[0];', 'measure q[1] -> c;'], ['size-mismatch', 4]], ['control: random program 0', ['qreg q[2];', 'creg c[2];', 'h q[0];', 'measure q[1] -> c[0];'], [['h', [0]], ['measure', 1, 0]]], ['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 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 38', ['qreg q[3];', 'creg c[1];', 'measure q -> c[0];', 'h q[0];', 'measure q[3] -> c[0];', 'cx q[2],q[0];'], ['size-mismatch', 2]], ['regression: random program 40', ['qreg q[1];', 'qreg r[1];', 'creg c[3];', 'measure r -> c;'], ['size-mismatch', 3]], ['regression: random program 28', ['qreg q[3];', 'creg c[2];', 'h q[0];', 'cx q[1],q[0];', 'measure q[1] -> c;'], ['size-mismatch', 4]], ['control: random program 6', ['qreg q[3];', 'creg c[2];', 'cx q[2],q;'], [['cx', [2, 0]], ['cx', [2, 1]], ['cx', [2, 2]]]], ['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 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]]]], ['control: random program 9', ['qreg q[1];', 'qreg r[1];', 'qreg s[1];', 'creg c[2];', 'h q[0];'], [['h', [0]]]]], [['regression: random program 43', ['qreg q[3];', 'creg c[2];', 'measure q[0] -> c;'], ['size-mismatch', 2]], ['regression: random program 48', ['qreg q[2];', 'creg c[3];', 'qreg r[2];', 'measure q[0] -> c[2];', 'measure q -> c;'], ['size-mismatch', 4]], ['regression: random program 53', ['qreg q[2];', 'creg c[3];', 'qreg r[2];', 'qreg s[3];', 'measure r -> c;', 'x q[1];', 'cx q[1],q[0];'], ['size-mismatch', 4]], ['control: 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: 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]]]], ['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 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]]]]]]
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:
measure q -> c with three qubits and two clbits silently drops the last qubit.

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


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)] = (nc, size)
                    nc += size
            elif head == 'measure':
                src, dst = rest.split('->')
                qs, cs = ref(src, qregs), ref(dst, qregs)
                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: measure size mismatch', ['qreg q[3];', 'creg c[2];', 'measure q -> c;'], ['size-mismatch', 2]], ['regression: measure into qreg name', ['qreg q[1];', 'qreg r[1];', 'measure q[0] -> r[0];'], ['unknown-register', 2]], ['control: creg declared between qregs', ['qreg a[1];', 'creg c[2];', 'qreg b[2];', 'h b[1];'], [['h', [2]]]], ['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]]]]], [['regression: random program 0', ['qreg q[2];', 'creg c[2];', 'h q[0];', 'measure q[1] -> c[0];'], [['h', [0]], ['measure', 1, 0]]], ['regression: random program 1', ['qreg q[3];', 'creg c[2];', 'h q;', 'measure q[0] -> c;'], ['size-mismatch', 3]], ['regression: measure into qreg name', ['qreg q[1];', 'qreg r[1];', 'measure q[0] -> r[0];'], ['unknown-register', 2]], ['control: register control single target', ['qreg q[3];', 'qreg r[1];', 'cx q,r[0];'], [['cx', [0, 3]], ['cx', [1, 3]], ['cx', [2, 3]]]], ['control: unknown register', ['qreg q[1];', 'h z[0];'], ['unknown-register', 1]], ['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]]]]], [['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: measure into qreg name', ['qreg q[1];', 'qreg r[1];', 'measure q[0] -> r[0];'], ['unknown-register', 2]], ['control: random program 6', ['qreg q[3];', 'creg c[2];', 'cx q[2],q;'], [['cx', [2, 0]], ['cx', [2, 1]], ['cx', [2, 2]]]], ['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 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]]]]], [['regression: 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 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]], ['regression: measure into qreg name', ['qreg q[1];', 'qreg r[1];', 'measure q[0] -> r[0];'], ['unknown-register', 2]], ['control: 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]]]], ['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 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]]]], ['control: 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 19', ['qreg q[1];', 'qreg r[1];', 'qreg s[2];', 'creg c[3];', 'measure s[0] -> c;', 'h q[0];'], ['size-mismatch', 4]], ['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: measure into qreg name', ['qreg q[1];', 'qreg r[1];', 'measure q[0] -> r[0];'], ['unknown-register', 2]], ['control: 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 23', ['qreg q[3];', 'creg c[1];', 'x q[0];'], [['x', [0]]]], ['control: random program 26', ['qreg q[2];', 'creg c[3];', 'qreg r[1];', 'creg d[1];', 'h r[0];'], [['h', [2]]]], ['control: random program 27', ['qreg q[1];', 'qreg r[3];', 'creg c[1];', 'x q;'], [['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)
                    nc += size
            elif head == 'measure':
                src, dst = rest.split('->')
                qs, cs = ref(src, qregs), ref(dst, {**qregs, **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: measure size mismatch', ['qreg q[3];', 'creg c[2];', 'measure q -> c;'], ['size-mismatch', 2]], ['regression: measure into qreg name', ['qreg q[1];', 'qreg r[1];', 'measure q[0] -> r[0];'], ['unknown-register', 2]], ['control: creg declared between qregs', ['qreg a[1];', 'creg c[2];', 'qreg b[2];', 'h b[1];'], [['h', [2]]]], ['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]]]]], [['regression: random program 0', ['qreg q[2];', 'creg c[2];', 'h q[0];', 'measure q[1] -> c[0];'], [['h', [0]], ['measure', 1, 0]]], ['regression: random program 1', ['qreg q[3];', 'creg c[2];', 'h q;', 'measure q[0] -> c;'], ['size-mismatch', 3]], ['regression: measure into qreg name', ['qreg q[1];', 'qreg r[1];', 'measure q[0] -> r[0];'], ['unknown-register', 2]], ['control: register control single target', ['qreg q[3];', 'qreg r[1];', 'cx q,r[0];'], [['cx', [0, 3]], ['cx', [1, 3]], ['cx', [2, 3]]]], ['control: unknown register', ['qreg q[1];', 'h z[0];'], ['unknown-register', 1]], ['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]]]]], [['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: measure into qreg name', ['qreg q[1];', 'qreg r[1];', 'measure q[0] -> r[0];'], ['unknown-register', 2]], ['control: random program 6', ['qreg q[3];', 'creg c[2];', 'cx q[2],q;'], [['cx', [2, 0]], ['cx', [2, 1]], ['cx', [2, 2]]]], ['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 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]]]]], [['regression: 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 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]], ['regression: measure into qreg name', ['qreg q[1];', 'qreg r[1];', 'measure q[0] -> r[0];'], ['unknown-register', 2]], ['control: 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]]]], ['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 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]]]], ['control: 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 19', ['qreg q[1];', 'qreg r[1];', 'qreg s[2];', 'creg c[3];', 'measure s[0] -> c;', 'h q[0];'], ['size-mismatch', 4]], ['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: measure into qreg name', ['qreg q[1];', 'qreg r[1];', 'measure q[0] -> r[0];'], ['unknown-register', 2]], ['control: 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 23', ['qreg q[3];', 'creg c[1];', 'x q[0];'], [['x', [0]]]], ['control: random program 26', ['qreg q[2];', 'creg c[3];', 'qreg r[1];', 'creg d[1];', 'h r[0];'], [['h', [2]]]], ['control: random program 27', ['qreg q[1];', 'qreg r[3];', 'creg c[1];', 'x q;'], [['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:
measure q[0] -> c[0] reports "unknown-register" for the creg c.

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


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)] = (nc, size)
                    nc += 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 = len(args[0])
                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: single control broadcast target', ['qreg q[1];', 'qreg r[3];', 'cx q[0],r;'], [['cx', [0, 1]], ['cx', [0, 2]], ['cx', [0, 3]]]], ['regression: random program 6', ['qreg q[3];', 'creg c[2];', 'cx q[2],q;'], [['cx', [2, 0]], ['cx', [2, 1]], ['cx', [2, 2]]]], ['repair check: register control single target', ['qreg q[3];', 'qreg r[1];', 'cx q,r[0];'], [['cx', [0, 3]], ['cx', [1, 3]], ['cx', [2, 3]]]], ['control: 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]]], ['control: creg declared between qregs', ['qreg a[1];', 'creg c[2];', 'qreg b[2];', 'h b[1];'], [['h', [2]]]], ['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]]], [['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 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]]]], ['repair check: random program 32', ['qreg q[2];', 'creg c[2];', 'qreg r[1];', 'creg d[3];', 'qreg s[3];', 'h q[0];', 'cx s,s[0];', 'measure s[2] -> c[1];'], [['h', [0]], ['cx', [3, 3]], ['cx', [4, 3]], ['cx', [5, 3]], ['measure', 5, 1]]], ['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 0', ['qreg q[2];', 'creg c[2];', 'h q[0];', 'measure q[1] -> c[0];'], [['h', [0]], ['measure', 1, 0]]]], [['regression: single control broadcast target', ['qreg q[1];', 'qreg r[3];', 'cx q[0],r;'], [['cx', [0, 1]], ['cx', [0, 2]], ['cx', [0, 3]]]], ['regression: random program 6', ['qreg q[3];', 'creg c[2];', 'cx q[2],q;'], [['cx', [2, 0]], ['cx', [2, 1]], ['cx', [2, 2]]]], ['repair check: random program 47', ['qreg q[1];', 'creg c[1];', 'qreg r[2];', 'creg d[3];', 'qreg s[3];', 'cx q,s[1];', 'cx s,r[1];'], [['cx', [0, 4]], ['cx', [3, 2]], ['cx', [4, 2]], ['cx', [5, 2]]]], ['control: random program 1', ['qreg q[3];', 'creg c[2];', 'h q;', 'measure q[0] -> c;'], ['size-mismatch', 3]], ['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]]], [['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 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]]]], ['repair check: register control single target', ['qreg q[3];', 'qreg r[1];', 'cx q,r[0];'], [['cx', [0, 3]], ['cx', [1, 3]], ['cx', [2, 3]]]], ['control: 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: 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 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]]]], ['control: random program 9', ['qreg q[1];', 'qreg r[1];', 'qreg s[1];', 'creg c[2];', 'h q[0];'], [['h', [0]]]]], [['regression: single control broadcast target', ['qreg q[1];', 'qreg r[3];', 'cx q[0],r;'], [['cx', [0, 1]], ['cx', [0, 2]], ['cx', [0, 3]]]], ['regression: random program 6', ['qreg q[3];', 'creg c[2];', 'cx q[2],q;'], [['cx', [2, 0]], ['cx', [2, 1]], ['cx', [2, 2]]]], ['repair check: random program 32', ['qreg q[2];', 'creg c[2];', 'qreg r[1];', 'creg d[3];', 'qreg s[3];', 'h q[0];', 'cx s,s[0];', 'measure s[2] -> c[1];'], [['h', [0]], ['cx', [3, 3]], ['cx', [4, 3]], ['cx', [5, 3]], ['measure', 5, 1]]], ['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]], ['control: 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: 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]]]]]]
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)
                    nc += 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 = len(args[-1])
                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: single control broadcast target', ['qreg q[1];', 'qreg r[3];', 'cx q[0],r;'], [['cx', [0, 1]], ['cx', [0, 2]], ['cx', [0, 3]]]], ['regression: random program 6', ['qreg q[3];', 'creg c[2];', 'cx q[2],q;'], [['cx', [2, 0]], ['cx', [2, 1]], ['cx', [2, 2]]]], ['repair check: register control single target', ['qreg q[3];', 'qreg r[1];', 'cx q,r[0];'], [['cx', [0, 3]], ['cx', [1, 3]], ['cx', [2, 3]]]], ['control: 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]]], ['control: creg declared between qregs', ['qreg a[1];', 'creg c[2];', 'qreg b[2];', 'h b[1];'], [['h', [2]]]], ['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]]], [['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 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]]]], ['repair check: random program 32', ['qreg q[2];', 'creg c[2];', 'qreg r[1];', 'creg d[3];', 'qreg s[3];', 'h q[0];', 'cx s,s[0];', 'measure s[2] -> c[1];'], [['h', [0]], ['cx', [3, 3]], ['cx', [4, 3]], ['cx', [5, 3]], ['measure', 5, 1]]], ['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 0', ['qreg q[2];', 'creg c[2];', 'h q[0];', 'measure q[1] -> c[0];'], [['h', [0]], ['measure', 1, 0]]]], [['regression: single control broadcast target', ['qreg q[1];', 'qreg r[3];', 'cx q[0],r;'], [['cx', [0, 1]], ['cx', [0, 2]], ['cx', [0, 3]]]], ['regression: random program 6', ['qreg q[3];', 'creg c[2];', 'cx q[2],q;'], [['cx', [2, 0]], ['cx', [2, 1]], ['cx', [2, 2]]]], ['repair check: random program 47', ['qreg q[1];', 'creg c[1];', 'qreg r[2];', 'creg d[3];', 'qreg s[3];', 'cx q,s[1];', 'cx s,r[1];'], [['cx', [0, 4]], ['cx', [3, 2]], ['cx', [4, 2]], ['cx', [5, 2]]]], ['control: random program 1', ['qreg q[3];', 'creg c[2];', 'h q;', 'measure q[0] -> c;'], ['size-mismatch', 3]], ['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]]], [['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 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]]]], ['repair check: register control single target', ['qreg q[3];', 'qreg r[1];', 'cx q,r[0];'], [['cx', [0, 3]], ['cx', [1, 3]], ['cx', [2, 3]]]], ['control: 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: 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 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]]]], ['control: random program 9', ['qreg q[1];', 'qreg r[1];', 'qreg s[1];', 'creg c[2];', 'h q[0];'], [['h', [0]]]]], [['regression: single control broadcast target', ['qreg q[1];', 'qreg r[3];', 'cx q[0],r;'], [['cx', [0, 1]], ['cx', [0, 2]], ['cx', [0, 3]]]], ['regression: random program 6', ['qreg q[3];', 'creg c[2];', 'cx q[2],q;'], [['cx', [2, 0]], ['cx', [2, 1]], ['cx', [2, 2]]]], ['repair check: random program 32', ['qreg q[2];', 'creg c[2];', 'qreg r[1];', 'creg d[3];', 'qreg s[3];', 'h q[0];', 'cx s,s[0];', 'measure s[2] -> c[1];'], [['h', [0]], ['cx', [3, 3]], ['cx', [4, 3]], ['cx', [5, 3]], ['measure', 5, 1]]], ['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]], ['control: 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: 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]]]]]]
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:
cx q[0],r with a three-qubit r emits one CNOT instead of three.

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


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(x):
    t = cliff = 0
    for idx, op in enumerate(x):
        name = op[0]
        if name in ('t', 'tdg'):
            t += 1
        elif name in ('h', 's', 'sdg', 'x', 'y', 'z', 'cx', 'cz', 'swap'):
            cliff += 1
        elif name == 'ccx':
            t += 7
            cliff += 8
        elif name == 'rz':
            a = Fraction(op[2]) % 2
            if (a * 2).denominator == 1:
                cliff += 1
            elif (a * 4).denominator == 1:
                t += 1
            else:
                return ['unsupported', idx]
        else:
            return ['unknown-gate', idx]
    return {'t_count': t, 'clifford_count': cliff}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: rz two pi', [['rz', 0, '2']], {'t_count': 0, 'clifford_count': 0}], ['regression: rz zero', [['rz', 0, '0']], {'t_count': 0, 'clifford_count': 0}], ['regression: rz four pi', [['rz', 0, '4']], {'t_count': 0, 'clifford_count': 0}], ['control: rz pi is Z', [['rz', 0, '1']], {'t_count': 0, 'clifford_count': 1}], ['control: rz half pi is S', [['rz', 0, '1/2']], {'t_count': 0, 'clifford_count': 1}], ['control: rz quarter pi', [['rz', 0, '1/4']], {'t_count': 1, 'clifford_count': 0}], ['control: tdg', [['tdg', 0]], {'t_count': 1, 'clifford_count': 0}]], [['regression: random t-count 0', [['rz', 0, '-1/4'], ['rz', 0, '3/4'], ['rz', 0, '4']], {'t_count': 2, 'clifford_count': 0}], ['regression: random t-count 1', [['rz', 0, '-2'], ['ccx', 0, 1, 2], ['rz', 0, '1'], ['rz', 0, '1'], ['rz', 0, '0'], ['x', 0]], {'t_count': 7, 'clifford_count': 11}], ['regression: random t-count 16', [['rz', 0, '3/4'], ['rz', 0, '-1'], ['tdg', 0], ['sdg', 0], ['rz', 0, '-2'], ['rz', 0, '3/2'], ['sdg', 0]], {'t_count': 2, 'clifford_count': 4}], ['control: toffoli', [['ccx', 0, 1, 2]], {'t_count': 7, 'clifford_count': 8}], ['control: unsupported angle', [['h', 0], ['rz', 0, '1/8']], ['unsupported', 1]], ['control: negative angles', [['rz', 0, '-1/4'], ['rz', 1, '-3/2']], {'t_count': 1, 'clifford_count': 1}], ['control: unknown gate', [['u3', 0]], ['unknown-gate', 0]]], [['regression: random t-count 6', [['ccx', 0, 1, 2], ['x', 0], ['x', 0], ['rz', 0, '0'], ['sdg', 0], ['t', 0], ['ccx', 0, 1, 2]], {'t_count': 15, 'clifford_count': 19}], ['regression: random t-count 16', [['rz', 0, '3/4'], ['rz', 0, '-1'], ['tdg', 0], ['sdg', 0], ['rz', 0, '-2'], ['rz', 0, '3/2'], ['sdg', 0]], {'t_count': 2, 'clifford_count': 4}], ['regression: random t-count 18', [['t', 0], ['rz', 0, '4'], ['rz', 0, '3/2'], ['ccx', 0, 1, 2], ['rz', 0, '3/4'], ['rz', 0, '-2']], {'t_count': 9, 'clifford_count': 9}], ['control: random t-count 2', [['sdg', 0], ['rz', 0, '1/4'], ['cx', 0]], {'t_count': 1, 'clifford_count': 2}], ['control: random t-count 4', [['ccx', 0, 1, 2]], {'t_count': 7, 'clifford_count': 8}], ['control: random t-count 5', [['ccx', 0, 1, 2], ['rz', 0, '1'], ['rz', 0, '5/4'], ['tdg', 0]], {'t_count': 9, 'clifford_count': 9}], ['control: random t-count 7', [['rz', 0, '1/2'], ['tdg', 0], ['rz', 0, '1'], ['h', 0]], {'t_count': 1, 'clifford_count': 3}]], [['regression: random t-count 23', [['rz', 0, '-2'], ['tdg', 0], ['ccx', 0, 1, 2], ['t', 0], ['rz', 0, '4'], ['rz', 0, '-1']], {'t_count': 9, 'clifford_count': 9}], ['regression: random t-count 31', [['rz', 0, '2'], ['h', 0], ['x', 0], ['x', 0], ['t', 0], ['rz', 0, '-2']], {'t_count': 1, 'clifford_count': 3}], ['regression: random t-count 36', [['rz', 0, '-2'], ['ccx', 0, 1, 2], ['cx', 0], ['x', 0], ['rz', 0, '4'], ['t', 0]], {'t_count': 8, 'clifford_count': 10}], ['control: random t-count 8', [['rz', 0, '-1/4'], ['ccx', 0, 1, 2], ['ccx', 0, 1, 2], ['ccx', 0, 1, 2], ['cx', 0]], {'t_count': 22, 'clifford_count': 25}], ['control: random t-count 9', [['rz', 0, '1/4']], {'t_count': 1, 'clifford_count': 0}], ['control: random t-count 10', [['ccx', 0, 1, 2], ['s', 0], ['ccx', 0, 1, 2], ['tdg', 0]], {'t_count': 15, 'clifford_count': 17}], ['control: random t-count 11', [['s', 0], ['rz', 0, '5/4'], ['rz', 0, '-1'], ['x', 0], ['t', 0]], {'t_count': 2, 'clifford_count': 3}]], [['regression: random t-count 36', [['rz', 0, '-2'], ['ccx', 0, 1, 2], ['cx', 0], ['x', 0], ['rz', 0, '4'], ['t', 0]], {'t_count': 8, 'clifford_count': 10}], ['regression: random t-count 37', [['t', 0], ['rz', 0, '-1'], ['cx', 0], ['t', 0], ['rz', 0, '1/4'], ['rz', 0, '2'], ['tdg', 0]], {'t_count': 4, 'clifford_count': 2}], ['regression: random t-count 42', [['cx', 0], ['rz', 0, '2'], ['rz', 0, '-1'], ['rz', 0, '1/4'], ['rz', 0, '3/4'], ['tdg', 0]], {'t_count': 3, 'clifford_count': 2}], ['control: random t-count 12', [['t', 0], ['tdg', 0], ['rz', 0, '-1']], {'t_count': 2, 'clifford_count': 1}], ['control: random t-count 13', [['rz', 0, '-1/4'], ['rz', 0, '7/2'], ['rz', 0, '3/4'], ['s', 0]], {'t_count': 2, 'clifford_count': 2}], ['control: random t-count 14', [['sdg', 0]], {'t_count': 0, 'clifford_count': 1}], ['control: random t-count 15', [['ccx', 0, 1, 2], ['sdg', 0], ['t', 0], ['rz', 0, '3/2']], {'t_count': 8, 'clifford_count': 10}]]]
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):
    t = cliff = 0
    for idx, op in enumerate(x):
        name = op[0]
        if name in ('t', 'tdg'):
            t += 1
        elif name in ('h', 's', 'sdg', 'x', 'y', 'z', 'cx', 'cz', 'swap'):
            cliff += 1
        elif name == 'ccx':
            t += 7
            cliff += 8
        elif name == 'rz':
            a = Fraction(op[2]) % 2
            if Fraction(op[2]) == 0:
                continue
            if (a * 2).denominator == 1:
                cliff += 1
            elif (a * 4).denominator == 1:
                t += 1
            else:
                return ['unsupported', idx]
        else:
            return ['unknown-gate', idx]
    return {'t_count': t, 'clifford_count': cliff}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: rz two pi', [['rz', 0, '2']], {'t_count': 0, 'clifford_count': 0}], ['regression: rz zero', [['rz', 0, '0']], {'t_count': 0, 'clifford_count': 0}], ['regression: rz four pi', [['rz', 0, '4']], {'t_count': 0, 'clifford_count': 0}], ['control: rz pi is Z', [['rz', 0, '1']], {'t_count': 0, 'clifford_count': 1}], ['control: rz half pi is S', [['rz', 0, '1/2']], {'t_count': 0, 'clifford_count': 1}], ['control: rz quarter pi', [['rz', 0, '1/4']], {'t_count': 1, 'clifford_count': 0}], ['control: tdg', [['tdg', 0]], {'t_count': 1, 'clifford_count': 0}]], [['regression: random t-count 0', [['rz', 0, '-1/4'], ['rz', 0, '3/4'], ['rz', 0, '4']], {'t_count': 2, 'clifford_count': 0}], ['regression: random t-count 1', [['rz', 0, '-2'], ['ccx', 0, 1, 2], ['rz', 0, '1'], ['rz', 0, '1'], ['rz', 0, '0'], ['x', 0]], {'t_count': 7, 'clifford_count': 11}], ['regression: random t-count 16', [['rz', 0, '3/4'], ['rz', 0, '-1'], ['tdg', 0], ['sdg', 0], ['rz', 0, '-2'], ['rz', 0, '3/2'], ['sdg', 0]], {'t_count': 2, 'clifford_count': 4}], ['control: toffoli', [['ccx', 0, 1, 2]], {'t_count': 7, 'clifford_count': 8}], ['control: unsupported angle', [['h', 0], ['rz', 0, '1/8']], ['unsupported', 1]], ['control: negative angles', [['rz', 0, '-1/4'], ['rz', 1, '-3/2']], {'t_count': 1, 'clifford_count': 1}], ['control: unknown gate', [['u3', 0]], ['unknown-gate', 0]]], [['regression: random t-count 6', [['ccx', 0, 1, 2], ['x', 0], ['x', 0], ['rz', 0, '0'], ['sdg', 0], ['t', 0], ['ccx', 0, 1, 2]], {'t_count': 15, 'clifford_count': 19}], ['regression: random t-count 16', [['rz', 0, '3/4'], ['rz', 0, '-1'], ['tdg', 0], ['sdg', 0], ['rz', 0, '-2'], ['rz', 0, '3/2'], ['sdg', 0]], {'t_count': 2, 'clifford_count': 4}], ['regression: random t-count 18', [['t', 0], ['rz', 0, '4'], ['rz', 0, '3/2'], ['ccx', 0, 1, 2], ['rz', 0, '3/4'], ['rz', 0, '-2']], {'t_count': 9, 'clifford_count': 9}], ['control: random t-count 2', [['sdg', 0], ['rz', 0, '1/4'], ['cx', 0]], {'t_count': 1, 'clifford_count': 2}], ['control: random t-count 4', [['ccx', 0, 1, 2]], {'t_count': 7, 'clifford_count': 8}], ['control: random t-count 5', [['ccx', 0, 1, 2], ['rz', 0, '1'], ['rz', 0, '5/4'], ['tdg', 0]], {'t_count': 9, 'clifford_count': 9}], ['control: random t-count 7', [['rz', 0, '1/2'], ['tdg', 0], ['rz', 0, '1'], ['h', 0]], {'t_count': 1, 'clifford_count': 3}]], [['regression: random t-count 23', [['rz', 0, '-2'], ['tdg', 0], ['ccx', 0, 1, 2], ['t', 0], ['rz', 0, '4'], ['rz', 0, '-1']], {'t_count': 9, 'clifford_count': 9}], ['regression: random t-count 31', [['rz', 0, '2'], ['h', 0], ['x', 0], ['x', 0], ['t', 0], ['rz', 0, '-2']], {'t_count': 1, 'clifford_count': 3}], ['regression: random t-count 36', [['rz', 0, '-2'], ['ccx', 0, 1, 2], ['cx', 0], ['x', 0], ['rz', 0, '4'], ['t', 0]], {'t_count': 8, 'clifford_count': 10}], ['control: random t-count 8', [['rz', 0, '-1/4'], ['ccx', 0, 1, 2], ['ccx', 0, 1, 2], ['ccx', 0, 1, 2], ['cx', 0]], {'t_count': 22, 'clifford_count': 25}], ['control: random t-count 9', [['rz', 0, '1/4']], {'t_count': 1, 'clifford_count': 0}], ['control: random t-count 10', [['ccx', 0, 1, 2], ['s', 0], ['ccx', 0, 1, 2], ['tdg', 0]], {'t_count': 15, 'clifford_count': 17}], ['control: random t-count 11', [['s', 0], ['rz', 0, '5/4'], ['rz', 0, '-1'], ['x', 0], ['t', 0]], {'t_count': 2, 'clifford_count': 3}]], [['regression: random t-count 36', [['rz', 0, '-2'], ['ccx', 0, 1, 2], ['cx', 0], ['x', 0], ['rz', 0, '4'], ['t', 0]], {'t_count': 8, 'clifford_count': 10}], ['regression: random t-count 37', [['t', 0], ['rz', 0, '-1'], ['cx', 0], ['t', 0], ['rz', 0, '1/4'], ['rz', 0, '2'], ['tdg', 0]], {'t_count': 4, 'clifford_count': 2}], ['regression: random t-count 42', [['cx', 0], ['rz', 0, '2'], ['rz', 0, '-1'], ['rz', 0, '1/4'], ['rz', 0, '3/4'], ['tdg', 0]], {'t_count': 3, 'clifford_count': 2}], ['control: random t-count 12', [['t', 0], ['tdg', 0], ['rz', 0, '-1']], {'t_count': 2, 'clifford_count': 1}], ['control: random t-count 13', [['rz', 0, '-1/4'], ['rz', 0, '7/2'], ['rz', 0, '3/4'], ['s', 0]], {'t_count': 2, 'clifford_count': 2}], ['control: random t-count 14', [['sdg', 0]], {'t_count': 0, 'clifford_count': 1}], ['control: random t-count 15', [['ccx', 0, 1, 2], ['sdg', 0], ['t', 0], ['rz', 0, '3/2']], {'t_count': 8, 'clifford_count': 10}]]]
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(0) and rz(2pi) each add one Clifford.

Input a gate list. t and tdg count one T each; h, s, sdg, x, y, z, cx, cz, swap count one Clifford; ccx costs 7 T and 8 Clifford; ["rz", q, a] with a in units of pi reduced mod 2 is skipped if 0, Clifford if a multiple of 1/2, one T if an odd multiple of 1/4, else ["unsupported", index]; other names give ["unknown-gate", index]. Return {"t_count", "clifford_count"}.

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):
    t = cliff = 0
    for idx, op in enumerate(x):
        name = op[0]
        if name in ('t', 'tdg'):
            t += 1
        elif name in ('h', 's', 'sdg', 'x', 'y', 'z', 'cx', 'cz', 'swap'):
            cliff += 1
        elif name == 'ccx':
            t += 7
            cliff += 8
        elif name == 'rz':
            a = Fraction(op[2]) % 2
            if a == 0:
                continue
            if (a * 4).denominator == 1:
                t += 1
            elif (a * 2).denominator == 1:
                cliff += 1
            else:
                return ['unsupported', idx]
        else:
            return ['unknown-gate', idx]
    return {'t_count': t, 'clifford_count': cliff}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: rz pi is Z', [['rz', 0, '1']], {'t_count': 0, 'clifford_count': 1}], ['regression: rz half pi is S', [['rz', 0, '1/2']], {'t_count': 0, 'clifford_count': 1}], ['regression: random t-count 1', [['rz', 0, '-2'], ['ccx', 0, 1, 2], ['rz', 0, '1'], ['rz', 0, '1'], ['rz', 0, '0'], ['x', 0]], {'t_count': 7, 'clifford_count': 11}], ['control: rz two pi', [['rz', 0, '2']], {'t_count': 0, 'clifford_count': 0}], ['control: rz zero', [['rz', 0, '0']], {'t_count': 0, 'clifford_count': 0}], ['control: rz quarter pi', [['rz', 0, '1/4']], {'t_count': 1, 'clifford_count': 0}], ['control: tdg', [['tdg', 0]], {'t_count': 1, 'clifford_count': 0}]], [['regression: random t-count 1', [['rz', 0, '-2'], ['ccx', 0, 1, 2], ['rz', 0, '1'], ['rz', 0, '1'], ['rz', 0, '0'], ['x', 0]], {'t_count': 7, 'clifford_count': 11}], ['regression: random t-count 3', [['rz', 0, '1/2'], ['rz', 0, '0'], ['t', 0]], {'t_count': 1, 'clifford_count': 1}], ['regression: random t-count 5', [['ccx', 0, 1, 2], ['rz', 0, '1'], ['rz', 0, '5/4'], ['tdg', 0]], {'t_count': 9, 'clifford_count': 9}], ['control: toffoli', [['ccx', 0, 1, 2]], {'t_count': 7, 'clifford_count': 8}], ['control: unsupported angle', [['h', 0], ['rz', 0, '1/8']], ['unsupported', 1]], ['control: rz four pi', [['rz', 0, '4']], {'t_count': 0, 'clifford_count': 0}], ['control: unknown gate', [['u3', 0]], ['unknown-gate', 0]]], [['regression: random t-count 7', [['rz', 0, '1/2'], ['tdg', 0], ['rz', 0, '1'], ['h', 0]], {'t_count': 1, 'clifford_count': 3}], ['regression: random t-count 11', [['s', 0], ['rz', 0, '5/4'], ['rz', 0, '-1'], ['x', 0], ['t', 0]], {'t_count': 2, 'clifford_count': 3}], ['regression: random t-count 12', [['t', 0], ['tdg', 0], ['rz', 0, '-1']], {'t_count': 2, 'clifford_count': 1}], ['control: random t-count 0', [['rz', 0, '-1/4'], ['rz', 0, '3/4'], ['rz', 0, '4']], {'t_count': 2, 'clifford_count': 0}], ['control: random t-count 2', [['sdg', 0], ['rz', 0, '1/4'], ['cx', 0]], {'t_count': 1, 'clifford_count': 2}], ['control: random t-count 4', [['ccx', 0, 1, 2]], {'t_count': 7, 'clifford_count': 8}], ['control: random t-count 6', [['ccx', 0, 1, 2], ['x', 0], ['x', 0], ['rz', 0, '0'], ['sdg', 0], ['t', 0], ['ccx', 0, 1, 2]], {'t_count': 15, 'clifford_count': 19}]], [['regression: random t-count 13', [['rz', 0, '-1/4'], ['rz', 0, '7/2'], ['rz', 0, '3/4'], ['s', 0]], {'t_count': 2, 'clifford_count': 2}], ['regression: random t-count 15', [['ccx', 0, 1, 2], ['sdg', 0], ['t', 0], ['rz', 0, '3/2']], {'t_count': 8, 'clifford_count': 10}], ['regression: random t-count 16', [['rz', 0, '3/4'], ['rz', 0, '-1'], ['tdg', 0], ['sdg', 0], ['rz', 0, '-2'], ['rz', 0, '3/2'], ['sdg', 0]], {'t_count': 2, 'clifford_count': 4}], ['control: random t-count 8', [['rz', 0, '-1/4'], ['ccx', 0, 1, 2], ['ccx', 0, 1, 2], ['ccx', 0, 1, 2], ['cx', 0]], {'t_count': 22, 'clifford_count': 25}], ['control: random t-count 9', [['rz', 0, '1/4']], {'t_count': 1, 'clifford_count': 0}], ['control: random t-count 10', [['ccx', 0, 1, 2], ['s', 0], ['ccx', 0, 1, 2], ['tdg', 0]], {'t_count': 15, 'clifford_count': 17}], ['control: random t-count 14', [['sdg', 0]], {'t_count': 0, 'clifford_count': 1}]], [['regression: random t-count 18', [['t', 0], ['rz', 0, '4'], ['rz', 0, '3/2'], ['ccx', 0, 1, 2], ['rz', 0, '3/4'], ['rz', 0, '-2']], {'t_count': 9, 'clifford_count': 9}], ['regression: random t-count 20', [['cx', 0], ['rz', 0, '-1'], ['ccx', 0, 1, 2], ['tdg', 0], ['rz', 0, '1/2'], ['t', 0]], {'t_count': 9, 'clifford_count': 11}], ['regression: random t-count 23', [['rz', 0, '-2'], ['tdg', 0], ['ccx', 0, 1, 2], ['t', 0], ['rz', 0, '4'], ['rz', 0, '-1']], {'t_count': 9, 'clifford_count': 9}], ['control: random t-count 17', [['tdg', 0]], {'t_count': 1, 'clifford_count': 0}], ['control: random t-count 19', [['rz', 0, '3/4'], ['rz', 0, '-1/4'], ['rz', 0, '5/4']], {'t_count': 3, 'clifford_count': 0}], ['control: random t-count 21', [['tdg', 0], ['rz', 0, '5/4']], {'t_count': 2, 'clifford_count': 0}], ['control: random t-count 22', [['tdg', 0], ['tdg', 0], ['tdg', 0], ['rz', 0, '-1/4'], ['x', 0]], {'t_count': 4, 'clifford_count': 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):
    t = cliff = 0
    for idx, op in enumerate(x):
        name = op[0]
        if name in ('t', 'tdg'):
            t += 1
        elif name in ('h', 's', 'sdg', 'x', 'y', 'z', 'cx', 'cz', 'swap'):
            cliff += 1
        elif name == 'ccx':
            t += 7
            cliff += 8
        elif name == 'rz':
            a = Fraction(op[2]) % 2
            if a == 0:
                continue
            if a.denominator == 2:
                cliff += 1
            elif (a * 4).denominator == 1:
                t += 1
            else:
                return ['unsupported', idx]
        else:
            return ['unknown-gate', idx]
    return {'t_count': t, 'clifford_count': cliff}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: rz pi is Z', [['rz', 0, '1']], {'t_count': 0, 'clifford_count': 1}], ['regression: rz half pi is S', [['rz', 0, '1/2']], {'t_count': 0, 'clifford_count': 1}], ['regression: random t-count 1', [['rz', 0, '-2'], ['ccx', 0, 1, 2], ['rz', 0, '1'], ['rz', 0, '1'], ['rz', 0, '0'], ['x', 0]], {'t_count': 7, 'clifford_count': 11}], ['control: rz two pi', [['rz', 0, '2']], {'t_count': 0, 'clifford_count': 0}], ['control: rz zero', [['rz', 0, '0']], {'t_count': 0, 'clifford_count': 0}], ['control: rz quarter pi', [['rz', 0, '1/4']], {'t_count': 1, 'clifford_count': 0}], ['control: tdg', [['tdg', 0]], {'t_count': 1, 'clifford_count': 0}]], [['regression: random t-count 1', [['rz', 0, '-2'], ['ccx', 0, 1, 2], ['rz', 0, '1'], ['rz', 0, '1'], ['rz', 0, '0'], ['x', 0]], {'t_count': 7, 'clifford_count': 11}], ['regression: random t-count 3', [['rz', 0, '1/2'], ['rz', 0, '0'], ['t', 0]], {'t_count': 1, 'clifford_count': 1}], ['regression: random t-count 5', [['ccx', 0, 1, 2], ['rz', 0, '1'], ['rz', 0, '5/4'], ['tdg', 0]], {'t_count': 9, 'clifford_count': 9}], ['control: toffoli', [['ccx', 0, 1, 2]], {'t_count': 7, 'clifford_count': 8}], ['control: unsupported angle', [['h', 0], ['rz', 0, '1/8']], ['unsupported', 1]], ['control: rz four pi', [['rz', 0, '4']], {'t_count': 0, 'clifford_count': 0}], ['control: unknown gate', [['u3', 0]], ['unknown-gate', 0]]], [['regression: random t-count 7', [['rz', 0, '1/2'], ['tdg', 0], ['rz', 0, '1'], ['h', 0]], {'t_count': 1, 'clifford_count': 3}], ['regression: random t-count 11', [['s', 0], ['rz', 0, '5/4'], ['rz', 0, '-1'], ['x', 0], ['t', 0]], {'t_count': 2, 'clifford_count': 3}], ['regression: random t-count 12', [['t', 0], ['tdg', 0], ['rz', 0, '-1']], {'t_count': 2, 'clifford_count': 1}], ['control: random t-count 0', [['rz', 0, '-1/4'], ['rz', 0, '3/4'], ['rz', 0, '4']], {'t_count': 2, 'clifford_count': 0}], ['control: random t-count 2', [['sdg', 0], ['rz', 0, '1/4'], ['cx', 0]], {'t_count': 1, 'clifford_count': 2}], ['control: random t-count 4', [['ccx', 0, 1, 2]], {'t_count': 7, 'clifford_count': 8}], ['control: random t-count 6', [['ccx', 0, 1, 2], ['x', 0], ['x', 0], ['rz', 0, '0'], ['sdg', 0], ['t', 0], ['ccx', 0, 1, 2]], {'t_count': 15, 'clifford_count': 19}]], [['regression: random t-count 13', [['rz', 0, '-1/4'], ['rz', 0, '7/2'], ['rz', 0, '3/4'], ['s', 0]], {'t_count': 2, 'clifford_count': 2}], ['regression: random t-count 15', [['ccx', 0, 1, 2], ['sdg', 0], ['t', 0], ['rz', 0, '3/2']], {'t_count': 8, 'clifford_count': 10}], ['regression: random t-count 16', [['rz', 0, '3/4'], ['rz', 0, '-1'], ['tdg', 0], ['sdg', 0], ['rz', 0, '-2'], ['rz', 0, '3/2'], ['sdg', 0]], {'t_count': 2, 'clifford_count': 4}], ['control: random t-count 8', [['rz', 0, '-1/4'], ['ccx', 0, 1, 2], ['ccx', 0, 1, 2], ['ccx', 0, 1, 2], ['cx', 0]], {'t_count': 22, 'clifford_count': 25}], ['control: random t-count 9', [['rz', 0, '1/4']], {'t_count': 1, 'clifford_count': 0}], ['control: random t-count 10', [['ccx', 0, 1, 2], ['s', 0], ['ccx', 0, 1, 2], ['tdg', 0]], {'t_count': 15, 'clifford_count': 17}], ['control: random t-count 14', [['sdg', 0]], {'t_count': 0, 'clifford_count': 1}]], [['regression: random t-count 18', [['t', 0], ['rz', 0, '4'], ['rz', 0, '3/2'], ['ccx', 0, 1, 2], ['rz', 0, '3/4'], ['rz', 0, '-2']], {'t_count': 9, 'clifford_count': 9}], ['regression: random t-count 20', [['cx', 0], ['rz', 0, '-1'], ['ccx', 0, 1, 2], ['tdg', 0], ['rz', 0, '1/2'], ['t', 0]], {'t_count': 9, 'clifford_count': 11}], ['regression: random t-count 23', [['rz', 0, '-2'], ['tdg', 0], ['ccx', 0, 1, 2], ['t', 0], ['rz', 0, '4'], ['rz', 0, '-1']], {'t_count': 9, 'clifford_count': 9}], ['control: random t-count 17', [['tdg', 0]], {'t_count': 1, 'clifford_count': 0}], ['control: random t-count 19', [['rz', 0, '3/4'], ['rz', 0, '-1/4'], ['rz', 0, '5/4']], {'t_count': 3, 'clifford_count': 0}], ['control: random t-count 21', [['tdg', 0], ['rz', 0, '5/4']], {'t_count': 2, 'clifford_count': 0}], ['control: random t-count 22', [['tdg', 0], ['tdg', 0], ['tdg', 0], ['rz', 0, '-1/4'], ['x', 0]], {'t_count': 4, 'clifford_count': 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:
rz(pi/2), an S gate, is counted as a T gate.

Input a gate list. t and tdg count one T each; h, s, sdg, x, y, z, cx, cz, swap count one Clifford; ccx costs 7 T and 8 Clifford; ["rz", q, a] with a in units of pi reduced mod 2 is skipped if 0, Clifford if a multiple of 1/2, one T if an odd multiple of 1/4, else ["unsupported", index]; other names give ["unknown-gate", index]. Return {"t_count", "clifford_count"}.

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):
    t = cliff = 0
    for idx, op in enumerate(x):
        name = op[0]
        if name in ('t',):
            t += 1
        elif name in ('h', 's', 'sdg', 'x', 'y', 'z', 'cx', 'cz', 'swap'):
            cliff += 1
        elif name == 'ccx':
            t += 7
            cliff += 8
        elif name == 'rz':
            a = Fraction(op[2]) % 2
            if a == 0:
                continue
            if (a * 2).denominator == 1:
                cliff += 1
            elif (a * 4).denominator == 1:
                t += 1
            else:
                return ['unsupported', idx]
        else:
            return ['unknown-gate', idx]
    return {'t_count': t, 'clifford_count': cliff}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: tdg', [['tdg', 0]], {'t_count': 1, 'clifford_count': 0}], ['regression: random t-count 5', [['ccx', 0, 1, 2], ['rz', 0, '1'], ['rz', 0, '5/4'], ['tdg', 0]], {'t_count': 9, 'clifford_count': 9}], ['regression: random t-count 7', [['rz', 0, '1/2'], ['tdg', 0], ['rz', 0, '1'], ['h', 0]], {'t_count': 1, 'clifford_count': 3}], ['control: rz two pi', [['rz', 0, '2']], {'t_count': 0, 'clifford_count': 0}], ['control: rz zero', [['rz', 0, '0']], {'t_count': 0, 'clifford_count': 0}], ['control: rz pi is Z', [['rz', 0, '1']], {'t_count': 0, 'clifford_count': 1}], ['control: rz half pi is S', [['rz', 0, '1/2']], {'t_count': 0, 'clifford_count': 1}]], [['regression: random t-count 10', [['ccx', 0, 1, 2], ['s', 0], ['ccx', 0, 1, 2], ['tdg', 0]], {'t_count': 15, 'clifford_count': 17}], ['regression: random t-count 12', [['t', 0], ['tdg', 0], ['rz', 0, '-1']], {'t_count': 2, 'clifford_count': 1}], ['regression: random t-count 7', [['rz', 0, '1/2'], ['tdg', 0], ['rz', 0, '1'], ['h', 0]], {'t_count': 1, 'clifford_count': 3}], ['control: rz quarter pi', [['rz', 0, '1/4']], {'t_count': 1, 'clifford_count': 0}], ['control: toffoli', [['ccx', 0, 1, 2]], {'t_count': 7, 'clifford_count': 8}], ['control: unsupported angle', [['h', 0], ['rz', 0, '1/8']], ['unsupported', 1]], ['control: negative angles', [['rz', 0, '-1/4'], ['rz', 1, '-3/2']], {'t_count': 1, 'clifford_count': 1}]], [['regression: random t-count 17', [['tdg', 0]], {'t_count': 1, 'clifford_count': 0}], ['regression: random t-count 20', [['cx', 0], ['rz', 0, '-1'], ['ccx', 0, 1, 2], ['tdg', 0], ['rz', 0, '1/2'], ['t', 0]], {'t_count': 9, 'clifford_count': 11}], ['regression: random t-count 12', [['t', 0], ['tdg', 0], ['rz', 0, '-1']], {'t_count': 2, 'clifford_count': 1}], ['control: rz four pi', [['rz', 0, '4']], {'t_count': 0, 'clifford_count': 0}], ['control: unknown gate', [['u3', 0]], ['unknown-gate', 0]], ['control: random t-count 0', [['rz', 0, '-1/4'], ['rz', 0, '3/4'], ['rz', 0, '4']], {'t_count': 2, 'clifford_count': 0}], ['control: random t-count 1', [['rz', 0, '-2'], ['ccx', 0, 1, 2], ['rz', 0, '1'], ['rz', 0, '1'], ['rz', 0, '0'], ['x', 0]], {'t_count': 7, 'clifford_count': 11}]], [['regression: random t-count 22', [['tdg', 0], ['tdg', 0], ['tdg', 0], ['rz', 0, '-1/4'], ['x', 0]], {'t_count': 4, 'clifford_count': 1}], ['regression: random t-count 23', [['rz', 0, '-2'], ['tdg', 0], ['ccx', 0, 1, 2], ['t', 0], ['rz', 0, '4'], ['rz', 0, '-1']], {'t_count': 9, 'clifford_count': 9}], ['regression: random t-count 17', [['tdg', 0]], {'t_count': 1, 'clifford_count': 0}], ['control: random t-count 2', [['sdg', 0], ['rz', 0, '1/4'], ['cx', 0]], {'t_count': 1, 'clifford_count': 2}], ['control: random t-count 3', [['rz', 0, '1/2'], ['rz', 0, '0'], ['t', 0]], {'t_count': 1, 'clifford_count': 1}], ['control: random t-count 4', [['ccx', 0, 1, 2]], {'t_count': 7, 'clifford_count': 8}], ['control: random t-count 6', [['ccx', 0, 1, 2], ['x', 0], ['x', 0], ['rz', 0, '0'], ['sdg', 0], ['t', 0], ['ccx', 0, 1, 2]], {'t_count': 15, 'clifford_count': 19}]], [['regression: random t-count 29', [['sdg', 0], ['tdg', 0], ['rz', 0, '-1'], ['tdg', 0], ['rz', 0, '1/2'], ['rz', 0, '7/2'], ['ccx', 0, 1, 2]], {'t_count': 9, 'clifford_count': 12}], ['regression: random t-count 37', [['t', 0], ['rz', 0, '-1'], ['cx', 0], ['t', 0], ['rz', 0, '1/4'], ['rz', 0, '2'], ['tdg', 0]], {'t_count': 4, 'clifford_count': 2}], ['regression: random t-count 21', [['tdg', 0], ['rz', 0, '5/4']], {'t_count': 2, 'clifford_count': 0}], ['control: random t-count 8', [['rz', 0, '-1/4'], ['ccx', 0, 1, 2], ['ccx', 0, 1, 2], ['ccx', 0, 1, 2], ['cx', 0]], {'t_count': 22, 'clifford_count': 25}], ['control: random t-count 9', [['rz', 0, '1/4']], {'t_count': 1, 'clifford_count': 0}], ['control: random t-count 11', [['s', 0], ['rz', 0, '5/4'], ['rz', 0, '-1'], ['x', 0], ['t', 0]], {'t_count': 2, 'clifford_count': 3}], ['control: random t-count 13', [['rz', 0, '-1/4'], ['rz', 0, '7/2'], ['rz', 0, '3/4'], ['s', 0]], {'t_count': 2, 'clifford_count': 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):
    t = cliff = 0
    for idx, op in enumerate(x):
        name = op[0]
        if name in ('t',):
            t += 1
        elif name in ('h', 's', 'sdg', 'tdg', 'x', 'y', 'z', 'cx', 'cz', 'swap'):
            cliff += 1
        elif name == 'ccx':
            t += 7
            cliff += 8
        elif name == 'rz':
            a = Fraction(op[2]) % 2
            if a == 0:
                continue
            if (a * 2).denominator == 1:
                cliff += 1
            elif (a * 4).denominator == 1:
                t += 1
            else:
                return ['unsupported', idx]
        else:
            return ['unknown-gate', idx]
    return {'t_count': t, 'clifford_count': cliff}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: tdg', [['tdg', 0]], {'t_count': 1, 'clifford_count': 0}], ['regression: random t-count 5', [['ccx', 0, 1, 2], ['rz', 0, '1'], ['rz', 0, '5/4'], ['tdg', 0]], {'t_count': 9, 'clifford_count': 9}], ['regression: random t-count 7', [['rz', 0, '1/2'], ['tdg', 0], ['rz', 0, '1'], ['h', 0]], {'t_count': 1, 'clifford_count': 3}], ['control: rz two pi', [['rz', 0, '2']], {'t_count': 0, 'clifford_count': 0}], ['control: rz zero', [['rz', 0, '0']], {'t_count': 0, 'clifford_count': 0}], ['control: rz pi is Z', [['rz', 0, '1']], {'t_count': 0, 'clifford_count': 1}], ['control: rz half pi is S', [['rz', 0, '1/2']], {'t_count': 0, 'clifford_count': 1}]], [['regression: random t-count 10', [['ccx', 0, 1, 2], ['s', 0], ['ccx', 0, 1, 2], ['tdg', 0]], {'t_count': 15, 'clifford_count': 17}], ['regression: random t-count 12', [['t', 0], ['tdg', 0], ['rz', 0, '-1']], {'t_count': 2, 'clifford_count': 1}], ['regression: random t-count 7', [['rz', 0, '1/2'], ['tdg', 0], ['rz', 0, '1'], ['h', 0]], {'t_count': 1, 'clifford_count': 3}], ['control: rz quarter pi', [['rz', 0, '1/4']], {'t_count': 1, 'clifford_count': 0}], ['control: toffoli', [['ccx', 0, 1, 2]], {'t_count': 7, 'clifford_count': 8}], ['control: unsupported angle', [['h', 0], ['rz', 0, '1/8']], ['unsupported', 1]], ['control: negative angles', [['rz', 0, '-1/4'], ['rz', 1, '-3/2']], {'t_count': 1, 'clifford_count': 1}]], [['regression: random t-count 17', [['tdg', 0]], {'t_count': 1, 'clifford_count': 0}], ['regression: random t-count 20', [['cx', 0], ['rz', 0, '-1'], ['ccx', 0, 1, 2], ['tdg', 0], ['rz', 0, '1/2'], ['t', 0]], {'t_count': 9, 'clifford_count': 11}], ['regression: random t-count 12', [['t', 0], ['tdg', 0], ['rz', 0, '-1']], {'t_count': 2, 'clifford_count': 1}], ['control: rz four pi', [['rz', 0, '4']], {'t_count': 0, 'clifford_count': 0}], ['control: unknown gate', [['u3', 0]], ['unknown-gate', 0]], ['control: random t-count 0', [['rz', 0, '-1/4'], ['rz', 0, '3/4'], ['rz', 0, '4']], {'t_count': 2, 'clifford_count': 0}], ['control: random t-count 1', [['rz', 0, '-2'], ['ccx', 0, 1, 2], ['rz', 0, '1'], ['rz', 0, '1'], ['rz', 0, '0'], ['x', 0]], {'t_count': 7, 'clifford_count': 11}]], [['regression: random t-count 22', [['tdg', 0], ['tdg', 0], ['tdg', 0], ['rz', 0, '-1/4'], ['x', 0]], {'t_count': 4, 'clifford_count': 1}], ['regression: random t-count 23', [['rz', 0, '-2'], ['tdg', 0], ['ccx', 0, 1, 2], ['t', 0], ['rz', 0, '4'], ['rz', 0, '-1']], {'t_count': 9, 'clifford_count': 9}], ['regression: random t-count 17', [['tdg', 0]], {'t_count': 1, 'clifford_count': 0}], ['control: random t-count 2', [['sdg', 0], ['rz', 0, '1/4'], ['cx', 0]], {'t_count': 1, 'clifford_count': 2}], ['control: random t-count 3', [['rz', 0, '1/2'], ['rz', 0, '0'], ['t', 0]], {'t_count': 1, 'clifford_count': 1}], ['control: random t-count 4', [['ccx', 0, 1, 2]], {'t_count': 7, 'clifford_count': 8}], ['control: random t-count 6', [['ccx', 0, 1, 2], ['x', 0], ['x', 0], ['rz', 0, '0'], ['sdg', 0], ['t', 0], ['ccx', 0, 1, 2]], {'t_count': 15, 'clifford_count': 19}]], [['regression: random t-count 29', [['sdg', 0], ['tdg', 0], ['rz', 0, '-1'], ['tdg', 0], ['rz', 0, '1/2'], ['rz', 0, '7/2'], ['ccx', 0, 1, 2]], {'t_count': 9, 'clifford_count': 12}], ['regression: random t-count 37', [['t', 0], ['rz', 0, '-1'], ['cx', 0], ['t', 0], ['rz', 0, '1/4'], ['rz', 0, '2'], ['tdg', 0]], {'t_count': 4, 'clifford_count': 2}], ['regression: random t-count 21', [['tdg', 0], ['rz', 0, '5/4']], {'t_count': 2, 'clifford_count': 0}], ['control: random t-count 8', [['rz', 0, '-1/4'], ['ccx', 0, 1, 2], ['ccx', 0, 1, 2], ['ccx', 0, 1, 2], ['cx', 0]], {'t_count': 22, 'clifford_count': 25}], ['control: random t-count 9', [['rz', 0, '1/4']], {'t_count': 1, 'clifford_count': 0}], ['control: random t-count 11', [['s', 0], ['rz', 0, '5/4'], ['rz', 0, '-1'], ['x', 0], ['t', 0]], {'t_count': 2, 'clifford_count': 3}], ['control: random t-count 13', [['rz', 0, '-1/4'], ['rz', 0, '7/2'], ['rz', 0, '3/4'], ['s', 0]], {'t_count': 2, 'clifford_count': 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:
A circuit containing tdg is rejected as ["unknown-gate", i].

Input a gate list. t and tdg count one T each; h, s, sdg, x, y, z, cx, cz, swap count one Clifford; ccx costs 7 T and 8 Clifford; ["rz", q, a] with a in units of pi reduced mod 2 is skipped if 0, Clifford if a multiple of 1/2, one T if an odd multiple of 1/4, else ["unsupported", index]; other names give ["unknown-gate", index]. Return {"t_count", "clifford_count"}.

split: open-access