broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
import cmath
N = 1
observations = []
def solve(x):
    n, j, inverse = x
    dim = 1 << n
    seq = []
    for q in reversed(range(n)):
        seq.append(('h', q))
        for k in reversed(range(q)):
            seq.append(('cp', k, q, math.pi / 2 ** (q - k + 1)))
    for q in range(n // 2):
        seq.append(('swap', q, n - 1 - q))
    if inverse:
        seq = [(g[0], g[1], g[2], -g[3]) if g[0] == 'cp' else g for g in reversed(seq)]
    st = [0j] * dim
    st[j] = 1 + 0j
    r = 1 / math.sqrt(2)
    for g in seq:
        if g[0] == 'h':
            m = 1 << g[1]
            for i in range(dim):
                if not i & m:
                    a, b = st[i], st[i | m]
                    st[i], st[i | m] = (a + b) * r, (a - b) * r
        elif g[0] == 'cp':
            mask = (1 << g[1]) | (1 << g[2])
            ph = cmath.exp(1j * g[3])
            for i in range(dim):
                if i & mask == mask:
                    st[i] *= ph
        else:
            a, b = 1 << g[1], 1 << g[2]
            for i in range(dim):
                if i & a and not i & b:
                    k2 = (i ^ a) | b
                    st[i], st[k2] = st[k2], st[i]
    return [[round(v.real, 6) + 0.0, round(v.imag, 6) + 0.0] for v in st]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: qft n=2 j=1', [2, 1, False], [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]], ['regression: qft n=2 j=1 inverse', [2, 1, True], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['regression: qft n=2 j=3', [2, 3, False], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['control: qft n=1 j=0', [1, 0, False], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=1 j=1', [1, 1, False], [[0.707107, 0.0], [-0.707107, 0.0]]], ['control: qft n=1 j=1 inverse', [1, 1, True], [[0.707107, 0.0], [-0.707107, 0.0]]]], [['regression: qft n=2 j=3 inverse', [2, 3, True], [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]], ['regression: qft n=3 j=1', [3, 1, False], [[0.353553, 0.0], [0.25, 0.25], [0.0, 0.353553], [-0.25, 0.25], [-0.353553, 0.0], [-0.25, -0.25], [0.0, -0.353553], [0.25, -0.25]]], ['regression: qft n=2 j=3', [2, 3, False], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['control: qft n=2 j=0', [2, 0, False], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=2 j=0 inverse', [2, 0, True], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=2 j=2', [2, 2, False], [[0.5, 0.0], [-0.5, 0.0], [0.5, 0.0], [-0.5, 0.0]]], ['control: qft n=2 j=2 inverse', [2, 2, True], [[0.5, 0.0], [-0.5, 0.0], [0.5, 0.0], [-0.5, 0.0]]]], [['regression: qft n=3 j=2', [3, 2, False], [[0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553], [0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553]]], ['regression: qft n=3 j=2 inverse', [3, 2, True], [[0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553], [0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553]]], ['regression: qft n=3 j=1', [3, 1, False], [[0.353553, 0.0], [0.25, 0.25], [0.0, 0.353553], [-0.25, 0.25], [-0.353553, 0.0], [-0.25, -0.25], [0.0, -0.353553], [0.25, -0.25]]], ['control: qft n=3 j=0', [3, 0, False], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=3 j=0 inverse', [3, 0, True], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=3 j=4', [3, 4, False], [[0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0]]], ['control: qft n=3 j=4 inverse', [3, 4, True], [[0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0]]]], [['regression: qft n=3 j=3 inverse', [3, 3, True], [[0.353553, 0.0], [-0.25, -0.25], [0.0, 0.353553], [0.25, -0.25], [-0.353553, 0.0], [0.25, 0.25], [0.0, -0.353553], [-0.25, 0.25]]], ['regression: qft n=3 j=5', [3, 5, False], [[0.353553, 0.0], [-0.25, -0.25], [0.0, 0.353553], [0.25, -0.25], [-0.353553, 0.0], [0.25, 0.25], [0.0, -0.353553], [-0.25, 0.25]]], ['regression: qft n=3 j=2', [3, 2, False], [[0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553], [0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553]]], ['control: qft n=4 j=0', [4, 0, False], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]], ['control: qft n=4 j=0 inverse', [4, 0, True], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]], ['control: qft n=1 j=0', [1, 0, False], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]]], [['regression: qft n=3 j=6', [3, 6, False], [[0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553], [0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553]]], ['regression: qft n=3 j=6 inverse', [3, 6, True], [[0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553], [0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553]]], ['regression: qft n=3 j=3', [3, 3, False], [[0.353553, 0.0], [-0.25, 0.25], [0.0, -0.353553], [0.25, 0.25], [-0.353553, 0.0], [0.25, -0.25], [0.0, 0.353553], [-0.25, -0.25]]], ['control: qft n=1 j=1', [1, 1, False], [[0.707107, 0.0], [-0.707107, 0.0]]], ['control: qft n=1 j=1 inverse', [1, 1, True], [[0.707107, 0.0], [-0.707107, 0.0]]], ['control: qft n=2 j=0', [2, 0, False], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=2 j=0 inverse', [2, 0, True], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
import cmath
N = 1
observations = []
def solve(x):
    n, j, inverse = x
    dim = 1 << n
    seq = []
    for q in reversed(range(n)):
        seq.append(('h', q))
        for k in reversed(range(q)):
            seq.append(('cp', k, q, math.pi / 2 ** (q - k - 1)))
    for q in range(n // 2):
        seq.append(('swap', q, n - 1 - q))
    if inverse:
        seq = [(g[0], g[1], g[2], -g[3]) if g[0] == 'cp' else g for g in reversed(seq)]
    st = [0j] * dim
    st[j] = 1 + 0j
    r = 1 / math.sqrt(2)
    for g in seq:
        if g[0] == 'h':
            m = 1 << g[1]
            for i in range(dim):
                if not i & m:
                    a, b = st[i], st[i | m]
                    st[i], st[i | m] = (a + b) * r, (a - b) * r
        elif g[0] == 'cp':
            mask = (1 << g[1]) | (1 << g[2])
            ph = cmath.exp(1j * g[3])
            for i in range(dim):
                if i & mask == mask:
                    st[i] *= ph
        else:
            a, b = 1 << g[1], 1 << g[2]
            for i in range(dim):
                if i & a and not i & b:
                    k2 = (i ^ a) | b
                    st[i], st[k2] = st[k2], st[i]
    return [[round(v.real, 6) + 0.0, round(v.imag, 6) + 0.0] for v in st]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: qft n=2 j=1', [2, 1, False], [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]], ['regression: qft n=2 j=1 inverse', [2, 1, True], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['regression: qft n=2 j=3', [2, 3, False], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['control: qft n=1 j=0', [1, 0, False], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=1 j=1', [1, 1, False], [[0.707107, 0.0], [-0.707107, 0.0]]], ['control: qft n=1 j=1 inverse', [1, 1, True], [[0.707107, 0.0], [-0.707107, 0.0]]]], [['regression: qft n=2 j=3 inverse', [2, 3, True], [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]], ['regression: qft n=3 j=1', [3, 1, False], [[0.353553, 0.0], [0.25, 0.25], [0.0, 0.353553], [-0.25, 0.25], [-0.353553, 0.0], [-0.25, -0.25], [0.0, -0.353553], [0.25, -0.25]]], ['regression: qft n=2 j=3', [2, 3, False], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['control: qft n=2 j=0', [2, 0, False], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=2 j=0 inverse', [2, 0, True], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=2 j=2', [2, 2, False], [[0.5, 0.0], [-0.5, 0.0], [0.5, 0.0], [-0.5, 0.0]]], ['control: qft n=2 j=2 inverse', [2, 2, True], [[0.5, 0.0], [-0.5, 0.0], [0.5, 0.0], [-0.5, 0.0]]]], [['regression: qft n=3 j=2', [3, 2, False], [[0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553], [0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553]]], ['regression: qft n=3 j=2 inverse', [3, 2, True], [[0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553], [0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553]]], ['regression: qft n=3 j=1', [3, 1, False], [[0.353553, 0.0], [0.25, 0.25], [0.0, 0.353553], [-0.25, 0.25], [-0.353553, 0.0], [-0.25, -0.25], [0.0, -0.353553], [0.25, -0.25]]], ['control: qft n=3 j=0', [3, 0, False], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=3 j=0 inverse', [3, 0, True], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=3 j=4', [3, 4, False], [[0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0]]], ['control: qft n=3 j=4 inverse', [3, 4, True], [[0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0]]]], [['regression: qft n=3 j=3 inverse', [3, 3, True], [[0.353553, 0.0], [-0.25, -0.25], [0.0, 0.353553], [0.25, -0.25], [-0.353553, 0.0], [0.25, 0.25], [0.0, -0.353553], [-0.25, 0.25]]], ['regression: qft n=3 j=5', [3, 5, False], [[0.353553, 0.0], [-0.25, -0.25], [0.0, 0.353553], [0.25, -0.25], [-0.353553, 0.0], [0.25, 0.25], [0.0, -0.353553], [-0.25, 0.25]]], ['regression: qft n=3 j=2', [3, 2, False], [[0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553], [0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553]]], ['control: qft n=4 j=0', [4, 0, False], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]], ['control: qft n=4 j=0 inverse', [4, 0, True], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]], ['control: qft n=1 j=0', [1, 0, False], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]]], [['regression: qft n=3 j=6', [3, 6, False], [[0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553], [0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553]]], ['regression: qft n=3 j=6 inverse', [3, 6, True], [[0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553], [0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553]]], ['regression: qft n=3 j=3', [3, 3, False], [[0.353553, 0.0], [-0.25, 0.25], [0.0, -0.353553], [0.25, 0.25], [-0.353553, 0.0], [0.25, -0.25], [0.0, 0.353553], [-0.25, -0.25]]], ['control: qft n=1 j=1', [1, 1, False], [[0.707107, 0.0], [-0.707107, 0.0]]], ['control: qft n=1 j=1 inverse', [1, 1, True], [[0.707107, 0.0], [-0.707107, 0.0]]], ['control: qft n=2 j=0', [2, 0, False], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=2 j=0 inverse', [2, 0, True], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
QFT of |1> on two qubits produces phases at multiples of 45 degrees instead of 90 degrees.

Input [n, j, inverse]. Build the textbook QFT on n qubits (qubit 0 = LSB): for q from n-1 down to 0 apply H(q) then CP(pi / 2**(q-k)) between q and each k < q (k descending), then swap q with n-1-q for q < n//2; the inverse is the reversed sequence with negated phases. Apply it to |j> and return the amplitudes as [re, im] rounded to 6 decimals (QFT|j> = sum_k e^{2 pi i jk/N}|k>/sqrt N).

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
import cmath
N = 1
observations = []
def solve(x):
    n, j, inverse = x
    dim = 1 << n
    seq = []
    for q in reversed(range(n)):
        seq.append(('h', q))
        for k in reversed(range(q)):
            seq.append(('cp', k, q, math.pi / 2 ** (q - k)))
    for q in range(0):
        seq.append(('swap', q, n - 1 - q))
    if inverse:
        seq = [(g[0], g[1], g[2], -g[3]) if g[0] == 'cp' else g for g in reversed(seq)]
    st = [0j] * dim
    st[j] = 1 + 0j
    r = 1 / math.sqrt(2)
    for g in seq:
        if g[0] == 'h':
            m = 1 << g[1]
            for i in range(dim):
                if not i & m:
                    a, b = st[i], st[i | m]
                    st[i], st[i | m] = (a + b) * r, (a - b) * r
        elif g[0] == 'cp':
            mask = (1 << g[1]) | (1 << g[2])
            ph = cmath.exp(1j * g[3])
            for i in range(dim):
                if i & mask == mask:
                    st[i] *= ph
        else:
            a, b = 1 << g[1], 1 << g[2]
            for i in range(dim):
                if i & a and not i & b:
                    k2 = (i ^ a) | b
                    st[i], st[k2] = st[k2], st[i]
    return [[round(v.real, 6) + 0.0, round(v.imag, 6) + 0.0] for v in st]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: qft n=2 j=1', [2, 1, False], [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]], ['regression: qft n=2 j=1 inverse', [2, 1, True], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['regression: qft n=2 j=2', [2, 2, False], [[0.5, 0.0], [-0.5, 0.0], [0.5, 0.0], [-0.5, 0.0]]], ['control: qft n=1 j=0', [1, 0, False], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=1 j=1', [1, 1, False], [[0.707107, 0.0], [-0.707107, 0.0]]], ['control: qft n=1 j=1 inverse', [1, 1, True], [[0.707107, 0.0], [-0.707107, 0.0]]]], [['regression: qft n=2 j=2 inverse', [2, 2, True], [[0.5, 0.0], [-0.5, 0.0], [0.5, 0.0], [-0.5, 0.0]]], ['regression: qft n=2 j=3', [2, 3, False], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['regression: qft n=2 j=2', [2, 2, False], [[0.5, 0.0], [-0.5, 0.0], [0.5, 0.0], [-0.5, 0.0]]], ['control: qft n=2 j=0', [2, 0, False], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=2 j=0 inverse', [2, 0, True], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=2 j=3 inverse', [2, 3, True], [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]], ['control: qft n=3 j=0', [3, 0, False], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]]], [['regression: qft n=3 j=1 inverse', [3, 1, True], [[0.353553, 0.0], [0.25, -0.25], [0.0, -0.353553], [-0.25, -0.25], [-0.353553, 0.0], [-0.25, 0.25], [0.0, 0.353553], [0.25, 0.25]]], ['regression: qft n=3 j=2', [3, 2, False], [[0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553], [0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553]]], ['regression: qft n=2 j=3', [2, 3, False], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['control: qft n=3 j=0 inverse', [3, 0, True], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=3 j=2 inverse', [3, 2, True], [[0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553], [0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553]]], ['control: qft n=3 j=5 inverse', [3, 5, True], [[0.353553, 0.0], [-0.25, 0.25], [0.0, -0.353553], [0.25, 0.25], [-0.353553, 0.0], [0.25, -0.25], [0.0, 0.353553], [-0.25, -0.25]]], ['control: qft n=3 j=7 inverse', [3, 7, True], [[0.353553, 0.0], [0.25, 0.25], [0.0, 0.353553], [-0.25, 0.25], [-0.353553, 0.0], [-0.25, -0.25], [0.0, -0.353553], [0.25, -0.25]]]], [['regression: qft n=3 j=3 inverse', [3, 3, True], [[0.353553, 0.0], [-0.25, -0.25], [0.0, 0.353553], [0.25, -0.25], [-0.353553, 0.0], [0.25, 0.25], [0.0, -0.353553], [-0.25, 0.25]]], ['regression: qft n=3 j=4', [3, 4, False], [[0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0]]], ['regression: qft n=3 j=1 inverse', [3, 1, True], [[0.353553, 0.0], [0.25, -0.25], [0.0, -0.353553], [-0.25, -0.25], [-0.353553, 0.0], [-0.25, 0.25], [0.0, 0.353553], [0.25, 0.25]]], ['control: qft n=4 j=0', [4, 0, False], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]], ['control: qft n=4 j=0 inverse', [4, 0, True], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]], ['control: qft n=4 j=6 inverse', [4, 6, True], [[0.25, 0.0], [-0.176777, -0.176777], [0.0, 0.25], [0.176777, -0.176777], [-0.25, 0.0], [0.176777, 0.176777], [0.0, -0.25], [-0.176777, 0.176777], [0.25, 0.0], [-0.176777, -0.176777], [0.0, 0.25], [0.176777, -0.176777], [-0.25, 0.0], [0.176777, 0.176777], [0.0, -0.25], [-0.176777, 0.176777]]], ['control: qft n=4 j=9 inverse', [4, 9, True], [[0.25, 0.0], [-0.23097, 0.095671], [0.176777, -0.176777], [-0.095671, 0.23097], [0.0, -0.25], [0.095671, 0.23097], [-0.176777, -0.176777], [0.23097, 0.095671], [-0.25, 0.0], [0.23097, -0.095671], [-0.176777, 0.176777], [0.095671, -0.23097], [0.0, 0.25], [-0.095671, -0.23097], [0.176777, 0.176777], [-0.23097, -0.095671]]]], [['regression: qft n=3 j=5', [3, 5, False], [[0.353553, 0.0], [-0.25, -0.25], [0.0, 0.353553], [0.25, -0.25], [-0.353553, 0.0], [0.25, 0.25], [0.0, -0.353553], [-0.25, 0.25]]], ['regression: qft n=3 j=6', [3, 6, False], [[0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553], [0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553]]], ['regression: qft n=3 j=3', [3, 3, False], [[0.353553, 0.0], [-0.25, 0.25], [0.0, -0.353553], [0.25, 0.25], [-0.353553, 0.0], [0.25, -0.25], [0.0, 0.353553], [-0.25, -0.25]]], ['control: qft n=4 j=15 inverse', [4, 15, True], [[0.25, 0.0], [0.23097, 0.095671], [0.176777, 0.176777], [0.095671, 0.23097], [0.0, 0.25], [-0.095671, 0.23097], [-0.176777, 0.176777], [-0.23097, 0.095671], [-0.25, 0.0], [-0.23097, -0.095671], [-0.176777, -0.176777], [-0.095671, -0.23097], [0.0, -0.25], [0.095671, -0.23097], [0.176777, -0.176777], [0.23097, -0.095671]]], ['control: qft n=1 j=0', [1, 0, False], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=1 j=1', [1, 1, False], [[0.707107, 0.0], [-0.707107, 0.0]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
import cmath
N = 1
observations = []
def solve(x):
    n, j, inverse = x
    dim = 1 << n
    seq = []
    for q in reversed(range(n)):
        seq.append(('h', q))
        for k in reversed(range(q)):
            seq.append(('cp', k, q, math.pi / 2 ** (q - k)))
    for q in range(n):
        seq.append(('swap', q, n - 1 - q))
    if inverse:
        seq = [(g[0], g[1], g[2], -g[3]) if g[0] == 'cp' else g for g in reversed(seq)]
    st = [0j] * dim
    st[j] = 1 + 0j
    r = 1 / math.sqrt(2)
    for g in seq:
        if g[0] == 'h':
            m = 1 << g[1]
            for i in range(dim):
                if not i & m:
                    a, b = st[i], st[i | m]
                    st[i], st[i | m] = (a + b) * r, (a - b) * r
        elif g[0] == 'cp':
            mask = (1 << g[1]) | (1 << g[2])
            ph = cmath.exp(1j * g[3])
            for i in range(dim):
                if i & mask == mask:
                    st[i] *= ph
        else:
            a, b = 1 << g[1], 1 << g[2]
            for i in range(dim):
                if i & a and not i & b:
                    k2 = (i ^ a) | b
                    st[i], st[k2] = st[k2], st[i]
    return [[round(v.real, 6) + 0.0, round(v.imag, 6) + 0.0] for v in st]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: qft n=2 j=1', [2, 1, False], [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]], ['regression: qft n=2 j=1 inverse', [2, 1, True], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['regression: qft n=2 j=2', [2, 2, False], [[0.5, 0.0], [-0.5, 0.0], [0.5, 0.0], [-0.5, 0.0]]], ['control: qft n=1 j=0', [1, 0, False], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=1 j=1', [1, 1, False], [[0.707107, 0.0], [-0.707107, 0.0]]], ['control: qft n=1 j=1 inverse', [1, 1, True], [[0.707107, 0.0], [-0.707107, 0.0]]]], [['regression: qft n=2 j=2 inverse', [2, 2, True], [[0.5, 0.0], [-0.5, 0.0], [0.5, 0.0], [-0.5, 0.0]]], ['regression: qft n=2 j=3', [2, 3, False], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['regression: qft n=2 j=2', [2, 2, False], [[0.5, 0.0], [-0.5, 0.0], [0.5, 0.0], [-0.5, 0.0]]], ['control: qft n=2 j=0', [2, 0, False], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=2 j=0 inverse', [2, 0, True], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=2 j=3 inverse', [2, 3, True], [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]], ['control: qft n=3 j=0', [3, 0, False], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]]], [['regression: qft n=3 j=1 inverse', [3, 1, True], [[0.353553, 0.0], [0.25, -0.25], [0.0, -0.353553], [-0.25, -0.25], [-0.353553, 0.0], [-0.25, 0.25], [0.0, 0.353553], [0.25, 0.25]]], ['regression: qft n=3 j=2', [3, 2, False], [[0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553], [0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553]]], ['regression: qft n=2 j=3', [2, 3, False], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['control: qft n=3 j=0 inverse', [3, 0, True], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=3 j=2 inverse', [3, 2, True], [[0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553], [0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553]]], ['control: qft n=3 j=5 inverse', [3, 5, True], [[0.353553, 0.0], [-0.25, 0.25], [0.0, -0.353553], [0.25, 0.25], [-0.353553, 0.0], [0.25, -0.25], [0.0, 0.353553], [-0.25, -0.25]]], ['control: qft n=3 j=7 inverse', [3, 7, True], [[0.353553, 0.0], [0.25, 0.25], [0.0, 0.353553], [-0.25, 0.25], [-0.353553, 0.0], [-0.25, -0.25], [0.0, -0.353553], [0.25, -0.25]]]], [['regression: qft n=3 j=3 inverse', [3, 3, True], [[0.353553, 0.0], [-0.25, -0.25], [0.0, 0.353553], [0.25, -0.25], [-0.353553, 0.0], [0.25, 0.25], [0.0, -0.353553], [-0.25, 0.25]]], ['regression: qft n=3 j=4', [3, 4, False], [[0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0]]], ['regression: qft n=3 j=1 inverse', [3, 1, True], [[0.353553, 0.0], [0.25, -0.25], [0.0, -0.353553], [-0.25, -0.25], [-0.353553, 0.0], [-0.25, 0.25], [0.0, 0.353553], [0.25, 0.25]]], ['control: qft n=4 j=0', [4, 0, False], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]], ['control: qft n=4 j=0 inverse', [4, 0, True], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]], ['control: qft n=4 j=6 inverse', [4, 6, True], [[0.25, 0.0], [-0.176777, -0.176777], [0.0, 0.25], [0.176777, -0.176777], [-0.25, 0.0], [0.176777, 0.176777], [0.0, -0.25], [-0.176777, 0.176777], [0.25, 0.0], [-0.176777, -0.176777], [0.0, 0.25], [0.176777, -0.176777], [-0.25, 0.0], [0.176777, 0.176777], [0.0, -0.25], [-0.176777, 0.176777]]], ['control: qft n=4 j=9 inverse', [4, 9, True], [[0.25, 0.0], [-0.23097, 0.095671], [0.176777, -0.176777], [-0.095671, 0.23097], [0.0, -0.25], [0.095671, 0.23097], [-0.176777, -0.176777], [0.23097, 0.095671], [-0.25, 0.0], [0.23097, -0.095671], [-0.176777, 0.176777], [0.095671, -0.23097], [0.0, 0.25], [-0.095671, -0.23097], [0.176777, 0.176777], [-0.23097, -0.095671]]]], [['regression: qft n=3 j=5', [3, 5, False], [[0.353553, 0.0], [-0.25, -0.25], [0.0, 0.353553], [0.25, -0.25], [-0.353553, 0.0], [0.25, 0.25], [0.0, -0.353553], [-0.25, 0.25]]], ['regression: qft n=3 j=6', [3, 6, False], [[0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553], [0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553]]], ['regression: qft n=3 j=3', [3, 3, False], [[0.353553, 0.0], [-0.25, 0.25], [0.0, -0.353553], [0.25, 0.25], [-0.353553, 0.0], [0.25, -0.25], [0.0, 0.353553], [-0.25, -0.25]]], ['control: qft n=4 j=15 inverse', [4, 15, True], [[0.25, 0.0], [0.23097, 0.095671], [0.176777, 0.176777], [0.095671, 0.23097], [0.0, 0.25], [-0.095671, 0.23097], [-0.176777, 0.176777], [-0.23097, 0.095671], [-0.25, 0.0], [-0.23097, -0.095671], [-0.176777, -0.176777], [-0.095671, -0.23097], [0.0, -0.25], [0.095671, -0.23097], [0.176777, -0.176777], [0.23097, -0.095671]]], ['control: qft n=1 j=0', [1, 0, False], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=1 j=1', [1, 1, False], [[0.707107, 0.0], [-0.707107, 0.0]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
QFT|1> on three qubits returns amplitudes whose frequencies are indexed in bit-reversed order.

Input [n, j, inverse]. Build the textbook QFT on n qubits (qubit 0 = LSB): for q from n-1 down to 0 apply H(q) then CP(pi / 2**(q-k)) between q and each k < q (k descending), then swap q with n-1-q for q < n//2; the inverse is the reversed sequence with negated phases. Apply it to |j> and return the amplitudes as [re, im] rounded to 6 decimals (QFT|j> = sum_k e^{2 pi i jk/N}|k>/sqrt N).

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
import cmath
N = 1
observations = []
def solve(x):
    n, j, inverse = x
    dim = 1 << n
    seq = []
    for q in reversed(range(n)):
        seq.append(('h', q))
        for k in reversed(range(q)):
            seq.append(('cp', k, q, math.pi / 2 ** (q - k)))
    for q in range(n // 2):
        seq.append(('swap', q, n - 1 - q))
    if inverse:
        seq = list(reversed(seq))
    st = [0j] * dim
    st[j] = 1 + 0j
    r = 1 / math.sqrt(2)
    for g in seq:
        if g[0] == 'h':
            m = 1 << g[1]
            for i in range(dim):
                if not i & m:
                    a, b = st[i], st[i | m]
                    st[i], st[i | m] = (a + b) * r, (a - b) * r
        elif g[0] == 'cp':
            mask = (1 << g[1]) | (1 << g[2])
            ph = cmath.exp(1j * g[3])
            for i in range(dim):
                if i & mask == mask:
                    st[i] *= ph
        else:
            a, b = 1 << g[1], 1 << g[2]
            for i in range(dim):
                if i & a and not i & b:
                    k2 = (i ^ a) | b
                    st[i], st[k2] = st[k2], st[i]
    return [[round(v.real, 6) + 0.0, round(v.imag, 6) + 0.0] for v in st]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: qft n=2 j=1 inverse', [2, 1, True], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['regression: qft n=2 j=3 inverse', [2, 3, True], [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]], ['repair check: qft n=2 j=2 inverse', [2, 2, True], [[0.5, 0.0], [-0.5, 0.0], [0.5, 0.0], [-0.5, 0.0]]], ['control: qft n=1 j=0', [1, 0, False], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=1 j=1', [1, 1, False], [[0.707107, 0.0], [-0.707107, 0.0]]], ['control: qft n=1 j=1 inverse', [1, 1, True], [[0.707107, 0.0], [-0.707107, 0.0]]]], [['regression: qft n=3 j=2 inverse', [3, 2, True], [[0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553], [0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553]]], ['regression: qft n=3 j=3 inverse', [3, 3, True], [[0.353553, 0.0], [-0.25, -0.25], [0.0, 0.353553], [0.25, -0.25], [-0.353553, 0.0], [0.25, 0.25], [0.0, -0.353553], [-0.25, 0.25]]], ['regression: qft n=2 j=3 inverse', [2, 3, True], [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]], ['control: qft n=2 j=0', [2, 0, False], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=2 j=0 inverse', [2, 0, True], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=2 j=1', [2, 1, False], [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]], ['control: qft n=2 j=2', [2, 2, False], [[0.5, 0.0], [-0.5, 0.0], [0.5, 0.0], [-0.5, 0.0]]]], [['regression: qft n=3 j=6 inverse', [3, 6, True], [[0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553], [0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553]]], ['regression: qft n=3 j=7 inverse', [3, 7, True], [[0.353553, 0.0], [0.25, 0.25], [0.0, 0.353553], [-0.25, 0.25], [-0.353553, 0.0], [-0.25, -0.25], [0.0, -0.353553], [0.25, -0.25]]], ['regression: qft n=3 j=2 inverse', [3, 2, True], [[0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553], [0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553]]], ['control: qft n=2 j=3', [2, 3, False], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['control: qft n=3 j=0', [3, 0, False], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=3 j=0 inverse', [3, 0, True], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=3 j=1', [3, 1, False], [[0.353553, 0.0], [0.25, 0.25], [0.0, 0.353553], [-0.25, 0.25], [-0.353553, 0.0], [-0.25, -0.25], [0.0, -0.353553], [0.25, -0.25]]]], [['regression: qft n=4 j=6 inverse', [4, 6, True], [[0.25, 0.0], [-0.176777, -0.176777], [0.0, 0.25], [0.176777, -0.176777], [-0.25, 0.0], [0.176777, 0.176777], [0.0, -0.25], [-0.176777, 0.176777], [0.25, 0.0], [-0.176777, -0.176777], [0.0, 0.25], [0.176777, -0.176777], [-0.25, 0.0], [0.176777, 0.176777], [0.0, -0.25], [-0.176777, 0.176777]]], ['regression: qft n=4 j=9 inverse', [4, 9, True], [[0.25, 0.0], [-0.23097, 0.095671], [0.176777, -0.176777], [-0.095671, 0.23097], [0.0, -0.25], [0.095671, 0.23097], [-0.176777, -0.176777], [0.23097, 0.095671], [-0.25, 0.0], [0.23097, -0.095671], [-0.176777, 0.176777], [0.095671, -0.23097], [0.0, 0.25], [-0.095671, -0.23097], [0.176777, 0.176777], [-0.23097, -0.095671]]], ['repair check: qft n=3 j=4 inverse', [3, 4, True], [[0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0]]], ['control: qft n=3 j=2', [3, 2, False], [[0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553], [0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553]]], ['control: qft n=3 j=3', [3, 3, False], [[0.353553, 0.0], [-0.25, 0.25], [0.0, -0.353553], [0.25, 0.25], [-0.353553, 0.0], [0.25, -0.25], [0.0, 0.353553], [-0.25, -0.25]]], ['control: qft n=3 j=4', [3, 4, False], [[0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0]]], ['control: qft n=3 j=5', [3, 5, False], [[0.353553, 0.0], [-0.25, -0.25], [0.0, 0.353553], [0.25, -0.25], [-0.353553, 0.0], [0.25, 0.25], [0.0, -0.353553], [-0.25, 0.25]]]], [['regression: qft n=4 j=15 inverse', [4, 15, True], [[0.25, 0.0], [0.23097, 0.095671], [0.176777, 0.176777], [0.095671, 0.23097], [0.0, 0.25], [-0.095671, 0.23097], [-0.176777, 0.176777], [-0.23097, 0.095671], [-0.25, 0.0], [-0.23097, -0.095671], [-0.176777, -0.176777], [-0.095671, -0.23097], [0.0, -0.25], [0.095671, -0.23097], [0.176777, -0.176777], [0.23097, -0.095671]]], ['regression: qft n=2 j=1 inverse', [2, 1, True], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['regression: qft n=3 j=6 inverse', [3, 6, True], [[0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553], [0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553]]], ['control: qft n=3 j=6', [3, 6, False], [[0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553], [0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553]]], ['control: qft n=3 j=7', [3, 7, False], [[0.353553, 0.0], [0.25, -0.25], [0.0, -0.353553], [-0.25, -0.25], [-0.353553, 0.0], [-0.25, 0.25], [0.0, 0.353553], [0.25, 0.25]]], ['control: qft n=4 j=0', [4, 0, False], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]], ['control: qft n=4 j=0 inverse', [4, 0, True], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
import cmath
N = 1
observations = []
def solve(x):
    n, j, inverse = x
    dim = 1 << n
    seq = []
    for q in reversed(range(n)):
        seq.append(('h', q))
        for k in reversed(range(q)):
            seq.append(('cp', k, q, math.pi / 2 ** (q - k)))
    for q in range(n // 2):
        seq.append(('swap', q, n - 1 - q))
    if inverse:
        seq = [(g[0], g[1], g[2], -g[3]) if g[0] == 'cp' else g for g in reversed(seq) if g[0] != 'swap'] + [g for g in seq if g[0] == 'swap']
    st = [0j] * dim
    st[j] = 1 + 0j
    r = 1 / math.sqrt(2)
    for g in seq:
        if g[0] == 'h':
            m = 1 << g[1]
            for i in range(dim):
                if not i & m:
                    a, b = st[i], st[i | m]
                    st[i], st[i | m] = (a + b) * r, (a - b) * r
        elif g[0] == 'cp':
            mask = (1 << g[1]) | (1 << g[2])
            ph = cmath.exp(1j * g[3])
            for i in range(dim):
                if i & mask == mask:
                    st[i] *= ph
        else:
            a, b = 1 << g[1], 1 << g[2]
            for i in range(dim):
                if i & a and not i & b:
                    k2 = (i ^ a) | b
                    st[i], st[k2] = st[k2], st[i]
    return [[round(v.real, 6) + 0.0, round(v.imag, 6) + 0.0] for v in st]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: qft n=2 j=1 inverse', [2, 1, True], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['regression: qft n=2 j=3 inverse', [2, 3, True], [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]], ['repair check: qft n=2 j=2 inverse', [2, 2, True], [[0.5, 0.0], [-0.5, 0.0], [0.5, 0.0], [-0.5, 0.0]]], ['control: qft n=1 j=0', [1, 0, False], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=1 j=1', [1, 1, False], [[0.707107, 0.0], [-0.707107, 0.0]]], ['control: qft n=1 j=1 inverse', [1, 1, True], [[0.707107, 0.0], [-0.707107, 0.0]]]], [['regression: qft n=3 j=2 inverse', [3, 2, True], [[0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553], [0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553]]], ['regression: qft n=3 j=3 inverse', [3, 3, True], [[0.353553, 0.0], [-0.25, -0.25], [0.0, 0.353553], [0.25, -0.25], [-0.353553, 0.0], [0.25, 0.25], [0.0, -0.353553], [-0.25, 0.25]]], ['regression: qft n=2 j=3 inverse', [2, 3, True], [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]], ['control: qft n=2 j=0', [2, 0, False], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=2 j=0 inverse', [2, 0, True], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=2 j=1', [2, 1, False], [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]], ['control: qft n=2 j=2', [2, 2, False], [[0.5, 0.0], [-0.5, 0.0], [0.5, 0.0], [-0.5, 0.0]]]], [['regression: qft n=3 j=6 inverse', [3, 6, True], [[0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553], [0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553]]], ['regression: qft n=3 j=7 inverse', [3, 7, True], [[0.353553, 0.0], [0.25, 0.25], [0.0, 0.353553], [-0.25, 0.25], [-0.353553, 0.0], [-0.25, -0.25], [0.0, -0.353553], [0.25, -0.25]]], ['regression: qft n=3 j=2 inverse', [3, 2, True], [[0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553], [0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553]]], ['control: qft n=2 j=3', [2, 3, False], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['control: qft n=3 j=0', [3, 0, False], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=3 j=0 inverse', [3, 0, True], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=3 j=1', [3, 1, False], [[0.353553, 0.0], [0.25, 0.25], [0.0, 0.353553], [-0.25, 0.25], [-0.353553, 0.0], [-0.25, -0.25], [0.0, -0.353553], [0.25, -0.25]]]], [['regression: qft n=4 j=6 inverse', [4, 6, True], [[0.25, 0.0], [-0.176777, -0.176777], [0.0, 0.25], [0.176777, -0.176777], [-0.25, 0.0], [0.176777, 0.176777], [0.0, -0.25], [-0.176777, 0.176777], [0.25, 0.0], [-0.176777, -0.176777], [0.0, 0.25], [0.176777, -0.176777], [-0.25, 0.0], [0.176777, 0.176777], [0.0, -0.25], [-0.176777, 0.176777]]], ['regression: qft n=4 j=9 inverse', [4, 9, True], [[0.25, 0.0], [-0.23097, 0.095671], [0.176777, -0.176777], [-0.095671, 0.23097], [0.0, -0.25], [0.095671, 0.23097], [-0.176777, -0.176777], [0.23097, 0.095671], [-0.25, 0.0], [0.23097, -0.095671], [-0.176777, 0.176777], [0.095671, -0.23097], [0.0, 0.25], [-0.095671, -0.23097], [0.176777, 0.176777], [-0.23097, -0.095671]]], ['repair check: qft n=3 j=4 inverse', [3, 4, True], [[0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0]]], ['control: qft n=3 j=2', [3, 2, False], [[0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553], [0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553]]], ['control: qft n=3 j=3', [3, 3, False], [[0.353553, 0.0], [-0.25, 0.25], [0.0, -0.353553], [0.25, 0.25], [-0.353553, 0.0], [0.25, -0.25], [0.0, 0.353553], [-0.25, -0.25]]], ['control: qft n=3 j=4', [3, 4, False], [[0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0], [0.353553, 0.0], [-0.353553, 0.0]]], ['control: qft n=3 j=5', [3, 5, False], [[0.353553, 0.0], [-0.25, -0.25], [0.0, 0.353553], [0.25, -0.25], [-0.353553, 0.0], [0.25, 0.25], [0.0, -0.353553], [-0.25, 0.25]]]], [['regression: qft n=4 j=15 inverse', [4, 15, True], [[0.25, 0.0], [0.23097, 0.095671], [0.176777, 0.176777], [0.095671, 0.23097], [0.0, 0.25], [-0.095671, 0.23097], [-0.176777, 0.176777], [-0.23097, 0.095671], [-0.25, 0.0], [-0.23097, -0.095671], [-0.176777, -0.176777], [-0.095671, -0.23097], [0.0, -0.25], [0.095671, -0.23097], [0.176777, -0.176777], [0.23097, -0.095671]]], ['regression: qft n=2 j=1 inverse', [2, 1, True], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['regression: qft n=3 j=6 inverse', [3, 6, True], [[0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553], [0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553]]], ['control: qft n=3 j=6', [3, 6, False], [[0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553], [0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553]]], ['control: qft n=3 j=7', [3, 7, False], [[0.353553, 0.0], [0.25, -0.25], [0.0, -0.353553], [-0.25, -0.25], [-0.353553, 0.0], [-0.25, 0.25], [0.0, 0.353553], [0.25, 0.25]]], ['control: qft n=4 j=0', [4, 0, False], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]], ['control: qft n=4 j=0 inverse', [4, 0, True], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
The inverse QFT applied to |j> is not the conjugate spectrum; round trips do not return to the input.

Input [n, j, inverse]. Build the textbook QFT on n qubits (qubit 0 = LSB): for q from n-1 down to 0 apply H(q) then CP(pi / 2**(q-k)) between q and each k < q (k descending), then swap q with n-1-q for q < n//2; the inverse is the reversed sequence with negated phases. Apply it to |j> and return the amplitudes as [re, im] rounded to 6 decimals (QFT|j> = sum_k e^{2 pi i jk/N}|k>/sqrt N).

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
import cmath
N = 1
observations = []
def solve(x):
    n, j, inverse = x
    dim = 1 << n
    seq = []
    for q in reversed(range(n)):
        seq.append(('h', q))
        for k in reversed(range(q + 1)):
            seq.append(('cp', k, q, math.pi / 2 ** (q - k)))
    for q in range(n // 2):
        seq.append(('swap', q, n - 1 - q))
    if inverse:
        seq = [(g[0], g[1], g[2], -g[3]) if g[0] == 'cp' else g for g in reversed(seq)]
    st = [0j] * dim
    st[j] = 1 + 0j
    r = 1 / math.sqrt(2)
    for g in seq:
        if g[0] == 'h':
            m = 1 << g[1]
            for i in range(dim):
                if not i & m:
                    a, b = st[i], st[i | m]
                    st[i], st[i | m] = (a + b) * r, (a - b) * r
        elif g[0] == 'cp':
            mask = (1 << g[1]) | (1 << g[2])
            ph = cmath.exp(1j * g[3])
            for i in range(dim):
                if i & mask == mask:
                    st[i] *= ph
        else:
            a, b = 1 << g[1], 1 << g[2]
            for i in range(dim):
                if i & a and not i & b:
                    k2 = (i ^ a) | b
                    st[i], st[k2] = st[k2], st[i]
    return [[round(v.real, 6) + 0.0, round(v.imag, 6) + 0.0] for v in st]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: qft n=1 j=0', [1, 0, False], [[0.707107, 0.0], [0.707107, 0.0]]], ['regression: qft n=1 j=1', [1, 1, False], [[0.707107, 0.0], [-0.707107, 0.0]]], ['regression: qft n=2 j=1', [2, 1, False], [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=2 j=0 inverse', [2, 0, True], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=3 j=0 inverse', [3, 0, True], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=4 j=0 inverse', [4, 0, True], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]]], [['regression: qft n=2 j=0', [2, 0, False], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['regression: qft n=2 j=1', [2, 1, False], [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]], ['regression: qft n=2 j=3', [2, 3, False], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=2 j=0 inverse', [2, 0, True], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=3 j=0 inverse', [3, 0, True], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=4 j=0 inverse', [4, 0, True], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]]], [['regression: qft n=2 j=2', [2, 2, False], [[0.5, 0.0], [-0.5, 0.0], [0.5, 0.0], [-0.5, 0.0]]], ['regression: qft n=2 j=2 inverse', [2, 2, True], [[0.5, 0.0], [-0.5, 0.0], [0.5, 0.0], [-0.5, 0.0]]], ['regression: qft n=3 j=1', [3, 1, False], [[0.353553, 0.0], [0.25, 0.25], [0.0, 0.353553], [-0.25, 0.25], [-0.353553, 0.0], [-0.25, -0.25], [0.0, -0.353553], [0.25, -0.25]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=2 j=0 inverse', [2, 0, True], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=3 j=0 inverse', [3, 0, True], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=4 j=0 inverse', [4, 0, True], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]]], [['regression: qft n=3 j=0', [3, 0, False], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['regression: qft n=3 j=1', [3, 1, False], [[0.353553, 0.0], [0.25, 0.25], [0.0, 0.353553], [-0.25, 0.25], [-0.353553, 0.0], [-0.25, -0.25], [0.0, -0.353553], [0.25, -0.25]]], ['regression: qft n=3 j=2 inverse', [3, 2, True], [[0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553], [0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=2 j=0 inverse', [2, 0, True], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=3 j=0 inverse', [3, 0, True], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=4 j=0 inverse', [4, 0, True], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]]], [['regression: qft n=3 j=2', [3, 2, False], [[0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553], [0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553]]], ['regression: qft n=3 j=2 inverse', [3, 2, True], [[0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553], [0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553]]], ['repair check: qft n=3 j=3 inverse', [3, 3, True], [[0.353553, 0.0], [-0.25, -0.25], [0.0, 0.353553], [0.25, -0.25], [-0.353553, 0.0], [0.25, 0.25], [0.0, -0.353553], [-0.25, 0.25]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=2 j=0 inverse', [2, 0, True], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=3 j=0 inverse', [3, 0, True], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=4 j=0 inverse', [4, 0, True], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
import cmath
N = 1
observations = []
def solve(x):
    n, j, inverse = x
    dim = 1 << n
    seq = []
    for q in reversed(range(n)):
        seq.append(('h', q))
        for k in reversed(range(1, q)):
            seq.append(('cp', k, q, math.pi / 2 ** (q - k)))
    for q in range(n // 2):
        seq.append(('swap', q, n - 1 - q))
    if inverse:
        seq = [(g[0], g[1], g[2], -g[3]) if g[0] == 'cp' else g for g in reversed(seq)]
    st = [0j] * dim
    st[j] = 1 + 0j
    r = 1 / math.sqrt(2)
    for g in seq:
        if g[0] == 'h':
            m = 1 << g[1]
            for i in range(dim):
                if not i & m:
                    a, b = st[i], st[i | m]
                    st[i], st[i | m] = (a + b) * r, (a - b) * r
        elif g[0] == 'cp':
            mask = (1 << g[1]) | (1 << g[2])
            ph = cmath.exp(1j * g[3])
            for i in range(dim):
                if i & mask == mask:
                    st[i] *= ph
        else:
            a, b = 1 << g[1], 1 << g[2]
            for i in range(dim):
                if i & a and not i & b:
                    k2 = (i ^ a) | b
                    st[i], st[k2] = st[k2], st[i]
    return [[round(v.real, 6) + 0.0, round(v.imag, 6) + 0.0] for v in st]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: qft n=1 j=0', [1, 0, False], [[0.707107, 0.0], [0.707107, 0.0]]], ['regression: qft n=1 j=1', [1, 1, False], [[0.707107, 0.0], [-0.707107, 0.0]]], ['regression: qft n=2 j=1', [2, 1, False], [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=2 j=0 inverse', [2, 0, True], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=3 j=0 inverse', [3, 0, True], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=4 j=0 inverse', [4, 0, True], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]]], [['regression: qft n=2 j=0', [2, 0, False], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['regression: qft n=2 j=1', [2, 1, False], [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]], ['regression: qft n=2 j=3', [2, 3, False], [[0.5, 0.0], [0.0, -0.5], [-0.5, 0.0], [0.0, 0.5]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=2 j=0 inverse', [2, 0, True], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=3 j=0 inverse', [3, 0, True], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=4 j=0 inverse', [4, 0, True], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]]], [['regression: qft n=2 j=2', [2, 2, False], [[0.5, 0.0], [-0.5, 0.0], [0.5, 0.0], [-0.5, 0.0]]], ['regression: qft n=2 j=2 inverse', [2, 2, True], [[0.5, 0.0], [-0.5, 0.0], [0.5, 0.0], [-0.5, 0.0]]], ['regression: qft n=3 j=1', [3, 1, False], [[0.353553, 0.0], [0.25, 0.25], [0.0, 0.353553], [-0.25, 0.25], [-0.353553, 0.0], [-0.25, -0.25], [0.0, -0.353553], [0.25, -0.25]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=2 j=0 inverse', [2, 0, True], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=3 j=0 inverse', [3, 0, True], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=4 j=0 inverse', [4, 0, True], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]]], [['regression: qft n=3 j=0', [3, 0, False], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['regression: qft n=3 j=1', [3, 1, False], [[0.353553, 0.0], [0.25, 0.25], [0.0, 0.353553], [-0.25, 0.25], [-0.353553, 0.0], [-0.25, -0.25], [0.0, -0.353553], [0.25, -0.25]]], ['regression: qft n=3 j=2 inverse', [3, 2, True], [[0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553], [0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=2 j=0 inverse', [2, 0, True], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=3 j=0 inverse', [3, 0, True], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=4 j=0 inverse', [4, 0, True], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]]], [['regression: qft n=3 j=2', [3, 2, False], [[0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553], [0.353553, 0.0], [0.0, 0.353553], [-0.353553, 0.0], [0.0, -0.353553]]], ['regression: qft n=3 j=2 inverse', [3, 2, True], [[0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553], [0.353553, 0.0], [0.0, -0.353553], [-0.353553, 0.0], [0.0, 0.353553]]], ['repair check: qft n=3 j=3 inverse', [3, 3, True], [[0.353553, 0.0], [-0.25, -0.25], [0.0, 0.353553], [0.25, -0.25], [-0.353553, 0.0], [0.25, 0.25], [0.0, -0.353553], [-0.25, 0.25]]], ['control: qft n=1 j=0 inverse', [1, 0, True], [[0.707107, 0.0], [0.707107, 0.0]]], ['control: qft n=2 j=0 inverse', [2, 0, True], [[0.5, 0.0], [0.5, 0.0], [0.5, 0.0], [0.5, 0.0]]], ['control: qft n=3 j=0 inverse', [3, 0, True], [[0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0], [0.353553, 0.0]]], ['control: qft n=4 j=0 inverse', [4, 0, True], [[0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0], [0.25, 0.0]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Every qubit gets an extra Z after its Hadamard, negating half of the spectrum.

Input [n, j, inverse]. Build the textbook QFT on n qubits (qubit 0 = LSB): for q from n-1 down to 0 apply H(q) then CP(pi / 2**(q-k)) between q and each k < q (k descending), then swap q with n-1-q for q < n//2; the inverse is the reversed sequence with negated phases. Apply it to |j> and return the amplitudes as [re, im] rounded to 6 decimals (QFT|j> = sum_k e^{2 pi i jk/N}|k>/sqrt N).

split: open-access


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

N = 1
observations = []
def solve(x):
    nq, nc, ops = x
    q = [0] * nq
    c = [0] * nc
    for op in ops:
        name = op[0]
        if name == 'x':
            q[op[1]] ^= 1
        elif name == 'cx':
            if q[op[1]]:
                q[op[2]] ^= 1
        elif name == 'ccx':
            if q[op[1]] and q[op[2]]:
                q[op[3]] ^= 1
        elif name == 'swap':
            a, b = op[1], op[2]
            q[a], q[b] = q[b], q[a]
        elif name == 'measure':
            _, qb, cb = op
            c[cb] = q[qb]
        elif name == 'reset':
            q[op[1]] = 0
        elif name == 'x_if':
            creg = int(''.join(str(bit) for bit in c), 2)
            if creg == op[2]:
                q[op[1]] ^= 1
    return {'qubits': ''.join(str(b) for b in reversed(q)), 'clbits': ''.join(str(b) for b in reversed(c))}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: feed-forward on register value 2', [2, 2, [['x', 1], ['measure', 1, 1], ['x_if', 0, 2]]], {'qubits': '11', 'clbits': '10'}], ['regression: random dynamic 59', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 0], ['x_if', 0, 1], ['reset', 2]]], {'qubits': '011', 'clbits': '001'}], ['control: reset excited qubit', [1, 1, [['x', 0], ['reset', 0], ['measure', 0, 0]]], {'qubits': '0', 'clbits': '0'}], ['control: toffoli one control', [3, 3, [['x', 0], ['ccx', 0, 1, 2]]], {'qubits': '001', 'clbits': '000'}], ['control: toffoli no controls', [3, 3, [['ccx', 0, 1, 2]]], {'qubits': '000', 'clbits': '000'}], ['control: measure into other clbit', [2, 2, [['x', 0], ['measure', 0, 1]]], {'qubits': '01', 'clbits': '10'}]], [['regression: random dynamic 59', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 0], ['x_if', 0, 1], ['reset', 2]]], {'qubits': '011', 'clbits': '001'}], ['regression: feed-forward on register value 2', [2, 2, [['x', 1], ['measure', 1, 1], ['x_if', 0, 2]]], {'qubits': '11', 'clbits': '10'}], ['control: swap excitation', [2, 2, [['x', 0], ['swap', 0, 1]]], {'qubits': '10', 'clbits': '00'}], ['control: asymmetric output', [3, 3, [['x', 0], ['measure', 0, 0]]], {'qubits': '001', 'clbits': '001'}], ['control: random dynamic 0', [4, 4, [['cx', 2, 1], ['reset', 2], ['x_if', 1, 5], ['x_if', 3, 6], ['reset', 2], ['cx', 3, 1], ['swap', 1, 2], ['x', 0]]], {'qubits': '0001', 'clbits': '0000'}], ['control: random dynamic 1', [2, 2, [['cx', 1, 0], ['x_if', 0, 2], ['x', 1], ['swap', 0, 1], ['reset', 1], ['cx', 1, 0], ['x', 1]]], {'qubits': '11', 'clbits': '00'}]], [['regression: feed-forward on register value 2', [2, 2, [['x', 1], ['measure', 1, 1], ['x_if', 0, 2]]], {'qubits': '11', 'clbits': '10'}], ['regression: random dynamic 59', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 0], ['x_if', 0, 1], ['reset', 2]]], {'qubits': '011', 'clbits': '001'}], ['control: random dynamic 2', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 1], ['x', 0], ['x_if', 2, 5], ['reset', 1], ['cx', 2, 0], ['swap', 1, 0], ['cx', 1, 0]]], {'qubits': '011', 'clbits': '010'}], ['control: random dynamic 3', [4, 4, [['ccx', 3, 0, 2], ['x_if', 1, 8], ['x_if', 1, 10], ['measure', 0, 3], ['reset', 0], ['measure', 0, 1]]], {'qubits': '0000', 'clbits': '0000'}], ['control: random dynamic 4', [2, 2, [['x', 0], ['measure', 1, 1]]], {'qubits': '01', 'clbits': '00'}], ['control: random dynamic 5', [4, 4, [['x_if', 1, 4], ['reset', 1], ['x_if', 1, 10], ['x', 0], ['measure', 2, 1], ['measure', 1, 0]]], {'qubits': '0001', 'clbits': '0000'}]], [['regression: random dynamic 59', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 0], ['x_if', 0, 1], ['reset', 2]]], {'qubits': '011', 'clbits': '001'}], ['regression: feed-forward on register value 2', [2, 2, [['x', 1], ['measure', 1, 1], ['x_if', 0, 2]]], {'qubits': '11', 'clbits': '10'}], ['control: random dynamic 6', [2, 2, [['reset', 0], ['x', 1], ['swap', 1, 0], ['reset', 0], ['measure', 1, 0], ['x_if', 0, 1], ['cx', 0, 1], ['swap', 1, 0], ['cx', 1, 0]]], {'qubits': '00', 'clbits': '00'}], ['control: random dynamic 7', [4, 4, [['ccx', 1, 0, 2], ['x_if', 1, 6]]], {'qubits': '0000', 'clbits': '0000'}], ['control: random dynamic 8', [3, 3, [['reset', 2], ['x', 1], ['ccx', 0, 2, 1], ['measure', 1, 1], ['reset', 2]]], {'qubits': '010', 'clbits': '010'}], ['control: random dynamic 9', [2, 2, [['cx', 0, 1], ['cx', 0, 1], ['measure', 0, 0], ['swap', 0, 1], ['measure', 0, 1], ['swap', 0, 1], ['measure', 0, 1], ['measure', 0, 0], ['swap', 1, 0]]], {'qubits': '00', 'clbits': '00'}]], [['regression: feed-forward on register value 2', [2, 2, [['x', 1], ['measure', 1, 1], ['x_if', 0, 2]]], {'qubits': '11', 'clbits': '10'}], ['regression: random dynamic 59', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 0], ['x_if', 0, 1], ['reset', 2]]], {'qubits': '011', 'clbits': '001'}], ['control: random dynamic 10', [3, 3, [['x', 2], ['measure', 2, 2], ['ccx', 2, 1, 0], ['swap', 0, 2], ['cx', 2, 0], ['x', 1], ['ccx', 2, 0, 1], ['cx', 2, 0], ['x', 1]]], {'qubits': '001', 'clbits': '100'}], ['control: random dynamic 11', [3, 3, [['reset', 2], ['x', 2]]], {'qubits': '100', 'clbits': '000'}], ['control: random dynamic 12', [3, 3, [['x', 1], ['x', 2]]], {'qubits': '110', 'clbits': '000'}], ['control: random dynamic 13', [2, 2, [['x_if', 1, 0], ['x', 0], ['swap', 1, 0], ['x', 1], ['cx', 1, 0], ['x_if', 1, 3], ['measure', 1, 1], ['swap', 1, 0], ['x', 1]]], {'qubits': '00', 'clbits': '00'}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    nq, nc, ops = x
    q = [0] * nq
    c = [0] * nc
    for op in ops:
        name = op[0]
        if name == 'x':
            q[op[1]] ^= 1
        elif name == 'cx':
            if q[op[1]]:
                q[op[2]] ^= 1
        elif name == 'ccx':
            if q[op[1]] and q[op[2]]:
                q[op[3]] ^= 1
        elif name == 'swap':
            a, b = op[1], op[2]
            q[a], q[b] = q[b], q[a]
        elif name == 'measure':
            _, qb, cb = op
            c[cb] = q[qb]
        elif name == 'reset':
            q[op[1]] = 0
        elif name == 'x_if':
            creg = sum(bit << (nc - i) for i, bit in enumerate(c))
            if creg == op[2]:
                q[op[1]] ^= 1
    return {'qubits': ''.join(str(b) for b in reversed(q)), 'clbits': ''.join(str(b) for b in reversed(c))}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: feed-forward on register value 2', [2, 2, [['x', 1], ['measure', 1, 1], ['x_if', 0, 2]]], {'qubits': '11', 'clbits': '10'}], ['regression: random dynamic 59', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 0], ['x_if', 0, 1], ['reset', 2]]], {'qubits': '011', 'clbits': '001'}], ['control: reset excited qubit', [1, 1, [['x', 0], ['reset', 0], ['measure', 0, 0]]], {'qubits': '0', 'clbits': '0'}], ['control: toffoli one control', [3, 3, [['x', 0], ['ccx', 0, 1, 2]]], {'qubits': '001', 'clbits': '000'}], ['control: toffoli no controls', [3, 3, [['ccx', 0, 1, 2]]], {'qubits': '000', 'clbits': '000'}], ['control: measure into other clbit', [2, 2, [['x', 0], ['measure', 0, 1]]], {'qubits': '01', 'clbits': '10'}]], [['regression: random dynamic 59', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 0], ['x_if', 0, 1], ['reset', 2]]], {'qubits': '011', 'clbits': '001'}], ['regression: feed-forward on register value 2', [2, 2, [['x', 1], ['measure', 1, 1], ['x_if', 0, 2]]], {'qubits': '11', 'clbits': '10'}], ['control: swap excitation', [2, 2, [['x', 0], ['swap', 0, 1]]], {'qubits': '10', 'clbits': '00'}], ['control: asymmetric output', [3, 3, [['x', 0], ['measure', 0, 0]]], {'qubits': '001', 'clbits': '001'}], ['control: random dynamic 0', [4, 4, [['cx', 2, 1], ['reset', 2], ['x_if', 1, 5], ['x_if', 3, 6], ['reset', 2], ['cx', 3, 1], ['swap', 1, 2], ['x', 0]]], {'qubits': '0001', 'clbits': '0000'}], ['control: random dynamic 1', [2, 2, [['cx', 1, 0], ['x_if', 0, 2], ['x', 1], ['swap', 0, 1], ['reset', 1], ['cx', 1, 0], ['x', 1]]], {'qubits': '11', 'clbits': '00'}]], [['regression: feed-forward on register value 2', [2, 2, [['x', 1], ['measure', 1, 1], ['x_if', 0, 2]]], {'qubits': '11', 'clbits': '10'}], ['regression: random dynamic 59', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 0], ['x_if', 0, 1], ['reset', 2]]], {'qubits': '011', 'clbits': '001'}], ['control: random dynamic 2', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 1], ['x', 0], ['x_if', 2, 5], ['reset', 1], ['cx', 2, 0], ['swap', 1, 0], ['cx', 1, 0]]], {'qubits': '011', 'clbits': '010'}], ['control: random dynamic 3', [4, 4, [['ccx', 3, 0, 2], ['x_if', 1, 8], ['x_if', 1, 10], ['measure', 0, 3], ['reset', 0], ['measure', 0, 1]]], {'qubits': '0000', 'clbits': '0000'}], ['control: random dynamic 4', [2, 2, [['x', 0], ['measure', 1, 1]]], {'qubits': '01', 'clbits': '00'}], ['control: random dynamic 5', [4, 4, [['x_if', 1, 4], ['reset', 1], ['x_if', 1, 10], ['x', 0], ['measure', 2, 1], ['measure', 1, 0]]], {'qubits': '0001', 'clbits': '0000'}]], [['regression: random dynamic 59', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 0], ['x_if', 0, 1], ['reset', 2]]], {'qubits': '011', 'clbits': '001'}], ['regression: feed-forward on register value 2', [2, 2, [['x', 1], ['measure', 1, 1], ['x_if', 0, 2]]], {'qubits': '11', 'clbits': '10'}], ['control: random dynamic 6', [2, 2, [['reset', 0], ['x', 1], ['swap', 1, 0], ['reset', 0], ['measure', 1, 0], ['x_if', 0, 1], ['cx', 0, 1], ['swap', 1, 0], ['cx', 1, 0]]], {'qubits': '00', 'clbits': '00'}], ['control: random dynamic 7', [4, 4, [['ccx', 1, 0, 2], ['x_if', 1, 6]]], {'qubits': '0000', 'clbits': '0000'}], ['control: random dynamic 8', [3, 3, [['reset', 2], ['x', 1], ['ccx', 0, 2, 1], ['measure', 1, 1], ['reset', 2]]], {'qubits': '010', 'clbits': '010'}], ['control: random dynamic 9', [2, 2, [['cx', 0, 1], ['cx', 0, 1], ['measure', 0, 0], ['swap', 0, 1], ['measure', 0, 1], ['swap', 0, 1], ['measure', 0, 1], ['measure', 0, 0], ['swap', 1, 0]]], {'qubits': '00', 'clbits': '00'}]], [['regression: feed-forward on register value 2', [2, 2, [['x', 1], ['measure', 1, 1], ['x_if', 0, 2]]], {'qubits': '11', 'clbits': '10'}], ['regression: random dynamic 59', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 0], ['x_if', 0, 1], ['reset', 2]]], {'qubits': '011', 'clbits': '001'}], ['control: random dynamic 10', [3, 3, [['x', 2], ['measure', 2, 2], ['ccx', 2, 1, 0], ['swap', 0, 2], ['cx', 2, 0], ['x', 1], ['ccx', 2, 0, 1], ['cx', 2, 0], ['x', 1]]], {'qubits': '001', 'clbits': '100'}], ['control: random dynamic 11', [3, 3, [['reset', 2], ['x', 2]]], {'qubits': '100', 'clbits': '000'}], ['control: random dynamic 12', [3, 3, [['x', 1], ['x', 2]]], {'qubits': '110', 'clbits': '000'}], ['control: random dynamic 13', [2, 2, [['x_if', 1, 0], ['x', 0], ['swap', 1, 0], ['x', 1], ['cx', 1, 0], ['x_if', 1, 3], ['measure', 1, 1], ['swap', 1, 0], ['x', 1]]], {'qubits': '00', 'clbits': '00'}]]]
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 condition on register value 2 fires when clbit 0 is set instead of clbit 1.

Input [nq, nc, ops] on computational basis states only: ["x", q], ["cx", c, t], ["ccx", a, b, t], ["swap", a, b], ["measure", q, c], ["reset", q], ["x_if", q, value] (flip q when the classical register value, clbit 0 = LSB, equals value). Return {"qubits": bits with qubit nq-1 leftmost, "clbits": bits with clbit nc-1 leftmost}.

split: open-access


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

N = 1
observations = []
def solve(x):
    nq, nc, ops = x
    q = [0] * nq
    c = [0] * nc
    for op in ops:
        name = op[0]
        if name == 'x':
            q[op[1]] ^= 1
        elif name == 'cx':
            if q[op[1]]:
                q[op[2]] ^= 1
        elif name == 'ccx':
            if q[op[1]] and q[op[2]]:
                q[op[3]] ^= 1
        elif name == 'swap':
            a, b = op[1], op[2]
            q[a], q[b] = q[b], q[a]
        elif name == 'measure':
            _, qb, cb = op
            c[cb] = q[qb]
        elif name == 'reset':
            q[op[1]] ^= 1
        elif name == 'x_if':
            creg = sum(bit << i for i, bit in enumerate(c))
            if creg == op[2]:
                q[op[1]] ^= 1
    return {'qubits': ''.join(str(b) for b in reversed(q)), 'clbits': ''.join(str(b) for b in reversed(c))}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: random dynamic 1', [2, 2, [['cx', 1, 0], ['x_if', 0, 2], ['x', 1], ['swap', 0, 1], ['reset', 1], ['cx', 1, 0], ['x', 1]]], {'qubits': '11', 'clbits': '00'}], ['regression: random dynamic 3', [4, 4, [['ccx', 3, 0, 2], ['x_if', 1, 8], ['x_if', 1, 10], ['measure', 0, 3], ['reset', 0], ['measure', 0, 1]]], {'qubits': '0000', 'clbits': '0000'}], ['repair check: random dynamic 2', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 1], ['x', 0], ['x_if', 2, 5], ['reset', 1], ['cx', 2, 0], ['swap', 1, 0], ['cx', 1, 0]]], {'qubits': '011', 'clbits': '010'}], ['control: feed-forward on register value 2', [2, 2, [['x', 1], ['measure', 1, 1], ['x_if', 0, 2]]], {'qubits': '11', 'clbits': '10'}], ['control: reset excited qubit', [1, 1, [['x', 0], ['reset', 0], ['measure', 0, 0]]], {'qubits': '0', 'clbits': '0'}], ['control: toffoli one control', [3, 3, [['x', 0], ['ccx', 0, 1, 2]]], {'qubits': '001', 'clbits': '000'}], ['control: toffoli no controls', [3, 3, [['ccx', 0, 1, 2]]], {'qubits': '000', 'clbits': '000'}]], [['regression: random dynamic 6', [2, 2, [['reset', 0], ['x', 1], ['swap', 1, 0], ['reset', 0], ['measure', 1, 0], ['x_if', 0, 1], ['cx', 0, 1], ['swap', 1, 0], ['cx', 1, 0]]], {'qubits': '00', 'clbits': '00'}], ['regression: random dynamic 11', [3, 3, [['reset', 2], ['x', 2]]], {'qubits': '100', 'clbits': '000'}], ['repair check: random dynamic 2', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 1], ['x', 0], ['x_if', 2, 5], ['reset', 1], ['cx', 2, 0], ['swap', 1, 0], ['cx', 1, 0]]], {'qubits': '011', 'clbits': '010'}], ['control: measure into other clbit', [2, 2, [['x', 0], ['measure', 0, 1]]], {'qubits': '01', 'clbits': '10'}], ['control: swap excitation', [2, 2, [['x', 0], ['swap', 0, 1]]], {'qubits': '10', 'clbits': '00'}], ['control: asymmetric output', [3, 3, [['x', 0], ['measure', 0, 0]]], {'qubits': '001', 'clbits': '001'}], ['control: random dynamic 0', [4, 4, [['cx', 2, 1], ['reset', 2], ['x_if', 1, 5], ['x_if', 3, 6], ['reset', 2], ['cx', 3, 1], ['swap', 1, 2], ['x', 0]]], {'qubits': '0001', 'clbits': '0000'}]], [['regression: random dynamic 15', [3, 3, [['cx', 2, 1], ['x_if', 0, 0], ['cx', 2, 1], ['ccx', 0, 1, 2], ['reset', 2], ['reset', 1], ['cx', 0, 1]]], {'qubits': '011', 'clbits': '000'}], ['regression: random dynamic 16', [2, 2, [['reset', 0], ['x_if', 0, 1], ['x', 0]]], {'qubits': '01', 'clbits': '00'}], ['repair check: random dynamic 2', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 1], ['x', 0], ['x_if', 2, 5], ['reset', 1], ['cx', 2, 0], ['swap', 1, 0], ['cx', 1, 0]]], {'qubits': '011', 'clbits': '010'}], ['control: random dynamic 4', [2, 2, [['x', 0], ['measure', 1, 1]]], {'qubits': '01', 'clbits': '00'}], ['control: random dynamic 7', [4, 4, [['ccx', 1, 0, 2], ['x_if', 1, 6]]], {'qubits': '0000', 'clbits': '0000'}], ['control: random dynamic 8', [3, 3, [['reset', 2], ['x', 1], ['ccx', 0, 2, 1], ['measure', 1, 1], ['reset', 2]]], {'qubits': '010', 'clbits': '010'}], ['control: random dynamic 9', [2, 2, [['cx', 0, 1], ['cx', 0, 1], ['measure', 0, 0], ['swap', 0, 1], ['measure', 0, 1], ['swap', 0, 1], ['measure', 0, 1], ['measure', 0, 0], ['swap', 1, 0]]], {'qubits': '00', 'clbits': '00'}]], [['regression: random dynamic 23', [4, 4, [['reset', 1], ['x', 3], ['x_if', 3, 0], ['swap', 0, 2], ['x', 0], ['x', 1], ['ccx', 1, 2, 0], ['measure', 2, 2], ['cx', 3, 0]]], {'qubits': '0011', 'clbits': '0000'}], ['regression: random dynamic 29', [2, 2, [['measure', 0, 0], ['swap', 1, 0], ['swap', 0, 1], ['reset', 0], ['swap', 1, 0], ['cx', 0, 1]]], {'qubits': '00', 'clbits': '00'}], ['repair check: random dynamic 2', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 1], ['x', 0], ['x_if', 2, 5], ['reset', 1], ['cx', 2, 0], ['swap', 1, 0], ['cx', 1, 0]]], {'qubits': '011', 'clbits': '010'}], ['control: random dynamic 10', [3, 3, [['x', 2], ['measure', 2, 2], ['ccx', 2, 1, 0], ['swap', 0, 2], ['cx', 2, 0], ['x', 1], ['ccx', 2, 0, 1], ['cx', 2, 0], ['x', 1]]], {'qubits': '001', 'clbits': '100'}], ['control: random dynamic 12', [3, 3, [['x', 1], ['x', 2]]], {'qubits': '110', 'clbits': '000'}], ['control: random dynamic 13', [2, 2, [['x_if', 1, 0], ['x', 0], ['swap', 1, 0], ['x', 1], ['cx', 1, 0], ['x_if', 1, 3], ['measure', 1, 1], ['swap', 1, 0], ['x', 1]]], {'qubits': '00', 'clbits': '00'}], ['control: random dynamic 18', [2, 2, [['swap', 0, 1], ['x', 0], ['cx', 0, 1], ['cx', 0, 1]]], {'qubits': '01', 'clbits': '00'}]], [['regression: random dynamic 33', [4, 4, [['x', 0], ['x_if', 1, 8], ['reset', 1], ['x', 2], ['x', 1], ['swap', 3, 2], ['x', 2]]], {'qubits': '1111', 'clbits': '0000'}], ['regression: random dynamic 34', [4, 4, [['cx', 0, 2], ['cx', 1, 2], ['measure', 2, 3], ['measure', 0, 1], ['ccx', 1, 0, 3], ['reset', 3], ['x_if', 1, 9], ['x', 3], ['reset', 2]]], {'qubits': '1000', 'clbits': '0000'}], ['repair check: random dynamic 2', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 1], ['x', 0], ['x_if', 2, 5], ['reset', 1], ['cx', 2, 0], ['swap', 1, 0], ['cx', 1, 0]]], {'qubits': '011', 'clbits': '010'}], ['control: random dynamic 19', [4, 4, [['swap', 3, 2], ['x_if', 0, 4], ['x', 3], ['cx', 1, 2]]], {'qubits': '1000', 'clbits': '0000'}], ['control: random dynamic 20', [2, 2, [['cx', 1, 0], ['x', 0], ['x_if', 0, 0], ['cx', 1, 0]]], {'qubits': '00', 'clbits': '00'}], ['control: random dynamic 21', [4, 4, [['swap', 1, 2], ['measure', 3, 0], ['x_if', 0, 10], ['swap', 0, 2], ['x_if', 2, 9], ['cx', 0, 2], ['x_if', 0, 15], ['cx', 0, 3]]], {'qubits': '0000', 'clbits': '0000'}], ['control: random dynamic 22', [4, 4, [['swap', 3, 1], ['x', 2], ['measure', 1, 2], ['ccx', 0, 1, 2]]], {'qubits': '0100', 'clbits': '0000'}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    nq, nc, ops = x
    q = [0] * nq
    c = [0] * nc
    for op in ops:
        name = op[0]
        if name == 'x':
            q[op[1]] ^= 1
        elif name == 'cx':
            if q[op[1]]:
                q[op[2]] ^= 1
        elif name == 'ccx':
            if q[op[1]] and q[op[2]]:
                q[op[3]] ^= 1
        elif name == 'swap':
            a, b = op[1], op[2]
            q[a], q[b] = q[b], q[a]
        elif name == 'measure':
            _, qb, cb = op
            c[cb] = q[qb]
        elif name == 'reset':
            c[0] = q[op[1]]
            q[op[1]] = 0
        elif name == 'x_if':
            creg = sum(bit << i for i, bit in enumerate(c))
            if creg == op[2]:
                q[op[1]] ^= 1
    return {'qubits': ''.join(str(b) for b in reversed(q)), 'clbits': ''.join(str(b) for b in reversed(c))}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: random dynamic 1', [2, 2, [['cx', 1, 0], ['x_if', 0, 2], ['x', 1], ['swap', 0, 1], ['reset', 1], ['cx', 1, 0], ['x', 1]]], {'qubits': '11', 'clbits': '00'}], ['regression: random dynamic 3', [4, 4, [['ccx', 3, 0, 2], ['x_if', 1, 8], ['x_if', 1, 10], ['measure', 0, 3], ['reset', 0], ['measure', 0, 1]]], {'qubits': '0000', 'clbits': '0000'}], ['repair check: random dynamic 2', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 1], ['x', 0], ['x_if', 2, 5], ['reset', 1], ['cx', 2, 0], ['swap', 1, 0], ['cx', 1, 0]]], {'qubits': '011', 'clbits': '010'}], ['control: feed-forward on register value 2', [2, 2, [['x', 1], ['measure', 1, 1], ['x_if', 0, 2]]], {'qubits': '11', 'clbits': '10'}], ['control: reset excited qubit', [1, 1, [['x', 0], ['reset', 0], ['measure', 0, 0]]], {'qubits': '0', 'clbits': '0'}], ['control: toffoli one control', [3, 3, [['x', 0], ['ccx', 0, 1, 2]]], {'qubits': '001', 'clbits': '000'}], ['control: toffoli no controls', [3, 3, [['ccx', 0, 1, 2]]], {'qubits': '000', 'clbits': '000'}]], [['regression: random dynamic 6', [2, 2, [['reset', 0], ['x', 1], ['swap', 1, 0], ['reset', 0], ['measure', 1, 0], ['x_if', 0, 1], ['cx', 0, 1], ['swap', 1, 0], ['cx', 1, 0]]], {'qubits': '00', 'clbits': '00'}], ['regression: random dynamic 11', [3, 3, [['reset', 2], ['x', 2]]], {'qubits': '100', 'clbits': '000'}], ['repair check: random dynamic 2', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 1], ['x', 0], ['x_if', 2, 5], ['reset', 1], ['cx', 2, 0], ['swap', 1, 0], ['cx', 1, 0]]], {'qubits': '011', 'clbits': '010'}], ['control: measure into other clbit', [2, 2, [['x', 0], ['measure', 0, 1]]], {'qubits': '01', 'clbits': '10'}], ['control: swap excitation', [2, 2, [['x', 0], ['swap', 0, 1]]], {'qubits': '10', 'clbits': '00'}], ['control: asymmetric output', [3, 3, [['x', 0], ['measure', 0, 0]]], {'qubits': '001', 'clbits': '001'}], ['control: random dynamic 0', [4, 4, [['cx', 2, 1], ['reset', 2], ['x_if', 1, 5], ['x_if', 3, 6], ['reset', 2], ['cx', 3, 1], ['swap', 1, 2], ['x', 0]]], {'qubits': '0001', 'clbits': '0000'}]], [['regression: random dynamic 15', [3, 3, [['cx', 2, 1], ['x_if', 0, 0], ['cx', 2, 1], ['ccx', 0, 1, 2], ['reset', 2], ['reset', 1], ['cx', 0, 1]]], {'qubits': '011', 'clbits': '000'}], ['regression: random dynamic 16', [2, 2, [['reset', 0], ['x_if', 0, 1], ['x', 0]]], {'qubits': '01', 'clbits': '00'}], ['repair check: random dynamic 2', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 1], ['x', 0], ['x_if', 2, 5], ['reset', 1], ['cx', 2, 0], ['swap', 1, 0], ['cx', 1, 0]]], {'qubits': '011', 'clbits': '010'}], ['control: random dynamic 4', [2, 2, [['x', 0], ['measure', 1, 1]]], {'qubits': '01', 'clbits': '00'}], ['control: random dynamic 7', [4, 4, [['ccx', 1, 0, 2], ['x_if', 1, 6]]], {'qubits': '0000', 'clbits': '0000'}], ['control: random dynamic 8', [3, 3, [['reset', 2], ['x', 1], ['ccx', 0, 2, 1], ['measure', 1, 1], ['reset', 2]]], {'qubits': '010', 'clbits': '010'}], ['control: random dynamic 9', [2, 2, [['cx', 0, 1], ['cx', 0, 1], ['measure', 0, 0], ['swap', 0, 1], ['measure', 0, 1], ['swap', 0, 1], ['measure', 0, 1], ['measure', 0, 0], ['swap', 1, 0]]], {'qubits': '00', 'clbits': '00'}]], [['regression: random dynamic 23', [4, 4, [['reset', 1], ['x', 3], ['x_if', 3, 0], ['swap', 0, 2], ['x', 0], ['x', 1], ['ccx', 1, 2, 0], ['measure', 2, 2], ['cx', 3, 0]]], {'qubits': '0011', 'clbits': '0000'}], ['regression: random dynamic 29', [2, 2, [['measure', 0, 0], ['swap', 1, 0], ['swap', 0, 1], ['reset', 0], ['swap', 1, 0], ['cx', 0, 1]]], {'qubits': '00', 'clbits': '00'}], ['repair check: random dynamic 2', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 1], ['x', 0], ['x_if', 2, 5], ['reset', 1], ['cx', 2, 0], ['swap', 1, 0], ['cx', 1, 0]]], {'qubits': '011', 'clbits': '010'}], ['control: random dynamic 10', [3, 3, [['x', 2], ['measure', 2, 2], ['ccx', 2, 1, 0], ['swap', 0, 2], ['cx', 2, 0], ['x', 1], ['ccx', 2, 0, 1], ['cx', 2, 0], ['x', 1]]], {'qubits': '001', 'clbits': '100'}], ['control: random dynamic 12', [3, 3, [['x', 1], ['x', 2]]], {'qubits': '110', 'clbits': '000'}], ['control: random dynamic 13', [2, 2, [['x_if', 1, 0], ['x', 0], ['swap', 1, 0], ['x', 1], ['cx', 1, 0], ['x_if', 1, 3], ['measure', 1, 1], ['swap', 1, 0], ['x', 1]]], {'qubits': '00', 'clbits': '00'}], ['control: random dynamic 18', [2, 2, [['swap', 0, 1], ['x', 0], ['cx', 0, 1], ['cx', 0, 1]]], {'qubits': '01', 'clbits': '00'}]], [['regression: random dynamic 33', [4, 4, [['x', 0], ['x_if', 1, 8], ['reset', 1], ['x', 2], ['x', 1], ['swap', 3, 2], ['x', 2]]], {'qubits': '1111', 'clbits': '0000'}], ['regression: random dynamic 34', [4, 4, [['cx', 0, 2], ['cx', 1, 2], ['measure', 2, 3], ['measure', 0, 1], ['ccx', 1, 0, 3], ['reset', 3], ['x_if', 1, 9], ['x', 3], ['reset', 2]]], {'qubits': '1000', 'clbits': '0000'}], ['repair check: random dynamic 2', [3, 3, [['cx', 2, 1], ['x', 1], ['measure', 1, 1], ['x', 0], ['x_if', 2, 5], ['reset', 1], ['cx', 2, 0], ['swap', 1, 0], ['cx', 1, 0]]], {'qubits': '011', 'clbits': '010'}], ['control: random dynamic 19', [4, 4, [['swap', 3, 2], ['x_if', 0, 4], ['x', 3], ['cx', 1, 2]]], {'qubits': '1000', 'clbits': '0000'}], ['control: random dynamic 20', [2, 2, [['cx', 1, 0], ['x', 0], ['x_if', 0, 0], ['cx', 1, 0]]], {'qubits': '00', 'clbits': '00'}], ['control: random dynamic 21', [4, 4, [['swap', 1, 2], ['measure', 3, 0], ['x_if', 0, 10], ['swap', 0, 2], ['x_if', 2, 9], ['cx', 0, 2], ['x_if', 0, 15], ['cx', 0, 3]]], {'qubits': '0000', 'clbits': '0000'}], ['control: random dynamic 22', [4, 4, [['swap', 3, 1], ['x', 2], ['measure', 1, 2], ['ccx', 0, 1, 2]]], {'qubits': '0100', 'clbits': '0000'}]]]
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:
Resetting a qubit that is already |0> leaves it in |1>.

Input [nq, nc, ops] on computational basis states only: ["x", q], ["cx", c, t], ["ccx", a, b, t], ["swap", a, b], ["measure", q, c], ["reset", q], ["x_if", q, value] (flip q when the classical register value, clbit 0 = LSB, equals value). Return {"qubits": bits with qubit nq-1 leftmost, "clbits": bits with clbit nc-1 leftmost}.

split: open-access