"""Validate a correlated phase-flip model using complex two-qubit matrices.""" from itertools import product from json import dumps from math import cos, exp, fsum, hypot, lgamma, log, log1p, pi, sin import numpy as np SIGNS = np.asarray(list(product((1, -1), repeat=2))) PARITY = SIGNS.prod(axis=1) READOUTS = np.column_stack((SIGNS, PARITY)) KETS = np.column_stack((np.ones(4), SIGNS[:, 1], SIGNS[:, 0], PARITY)) / 2 ALPHA = 0.01 def analytic_spectrum(a, b, shared, phase): cosine, sine = cos(phase / 2), sin(phase / 2) r_plus = hypot(cosine * (a + b), sine * (a - b)) r_minus = hypot(cosine * (a - b), sine * (a + b)) return sorted([(1 + shared + r_plus) / 4, (1 + shared - r_plus) / 4, (1 - shared + r_minus) / 4, (1 - shared - r_minus) / 4]) def direct_spectrum(probabilities, phase): amplitudes = KETS * np.exp(1j * phase * PARITY / 4)[None, :] density = sum(p * np.outer(ket, ket.conj()) for p, ket in zip(probabilities, amplitudes)) assert np.allclose(density.diagonal(), .25, rtol=0, atol=5e-16) partial_transpose = density.reshape(2, 2, 2, 2).transpose(0, 3, 2, 1).reshape(4, 4) return np.linalg.eigvalsh(partial_transpose) def joint_probabilities(a, b, shared): return (1 + READOUTS @ [a, b, shared]) / 4 def binomial_probabilities(shots, probability): factorial = lgamma(shots + 1) hit, miss = log(probability), log1p(-probability) return [exp(factorial - lgamma(k + 1) - lgamma(shots - k + 1) + k * hit + (shots - k) * miss) for k in range(shots + 1)] def design(shots, probability): null = binomial_probabilities(shots, .5) cutoff = shots while cutoff > 0 and fsum(null[cutoff-1:]) <= ALPHA: cutoff -= 1 size = fsum(null[cutoff:]) power = fsum(binomial_probabilities(shots, probability)[cutoff:]) assert size <= ALPHA < fsum(null[cutoff-1:]) return {"shots": shots, "acceptAboveOrEqual": cutoff, "nullTail": size, "powerAtExample": power} random = np.random.default_rng(20261130) worst = 0.0 ordered = 0 for probabilities, phase in zip(random.dirichlet([.5] * 4, size=20000), random.uniform(-pi, pi, 20000)): a, b, shared = probabilities @ READOUTS # Locally relabel the two calibration outcomes to make both means nonnegative. a_sign, b_sign = (1 if a >= 0 else -1), (1 if b >= 0 else -1) observed = direct_spectrum(probabilities, phase) expected = analytic_spectrum(abs(a), abs(b), a_sign * b_sign * shared, phase) error = max(abs(observed - expected)) assert error < 2e-15 worst = max(worst, error) ordered += 1 examples = [] for label, shared in [('Least correlated allowed', -.10), ('Independent flips', .2025), ('Always shared flips', 1)]: a = b = .45 expected = analytic_spectrum(a, b, shared, pi) joint = joint_probabilities(a, b, shared) assert min(joint) >= -5e-16 assert max(abs(direct_spectrum(joint, pi) - expected)) < 2e-15 examples.append({"condition": label, "visibilityEachArm": a, "pairedSignCorrelation": shared, "bothPositiveProbability": float(joint[0]), "negativity": max(0, -expected[0])}) single_setup = design(100, examples[2]["bothPositiveProbability"]) mild_setup = design(6000, examples[1]["bothPositiveProbability"]) assert single_setup["powerAtExample"] > .90 and mild_setup["powerAtExample"] > .90 print(dumps({ "purpose": "Synthetic controlled-phase circuit with local correlated binary phase flips. No experiment was performed.", "densityMatrixChecks": ordered, "maximumAbsoluteSpectrumError": worst, "examplesAtPi": examples, "fixedSampleDesign": {"oneSidedAlpha": ALPHA, "sharedExample": single_setup, "independentExample": mild_setup}, }, indent=2))