{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "model",
   "metadata": {},
   "source": [
    "# Hidden or Erased? A Four-Qubit Recovery Check\n",
    "\n",
    "This exact, noiseless model uses a reference R, a message M and two key qubits, KX and KZ. It requires Python 3 with NumPy. All results come from density matrices, without sampling or quantum hardware.\n",
    "\n",
    "Prepare R and M in a Bell state and both keys in |+⟩. Apply controlled-Z from KZ to M, then controlled-X from KX to M. The decoder applies the inverse sequence. Its output is scored by the overlap of R and M with the original Bell state.\n"
   ]
  },
  {
   "cell_type": "code",
   "id": "encoder",
   "metadata": {},
   "source": [
    "import numpy as np\n",
    "\n",
    "bell = np.array([1, 0, 0, 1]) / np.sqrt(2)\n",
    "keys = np.ones(4) / 2\n",
    "zero = np.array([1, 0])\n",
    "\n",
    "def density(state):\n",
    "    return np.outer(state, state.conj())\n",
    "\n",
    "def reference_and_message(state):\n",
    "    return np.trace(state.reshape(4, 4, 4, 4), axis1=1, axis2=3)\n",
    "\n",
    "# Basis order: reference, message, X key, Z key.\n",
    "encoder = np.zeros((16, 16), dtype=complex)\n",
    "for source in range(16):\n",
    "    reference, message, key_x, key_z = np.unravel_index(source, (2, 2, 2, 2))\n",
    "    output = (reference, message ^ key_x, key_x, key_z)\n",
    "    target = np.ravel_multi_index(output, (2, 2, 2, 2))\n",
    "    encoder[target, source] = (-1) ** (message * key_z)\n",
    "decoder = encoder.conj().T\n",
    "np.testing.assert_allclose(decoder @ encoder, np.eye(16))\n"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "fidelity",
   "metadata": {},
   "source": [
    "key_state = density(keys)\n",
    "hidden = density(encoder @ np.kron(bell, keys))\n",
    "withheld = np.kron(reference_and_message(hidden), key_state)\n",
    "\n",
    "# The erased input is replaced by |0⟩ independently of the reference.\n",
    "erased_input = np.kron(np.kron(np.eye(2) / 2, density(zero)), key_state)\n",
    "erased = encoder @ erased_input @ decoder\n",
    "\n",
    "for state in (hidden, erased):\n",
    "    message = np.trace(reference_and_message(state).reshape(2, 2, 2, 2), axis1=0, axis2=2)\n",
    "    np.testing.assert_allclose(message, np.eye(2) / 2)\n",
    "\n",
    "for label, state, expected in [\n",
    "    (\"Both original keys available\", hidden, 1.0),\n",
    "    (\"Original keys withheld\", withheld, 0.25),\n",
    "    (\"Input erased before encoding\", erased, 0.25),\n",
    "]:\n",
    "    recovered = reference_and_message(decoder @ state @ encoder)\n",
    "    fidelity = float(np.real(bell.conj() @ recovered @ bell))\n",
    "    np.testing.assert_allclose(fidelity, expected)\n",
    "    print(f\"{label}: {fidelity:.2f} Bell-state fidelity\")\n"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "scope",
   "metadata": {},
   "source": [
    "The message alone is maximally mixed in both the hidden and erased cases. With both original keys, the decoder restores the Bell state. The two controls yield the overlap of an uncorrelated output with the Bell target in this setup. This calculation tests the specified circuit and its controls. A device run would add hardware noise, finite sampling and calibration.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
