"""Ideal Bell write–read baseline. Run with Python 3 and NumPy."""
import numpy as np

# Qubit order is reference, system, memory. The memory starts in |0>.
initial = np.zeros((2, 2, 2), dtype=complex)
initial[0, 0, 0] = initial[1, 1, 0] = 1 / np.sqrt(2)
x = np.array([[0, 1], [1, 0]])
z = np.diag([1, -1])
observables = [np.kron(np.kron(p, p), np.eye(2)) for p in (x, z)]


def correlations(state):
    vector = state.ravel()
    return np.array([np.vdot(vector, op @ vector).real for op in observables])


written = initial.swapaxes(1, 2).copy()
assert np.allclose(written[:, 1, :], 0)  # Original system cleared by the write.
intact = written.swapaxes(1, 2)
flipped = (written * np.array([1, -1])).swapaxes(1, 2)
conditions = [
    ("Direct Bell", correlations(initial), [1, 1]),
    ("Coherent write-read", correlations(intact), [1, 1]),
    ("Memory Z flip", correlations(flipped), [-1, 1]),
    ("Balanced dephasing", (correlations(intact) + correlations(flipped)) / 2, [0, 1]),
]
print("Ideal state-vector expectations (no hardware shots)")
for label, values, expected in conditions:
    np.testing.assert_allclose(values, expected, atol=1e-12)
    print(f"{label:20} XX {values[0]:+.3f}  ZZ {values[1]:+.3f}")
