{ "cells": [ { "cell_type": "markdown", "id": "8abea617", "metadata": {}, "source": [ "# 22 - Advanced Gates Showcase\n", "\n", "Explore all 50+ gates available in QuantSDK.\n", "\n", "**Concepts:** Full gate library, rotation gates, controlled gates, multi-qubit gates" ] }, { "cell_type": "code", "execution_count": null, "id": "89a8224b", "metadata": {}, "outputs": [], "source": [ "import quantsdk as qs\n", "import math" ] }, { "cell_type": "markdown", "id": "2e02881d", "metadata": {}, "source": [ "## Single-Qubit Gates" ] }, { "cell_type": "code", "execution_count": null, "id": "97e9a76e", "metadata": {}, "outputs": [], "source": [ "# Pauli gates\n", "for gate_name in ['X', 'Y', 'Z']:\n", " circuit = qs.Circuit(1, name=gate_name)\n", " getattr(circuit, gate_name.lower())(0)\n", " circuit.measure(0)\n", " result = qs.run(circuit, shots=100, seed=42)\n", " print(f\"{gate_name} gate: {result.counts}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "3dc0eee1", "metadata": {}, "outputs": [], "source": [ "# Phase gates: S, Sdg, T, Tdg\n", "for gate in ['s', 'sdg', 't', 'tdg']:\n", " circuit = qs.Circuit(1, name=gate)\n", " circuit.h(0) # Create superposition first\n", " getattr(circuit, gate)(0)\n", " circuit.h(0)\n", " circuit.measure(0)\n", " result = qs.run(circuit, shots=1000, seed=42)\n", " print(f\"{gate:4s}: {result.counts}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "d63038a7", "metadata": {}, "outputs": [], "source": [ "# SX gate (sqrt-X)\n", "circuit = qs.Circuit(1)\n", "circuit.sx(0).sx(0) # SX^2 = X\n", "circuit.measure(0)\n", "result = qs.run(circuit, shots=100, seed=42)\n", "print(f\"SX*SX = X: {result.counts} (should be all 1s)\")\n", "\n", "# Identity\n", "circuit = qs.Circuit(1)\n", "circuit.i(0)\n", "circuit.measure(0)\n", "result = qs.run(circuit, shots=100, seed=42)\n", "print(f\"I gate: {result.counts} (should be all 0s)\")" ] }, { "cell_type": "markdown", "id": "5db90bb6", "metadata": {}, "source": [ "## Rotation Gates" ] }, { "cell_type": "code", "execution_count": null, "id": "f6f80a7d", "metadata": {}, "outputs": [], "source": [ "# RX, RY, RZ at pi\n", "for gate in ['rx', 'ry', 'rz']:\n", " circuit = qs.Circuit(1, name=gate)\n", " getattr(circuit, gate)(0, math.pi)\n", " circuit.measure(0)\n", " result = qs.run(circuit, shots=100, seed=42)\n", " print(f\"{gate}(pi): {result.counts}\")\n", "\n", "print()\n", "\n", "# U3 gate (most general single-qubit)\n", "circuit = qs.Circuit(1)\n", "circuit.u3(0, math.pi, 0, math.pi) # Equivalent to X\n", "circuit.measure(0)\n", "result = qs.run(circuit, shots=100, seed=42)\n", "print(f\"U3(pi,0,pi) = X: {result.counts}\")\n", "\n", "# Phase gate\n", "circuit = qs.Circuit(1)\n", "circuit.h(0).phase(0, math.pi / 4).h(0)\n", "circuit.measure(0)\n", "result = qs.run(circuit, shots=1000, seed=42)\n", "print(f\"Phase(pi/4): {result.counts}\")" ] }, { "cell_type": "markdown", "id": "0a253b76", "metadata": {}, "source": [ "## Controlled Gates" ] }, { "cell_type": "code", "execution_count": null, "id": "0b1a9278", "metadata": {}, "outputs": [], "source": [ "# CX, CY, CZ\n", "for gate in ['cx', 'cy', 'cz']:\n", " circuit = qs.Circuit(2)\n", " circuit.x(0) # Control = |1>\n", " getattr(circuit, gate)(0, 1)\n", " circuit.measure_all()\n", " result = qs.run(circuit, shots=100, seed=42)\n", " print(f\"{gate.upper()} (ctrl=1): {result.most_likely}\")\n", "\n", "print()\n", "\n", "# CH (controlled-Hadamard)\n", "circuit = qs.Circuit(2)\n", "circuit.x(0).ch(0, 1).measure_all()\n", "result = qs.run(circuit, shots=1000, seed=42)\n", "print(f\"CH (ctrl=1): {result.counts}\")\n", "\n", "# CS, CSdg\n", "for gate in ['cs', 'csdg']:\n", " circuit = qs.Circuit(2)\n", " circuit.h(1).x(0)\n", " getattr(circuit, gate)(0, 1)\n", " circuit.h(1).measure_all()\n", " result = qs.run(circuit, shots=1000, seed=42)\n", " print(f\"{gate.upper()}: {result.counts}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "26fee215", "metadata": {}, "outputs": [], "source": [ "# Controlled rotations: CRX, CRY, CRZ, CP\n", "for gate in ['crx', 'cry', 'crz', 'cp']:\n", " circuit = qs.Circuit(2)\n", " circuit.x(0) # Control = 1\n", " getattr(circuit, gate)(0, 1, math.pi / 2)\n", " circuit.measure_all()\n", " result = qs.run(circuit, shots=1000, seed=42)\n", " print(f\"{gate.upper()}(pi/2): {result.counts}\")" ] }, { "cell_type": "markdown", "id": "53780f7f", "metadata": {}, "source": [ "## Two-Qubit Special Gates" ] }, { "cell_type": "code", "execution_count": null, "id": "85ff69ab", "metadata": {}, "outputs": [], "source": [ "# SWAP\n", "circuit = qs.Circuit(2)\n", "circuit.x(0).swap(0, 1).measure_all()\n", "result = qs.run(circuit, shots=100, seed=42)\n", "print(f\"SWAP |10> -> {result.most_likely}\")\n", "\n", "# iSWAP\n", "circuit = qs.Circuit(2)\n", "circuit.x(0).iswap(0, 1).measure_all()\n", "result = qs.run(circuit, shots=1000, seed=42)\n", "print(f\"iSWAP |10>: {result.counts}\")\n", "\n", "# DCX (double CNOT)\n", "circuit = qs.Circuit(2)\n", "circuit.x(0).dcx(0, 1).measure_all()\n", "result = qs.run(circuit, shots=100, seed=42)\n", "print(f\"DCX |10>: {result.most_likely}\")\n", "\n", "# ECR gate\n", "circuit = qs.Circuit(2)\n", "circuit.ecr(0, 1).measure_all()\n", "result = qs.run(circuit, shots=1000, seed=42)\n", "print(f\"ECR |00>: {result.counts}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "01363607", "metadata": {}, "outputs": [], "source": [ "# Ising coupling gates: RZZ, RXX, RYY, RZX\n", "for gate in ['rzz', 'rxx', 'ryy', 'rzx']:\n", " circuit = qs.Circuit(2)\n", " getattr(circuit, gate)(0, 1, math.pi / 4)\n", " circuit.measure_all()\n", " result = qs.run(circuit, shots=1000, seed=42)\n", " print(f\"{gate.upper()}(pi/4): {result.counts}\")" ] }, { "cell_type": "markdown", "id": "2b6b2078", "metadata": {}, "source": [ "## Three-Qubit Gates" ] }, { "cell_type": "code", "execution_count": null, "id": "ae8c05a7", "metadata": {}, "outputs": [], "source": [ "# Toffoli (CCX)\n", "circuit = qs.Circuit(3)\n", "circuit.x(0).x(1).ccx(0, 1, 2).measure_all()\n", "result = qs.run(circuit, shots=100, seed=42)\n", "print(f\"Toffoli |110> -> {result.most_likely} (target flipped)\")\n", "\n", "# CCZ\n", "circuit = qs.Circuit(3)\n", "circuit.x(0).x(1).h(2).ccz(0, 1, 2).h(2).measure_all()\n", "result = qs.run(circuit, shots=100, seed=42)\n", "print(f\"CCZ equiv: {result.most_likely}\")\n", "\n", "# Fredkin (CSWAP)\n", "circuit = qs.Circuit(3)\n", "circuit.x(0).x(1).cswap(0, 1, 2).measure_all() # Swap q1,q2 when q0=1\n", "result = qs.run(circuit, shots=100, seed=42)\n", "print(f\"Fredkin |110> -> {result.most_likely} (q1,q2 swapped)\")" ] }, { "cell_type": "markdown", "id": "635c13f2", "metadata": {}, "source": [ "## Gate Count Summary" ] }, { "cell_type": "code", "execution_count": null, "id": "43906dc1", "metadata": {}, "outputs": [], "source": [ "from quantsdk.gates import GATE_MAP\n", "\n", "print(f\"Total gates in GATE_MAP: {len(GATE_MAP)}\")\n", "print(f\"\\nAll gate names:\")\n", "for i, name in enumerate(sorted(GATE_MAP.keys())):\n", " print(f\" {name:12s}\", end=\"\")\n", " if (i + 1) % 5 == 0:\n", " print()" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }