{ "cells": [ { "cell_type": "markdown", "id": "fd407eed", "metadata": {}, "source": [ "# 03 - GHZ State\n", "\n", "Create a Greenberger-Horne-Zeilinger (GHZ) state — multi-qubit entanglement.\n", "\n", "$$|GHZ\\rangle = \\frac{1}{\\sqrt{2}}(|000\\rangle + |111\\rangle)$$\n", "\n", "**Concepts:** Multi-qubit entanglement, scaling, circuit depth" ] }, { "cell_type": "code", "execution_count": null, "id": "868f8761", "metadata": {}, "outputs": [], "source": [ "import quantsdk as qs" ] }, { "cell_type": "markdown", "id": "995bc7ad", "metadata": {}, "source": [ "## 3-Qubit GHZ" ] }, { "cell_type": "code", "execution_count": null, "id": "fa05b2ee", "metadata": {}, "outputs": [], "source": [ "ghz3 = qs.Circuit(3, name=\"GHZ-3\")\n", "ghz3.h(0).cx(0, 1).cx(0, 2).measure_all()\n", "\n", "result = qs.run(ghz3, shots=2000, seed=42)\n", "print(f\"3-qubit GHZ: {result.counts}\")\n", "print(f\"Only |000> and |111> — all qubits entangled!\")" ] }, { "cell_type": "markdown", "id": "5b1ab380", "metadata": {}, "source": [ "## Scaling GHZ to N Qubits" ] }, { "cell_type": "code", "execution_count": null, "id": "a4f1e9d5", "metadata": {}, "outputs": [], "source": [ "def make_ghz(n: int) -> qs.Circuit:\n", " \"\"\"Create an n-qubit GHZ state.\"\"\"\n", " circuit = qs.Circuit(n, name=f\"GHZ-{n}\")\n", " circuit.h(0)\n", " for i in range(1, n):\n", " circuit.cx(0, i)\n", " circuit.measure_all()\n", " return circuit\n", "\n", "for n in [3, 5, 8, 10]:\n", " circuit = make_ghz(n)\n", " result = qs.run(circuit, shots=1000, seed=42)\n", " all_zeros = '0' * n\n", " all_ones = '1' * n\n", " p0 = result.counts.get(all_zeros, 0) / 1000\n", " p1 = result.counts.get(all_ones, 0) / 1000\n", " print(f\"GHZ-{n:2d}: P({all_zeros})={p0:.3f}, P({all_ones})={p1:.3f}, depth={circuit.depth}\")" ] }, { "cell_type": "markdown", "id": "a7bc281f", "metadata": {}, "source": [ "## W State (Alternative Entanglement)\n", "\n", "The W state distributes entanglement differently:\n", "\n", "$$|W\\rangle = \\frac{1}{\\sqrt{3}}(|001\\rangle + |010\\rangle + |100\\rangle)$$" ] }, { "cell_type": "code", "execution_count": null, "id": "4228d23f", "metadata": {}, "outputs": [], "source": [ "import math\n", "\n", "# Approximate 3-qubit W state\n", "w_state = qs.Circuit(3, name=\"W-state\")\n", "w_state.ry(0, 2 * math.acos(math.sqrt(1/3))) # P(|1>) = 1/3\n", "w_state.ch(0, 1) # Controlled-H\n", "w_state.cx(1, 2) # Spread entanglement\n", "w_state.cx(0, 1) # Correct state\n", "w_state.x(0) # Flip q0\n", "w_state.measure_all()\n", "\n", "result = qs.run(w_state, shots=3000, seed=42)\n", "print(f\"W state: {result.counts}\")\n", "print(f\"Probabilities: {result.probabilities}\")" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }