broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
N = 1
observations = []
def solve(x):
    n, amps, qubits = x
    if len(amps) != 1 << n:
        return 'bad-length'
    if len(set(qubits)) != len(qubits):
        return 'duplicate-qubit'
    if any(q < 0 or q >= n for q in qubits):
        return 'bad-qubit'
    total = sum(re * re + im * im for re, im in amps)
    if total <= 1e-12:
        return 'zero-state'
    acc = {}
    for idx, (a, b) in enumerate(amps):
        p = math.hypot(a, b)
        if p == 0:
            continue
        key = ''.join('1' if idx >> q & 1 else '0' for q in reversed(qubits))
        acc[key] = acc.get(key, 0.0) + p
    out = {}
    for key in sorted(acc):
        v = round(acc[key] / total, 6)
        if v > 0:
            out[key] = v
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: bell state on both qubits', [2, [[0.707107, 0], [0, 0], [0, 0], [0.707107, 0]], [0, 1]], {'00': 0.5, '11': 0.5}], ['regression: tiny but valid state', [1, [[0.0001, 0], [0, 0.0002]], [0]], {'0': 0.2, '1': 0.8}], ['regression: unnormalized pair', [1, [[3, 0], [0, 4]], [0]], {'0': 0.36, '1': 0.64}], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}], ['control: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit']], [['regression: random state 0', [2, [[0.0, 0.0], [0.282, -0.043], [-0.278, 0.017], [0.0, 0.0]], [0, 1]], {'01': 0.511954, '10': 0.488046}], ['regression: random state 1', [2, [[0.129, 0.125], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], [1, 0]], {'00': 1.0}], ['regression: random state 2', [1, [[0.541, -0.923], [-0.479, -0.646]], [0]], {'0': 0.638959, '1': 0.361041}], ['control: duplicate qubits', [2, [[0.6, 0], [0, 0.8], [0, 0], [0, 0]], [0, 0]], 'duplicate-qubit'], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length'], ['control: all-zero state', [1, [[0, 0], [0, 0]], [0]], 'zero-state'], ['control: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state']], [['regression: random state 3', [2, [[0.379, 0.308], [-0.129, -0.524], [0.79, -0.299], [-0.403, -0.327]], [0, 1]], {'00': 0.157683, '01': 0.192532, '10': 0.471717, '11': 0.178068}], ['regression: random state 4', [1, [[-0.217, 0.525], [-0.308, -0.181]], [0]], {'0': 0.716602, '1': 0.283398}], ['regression: random state 2', [1, [[0.541, -0.923], [-0.479, -0.646]], [0]], {'0': 0.638959, '1': 0.361041}], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}], ['control: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit']], [['regression: random state 6', [3, [[-0.119, 0.092], [0.264, -0.223], [-0.189, 0.034], [-0.22, -0.267], [-0.291, -0.092], [0.09, -0.02], [0.062, -0.284], [-0.239, 0.039]], [2]], {'0': 0.54953, '1': 0.45047}], ['regression: random state 7', [1, [[0.725, -0.825], [-0.233, -0.266]], [0]], {'0': 0.906073, '1': 0.093927}], ['regression: random state 4', [1, [[-0.217, 0.525], [-0.308, -0.181]], [0]], {'0': 0.716602, '1': 0.283398}], ['control: duplicate qubits', [2, [[0.6, 0], [0, 0.8], [0, 0], [0, 0]], [0, 0]], 'duplicate-qubit'], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length'], ['control: all-zero state', [1, [[0, 0], [0, 0]], [0]], 'zero-state'], ['control: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state']], [['regression: random state 9', [1, [[-0.25, -0.175], [0.0, 0.0]], [0]], {'0': 1.0}], ['regression: random state 10', [3, [[-0.064, 0.212], [0.264, -0.114], [-0.15, -0.24], [0.031, 0.141], [0.037, -0.002], [0.023, -0.229], [0.193, -0.153], [0.251, 0.097]], [0, 1, 2]], {'000': 0.116738, '001': 0.196846, '010': 0.190676, '011': 0.049614, '100': 0.003268, '101': 0.126094, '110': 0.144395, '111': 0.17237}], ['regression: random state 6', [3, [[-0.119, 0.092], [0.264, -0.223], [-0.189, 0.034], [-0.22, -0.267], [-0.291, -0.092], [0.09, -0.02], [0.062, -0.284], [-0.239, 0.039]], [2]], {'0': 0.54953, '1': 0.45047}], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}], ['control: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit']]]
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
N = 1
observations = []
def solve(x):
    n, amps, qubits = x
    if len(amps) != 1 << n:
        return 'bad-length'
    if len(set(qubits)) != len(qubits):
        return 'duplicate-qubit'
    if any(q < 0 or q >= n for q in qubits):
        return 'bad-qubit'
    total = sum(re * re + im * im for re, im in amps)
    if total <= 1e-12:
        return 'zero-state'
    acc = {}
    for idx, (a, b) in enumerate(amps):
        p = a * a - b * b
        if p == 0:
            continue
        key = ''.join('1' if idx >> q & 1 else '0' for q in reversed(qubits))
        acc[key] = acc.get(key, 0.0) + p
    out = {}
    for key in sorted(acc):
        v = round(acc[key] / total, 6)
        if v > 0:
            out[key] = v
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: bell state on both qubits', [2, [[0.707107, 0], [0, 0], [0, 0], [0.707107, 0]], [0, 1]], {'00': 0.5, '11': 0.5}], ['regression: tiny but valid state', [1, [[0.0001, 0], [0, 0.0002]], [0]], {'0': 0.2, '1': 0.8}], ['regression: unnormalized pair', [1, [[3, 0], [0, 4]], [0]], {'0': 0.36, '1': 0.64}], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}], ['control: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit']], [['regression: random state 0', [2, [[0.0, 0.0], [0.282, -0.043], [-0.278, 0.017], [0.0, 0.0]], [0, 1]], {'01': 0.511954, '10': 0.488046}], ['regression: random state 1', [2, [[0.129, 0.125], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], [1, 0]], {'00': 1.0}], ['regression: random state 2', [1, [[0.541, -0.923], [-0.479, -0.646]], [0]], {'0': 0.638959, '1': 0.361041}], ['control: duplicate qubits', [2, [[0.6, 0], [0, 0.8], [0, 0], [0, 0]], [0, 0]], 'duplicate-qubit'], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length'], ['control: all-zero state', [1, [[0, 0], [0, 0]], [0]], 'zero-state'], ['control: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state']], [['regression: random state 3', [2, [[0.379, 0.308], [-0.129, -0.524], [0.79, -0.299], [-0.403, -0.327]], [0, 1]], {'00': 0.157683, '01': 0.192532, '10': 0.471717, '11': 0.178068}], ['regression: random state 4', [1, [[-0.217, 0.525], [-0.308, -0.181]], [0]], {'0': 0.716602, '1': 0.283398}], ['regression: random state 2', [1, [[0.541, -0.923], [-0.479, -0.646]], [0]], {'0': 0.638959, '1': 0.361041}], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}], ['control: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit']], [['regression: random state 6', [3, [[-0.119, 0.092], [0.264, -0.223], [-0.189, 0.034], [-0.22, -0.267], [-0.291, -0.092], [0.09, -0.02], [0.062, -0.284], [-0.239, 0.039]], [2]], {'0': 0.54953, '1': 0.45047}], ['regression: random state 7', [1, [[0.725, -0.825], [-0.233, -0.266]], [0]], {'0': 0.906073, '1': 0.093927}], ['regression: random state 4', [1, [[-0.217, 0.525], [-0.308, -0.181]], [0]], {'0': 0.716602, '1': 0.283398}], ['control: duplicate qubits', [2, [[0.6, 0], [0, 0.8], [0, 0], [0, 0]], [0, 0]], 'duplicate-qubit'], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length'], ['control: all-zero state', [1, [[0, 0], [0, 0]], [0]], 'zero-state'], ['control: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state']], [['regression: random state 9', [1, [[-0.25, -0.175], [0.0, 0.0]], [0]], {'0': 1.0}], ['regression: random state 10', [3, [[-0.064, 0.212], [0.264, -0.114], [-0.15, -0.24], [0.031, 0.141], [0.037, -0.002], [0.023, -0.229], [0.193, -0.153], [0.251, 0.097]], [0, 1, 2]], {'000': 0.116738, '001': 0.196846, '010': 0.190676, '011': 0.049614, '100': 0.003268, '101': 0.126094, '110': 0.144395, '111': 0.17237}], ['regression: random state 6', [3, [[-0.119, 0.092], [0.264, -0.223], [-0.189, 0.034], [-0.22, -0.267], [-0.291, -0.092], [0.09, -0.02], [0.062, -0.284], [-0.239, 0.039]], [2]], {'0': 0.54953, '1': 0.45047}], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}], ['control: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit']]]
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:
An unequal superposition such as 0.6|0> + 0.8i|1> is reported as 3/7 and 4/7 instead of 0.36 and 0.64.

Input [n, amps, qubits]; amps are 2**n [re, im] pairs (qubit 0 = LSB), possibly unnormalized. Return the marginal outcome distribution over the listed qubits as {bitstring: probability} where the first listed qubit is the rightmost character, probabilities are normalized by the total squared norm, rounded to 6 decimals after summation, and zero entries are omitted. Errors: "bad-length", "duplicate-qubit", "bad-qubit", "zero-state" (total squared norm <= 1e-12).

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
N = 1
observations = []
def solve(x):
    n, amps, qubits = x
    if len(amps) != 1 << n:
        return 'bad-length'
    if len(set(qubits)) != len(qubits):
        return 'duplicate-qubit'
    if any(q < 0 or q >= n for q in qubits):
        return 'bad-qubit'
    total = sum(re * re + im * im for re, im in amps)
    if total <= 1e-12:
        return 'zero-state'
    acc = {}
    for idx, (a, b) in enumerate(amps):
        p = a * a + b * b
        if p == 0:
            continue
        key = ''.join('1' if idx >> q & 1 else '0' for q in reversed(qubits))
        acc[key] = acc.get(key, 0.0) + p
    out = {}
    for key in sorted(acc):
        v = round(acc[key], 6)
        if v > 0:
            out[key] = v
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: tiny but valid state', [1, [[0.0001, 0], [0, 0.0002]], [0]], {'0': 0.2, '1': 0.8}], ['regression: unnormalized pair', [1, [[3, 0], [0, 4]], [0]], {'0': 0.36, '1': 0.64}], ['regression: random state 0', [2, [[0.0, 0.0], [0.282, -0.043], [-0.278, 0.017], [0.0, 0.0]], [0, 1]], {'01': 0.511954, '10': 0.488046}], ['control: bell state on both qubits', [2, [[0.707107, 0], [0, 0], [0, 0], [0.707107, 0]], [0, 1]], {'00': 0.5, '11': 0.5}], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}], ['control: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit']], [['regression: random state 1', [2, [[0.129, 0.125], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], [1, 0]], {'00': 1.0}], ['regression: random state 2', [1, [[0.541, -0.923], [-0.479, -0.646]], [0]], {'0': 0.638959, '1': 0.361041}], ['regression: random state 0', [2, [[0.0, 0.0], [0.282, -0.043], [-0.278, 0.017], [0.0, 0.0]], [0, 1]], {'01': 0.511954, '10': 0.488046}], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit'], ['control: duplicate qubits', [2, [[0.6, 0], [0, 0.8], [0, 0], [0, 0]], [0, 0]], 'duplicate-qubit'], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length'], ['control: all-zero state', [1, [[0, 0], [0, 0]], [0]], 'zero-state']], [['regression: random state 4', [1, [[-0.217, 0.525], [-0.308, -0.181]], [0]], {'0': 0.716602, '1': 0.283398}], ['regression: random state 5', [2, [[0.175, -0.97], [0.914, 0.939], [-0.22, 0.959], [-0.79, 0.837]], [1]], {'0': 0.539737, '1': 0.460263}], ['regression: random state 2', [1, [[0.541, -0.923], [-0.479, -0.646]], [0]], {'0': 0.638959, '1': 0.361041}], ['control: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state'], ['control: bell state on both qubits', [2, [[0.707107, 0], [0, 0], [0, 0], [0.707107, 0]], [0, 1]], {'00': 0.5, '11': 0.5}], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}]], [['regression: random state 7', [1, [[0.725, -0.825], [-0.233, -0.266]], [0]], {'0': 0.906073, '1': 0.093927}], ['regression: random state 8', [2, [[0.258, -0.148], [-0.044, -0.154], [0.09, 0.21], [0.28, 0.265]], [0]], {'0': 0.446643, '1': 0.553357}], ['regression: random state 4', [1, [[-0.217, 0.525], [-0.308, -0.181]], [0]], {'0': 0.716602, '1': 0.283398}], ['control: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit'], ['control: duplicate qubits', [2, [[0.6, 0], [0, 0.8], [0, 0], [0, 0]], [0, 0]], 'duplicate-qubit'], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length']], [['regression: random state 10', [3, [[-0.064, 0.212], [0.264, -0.114], [-0.15, -0.24], [0.031, 0.141], [0.037, -0.002], [0.023, -0.229], [0.193, -0.153], [0.251, 0.097]], [0, 1, 2]], {'000': 0.116738, '001': 0.196846, '010': 0.190676, '011': 0.049614, '100': 0.003268, '101': 0.126094, '110': 0.144395, '111': 0.17237}], ['regression: random state 11', [2, [[-0.211, 0.28], [0.154, -0.18], [-0.016, -0.095], [-0.157, 0.075]], [0]], {'0': 0.604789, '1': 0.395211}], ['regression: random state 6', [3, [[-0.119, 0.092], [0.264, -0.223], [-0.189, 0.034], [-0.22, -0.267], [-0.291, -0.092], [0.09, -0.02], [0.062, -0.284], [-0.239, 0.039]], [2]], {'0': 0.54953, '1': 0.45047}], ['control: all-zero state', [1, [[0, 0], [0, 0]], [0]], 'zero-state'], ['control: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state'], ['control: bell state on both qubits', [2, [[0.707107, 0], [0, 0], [0, 0], [0.707107, 0]], [0, 1]], {'00': 0.5, '11': 0.5}], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.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
N = 1
observations = []
def solve(x):
    n, amps, qubits = x
    if len(amps) != 1 << n:
        return 'bad-length'
    if len(set(qubits)) != len(qubits):
        return 'duplicate-qubit'
    if any(q < 0 or q >= n for q in qubits):
        return 'bad-qubit'
    total = sum(re * re + im * im for re, im in amps)
    if total <= 1e-12:
        return 'zero-state'
    acc = {}
    for idx, (a, b) in enumerate(amps):
        p = a * a + b * b
        if p == 0:
            continue
        key = ''.join('1' if idx >> q & 1 else '0' for q in reversed(qubits))
        acc[key] = acc.get(key, 0.0) + p
    out = {}
    for key in sorted(acc):
        v = round(acc[key] / math.sqrt(total), 6)
        if v > 0:
            out[key] = v
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: tiny but valid state', [1, [[0.0001, 0], [0, 0.0002]], [0]], {'0': 0.2, '1': 0.8}], ['regression: unnormalized pair', [1, [[3, 0], [0, 4]], [0]], {'0': 0.36, '1': 0.64}], ['regression: random state 0', [2, [[0.0, 0.0], [0.282, -0.043], [-0.278, 0.017], [0.0, 0.0]], [0, 1]], {'01': 0.511954, '10': 0.488046}], ['control: bell state on both qubits', [2, [[0.707107, 0], [0, 0], [0, 0], [0.707107, 0]], [0, 1]], {'00': 0.5, '11': 0.5}], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}], ['control: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit']], [['regression: random state 1', [2, [[0.129, 0.125], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], [1, 0]], {'00': 1.0}], ['regression: random state 2', [1, [[0.541, -0.923], [-0.479, -0.646]], [0]], {'0': 0.638959, '1': 0.361041}], ['regression: random state 0', [2, [[0.0, 0.0], [0.282, -0.043], [-0.278, 0.017], [0.0, 0.0]], [0, 1]], {'01': 0.511954, '10': 0.488046}], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit'], ['control: duplicate qubits', [2, [[0.6, 0], [0, 0.8], [0, 0], [0, 0]], [0, 0]], 'duplicate-qubit'], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length'], ['control: all-zero state', [1, [[0, 0], [0, 0]], [0]], 'zero-state']], [['regression: random state 4', [1, [[-0.217, 0.525], [-0.308, -0.181]], [0]], {'0': 0.716602, '1': 0.283398}], ['regression: random state 5', [2, [[0.175, -0.97], [0.914, 0.939], [-0.22, 0.959], [-0.79, 0.837]], [1]], {'0': 0.539737, '1': 0.460263}], ['regression: random state 2', [1, [[0.541, -0.923], [-0.479, -0.646]], [0]], {'0': 0.638959, '1': 0.361041}], ['control: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state'], ['control: bell state on both qubits', [2, [[0.707107, 0], [0, 0], [0, 0], [0.707107, 0]], [0, 1]], {'00': 0.5, '11': 0.5}], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}]], [['regression: random state 7', [1, [[0.725, -0.825], [-0.233, -0.266]], [0]], {'0': 0.906073, '1': 0.093927}], ['regression: random state 8', [2, [[0.258, -0.148], [-0.044, -0.154], [0.09, 0.21], [0.28, 0.265]], [0]], {'0': 0.446643, '1': 0.553357}], ['regression: random state 4', [1, [[-0.217, 0.525], [-0.308, -0.181]], [0]], {'0': 0.716602, '1': 0.283398}], ['control: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit'], ['control: duplicate qubits', [2, [[0.6, 0], [0, 0.8], [0, 0], [0, 0]], [0, 0]], 'duplicate-qubit'], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length']], [['regression: random state 10', [3, [[-0.064, 0.212], [0.264, -0.114], [-0.15, -0.24], [0.031, 0.141], [0.037, -0.002], [0.023, -0.229], [0.193, -0.153], [0.251, 0.097]], [0, 1, 2]], {'000': 0.116738, '001': 0.196846, '010': 0.190676, '011': 0.049614, '100': 0.003268, '101': 0.126094, '110': 0.144395, '111': 0.17237}], ['regression: random state 11', [2, [[-0.211, 0.28], [0.154, -0.18], [-0.016, -0.095], [-0.157, 0.075]], [0]], {'0': 0.604789, '1': 0.395211}], ['regression: random state 6', [3, [[-0.119, 0.092], [0.264, -0.223], [-0.189, 0.034], [-0.22, -0.267], [-0.291, -0.092], [0.09, -0.02], [0.062, -0.284], [-0.239, 0.039]], [2]], {'0': 0.54953, '1': 0.45047}], ['control: all-zero state', [1, [[0, 0], [0, 0]], [0]], 'zero-state'], ['control: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state'], ['control: bell state on both qubits', [2, [[0.707107, 0], [0, 0], [0, 0], [0.707107, 0]], [0, 1]], {'00': 0.5, '11': 0.5}], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A state given as 3|0> + 4i|1> yields probabilities 9 and 16 instead of 0.36 and 0.64.

Input [n, amps, qubits]; amps are 2**n [re, im] pairs (qubit 0 = LSB), possibly unnormalized. Return the marginal outcome distribution over the listed qubits as {bitstring: probability} where the first listed qubit is the rightmost character, probabilities are normalized by the total squared norm, rounded to 6 decimals after summation, and zero entries are omitted. Errors: "bad-length", "duplicate-qubit", "bad-qubit", "zero-state" (total squared norm <= 1e-12).

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
N = 1
observations = []
def solve(x):
    n, amps, qubits = x
    if len(amps) != 1 << n:
        return 'bad-length'
    if len(set(qubits)) != len(qubits):
        return 'duplicate-qubit'
    if any(q < 0 or q > n for q in qubits):
        return 'bad-qubit'
    total = sum(re * re + im * im for re, im in amps)
    if total <= 1e-12:
        return 'zero-state'
    acc = {}
    for idx, (a, b) in enumerate(amps):
        p = a * a + b * b
        if p == 0:
            continue
        key = ''.join('1' if idx >> q & 1 else '0' for q in reversed(qubits))
        acc[key] = acc.get(key, 0.0) + p
    out = {}
    for key in sorted(acc):
        v = round(acc[key] / total, 6)
        if v > 0:
            out[key] = v
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: bell state on both qubits', [2, [[0.707107, 0], [0, 0], [0, 0], [0.707107, 0]], [0, 1]], {'00': 0.5, '11': 0.5}], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit'], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length']], [['regression: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: duplicate qubits', [2, [[0.6, 0], [0, 0.8], [0, 0], [0, 0]], [0, 0]], 'duplicate-qubit'], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length'], ['control: all-zero state', [1, [[0, 0], [0, 0]], [0]], 'zero-state'], ['control: tiny but valid state', [1, [[0.0001, 0], [0, 0.0002]], [0]], {'0': 0.2, '1': 0.8}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}]], [['regression: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: unnormalized pair', [1, [[3, 0], [0, 4]], [0]], {'0': 0.36, '1': 0.64}], ['control: random state 0', [2, [[0.0, 0.0], [0.282, -0.043], [-0.278, 0.017], [0.0, 0.0]], [0, 1]], {'01': 0.511954, '10': 0.488046}], ['control: random state 1', [2, [[0.129, 0.125], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], [1, 0]], {'00': 1.0}], ['control: random state 2', [1, [[0.541, -0.923], [-0.479, -0.646]], [0]], {'0': 0.638959, '1': 0.361041}], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit']], [['regression: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: random state 3', [2, [[0.379, 0.308], [-0.129, -0.524], [0.79, -0.299], [-0.403, -0.327]], [0, 1]], {'00': 0.157683, '01': 0.192532, '10': 0.471717, '11': 0.178068}], ['control: random state 4', [1, [[-0.217, 0.525], [-0.308, -0.181]], [0]], {'0': 0.716602, '1': 0.283398}], ['control: random state 5', [2, [[0.175, -0.97], [0.914, 0.939], [-0.22, 0.959], [-0.79, 0.837]], [1]], {'0': 0.539737, '1': 0.460263}], ['control: random state 6', [3, [[-0.119, 0.092], [0.264, -0.223], [-0.189, 0.034], [-0.22, -0.267], [-0.291, -0.092], [0.09, -0.02], [0.062, -0.284], [-0.239, 0.039]], [2]], {'0': 0.54953, '1': 0.45047}], ['control: duplicate qubits', [2, [[0.6, 0], [0, 0.8], [0, 0], [0, 0]], [0, 0]], 'duplicate-qubit']], [['regression: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: random state 7', [1, [[0.725, -0.825], [-0.233, -0.266]], [0]], {'0': 0.906073, '1': 0.093927}], ['control: random state 8', [2, [[0.258, -0.148], [-0.044, -0.154], [0.09, 0.21], [0.28, 0.265]], [0]], {'0': 0.446643, '1': 0.553357}], ['control: random state 9', [1, [[-0.25, -0.175], [0.0, 0.0]], [0]], {'0': 1.0}], ['control: random state 10', [3, [[-0.064, 0.212], [0.264, -0.114], [-0.15, -0.24], [0.031, 0.141], [0.037, -0.002], [0.023, -0.229], [0.193, -0.153], [0.251, 0.097]], [0, 1, 2]], {'000': 0.116738, '001': 0.196846, '010': 0.190676, '011': 0.049614, '100': 0.003268, '101': 0.126094, '110': 0.144395, '111': 0.17237}], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length']]]
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
N = 1
observations = []
def solve(x):
    n, amps, qubits = x
    if len(amps) != 1 << n:
        return 'bad-length'
    if len(set(qubits)) != len(qubits):
        return 'duplicate-qubit'
    if any(q < 0 or q >= 1 << n for q in qubits):
        return 'bad-qubit'
    total = sum(re * re + im * im for re, im in amps)
    if total <= 1e-12:
        return 'zero-state'
    acc = {}
    for idx, (a, b) in enumerate(amps):
        p = a * a + b * b
        if p == 0:
            continue
        key = ''.join('1' if idx >> q & 1 else '0' for q in reversed(qubits))
        acc[key] = acc.get(key, 0.0) + p
    out = {}
    for key in sorted(acc):
        v = round(acc[key] / total, 6)
        if v > 0:
            out[key] = v
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: bell state on both qubits', [2, [[0.707107, 0], [0, 0], [0, 0], [0.707107, 0]], [0, 1]], {'00': 0.5, '11': 0.5}], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit'], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length']], [['regression: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: duplicate qubits', [2, [[0.6, 0], [0, 0.8], [0, 0], [0, 0]], [0, 0]], 'duplicate-qubit'], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length'], ['control: all-zero state', [1, [[0, 0], [0, 0]], [0]], 'zero-state'], ['control: tiny but valid state', [1, [[0.0001, 0], [0, 0.0002]], [0]], {'0': 0.2, '1': 0.8}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}]], [['regression: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: unnormalized pair', [1, [[3, 0], [0, 4]], [0]], {'0': 0.36, '1': 0.64}], ['control: random state 0', [2, [[0.0, 0.0], [0.282, -0.043], [-0.278, 0.017], [0.0, 0.0]], [0, 1]], {'01': 0.511954, '10': 0.488046}], ['control: random state 1', [2, [[0.129, 0.125], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], [1, 0]], {'00': 1.0}], ['control: random state 2', [1, [[0.541, -0.923], [-0.479, -0.646]], [0]], {'0': 0.638959, '1': 0.361041}], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit']], [['regression: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: random state 3', [2, [[0.379, 0.308], [-0.129, -0.524], [0.79, -0.299], [-0.403, -0.327]], [0, 1]], {'00': 0.157683, '01': 0.192532, '10': 0.471717, '11': 0.178068}], ['control: random state 4', [1, [[-0.217, 0.525], [-0.308, -0.181]], [0]], {'0': 0.716602, '1': 0.283398}], ['control: random state 5', [2, [[0.175, -0.97], [0.914, 0.939], [-0.22, 0.959], [-0.79, 0.837]], [1]], {'0': 0.539737, '1': 0.460263}], ['control: random state 6', [3, [[-0.119, 0.092], [0.264, -0.223], [-0.189, 0.034], [-0.22, -0.267], [-0.291, -0.092], [0.09, -0.02], [0.062, -0.284], [-0.239, 0.039]], [2]], {'0': 0.54953, '1': 0.45047}], ['control: duplicate qubits', [2, [[0.6, 0], [0, 0.8], [0, 0], [0, 0]], [0, 0]], 'duplicate-qubit']], [['regression: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: random state 7', [1, [[0.725, -0.825], [-0.233, -0.266]], [0]], {'0': 0.906073, '1': 0.093927}], ['control: random state 8', [2, [[0.258, -0.148], [-0.044, -0.154], [0.09, 0.21], [0.28, 0.265]], [0]], {'0': 0.446643, '1': 0.553357}], ['control: random state 9', [1, [[-0.25, -0.175], [0.0, 0.0]], [0]], {'0': 1.0}], ['control: random state 10', [3, [[-0.064, 0.212], [0.264, -0.114], [-0.15, -0.24], [0.031, 0.141], [0.037, -0.002], [0.023, -0.229], [0.193, -0.153], [0.251, 0.097]], [0, 1, 2]], {'000': 0.116738, '001': 0.196846, '010': 0.190676, '011': 0.049614, '100': 0.003268, '101': 0.126094, '110': 0.144395, '111': 0.17237}], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length']]]
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:
Asking for qubit 2 of a two-qubit state returns {"0": 1.0} instead of the "bad-qubit" error.

Input [n, amps, qubits]; amps are 2**n [re, im] pairs (qubit 0 = LSB), possibly unnormalized. Return the marginal outcome distribution over the listed qubits as {bitstring: probability} where the first listed qubit is the rightmost character, probabilities are normalized by the total squared norm, rounded to 6 decimals after summation, and zero entries are omitted. Errors: "bad-length", "duplicate-qubit", "bad-qubit", "zero-state" (total squared norm <= 1e-12).

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
N = 1
observations = []
def solve(x):
    n, amps, qubits = x
    if len(amps) != 1 << n:
        return 'bad-length'
    if len(set(qubits)) != len(qubits):
        return 'duplicate-qubit'
    if any(q < 0 or q >= n for q in qubits):
        return 'bad-qubit'
    total = sum(re * re + im * im for re, im in amps)
    if total <= 1e-12:
        return 'zero-state'
    acc = {}
    for idx, (a, b) in enumerate(amps):
        p = a * a + b * b
        if p == 0:
            continue
        key = ''.join('1' if idx >> q & 1 else '0' for q in reversed(qubits))
        acc[key] = acc.get(key, 0.0) + p
    out = {}
    for key in sorted(acc):
        v = round(acc[key], 6) / round(total, 6)
        if v > 0:
            out[key] = v
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: bell state on both qubits', [2, [[0.707107, 0], [0, 0], [0, 0], [0.707107, 0]], [0, 1]], {'00': 0.5, '11': 0.5}], ['regression: random state 0', [2, [[0.0, 0.0], [0.282, -0.043], [-0.278, 0.017], [0.0, 0.0]], [0, 1]], {'01': 0.511954, '10': 0.488046}], ['repair check: random state 1', [2, [[0.129, 0.125], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], [1, 0]], {'00': 1.0}], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}], ['control: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit']], [['regression: random state 3', [2, [[0.379, 0.308], [-0.129, -0.524], [0.79, -0.299], [-0.403, -0.327]], [0, 1]], {'00': 0.157683, '01': 0.192532, '10': 0.471717, '11': 0.178068}], ['regression: random state 4', [1, [[-0.217, 0.525], [-0.308, -0.181]], [0]], {'0': 0.716602, '1': 0.283398}], ['regression: random state 2', [1, [[0.541, -0.923], [-0.479, -0.646]], [0]], {'0': 0.638959, '1': 0.361041}], ['control: duplicate qubits', [2, [[0.6, 0], [0, 0.8], [0, 0], [0, 0]], [0, 0]], 'duplicate-qubit'], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length'], ['control: all-zero state', [1, [[0, 0], [0, 0]], [0]], 'zero-state'], ['control: unnormalized pair', [1, [[3, 0], [0, 4]], [0]], {'0': 0.36, '1': 0.64}]], [['regression: random state 6', [3, [[-0.119, 0.092], [0.264, -0.223], [-0.189, 0.034], [-0.22, -0.267], [-0.291, -0.092], [0.09, -0.02], [0.062, -0.284], [-0.239, 0.039]], [2]], {'0': 0.54953, '1': 0.45047}], ['regression: random state 7', [1, [[0.725, -0.825], [-0.233, -0.266]], [0]], {'0': 0.906073, '1': 0.093927}], ['regression: random state 4', [1, [[-0.217, 0.525], [-0.308, -0.181]], [0]], {'0': 0.716602, '1': 0.283398}], ['control: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state'], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}], ['control: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit']], [['regression: random state 10', [3, [[-0.064, 0.212], [0.264, -0.114], [-0.15, -0.24], [0.031, 0.141], [0.037, -0.002], [0.023, -0.229], [0.193, -0.153], [0.251, 0.097]], [0, 1, 2]], {'000': 0.116738, '001': 0.196846, '010': 0.190676, '011': 0.049614, '100': 0.003268, '101': 0.126094, '110': 0.144395, '111': 0.17237}], ['regression: random state 11', [2, [[-0.211, 0.28], [0.154, -0.18], [-0.016, -0.095], [-0.157, 0.075]], [0]], {'0': 0.604789, '1': 0.395211}], ['regression: random state 6', [3, [[-0.119, 0.092], [0.264, -0.223], [-0.189, 0.034], [-0.22, -0.267], [-0.291, -0.092], [0.09, -0.02], [0.062, -0.284], [-0.239, 0.039]], [2]], {'0': 0.54953, '1': 0.45047}], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit'], ['control: duplicate qubits', [2, [[0.6, 0], [0, 0.8], [0, 0], [0, 0]], [0, 0]], 'duplicate-qubit'], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length'], ['control: all-zero state', [1, [[0, 0], [0, 0]], [0]], 'zero-state']], [['regression: random state 13', [3, [[0.624, -0.422], [-0.596, 0.926], [-0.719, 1.0], [-0.208, -0.957], [-0.398, 0.533], [-0.189, -0.721], [-0.845, -0.901], [0.144, -0.267]], [0, 1, 2]], {'000': 0.082574, '001': 0.176465, '010': 0.220741, '011': 0.139566, '100': 0.064389, '101': 0.080843, '110': 0.222031, '111': 0.013391}], ['regression: random state 14', [3, [[-0.144, 0.15], [-0.052, 0.123], [0.0, 0.0], [0.145, -0.129], [0.077, 0.218], [0.248, 0.026], [0.0, 0.0], [0.256, 0.015]], [2, 0, 1]], {'000': 0.154343, '001': 0.190816, '010': 0.06366, '011': 0.221969, '110': 0.134459, '111': 0.234753}], ['regression: random state 8', [2, [[0.258, -0.148], [-0.044, -0.154], [0.09, 0.21], [0.28, 0.265]], [0]], {'0': 0.446643, '1': 0.553357}], ['control: unnormalized pair', [1, [[3, 0], [0, 4]], [0]], {'0': 0.36, '1': 0.64}], ['control: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state'], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.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
N = 1
observations = []
def solve(x):
    n, amps, qubits = x
    if len(amps) != 1 << n:
        return 'bad-length'
    if len(set(qubits)) != len(qubits):
        return 'duplicate-qubit'
    if any(q < 0 or q >= n for q in qubits):
        return 'bad-qubit'
    total = sum(re * re + im * im for re, im in amps)
    if total <= 1e-12:
        return 'zero-state'
    acc = {}
    for idx, (a, b) in enumerate(amps):
        p = a * a + b * b
        if p == 0:
            continue
        key = ''.join('1' if idx >> q & 1 else '0' for q in reversed(qubits))
        acc[key] = acc.get(key, 0.0) + p
    out = {}
    for key in sorted(acc):
        v = round(round(acc[key], 3) / total, 6)
        if v > 0:
            out[key] = v
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: bell state on both qubits', [2, [[0.707107, 0], [0, 0], [0, 0], [0.707107, 0]], [0, 1]], {'00': 0.5, '11': 0.5}], ['regression: random state 0', [2, [[0.0, 0.0], [0.282, -0.043], [-0.278, 0.017], [0.0, 0.0]], [0, 1]], {'01': 0.511954, '10': 0.488046}], ['repair check: random state 1', [2, [[0.129, 0.125], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], [1, 0]], {'00': 1.0}], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}], ['control: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit']], [['regression: random state 3', [2, [[0.379, 0.308], [-0.129, -0.524], [0.79, -0.299], [-0.403, -0.327]], [0, 1]], {'00': 0.157683, '01': 0.192532, '10': 0.471717, '11': 0.178068}], ['regression: random state 4', [1, [[-0.217, 0.525], [-0.308, -0.181]], [0]], {'0': 0.716602, '1': 0.283398}], ['regression: random state 2', [1, [[0.541, -0.923], [-0.479, -0.646]], [0]], {'0': 0.638959, '1': 0.361041}], ['control: duplicate qubits', [2, [[0.6, 0], [0, 0.8], [0, 0], [0, 0]], [0, 0]], 'duplicate-qubit'], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length'], ['control: all-zero state', [1, [[0, 0], [0, 0]], [0]], 'zero-state'], ['control: unnormalized pair', [1, [[3, 0], [0, 4]], [0]], {'0': 0.36, '1': 0.64}]], [['regression: random state 6', [3, [[-0.119, 0.092], [0.264, -0.223], [-0.189, 0.034], [-0.22, -0.267], [-0.291, -0.092], [0.09, -0.02], [0.062, -0.284], [-0.239, 0.039]], [2]], {'0': 0.54953, '1': 0.45047}], ['regression: random state 7', [1, [[0.725, -0.825], [-0.233, -0.266]], [0]], {'0': 0.906073, '1': 0.093927}], ['regression: random state 4', [1, [[-0.217, 0.525], [-0.308, -0.181]], [0]], {'0': 0.716602, '1': 0.283398}], ['control: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state'], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}], ['control: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit']], [['regression: random state 10', [3, [[-0.064, 0.212], [0.264, -0.114], [-0.15, -0.24], [0.031, 0.141], [0.037, -0.002], [0.023, -0.229], [0.193, -0.153], [0.251, 0.097]], [0, 1, 2]], {'000': 0.116738, '001': 0.196846, '010': 0.190676, '011': 0.049614, '100': 0.003268, '101': 0.126094, '110': 0.144395, '111': 0.17237}], ['regression: random state 11', [2, [[-0.211, 0.28], [0.154, -0.18], [-0.016, -0.095], [-0.157, 0.075]], [0]], {'0': 0.604789, '1': 0.395211}], ['regression: random state 6', [3, [[-0.119, 0.092], [0.264, -0.223], [-0.189, 0.034], [-0.22, -0.267], [-0.291, -0.092], [0.09, -0.02], [0.062, -0.284], [-0.239, 0.039]], [2]], {'0': 0.54953, '1': 0.45047}], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit'], ['control: duplicate qubits', [2, [[0.6, 0], [0, 0.8], [0, 0], [0, 0]], [0, 0]], 'duplicate-qubit'], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length'], ['control: all-zero state', [1, [[0, 0], [0, 0]], [0]], 'zero-state']], [['regression: random state 13', [3, [[0.624, -0.422], [-0.596, 0.926], [-0.719, 1.0], [-0.208, -0.957], [-0.398, 0.533], [-0.189, -0.721], [-0.845, -0.901], [0.144, -0.267]], [0, 1, 2]], {'000': 0.082574, '001': 0.176465, '010': 0.220741, '011': 0.139566, '100': 0.064389, '101': 0.080843, '110': 0.222031, '111': 0.013391}], ['regression: random state 14', [3, [[-0.144, 0.15], [-0.052, 0.123], [0.0, 0.0], [0.145, -0.129], [0.077, 0.218], [0.248, 0.026], [0.0, 0.0], [0.256, 0.015]], [2, 0, 1]], {'000': 0.154343, '001': 0.190816, '010': 0.06366, '011': 0.221969, '110': 0.134459, '111': 0.234753}], ['regression: random state 8', [2, [[0.258, -0.148], [-0.044, -0.154], [0.09, 0.21], [0.28, 0.265]], [0]], {'0': 0.446643, '1': 0.553357}], ['control: unnormalized pair', [1, [[3, 0], [0, 4]], [0]], {'0': 0.36, '1': 0.64}], ['control: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state'], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.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:
Unnormalized states produce probabilities such as 0.3076923076 with many digits instead of values rounded to 6 decimals.

Input [n, amps, qubits]; amps are 2**n [re, im] pairs (qubit 0 = LSB), possibly unnormalized. Return the marginal outcome distribution over the listed qubits as {bitstring: probability} where the first listed qubit is the rightmost character, probabilities are normalized by the total squared norm, rounded to 6 decimals after summation, and zero entries are omitted. Errors: "bad-length", "duplicate-qubit", "bad-qubit", "zero-state" (total squared norm <= 1e-12).

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
N = 1
observations = []
def solve(x):
    n, amps, qubits = x
    if len(amps) != 1 << n:
        return 'bad-length'
    if len(set(qubits)) != len(qubits):
        return 'duplicate-qubit'
    if any(q < 0 or q >= n for q in qubits):
        return 'bad-qubit'
    total = sum(re * re + im * im for re, im in amps)
    if total == 0:
        return 'zero-state'
    acc = {}
    for idx, (a, b) in enumerate(amps):
        p = a * a + b * b
        if p == 0:
            continue
        key = ''.join('1' if idx >> q & 1 else '0' for q in reversed(qubits))
        acc[key] = acc.get(key, 0.0) + p
    out = {}
    for key in sorted(acc):
        v = round(acc[key] / total, 6)
        if v > 0:
            out[key] = v
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state'], ['repair check: tiny but valid state', [1, [[0.0001, 0], [0, 0.0002]], [0]], {'0': 0.2, '1': 0.8}], ['control: bell state on both qubits', [2, [[0.707107, 0], [0, 0], [0, 0], [0.707107, 0]], [0, 1]], {'00': 0.5, '11': 0.5}], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}], ['control: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['repair check: small valid state', [1, [[0.0005, 0], [0, 0.0005]], [0]], {'0': 0.5, '1': 0.5}]], [['regression: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state'], ['repair check: tiny but valid state', [1, [[0.0001, 0], [0, 0.0002]], [0]], {'0': 0.2, '1': 0.8}], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit'], ['control: duplicate qubits', [2, [[0.6, 0], [0, 0.8], [0, 0], [0, 0]], [0, 0]], 'duplicate-qubit'], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length'], ['control: all-zero state', [1, [[0, 0], [0, 0]], [0]], 'zero-state'], ['repair check: small valid state', [1, [[0.0005, 0], [0, 0.0005]], [0]], {'0': 0.5, '1': 0.5}]], [['regression: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state'], ['repair check: tiny but valid state', [1, [[0.0001, 0], [0, 0.0002]], [0]], {'0': 0.2, '1': 0.8}], ['control: unnormalized pair', [1, [[3, 0], [0, 4]], [0]], {'0': 0.36, '1': 0.64}], ['control: random state 0', [2, [[0.0, 0.0], [0.282, -0.043], [-0.278, 0.017], [0.0, 0.0]], [0, 1]], {'01': 0.511954, '10': 0.488046}], ['control: random state 1', [2, [[0.129, 0.125], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], [1, 0]], {'00': 1.0}], ['control: random state 2', [1, [[0.541, -0.923], [-0.479, -0.646]], [0]], {'0': 0.638959, '1': 0.361041}], ['repair check: small valid state', [1, [[0.0005, 0], [0, 0.0005]], [0]], {'0': 0.5, '1': 0.5}]], [['regression: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state'], ['repair check: tiny but valid state', [1, [[0.0001, 0], [0, 0.0002]], [0]], {'0': 0.2, '1': 0.8}], ['control: random state 3', [2, [[0.379, 0.308], [-0.129, -0.524], [0.79, -0.299], [-0.403, -0.327]], [0, 1]], {'00': 0.157683, '01': 0.192532, '10': 0.471717, '11': 0.178068}], ['control: random state 4', [1, [[-0.217, 0.525], [-0.308, -0.181]], [0]], {'0': 0.716602, '1': 0.283398}], ['control: random state 5', [2, [[0.175, -0.97], [0.914, 0.939], [-0.22, 0.959], [-0.79, 0.837]], [1]], {'0': 0.539737, '1': 0.460263}], ['control: random state 6', [3, [[-0.119, 0.092], [0.264, -0.223], [-0.189, 0.034], [-0.22, -0.267], [-0.291, -0.092], [0.09, -0.02], [0.062, -0.284], [-0.239, 0.039]], [2]], {'0': 0.54953, '1': 0.45047}], ['repair check: small valid state', [1, [[0.0005, 0], [0, 0.0005]], [0]], {'0': 0.5, '1': 0.5}]], [['regression: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state'], ['repair check: tiny but valid state', [1, [[0.0001, 0], [0, 0.0002]], [0]], {'0': 0.2, '1': 0.8}], ['control: random state 7', [1, [[0.725, -0.825], [-0.233, -0.266]], [0]], {'0': 0.906073, '1': 0.093927}], ['control: random state 8', [2, [[0.258, -0.148], [-0.044, -0.154], [0.09, 0.21], [0.28, 0.265]], [0]], {'0': 0.446643, '1': 0.553357}], ['control: random state 9', [1, [[-0.25, -0.175], [0.0, 0.0]], [0]], {'0': 1.0}], ['control: random state 10', [3, [[-0.064, 0.212], [0.264, -0.114], [-0.15, -0.24], [0.031, 0.141], [0.037, -0.002], [0.023, -0.229], [0.193, -0.153], [0.251, 0.097]], [0, 1, 2]], {'000': 0.116738, '001': 0.196846, '010': 0.190676, '011': 0.049614, '100': 0.003268, '101': 0.126094, '110': 0.144395, '111': 0.17237}], ['repair check: small valid state', [1, [[0.0005, 0], [0, 0.0005]], [0]], {'0': 0.5, '1': 0.5}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
N = 1
observations = []
def solve(x):
    n, amps, qubits = x
    if len(amps) != 1 << n:
        return 'bad-length'
    if len(set(qubits)) != len(qubits):
        return 'duplicate-qubit'
    if any(q < 0 or q >= n for q in qubits):
        return 'bad-qubit'
    total = sum(re * re + im * im for re, im in amps)
    if total <= 1e-6:
        return 'zero-state'
    acc = {}
    for idx, (a, b) in enumerate(amps):
        p = a * a + b * b
        if p == 0:
            continue
        key = ''.join('1' if idx >> q & 1 else '0' for q in reversed(qubits))
        acc[key] = acc.get(key, 0.0) + p
    out = {}
    for key in sorted(acc):
        v = round(acc[key] / total, 6)
        if v > 0:
            out[key] = v
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state'], ['repair check: tiny but valid state', [1, [[0.0001, 0], [0, 0.0002]], [0]], {'0': 0.2, '1': 0.8}], ['control: bell state on both qubits', [2, [[0.707107, 0], [0, 0], [0, 0], [0.707107, 0]], [0, 1]], {'00': 0.5, '11': 0.5}], ['control: reversed qubit list', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1, 0]], {'10': 1.0}], ['control: single qubit marginal of |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], [1]], {'0': 1.0}], ['control: out of range qubit equals n', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [2]], 'bad-qubit'], ['repair check: small valid state', [1, [[0.0005, 0], [0, 0.0005]], [0]], {'0': 0.5, '1': 0.5}]], [['regression: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state'], ['repair check: tiny but valid state', [1, [[0.0001, 0], [0, 0.0002]], [0]], {'0': 0.2, '1': 0.8}], ['control: negative qubit index', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], [-1]], 'bad-qubit'], ['control: duplicate qubits', [2, [[0.6, 0], [0, 0.8], [0, 0], [0, 0]], [0, 0]], 'duplicate-qubit'], ['control: wrong amplitude count', [2, [[1, 0], [0, 0]], [0]], 'bad-length'], ['control: all-zero state', [1, [[0, 0], [0, 0]], [0]], 'zero-state'], ['repair check: small valid state', [1, [[0.0005, 0], [0, 0.0005]], [0]], {'0': 0.5, '1': 0.5}]], [['regression: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state'], ['repair check: tiny but valid state', [1, [[0.0001, 0], [0, 0.0002]], [0]], {'0': 0.2, '1': 0.8}], ['control: unnormalized pair', [1, [[3, 0], [0, 4]], [0]], {'0': 0.36, '1': 0.64}], ['control: random state 0', [2, [[0.0, 0.0], [0.282, -0.043], [-0.278, 0.017], [0.0, 0.0]], [0, 1]], {'01': 0.511954, '10': 0.488046}], ['control: random state 1', [2, [[0.129, 0.125], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], [1, 0]], {'00': 1.0}], ['control: random state 2', [1, [[0.541, -0.923], [-0.479, -0.646]], [0]], {'0': 0.638959, '1': 0.361041}], ['repair check: small valid state', [1, [[0.0005, 0], [0, 0.0005]], [0]], {'0': 0.5, '1': 0.5}]], [['regression: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state'], ['repair check: tiny but valid state', [1, [[0.0001, 0], [0, 0.0002]], [0]], {'0': 0.2, '1': 0.8}], ['control: random state 3', [2, [[0.379, 0.308], [-0.129, -0.524], [0.79, -0.299], [-0.403, -0.327]], [0, 1]], {'00': 0.157683, '01': 0.192532, '10': 0.471717, '11': 0.178068}], ['control: random state 4', [1, [[-0.217, 0.525], [-0.308, -0.181]], [0]], {'0': 0.716602, '1': 0.283398}], ['control: random state 5', [2, [[0.175, -0.97], [0.914, 0.939], [-0.22, 0.959], [-0.79, 0.837]], [1]], {'0': 0.539737, '1': 0.460263}], ['control: random state 6', [3, [[-0.119, 0.092], [0.264, -0.223], [-0.189, 0.034], [-0.22, -0.267], [-0.291, -0.092], [0.09, -0.02], [0.062, -0.284], [-0.239, 0.039]], [2]], {'0': 0.54953, '1': 0.45047}], ['repair check: small valid state', [1, [[0.0005, 0], [0, 0.0005]], [0]], {'0': 0.5, '1': 0.5}]], [['regression: near zero state', [1, [[1e-08, 0], [0, 1e-08]], [0]], 'zero-state'], ['repair check: tiny but valid state', [1, [[0.0001, 0], [0, 0.0002]], [0]], {'0': 0.2, '1': 0.8}], ['control: random state 7', [1, [[0.725, -0.825], [-0.233, -0.266]], [0]], {'0': 0.906073, '1': 0.093927}], ['control: random state 8', [2, [[0.258, -0.148], [-0.044, -0.154], [0.09, 0.21], [0.28, 0.265]], [0]], {'0': 0.446643, '1': 0.553357}], ['control: random state 9', [1, [[-0.25, -0.175], [0.0, 0.0]], [0]], {'0': 1.0}], ['control: random state 10', [3, [[-0.064, 0.212], [0.264, -0.114], [-0.15, -0.24], [0.031, 0.141], [0.037, -0.002], [0.023, -0.229], [0.193, -0.153], [0.251, 0.097]], [0, 1, 2]], {'000': 0.116738, '001': 0.196846, '010': 0.190676, '011': 0.049614, '100': 0.003268, '101': 0.126094, '110': 0.144395, '111': 0.17237}], ['repair check: small valid state', [1, [[0.0005, 0], [0, 0.0005]], [0]], {'0': 0.5, '1': 0.5}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A register whose amplitudes are about 1e-8 is normalized into a confident distribution instead of reporting "zero-state".

Input [n, amps, qubits]; amps are 2**n [re, im] pairs (qubit 0 = LSB), possibly unnormalized. Return the marginal outcome distribution over the listed qubits as {bitstring: probability} where the first listed qubit is the rightmost character, probabilities are normalized by the total squared norm, rounded to 6 decimals after summation, and zero entries are omitted. Errors: "bad-length", "duplicate-qubit", "bad-qubit", "zero-state" (total squared norm <= 1e-12).

split: open-access


prompt:
Projecting qubit 1 onto outcome 1 reports "impossible" even when that qubit is certainly excited.

Input [n, amps, q, outcome]; measure qubit q (0 = LSB) of the possibly unnormalized state. Return {"probability": p, "state": post} with p normalized by the total squared norm and the post-measurement state renormalized to unit norm with the other branch zeroed; values rounded to 6 decimals. Return "impossible" when p < 1e-12.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
N = 1
observations = []
def solve(x):
    n, amps, q, outcome = x
    psi = [complex(a, b) for a, b in amps]
    keep = [((i >> q) & 1) == outcome for i in range(len(psi))]
    p = sum(abs(v) ** 2 for i, v in enumerate(psi) if keep[i])
    norm = sum(abs(v) ** 2 for v in psi)
    prob = p / norm
    if prob < 1e-12:
        return 'impossible'
    scale = 1 / p
    post = [v * scale if keep[i] else 0j for i, v in enumerate(psi)]
    return {'probability': round(prob, 6) + 0.0, 'state': [[round(v.real, 6) + 0.0, round(v.imag, 6) + 0.0] for v in post]}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: measure 0 on |+>', [1, [[0.707107, 0], [0.707107, 0]], 0, 0], {'probability': 0.5, 'state': [[1.0, 0.0], [0.0, 0.0]]}], ['regression: measure 1 on |+>', [1, [[0.707107, 0], [0.707107, 0]], 0, 1], {'probability': 0.5, 'state': [[0.0, 0.0], [1.0, 0.0]]}], ['regression: unnormalized bell measure qubit 1', [2, [[2, 0], [0, 0], [0, 0], [2, 0]], 1, 0], {'probability': 0.5, 'state': [[1.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}], ['control: impossible outcome', [1, [[1, 0], [0, 0]], 0, 1], 'impossible'], ['control: near impossible branch', [1, [[1, 0], [1e-08, 0]], 0, 1], 'impossible'], ['control: random collapse 1', [2, [[0.5, 0.0], [0.0, 0.0], [0.0, -0.0], [-0.0, -0.0]], 0, 1], 'impossible'], ['control: random collapse 2', [2, [[0.0, 0.0], [0.0, 0.0], [0.591, 0.404], [0.405, 0.306]], 1, 0], 'impossible']], [['regression: tiny norm state', [1, [[3e-07, 0], [0, 4e-07]], 0, 1], {'probability': 0.64, 'state': [[0.0, 0.0], [0.0, 1.0]]}], ['regression: random collapse 0', [1, [[-0.305, 0.494], [0.447, -0.241]], 0, 0], {'probability': 0.566536, 'state': [[-0.525346, 0.850889], [0.0, 0.0]]}], ['regression: random collapse 3', [3, [[0.755, 0.514], [0.624, 0.382], [-0.745, 0.675], [-0.295, 0.287], [0.799, -0.013], [-0.459, 0.107], [0.751, 0.682], [0.988, 0.816]], 0, 1], {'probability': 0.422407, 'state': [[0.0, 0.0], [0.38933, 0.23834], [0.0, 0.0], [-0.184058, 0.179067], [0.0, 0.0], [-0.286382, 0.06676], [0.0, 0.0], [0.616439, 0.509123]]}], ['control: random collapse 7', [2, [[0.5, 0.0], [-0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0, 1], 'impossible'], ['control: random collapse 12', [1, [[0.5, 0.0], [0.0, 0.0]], 0, 1], 'impossible'], ['control: random collapse 16', [2, [[0.5, 0.0], [-0.0, 0.0], [0.0, -0.0], [-0.0, 0.0]], 0, 1], 'impossible'], ['control: random collapse 22', [2, [[0.5, 0.0], [-0.0, -0.0], [0.0, 0.0], [-0.0, -0.0]], 0, 1], 'impossible']], [['regression: random collapse 4', [1, [[-0.125, -0.177], [0.075, 0.432]], 0, 0], {'probability': 0.196294, 'state': [[-0.576864, -0.81684], [0.0, 0.0]]}], ['regression: random collapse 5', [3, [[0.5, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [-0.0, 0.0], [0.0, 0.0], [-0.0, -0.0]], 1, 0], {'probability': 1.0, 'state': [[1.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}], ['regression: random collapse 6', [3, [[-0.436, -0.015], [-0.35, 0.017], [0.17, 0.002], [0.369, -0.461], [0.411, -0.033], [-0.475, 0.29], [0.379, -0.067], [-0.298, 0.408]], 2, 0], {'probability': 0.438863, 'state': [[-0.524618, -0.018049], [-0.421138, 0.020455], [0.204553, 0.002407], [0.444, -0.554699], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}], ['control: random collapse 23', [2, [[0.5, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, -0.0]], 0, 1], 'impossible'], ['control: random collapse 34', [2, [[0.5, 0.0], [0.0, 0.0], [0.0, 0.0], [-0.0, 0.0]], 1, 1], 'impossible'], ['control: random collapse 37', [3, [[0.5, 0.0], [-0.0, -0.0], [0.0, -0.0], [0.0, 0.0], [0.0, -0.0], [-0.0, 0.0], [-0.0, -0.0], [-0.0, 0.0]], 0, 1], 'impossible'], ['control: impossible outcome', [1, [[1, 0], [0, 0]], 0, 1], 'impossible']], [['regression: random collapse 8', [2, [[-1.464, -0.995], [0.052, 0.311], [0.0, 0.0], [0.0, 0.0]], 0, 0], {'probability': 0.969244, 'state': [[-0.827063, -0.562109], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}], ['regression: random collapse 9', [1, [[0.307, 0.202], [-0.161, 0.592]], 0, 0], {'probability': 0.264065, 'state': [[0.835384, 0.549666], [0.0, 0.0]]}], ['regression: random collapse 6', [3, [[-0.436, -0.015], [-0.35, 0.017], [0.17, 0.002], [0.369, -0.461], [0.411, -0.033], [-0.475, 0.29], [0.379, -0.067], [-0.298, 0.408]], 2, 0], {'probability': 0.438863, 'state': [[-0.524618, -0.018049], [-0.421138, 0.020455], [0.204553, 0.002407], [0.444, -0.554699], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}], ['control: near impossible branch', [1, [[1, 0], [1e-08, 0]], 0, 1], 'impossible'], ['control: random collapse 1', [2, [[0.5, 0.0], [0.0, 0.0], [0.0, -0.0], [-0.0, -0.0]], 0, 1], 'impossible'], ['control: random collapse 2', [2, [[0.0, 0.0], [0.0, 0.0], [0.591, 0.404], [0.405, 0.306]], 1, 0], 'impossible'], ['control: random collapse 7', [2, [[0.5, 0.0], [-0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0, 1], 'impossible']], [['regression: random collapse 11', [2, [[0.414, -0.429], [-0.452, -0.264], [0.422, -0.171], [-0.123, 0.056]], 0, 1], {'probability': 0.34182, 'state': [[0.0, 0.0], [-0.836084, -0.488332], [0.0, 0.0], [-0.227518, 0.103586]]}], ['regression: random collapse 13', [3, [[0.164, 0.188], [0.288, -0.262], [0.203, 0.372], [0.485, -0.006], [-0.386, -0.389], [-0.128, 0.167], [-0.162, 0.438], [-0.379, -0.043]], 0, 1], {'probability': 0.431321, 'state': [[0.0, 0.0], [0.379272, -0.345032], [0.0, 0.0], [0.638704, -0.007901], [0.0, 0.0], [-0.168565, 0.219925], [0.0, 0.0], [-0.499111, -0.056627]]}], ['regression: random collapse 9', [1, [[0.307, 0.202], [-0.161, 0.592]], 0, 0], {'probability': 0.264065, 'state': [[0.835384, 0.549666], [0.0, 0.0]]}], ['control: random collapse 12', [1, [[0.5, 0.0], [0.0, 0.0]], 0, 1], 'impossible'], ['control: random collapse 16', [2, [[0.5, 0.0], [-0.0, 0.0], [0.0, -0.0], [-0.0, 0.0]], 0, 1], 'impossible'], ['control: random collapse 22', [2, [[0.5, 0.0], [-0.0, -0.0], [0.0, 0.0], [-0.0, -0.0]], 0, 1], 'impossible'], ['control: random collapse 23', [2, [[0.5, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, -0.0]], 0, 1], 'impossible']]]
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
N = 1
observations = []
def solve(x):
    n, amps, q, outcome = x
    psi = [complex(a, b) for a, b in amps]
    keep = [((i >> q) & 1) == outcome for i in range(len(psi))]
    p = sum(abs(v) ** 2 for i, v in enumerate(psi) if keep[i])
    norm = sum(abs(v) ** 2 for v in psi)
    prob = p / norm
    if prob < 1e-12:
        return 'impossible'
    scale = 1 / math.sqrt(prob)
    post = [v * scale if keep[i] else 0j for i, v in enumerate(psi)]
    return {'probability': round(prob, 6) + 0.0, 'state': [[round(v.real, 6) + 0.0, round(v.imag, 6) + 0.0] for v in post]}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: measure 0 on |+>', [1, [[0.707107, 0], [0.707107, 0]], 0, 0], {'probability': 0.5, 'state': [[1.0, 0.0], [0.0, 0.0]]}], ['regression: measure 1 on |+>', [1, [[0.707107, 0], [0.707107, 0]], 0, 1], {'probability': 0.5, 'state': [[0.0, 0.0], [1.0, 0.0]]}], ['regression: unnormalized bell measure qubit 1', [2, [[2, 0], [0, 0], [0, 0], [2, 0]], 1, 0], {'probability': 0.5, 'state': [[1.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}], ['control: impossible outcome', [1, [[1, 0], [0, 0]], 0, 1], 'impossible'], ['control: near impossible branch', [1, [[1, 0], [1e-08, 0]], 0, 1], 'impossible'], ['control: random collapse 1', [2, [[0.5, 0.0], [0.0, 0.0], [0.0, -0.0], [-0.0, -0.0]], 0, 1], 'impossible'], ['control: random collapse 2', [2, [[0.0, 0.0], [0.0, 0.0], [0.591, 0.404], [0.405, 0.306]], 1, 0], 'impossible']], [['regression: tiny norm state', [1, [[3e-07, 0], [0, 4e-07]], 0, 1], {'probability': 0.64, 'state': [[0.0, 0.0], [0.0, 1.0]]}], ['regression: random collapse 0', [1, [[-0.305, 0.494], [0.447, -0.241]], 0, 0], {'probability': 0.566536, 'state': [[-0.525346, 0.850889], [0.0, 0.0]]}], ['regression: random collapse 3', [3, [[0.755, 0.514], [0.624, 0.382], [-0.745, 0.675], [-0.295, 0.287], [0.799, -0.013], [-0.459, 0.107], [0.751, 0.682], [0.988, 0.816]], 0, 1], {'probability': 0.422407, 'state': [[0.0, 0.0], [0.38933, 0.23834], [0.0, 0.0], [-0.184058, 0.179067], [0.0, 0.0], [-0.286382, 0.06676], [0.0, 0.0], [0.616439, 0.509123]]}], ['control: random collapse 7', [2, [[0.5, 0.0], [-0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0, 1], 'impossible'], ['control: random collapse 12', [1, [[0.5, 0.0], [0.0, 0.0]], 0, 1], 'impossible'], ['control: random collapse 16', [2, [[0.5, 0.0], [-0.0, 0.0], [0.0, -0.0], [-0.0, 0.0]], 0, 1], 'impossible'], ['control: random collapse 22', [2, [[0.5, 0.0], [-0.0, -0.0], [0.0, 0.0], [-0.0, -0.0]], 0, 1], 'impossible']], [['regression: random collapse 4', [1, [[-0.125, -0.177], [0.075, 0.432]], 0, 0], {'probability': 0.196294, 'state': [[-0.576864, -0.81684], [0.0, 0.0]]}], ['regression: random collapse 5', [3, [[0.5, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [-0.0, 0.0], [0.0, 0.0], [-0.0, -0.0]], 1, 0], {'probability': 1.0, 'state': [[1.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}], ['regression: random collapse 6', [3, [[-0.436, -0.015], [-0.35, 0.017], [0.17, 0.002], [0.369, -0.461], [0.411, -0.033], [-0.475, 0.29], [0.379, -0.067], [-0.298, 0.408]], 2, 0], {'probability': 0.438863, 'state': [[-0.524618, -0.018049], [-0.421138, 0.020455], [0.204553, 0.002407], [0.444, -0.554699], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}], ['control: random collapse 23', [2, [[0.5, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, -0.0]], 0, 1], 'impossible'], ['control: random collapse 34', [2, [[0.5, 0.0], [0.0, 0.0], [0.0, 0.0], [-0.0, 0.0]], 1, 1], 'impossible'], ['control: random collapse 37', [3, [[0.5, 0.0], [-0.0, -0.0], [0.0, -0.0], [0.0, 0.0], [0.0, -0.0], [-0.0, 0.0], [-0.0, -0.0], [-0.0, 0.0]], 0, 1], 'impossible'], ['control: impossible outcome', [1, [[1, 0], [0, 0]], 0, 1], 'impossible']], [['regression: random collapse 8', [2, [[-1.464, -0.995], [0.052, 0.311], [0.0, 0.0], [0.0, 0.0]], 0, 0], {'probability': 0.969244, 'state': [[-0.827063, -0.562109], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}], ['regression: random collapse 9', [1, [[0.307, 0.202], [-0.161, 0.592]], 0, 0], {'probability': 0.264065, 'state': [[0.835384, 0.549666], [0.0, 0.0]]}], ['regression: random collapse 6', [3, [[-0.436, -0.015], [-0.35, 0.017], [0.17, 0.002], [0.369, -0.461], [0.411, -0.033], [-0.475, 0.29], [0.379, -0.067], [-0.298, 0.408]], 2, 0], {'probability': 0.438863, 'state': [[-0.524618, -0.018049], [-0.421138, 0.020455], [0.204553, 0.002407], [0.444, -0.554699], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}], ['control: near impossible branch', [1, [[1, 0], [1e-08, 0]], 0, 1], 'impossible'], ['control: random collapse 1', [2, [[0.5, 0.0], [0.0, 0.0], [0.0, -0.0], [-0.0, -0.0]], 0, 1], 'impossible'], ['control: random collapse 2', [2, [[0.0, 0.0], [0.0, 0.0], [0.591, 0.404], [0.405, 0.306]], 1, 0], 'impossible'], ['control: random collapse 7', [2, [[0.5, 0.0], [-0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0, 1], 'impossible']], [['regression: random collapse 11', [2, [[0.414, -0.429], [-0.452, -0.264], [0.422, -0.171], [-0.123, 0.056]], 0, 1], {'probability': 0.34182, 'state': [[0.0, 0.0], [-0.836084, -0.488332], [0.0, 0.0], [-0.227518, 0.103586]]}], ['regression: random collapse 13', [3, [[0.164, 0.188], [0.288, -0.262], [0.203, 0.372], [0.485, -0.006], [-0.386, -0.389], [-0.128, 0.167], [-0.162, 0.438], [-0.379, -0.043]], 0, 1], {'probability': 0.431321, 'state': [[0.0, 0.0], [0.379272, -0.345032], [0.0, 0.0], [0.638704, -0.007901], [0.0, 0.0], [-0.168565, 0.219925], [0.0, 0.0], [-0.499111, -0.056627]]}], ['regression: random collapse 9', [1, [[0.307, 0.202], [-0.161, 0.592]], 0, 0], {'probability': 0.264065, 'state': [[0.835384, 0.549666], [0.0, 0.0]]}], ['control: random collapse 12', [1, [[0.5, 0.0], [0.0, 0.0]], 0, 1], 'impossible'], ['control: random collapse 16', [2, [[0.5, 0.0], [-0.0, 0.0], [0.0, -0.0], [-0.0, 0.0]], 0, 1], 'impossible'], ['control: random collapse 22', [2, [[0.5, 0.0], [-0.0, -0.0], [0.0, 0.0], [-0.0, -0.0]], 0, 1], 'impossible'], ['control: random collapse 23', [2, [[0.5, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, -0.0]], 0, 1], 'impossible']]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
After measuring |+> the post-measurement amplitude is 1.414 instead of 1.0.

Input [n, amps, q, outcome]; measure qubit q (0 = LSB) of the possibly unnormalized state. Return {"probability": p, "state": post} with p normalized by the total squared norm and the post-measurement state renormalized to unit norm with the other branch zeroed; values rounded to 6 decimals. Return "impossible" when p < 1e-12.

split: open-access


prompt:
An input 2|00> + 2|11> reports probability 4 for outcome 0 of qubit 1.

Input [n, amps, q, outcome]; measure qubit q (0 = LSB) of the possibly unnormalized state. Return {"probability": p, "state": post} with p normalized by the total squared norm and the post-measurement state renormalized to unit norm with the other branch zeroed; values rounded to 6 decimals. Return "impossible" when p < 1e-12.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
N = 1
observations = []
def solve(x):
    n, amps, q, outcome = x
    psi = [complex(a, b) for a, b in amps]
    keep = [((i >> q) & 1) == outcome for i in range(len(psi))]
    p = sum(abs(v) ** 2 for i, v in enumerate(psi) if keep[i])
    norm = sum(abs(v) ** 2 for v in psi)
    prob = p / norm
    if p < 1e-12:
        return 'impossible'
    scale = 1 / math.sqrt(p)
    post = [v * scale if keep[i] else 0j for i, v in enumerate(psi)]
    return {'probability': round(prob, 6) + 0.0, 'state': [[round(v.real, 6) + 0.0, round(v.imag, 6) + 0.0] for v in post]}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: tiny norm state', [1, [[3e-07, 0], [0, 4e-07]], 0, 1], {'probability': 0.64, 'state': [[0.0, 0.0], [0.0, 1.0]]}], ['repair check: near impossible branch', [1, [[1, 0], [1e-08, 0]], 0, 1], 'impossible'], ['control: measure 0 on |+>', [1, [[0.707107, 0], [0.707107, 0]], 0, 0], {'probability': 0.5, 'state': [[1.0, 0.0], [0.0, 0.0]]}], ['control: measure 1 on |+>', [1, [[0.707107, 0], [0.707107, 0]], 0, 1], {'probability': 0.5, 'state': [[0.0, 0.0], [1.0, 0.0]]}], ['control: impossible outcome', [1, [[1, 0], [0, 0]], 0, 1], 'impossible'], ['control: unnormalized bell measure qubit 1', [2, [[2, 0], [0, 0], [0, 0], [2, 0]], 1, 0], {'probability': 0.5, 'state': [[1.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}]], [['regression: tiny norm state', [1, [[3e-07, 0], [0, 4e-07]], 0, 1], {'probability': 0.64, 'state': [[0.0, 0.0], [0.0, 1.0]]}], ['repair check: near impossible branch', [1, [[1, 0], [1e-08, 0]], 0, 1], 'impossible'], ['control: random collapse 0', [1, [[-0.305, 0.494], [0.447, -0.241]], 0, 0], {'probability': 0.566536, 'state': [[-0.525346, 0.850889], [0.0, 0.0]]}], ['control: random collapse 1', [2, [[0.5, 0.0], [0.0, 0.0], [0.0, -0.0], [-0.0, -0.0]], 0, 1], 'impossible'], ['control: random collapse 2', [2, [[0.0, 0.0], [0.0, 0.0], [0.591, 0.404], [0.405, 0.306]], 1, 0], 'impossible'], ['control: random collapse 3', [3, [[0.755, 0.514], [0.624, 0.382], [-0.745, 0.675], [-0.295, 0.287], [0.799, -0.013], [-0.459, 0.107], [0.751, 0.682], [0.988, 0.816]], 0, 1], {'probability': 0.422407, 'state': [[0.0, 0.0], [0.38933, 0.23834], [0.0, 0.0], [-0.184058, 0.179067], [0.0, 0.0], [-0.286382, 0.06676], [0.0, 0.0], [0.616439, 0.509123]]}]], [['regression: tiny norm state', [1, [[3e-07, 0], [0, 4e-07]], 0, 1], {'probability': 0.64, 'state': [[0.0, 0.0], [0.0, 1.0]]}], ['repair check: near impossible branch', [1, [[1, 0], [1e-08, 0]], 0, 1], 'impossible'], ['control: random collapse 4', [1, [[-0.125, -0.177], [0.075, 0.432]], 0, 0], {'probability': 0.196294, 'state': [[-0.576864, -0.81684], [0.0, 0.0]]}], ['control: random collapse 5', [3, [[0.5, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [-0.0, 0.0], [0.0, 0.0], [-0.0, -0.0]], 1, 0], {'probability': 1.0, 'state': [[1.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}], ['control: random collapse 6', [3, [[-0.436, -0.015], [-0.35, 0.017], [0.17, 0.002], [0.369, -0.461], [0.411, -0.033], [-0.475, 0.29], [0.379, -0.067], [-0.298, 0.408]], 2, 0], {'probability': 0.438863, 'state': [[-0.524618, -0.018049], [-0.421138, 0.020455], [0.204553, 0.002407], [0.444, -0.554699], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}], ['control: random collapse 7', [2, [[0.5, 0.0], [-0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0, 1], 'impossible']], [['regression: tiny norm state', [1, [[3e-07, 0], [0, 4e-07]], 0, 1], {'probability': 0.64, 'state': [[0.0, 0.0], [0.0, 1.0]]}], ['repair check: near impossible branch', [1, [[1, 0], [1e-08, 0]], 0, 1], 'impossible'], ['control: random collapse 8', [2, [[-1.464, -0.995], [0.052, 0.311], [0.0, 0.0], [0.0, 0.0]], 0, 0], {'probability': 0.969244, 'state': [[-0.827063, -0.562109], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}], ['control: random collapse 9', [1, [[0.307, 0.202], [-0.161, 0.592]], 0, 0], {'probability': 0.264065, 'state': [[0.835384, 0.549666], [0.0, 0.0]]}], ['control: random collapse 10', [2, [[0.359, -0.323], [-0.182, -0.34], [0.238, 0.218], [-0.224, 0.46]], 0, 1], {'probability': 0.548886, 'state': [[0.0, 0.0], [-0.284063, -0.530667], [0.0, 0.0], [-0.349616, 0.717962]]}], ['control: random collapse 11', [2, [[0.414, -0.429], [-0.452, -0.264], [0.422, -0.171], [-0.123, 0.056]], 0, 1], {'probability': 0.34182, 'state': [[0.0, 0.0], [-0.836084, -0.488332], [0.0, 0.0], [-0.227518, 0.103586]]}]], [['regression: tiny norm state', [1, [[3e-07, 0], [0, 4e-07]], 0, 1], {'probability': 0.64, 'state': [[0.0, 0.0], [0.0, 1.0]]}], ['repair check: near impossible branch', [1, [[1, 0], [1e-08, 0]], 0, 1], 'impossible'], ['control: random collapse 12', [1, [[0.5, 0.0], [0.0, 0.0]], 0, 1], 'impossible'], ['control: random collapse 13', [3, [[0.164, 0.188], [0.288, -0.262], [0.203, 0.372], [0.485, -0.006], [-0.386, -0.389], [-0.128, 0.167], [-0.162, 0.438], [-0.379, -0.043]], 0, 1], {'probability': 0.431321, 'state': [[0.0, 0.0], [0.379272, -0.345032], [0.0, 0.0], [0.638704, -0.007901], [0.0, 0.0], [-0.168565, 0.219925], [0.0, 0.0], [-0.499111, -0.056627]]}], ['control: random collapse 14', [2, [[-1.5, 0.759], [0.0, 0.0], [0.0, 0.0], [-2.588, 1.898]], 1, 0], {'probability': 0.2153, 'state': [[-0.892275, 0.451491], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}], ['control: random collapse 15', [1, [[0.489, -0.285], [-0.403, 0.425]], 0, 1], {'probability': 0.5171, 'state': [[0.0, 0.0], [-0.688076, 0.725639]]}]]]
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
N = 1
observations = []
def solve(x):
    n, amps, q, outcome = x
    psi = [complex(a, b) for a, b in amps]
    keep = [((i >> q) & 1) == outcome for i in range(len(psi))]
    p = sum(abs(v) ** 2 for i, v in enumerate(psi) if keep[i])
    norm = sum(abs(v) ** 2 for v in psi)
    prob = p / norm
    if prob <= 0.0:
        return 'impossible'
    scale = 1 / math.sqrt(p)
    post = [v * scale if keep[i] else 0j for i, v in enumerate(psi)]
    return {'probability': round(prob, 6) + 0.0, 'state': [[round(v.real, 6) + 0.0, round(v.imag, 6) + 0.0] for v in post]}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: tiny norm state', [1, [[3e-07, 0], [0, 4e-07]], 0, 1], {'probability': 0.64, 'state': [[0.0, 0.0], [0.0, 1.0]]}], ['repair check: near impossible branch', [1, [[1, 0], [1e-08, 0]], 0, 1], 'impossible'], ['control: measure 0 on |+>', [1, [[0.707107, 0], [0.707107, 0]], 0, 0], {'probability': 0.5, 'state': [[1.0, 0.0], [0.0, 0.0]]}], ['control: measure 1 on |+>', [1, [[0.707107, 0], [0.707107, 0]], 0, 1], {'probability': 0.5, 'state': [[0.0, 0.0], [1.0, 0.0]]}], ['control: impossible outcome', [1, [[1, 0], [0, 0]], 0, 1], 'impossible'], ['control: unnormalized bell measure qubit 1', [2, [[2, 0], [0, 0], [0, 0], [2, 0]], 1, 0], {'probability': 0.5, 'state': [[1.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}]], [['regression: tiny norm state', [1, [[3e-07, 0], [0, 4e-07]], 0, 1], {'probability': 0.64, 'state': [[0.0, 0.0], [0.0, 1.0]]}], ['repair check: near impossible branch', [1, [[1, 0], [1e-08, 0]], 0, 1], 'impossible'], ['control: random collapse 0', [1, [[-0.305, 0.494], [0.447, -0.241]], 0, 0], {'probability': 0.566536, 'state': [[-0.525346, 0.850889], [0.0, 0.0]]}], ['control: random collapse 1', [2, [[0.5, 0.0], [0.0, 0.0], [0.0, -0.0], [-0.0, -0.0]], 0, 1], 'impossible'], ['control: random collapse 2', [2, [[0.0, 0.0], [0.0, 0.0], [0.591, 0.404], [0.405, 0.306]], 1, 0], 'impossible'], ['control: random collapse 3', [3, [[0.755, 0.514], [0.624, 0.382], [-0.745, 0.675], [-0.295, 0.287], [0.799, -0.013], [-0.459, 0.107], [0.751, 0.682], [0.988, 0.816]], 0, 1], {'probability': 0.422407, 'state': [[0.0, 0.0], [0.38933, 0.23834], [0.0, 0.0], [-0.184058, 0.179067], [0.0, 0.0], [-0.286382, 0.06676], [0.0, 0.0], [0.616439, 0.509123]]}]], [['regression: tiny norm state', [1, [[3e-07, 0], [0, 4e-07]], 0, 1], {'probability': 0.64, 'state': [[0.0, 0.0], [0.0, 1.0]]}], ['repair check: near impossible branch', [1, [[1, 0], [1e-08, 0]], 0, 1], 'impossible'], ['control: random collapse 4', [1, [[-0.125, -0.177], [0.075, 0.432]], 0, 0], {'probability': 0.196294, 'state': [[-0.576864, -0.81684], [0.0, 0.0]]}], ['control: random collapse 5', [3, [[0.5, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [-0.0, 0.0], [0.0, 0.0], [-0.0, -0.0]], 1, 0], {'probability': 1.0, 'state': [[1.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}], ['control: random collapse 6', [3, [[-0.436, -0.015], [-0.35, 0.017], [0.17, 0.002], [0.369, -0.461], [0.411, -0.033], [-0.475, 0.29], [0.379, -0.067], [-0.298, 0.408]], 2, 0], {'probability': 0.438863, 'state': [[-0.524618, -0.018049], [-0.421138, 0.020455], [0.204553, 0.002407], [0.444, -0.554699], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}], ['control: random collapse 7', [2, [[0.5, 0.0], [-0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], 0, 1], 'impossible']], [['regression: tiny norm state', [1, [[3e-07, 0], [0, 4e-07]], 0, 1], {'probability': 0.64, 'state': [[0.0, 0.0], [0.0, 1.0]]}], ['repair check: near impossible branch', [1, [[1, 0], [1e-08, 0]], 0, 1], 'impossible'], ['control: random collapse 8', [2, [[-1.464, -0.995], [0.052, 0.311], [0.0, 0.0], [0.0, 0.0]], 0, 0], {'probability': 0.969244, 'state': [[-0.827063, -0.562109], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}], ['control: random collapse 9', [1, [[0.307, 0.202], [-0.161, 0.592]], 0, 0], {'probability': 0.264065, 'state': [[0.835384, 0.549666], [0.0, 0.0]]}], ['control: random collapse 10', [2, [[0.359, -0.323], [-0.182, -0.34], [0.238, 0.218], [-0.224, 0.46]], 0, 1], {'probability': 0.548886, 'state': [[0.0, 0.0], [-0.284063, -0.530667], [0.0, 0.0], [-0.349616, 0.717962]]}], ['control: random collapse 11', [2, [[0.414, -0.429], [-0.452, -0.264], [0.422, -0.171], [-0.123, 0.056]], 0, 1], {'probability': 0.34182, 'state': [[0.0, 0.0], [-0.836084, -0.488332], [0.0, 0.0], [-0.227518, 0.103586]]}]], [['regression: tiny norm state', [1, [[3e-07, 0], [0, 4e-07]], 0, 1], {'probability': 0.64, 'state': [[0.0, 0.0], [0.0, 1.0]]}], ['repair check: near impossible branch', [1, [[1, 0], [1e-08, 0]], 0, 1], 'impossible'], ['control: random collapse 12', [1, [[0.5, 0.0], [0.0, 0.0]], 0, 1], 'impossible'], ['control: random collapse 13', [3, [[0.164, 0.188], [0.288, -0.262], [0.203, 0.372], [0.485, -0.006], [-0.386, -0.389], [-0.128, 0.167], [-0.162, 0.438], [-0.379, -0.043]], 0, 1], {'probability': 0.431321, 'state': [[0.0, 0.0], [0.379272, -0.345032], [0.0, 0.0], [0.638704, -0.007901], [0.0, 0.0], [-0.168565, 0.219925], [0.0, 0.0], [-0.499111, -0.056627]]}], ['control: random collapse 14', [2, [[-1.5, 0.759], [0.0, 0.0], [0.0, 0.0], [-2.588, 1.898]], 1, 0], {'probability': 0.2153, 'state': [[-0.892275, 0.451491], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]}], ['control: random collapse 15', [1, [[0.489, -0.285], [-0.403, 0.425]], 0, 1], {'probability': 0.5171, 'state': [[0.0, 0.0], [-0.688076, 0.725639]]}]]]
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 valid state stored with amplitudes around 1e-7 is reported as "impossible" although the outcome has probability 0.64.

Input [n, amps, q, outcome]; measure qubit q (0 = LSB) of the possibly unnormalized state. Return {"probability": p, "state": post} with p normalized by the total squared norm and the post-measurement state renormalized to unit norm with the other branch zeroed; values rounded to 6 decimals. Return "impossible" when p < 1e-12.

split: open-access


prompt:
After observing 0 on |+> the returned state still has a 0.707 amplitude on |1>.

Input [n, amps, q, outcome]; measure qubit q (0 = LSB) of the possibly unnormalized state. Return {"probability": p, "state": post} with p normalized by the total squared norm and the post-measurement state renormalized to unit norm with the other branch zeroed; values rounded to 6 decimals. Return "impossible" when p < 1e-12.

split: open-access


prompt:
<ZI> on |01> (qubit 0 excited) evaluates to -1 instead of +1.

Input [n, amps, pauli]; pauli is an n-character string over I,X,Y,Z whose rightmost character acts on qubit 0 (LSB). Return the real expectation <psi|P|psi>/<psi|psi> rounded to 6 decimals. Errors: "length-mismatch", "bad-pauli" (any character outside uppercase IXYZ).

split: open-access


prompt:
<Y> on the |+i> eigenstate evaluates to -1 instead of +1.

Input [n, amps, pauli]; pauli is an n-character string over I,X,Y,Z whose rightmost character acts on qubit 0 (LSB). Return the real expectation <psi|P|psi>/<psi|psi> rounded to 6 decimals. Errors: "length-mismatch", "bad-pauli" (any character outside uppercase IXYZ).

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
N = 1
observations = []
def solve(x):
    n, amps, pauli = x
    if len(pauli) != n:
        return 'length-mismatch'
    if any(c not in 'IXYZ' for c in pauli):
        return 'bad-pauli'
    psi = [complex(a, b) for a, b in amps]
    acc = 0j
    for i, v in enumerate(psi):
        j = i
        phase = 1 + 0j
        for k, c in enumerate(pauli):
            q = n - 1 - k
            bit = (i >> q) & 1
            if c in 'XY':
                j ^= 1 << q
            if c == 'Z' and bit:
                phase = -phase
            elif c == 'Y':
                phase *= 1j if bit == 0 else -1j
        acc += psi[j] * phase * v
    norm = sum(abs(v) ** 2 for v in psi)
    return round((acc / norm).real, 6) + 0.0
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: Y on |+i>', [1, [[0.707107, 0], [0, 0.707107]], 'Y'], 1.0], ['regression: unnormalized Z', [1, [[3, 0], [0, 4]], 'Z'], -0.28], ['regression: random expectation 4', [1, [[-0.408, 0.461], [-0.359, 0.326]], 'Y'], 0.105809], ['control: ZI on |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], 'ZI'], 1.0], ['control: IZ on |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], 'IZ'], -1.0], ['control: X on |+>', [1, [[0.707107, 0], [0.707107, 0]], 'X'], 1.0], ['control: short string', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], 'Z'], 'length-mismatch']], [['regression: random expectation 2', [1, [[0.489, 0.112], [-0.446, 0.868]], 'X'], -0.200793], ['regression: random expectation 3', [1, [[-0.119, -0.141], [0.0, 0.0]], 'Z'], 1.0], ['regression: random expectation 13', [2, [[-0.447, -0.821], [0.189, -0.56], [0.105, 0.959], [0.741, -0.692]], 'XY'], 0.425941], ['control: long string', [1, [[1, 0], [0, 0]], 'ZZ'], 'length-mismatch'], ['control: short X on bell', [2, [[0.707107, 0], [0, 0], [0, 0], [0.707107, 0]], 'X'], 'length-mismatch'], ['control: short Y on three qubits', [3, [[0.5, 0], [0, 0.5], [0, 0], [0.5, 0], [0, 0], [0, 0], [0, 0], [0.5, 0]], 'YZ'], 'length-mismatch'], ['control: short Z on excited high qubit', [2, [[0, 0], [0, 0], [1, 0], [0, 0]], 'Z'], 'length-mismatch']], [['regression: random expectation 5', [3, [[0.616, -0.07], [-0.126, -0.492], [-0.385, 0.046], [-0.714, -0.852], [0.968, -0.035], [-0.129, -0.794], [-0.299, 0.172], [0.772, -0.916]], 'YYZ'], -0.229604], ['regression: random expectation 6', [2, [[0.423, -0.627], [-0.829, 0.103], [0.962, -0.142], [-0.494, -0.183]], 'XX'], -0.7271], ['regression: random expectation 15', [2, [[0.599, 0.301], [1.478, -0.323], [-0.934, -1.434], [0.207, -0.762]], 'YZ'], 0.153104], ['control: short ZX', [3, [[0.6, 0], [0.8, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], 'ZX'], 'length-mismatch'], ['control: lowercase pauli', [1, [[1, 0], [0, 0]], 'z'], 'bad-pauli'], ['control: random expectation 1', [1, [[0.0, 0.0], [1.97, 0.65]], 'X'], 0.0], ['control: random expectation 22', [1, [[0.5, 0.0], [0.0, 0.0]], 'Z'], 1.0]], [['regression: random expectation 8', [1, [[0.52, 0.586], [-0.138, -0.781]], 'I'], 1.0], ['regression: random expectation 9', [3, [[0.685, -0.091], [1.56, 1.459], [-1.17, 0.687], [-0.305, 1.609], [-0.876, -1.673], [-0.535, 0.008], [-1.818, 1.331], [1.693, 1.162]], 'IXI'], -0.046053], ['regression: random expectation 19', [3, [[-1.467, 1.96], [1.04, 1.59], [0.568, -0.807], [-1.506, -0.905], [-0.169, 1.361], [-1.64, -0.753], [1.853, -1.891], [0.222, 0.162]], 'ZIY'], -0.70921], ['control: random expectation 45', [1, [[0.0, 0.0], [-0.122, 1.228]], 'Y'], 0.0], ['control: ZI on |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], 'ZI'], 1.0], ['control: IZ on |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], 'IZ'], -1.0], ['control: X on |+>', [1, [[0.707107, 0], [0.707107, 0]], 'X'], 1.0]], [['regression: random expectation 11', [1, [[-0.422, -1.932], [-1.435, 1.833]], 'Z'], -0.161676], ['regression: random expectation 12', [3, [[1.741, 0.945], [0.0, 0.0], [0.705, -0.271], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [-1.277, -0.575], [-0.732, 0.787]], 'ZZX'], 0.12672], ['regression: random expectation 26', [2, [[0.234, -0.739], [-0.627, 0.79], [0.0, 0.0], [0.107, -0.508]], 'YI'], 0.247917], ['control: short string', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], 'Z'], 'length-mismatch'], ['control: long string', [1, [[1, 0], [0, 0]], 'ZZ'], 'length-mismatch'], ['control: short X on bell', [2, [[0.707107, 0], [0, 0], [0, 0], [0.707107, 0]], 'X'], 'length-mismatch'], ['control: short Y on three qubits', [3, [[0.5, 0], [0, 0.5], [0, 0], [0.5, 0], [0, 0], [0, 0], [0, 0], [0.5, 0]], 'YZ'], 'length-mismatch']]]
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
N = 1
observations = []
def solve(x):
    n, amps, pauli = x
    if len(pauli) != n:
        return 'length-mismatch'
    if any(c not in 'IXYZ' for c in pauli):
        return 'bad-pauli'
    psi = [complex(a, b) for a, b in amps]
    acc = 0j
    for i, v in enumerate(psi):
        j = i
        phase = 1 + 0j
        for k, c in enumerate(pauli):
            q = n - 1 - k
            bit = (i >> q) & 1
            if c in 'XY':
                j ^= 1 << q
            if c == 'Z' and bit:
                phase = -phase
            elif c == 'Y':
                phase *= 1j if bit == 0 else -1j
        acc += psi[j].conjugate() * phase.conjugate() * v
    norm = sum(abs(v) ** 2 for v in psi)
    return round((acc / norm).real, 6) + 0.0
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: Y on |+i>', [1, [[0.707107, 0], [0, 0.707107]], 'Y'], 1.0], ['regression: unnormalized Z', [1, [[3, 0], [0, 4]], 'Z'], -0.28], ['regression: random expectation 4', [1, [[-0.408, 0.461], [-0.359, 0.326]], 'Y'], 0.105809], ['control: ZI on |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], 'ZI'], 1.0], ['control: IZ on |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], 'IZ'], -1.0], ['control: X on |+>', [1, [[0.707107, 0], [0.707107, 0]], 'X'], 1.0], ['control: short string', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], 'Z'], 'length-mismatch']], [['regression: random expectation 2', [1, [[0.489, 0.112], [-0.446, 0.868]], 'X'], -0.200793], ['regression: random expectation 3', [1, [[-0.119, -0.141], [0.0, 0.0]], 'Z'], 1.0], ['regression: random expectation 13', [2, [[-0.447, -0.821], [0.189, -0.56], [0.105, 0.959], [0.741, -0.692]], 'XY'], 0.425941], ['control: long string', [1, [[1, 0], [0, 0]], 'ZZ'], 'length-mismatch'], ['control: short X on bell', [2, [[0.707107, 0], [0, 0], [0, 0], [0.707107, 0]], 'X'], 'length-mismatch'], ['control: short Y on three qubits', [3, [[0.5, 0], [0, 0.5], [0, 0], [0.5, 0], [0, 0], [0, 0], [0, 0], [0.5, 0]], 'YZ'], 'length-mismatch'], ['control: short Z on excited high qubit', [2, [[0, 0], [0, 0], [1, 0], [0, 0]], 'Z'], 'length-mismatch']], [['regression: random expectation 5', [3, [[0.616, -0.07], [-0.126, -0.492], [-0.385, 0.046], [-0.714, -0.852], [0.968, -0.035], [-0.129, -0.794], [-0.299, 0.172], [0.772, -0.916]], 'YYZ'], -0.229604], ['regression: random expectation 6', [2, [[0.423, -0.627], [-0.829, 0.103], [0.962, -0.142], [-0.494, -0.183]], 'XX'], -0.7271], ['regression: random expectation 15', [2, [[0.599, 0.301], [1.478, -0.323], [-0.934, -1.434], [0.207, -0.762]], 'YZ'], 0.153104], ['control: short ZX', [3, [[0.6, 0], [0.8, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], 'ZX'], 'length-mismatch'], ['control: lowercase pauli', [1, [[1, 0], [0, 0]], 'z'], 'bad-pauli'], ['control: random expectation 1', [1, [[0.0, 0.0], [1.97, 0.65]], 'X'], 0.0], ['control: random expectation 22', [1, [[0.5, 0.0], [0.0, 0.0]], 'Z'], 1.0]], [['regression: random expectation 8', [1, [[0.52, 0.586], [-0.138, -0.781]], 'I'], 1.0], ['regression: random expectation 9', [3, [[0.685, -0.091], [1.56, 1.459], [-1.17, 0.687], [-0.305, 1.609], [-0.876, -1.673], [-0.535, 0.008], [-1.818, 1.331], [1.693, 1.162]], 'IXI'], -0.046053], ['regression: random expectation 19', [3, [[-1.467, 1.96], [1.04, 1.59], [0.568, -0.807], [-1.506, -0.905], [-0.169, 1.361], [-1.64, -0.753], [1.853, -1.891], [0.222, 0.162]], 'ZIY'], -0.70921], ['control: random expectation 45', [1, [[0.0, 0.0], [-0.122, 1.228]], 'Y'], 0.0], ['control: ZI on |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], 'ZI'], 1.0], ['control: IZ on |01>', [2, [[0, 0], [1, 0], [0, 0], [0, 0]], 'IZ'], -1.0], ['control: X on |+>', [1, [[0.707107, 0], [0.707107, 0]], 'X'], 1.0]], [['regression: random expectation 11', [1, [[-0.422, -1.932], [-1.435, 1.833]], 'Z'], -0.161676], ['regression: random expectation 12', [3, [[1.741, 0.945], [0.0, 0.0], [0.705, -0.271], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [-1.277, -0.575], [-0.732, 0.787]], 'ZZX'], 0.12672], ['regression: random expectation 26', [2, [[0.234, -0.739], [-0.627, 0.79], [0.0, 0.0], [0.107, -0.508]], 'YI'], 0.247917], ['control: short string', [2, [[1, 0], [0, 0], [0, 0], [0, 0]], 'Z'], 'length-mismatch'], ['control: long string', [1, [[1, 0], [0, 0]], 'ZZ'], 'length-mismatch'], ['control: short X on bell', [2, [[0.707107, 0], [0, 0], [0, 0], [0.707107, 0]], 'X'], 'length-mismatch'], ['control: short Y on three qubits', [3, [[0.5, 0], [0, 0.5], [0, 0], [0.5, 0], [0, 0], [0, 0], [0, 0], [0.5, 0]], 'YZ'], 'length-mismatch']]]
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:
Complex states return expectations that differ from <psi|P|psi>, e.g. <X> on |+i> is nonzero.

Input [n, amps, pauli]; pauli is an n-character string over I,X,Y,Z whose rightmost character acts on qubit 0 (LSB). Return the real expectation <psi|P|psi>/<psi|psi> rounded to 6 decimals. Errors: "length-mismatch", "bad-pauli" (any character outside uppercase IXYZ).

split: open-access