{ "cells": [ { "cell_type": "markdown", "id": "59ea7797", "metadata": {}, "source": [ "# 05 - Multi-Qubit Gates\n", "\n", "Explore two-qubit and three-qubit gates: CNOT, CZ, SWAP, Toffoli, Fredkin, and more.\n", "\n", "**Concepts:** Controlled gates, SWAP family, Toffoli, Fredkin" ] }, { "cell_type": "code", "execution_count": null, "id": "f40bef98", "metadata": {}, "outputs": [], "source": [ "import quantsdk as qs\n", "import math" ] }, { "cell_type": "markdown", "id": "8058c63d", "metadata": {}, "source": [ "## CNOT (CX) — The Entanglement Gate" ] }, { "cell_type": "code", "execution_count": null, "id": "b2aa79be", "metadata": {}, "outputs": [], "source": [ "# CNOT flips target when control is |1>\n", "for ctrl_state in ['0', '1']:\n", " circuit = qs.Circuit(2)\n", " if ctrl_state == '1':\n", " circuit.x(0) # Set control to |1>\n", " circuit.cx(0, 1).measure_all()\n", " result = qs.run(circuit, shots=100, seed=42)\n", " print(f\"CNOT with control={ctrl_state}: {result.counts}\")" ] }, { "cell_type": "markdown", "id": "d81fb2de", "metadata": {}, "source": [ "## Controlled Gates (CY, CZ, CH, CS, CSX)" ] }, { "cell_type": "code", "execution_count": null, "id": "9ae8c10a", "metadata": {}, "outputs": [], "source": [ "controlled_gates = [\n", " (\"CY\", lambda c: c.cy(0, 1)),\n", " (\"CZ\", lambda c: c.cz(0, 1)),\n", " (\"CH\", lambda c: c.ch(0, 1)),\n", " (\"CS\", lambda c: c.cs(0, 1)),\n", " (\"CSX\", lambda c: c.csx(0, 1)),\n", "]\n", "\n", "print(\"Controlled gates with control=|1>:\")\n", "for name, apply_gate in controlled_gates:\n", " circuit = qs.Circuit(2)\n", " circuit.x(0) # Set control to |1>\n", " apply_gate(circuit)\n", " circuit.measure_all()\n", " result = qs.run(circuit, shots=1000, seed=42)\n", " print(f\" {name:4s}: {result.counts}\")" ] }, { "cell_type": "markdown", "id": "da9e5015", "metadata": {}, "source": [ "## Controlled Rotations (CRX, CRY, CRZ, CP)" ] }, { "cell_type": "code", "execution_count": null, "id": "47510dc6", "metadata": {}, "outputs": [], "source": [ "# CRY with control=|1>\n", "for theta in [0, math.pi/4, math.pi/2, math.pi]:\n", " circuit = qs.Circuit(2).x(0).cry(0, 1, theta).measure_all()\n", " result = qs.run(circuit, shots=1000, seed=42)\n", " p_target_1 = sum(c for bs, c in result.counts.items() if bs[1] == '1') / 1000\n", " print(f\"CRY(theta={theta:.2f}): P(target=|1>) = {p_target_1:.3f}\")" ] }, { "cell_type": "markdown", "id": "863667ee", "metadata": {}, "source": [ "## SWAP Gate Family" ] }, { "cell_type": "code", "execution_count": null, "id": "6951725f", "metadata": {}, "outputs": [], "source": [ "# SWAP: exchanges qubit states\n", "circuit = qs.Circuit(2).x(0).swap(0, 1).measure_all() # |10> -> |01>\n", "result = qs.run(circuit, shots=100, seed=42)\n", "print(f\"SWAP |10> = {result.counts}\")\n", "\n", "# iSWAP: SWAP with a phase\n", "circuit = qs.Circuit(2).x(0).iswap(0, 1).measure_all()\n", "result = qs.run(circuit, shots=100, seed=42)\n", "print(f\"iSWAP |10> = {result.counts}\")\n", "\n", "# DCX (Double-CX)\n", "circuit = qs.Circuit(2).x(0).dcx(0, 1).measure_all()\n", "result = qs.run(circuit, shots=100, seed=42)\n", "print(f\"DCX |10> = {result.counts}\")" ] }, { "cell_type": "markdown", "id": "d2ce118a", "metadata": {}, "source": [ "## Three-Qubit Gates" ] }, { "cell_type": "code", "execution_count": null, "id": "b7156555", "metadata": {}, "outputs": [], "source": [ "# Toffoli (CCX): AND gate — flips target only when both controls are |1>\n", "for input_state in ['000', '010', '100', '110']:\n", " circuit = qs.Circuit(3)\n", " if input_state[0] == '1': circuit.x(0)\n", " if input_state[1] == '1': circuit.x(1)\n", " circuit.ccx(0, 1, 2).measure_all()\n", " result = qs.run(circuit, shots=100, seed=42)\n", " print(f\"Toffoli |{input_state}> = {result.most_likely}\")\n", "\n", "print(\"\\nToffoli acts as AND gate on the target qubit!\")" ] }, { "cell_type": "code", "execution_count": null, "id": "9275b84b", "metadata": {}, "outputs": [], "source": [ "# Fredkin (CSWAP): Controlled-SWAP\n", "# Swaps qubits 1,2 only when control (qubit 0) is |1>\n", "circuit = qs.Circuit(3).x(0).x(1).cswap(0, 1, 2).measure_all()\n", "result = qs.run(circuit, shots=100, seed=42)\n", "print(f\"Fredkin |110> = {result.most_likely} (swapped q1,q2)\")\n", "\n", "circuit = qs.Circuit(3).x(1).cswap(0, 1, 2).measure_all()\n", "result = qs.run(circuit, shots=100, seed=42)\n", "print(f\"Fredkin |010> = {result.most_likely} (control=0, no swap)\")" ] }, { "cell_type": "code", "execution_count": null, "id": "05eeced7", "metadata": {}, "outputs": [], "source": [ "# CCZ: Controlled-CZ\n", "# Applies phase -1 only to |111>\n", "circuit = qs.Circuit(3)\n", "circuit.h(0).h(1).h(2) # Superposition\n", "circuit.ccz(0, 1, 2) # Phase flip |111>\n", "circuit.h(0).h(1).h(2) # Interfere\n", "circuit.measure_all()\n", "result = qs.run(circuit, shots=2000, seed=42)\n", "print(f\"H-CCZ-H on |+++>: {result.counts}\")" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }