{ "cells": [ { "cell_type": "markdown", "id": "b0f643ee", "metadata": {}, "source": [ "# 04 - Single-Qubit Gates\n", "\n", "Explore all single-qubit gates in QuantSDK's gate library.\n", "\n", "**Concepts:** Pauli gates, Clifford gates, rotation gates, parameterized gates" ] }, { "cell_type": "code", "execution_count": null, "id": "18f355c8", "metadata": {}, "outputs": [], "source": [ "import quantsdk as qs\n", "import math" ] }, { "cell_type": "markdown", "id": "f7187463", "metadata": {}, "source": [ "## Pauli Gates (X, Y, Z)\n", "\n", "- **X** (NOT): Flips |0> to |1> and vice versa\n", "- **Y**: Rotation around Y-axis by pi\n", "- **Z**: Phase flip (|1> gets a minus sign)" ] }, { "cell_type": "code", "execution_count": null, "id": "f946a6b0", "metadata": {}, "outputs": [], "source": [ "# X gate: NOT\n", "circuit = qs.Circuit(1).x(0).measure_all()\n", "result = qs.run(circuit, shots=100, seed=42)\n", "print(f\"X|0> = {result.counts}\") # Always |1>\n", "\n", "# Y gate\n", "circuit = qs.Circuit(1).y(0).measure_all()\n", "result = qs.run(circuit, shots=100, seed=42)\n", "print(f\"Y|0> = {result.counts}\") # Always |1> (with phase)\n", "\n", "# Z gate\n", "circuit = qs.Circuit(1).z(0).measure_all()\n", "result = qs.run(circuit, shots=100, seed=42)\n", "print(f\"Z|0> = {result.counts}\") # Still |0> (Z only affects phase of |1>)" ] }, { "cell_type": "markdown", "id": "9d437298", "metadata": {}, "source": [ "## Hadamard Gate (H)\n", "\n", "Creates equal superposition." ] }, { "cell_type": "code", "execution_count": null, "id": "f5bfdad2", "metadata": {}, "outputs": [], "source": [ "# Single Hadamard\n", "circuit = qs.Circuit(1).h(0).measure_all()\n", "result = qs.run(circuit, shots=1000, seed=42)\n", "print(f\"H|0> = {result.counts}\")\n", "\n", "# Double Hadamard = Identity\n", "circuit = qs.Circuit(1).h(0).h(0).measure_all()\n", "result = qs.run(circuit, shots=1000, seed=42)\n", "print(f\"H*H|0> = {result.counts}\") # Back to |0>" ] }, { "cell_type": "markdown", "id": "b57318e9", "metadata": {}, "source": [ "## Phase Gates (S, Sdg, T, Tdg, SX, SXdg)" ] }, { "cell_type": "code", "execution_count": null, "id": "1ae738ac", "metadata": {}, "outputs": [], "source": [ "gates = [\n", " (\"S\", lambda c: c.s(0)),\n", " (\"Sdg\", lambda c: c.sdg(0)),\n", " (\"T\", lambda c: c.t(0)),\n", " (\"Tdg\", lambda c: c.tdg(0)),\n", " (\"SX\", lambda c: c.sx(0)),\n", " (\"SXdg\", lambda c: c.sxdg(0)),\n", "]\n", "\n", "for name, apply_gate in gates:\n", " circuit = qs.Circuit(1)\n", " circuit.h(0) # Put in superposition first\n", " apply_gate(circuit)\n", " circuit.h(0) # Interfere\n", " circuit.measure_all()\n", " result = qs.run(circuit, shots=1000, seed=42)\n", " print(f\"H-{name:4s}-H|0> = {result.counts}\")" ] }, { "cell_type": "markdown", "id": "6da76c49", "metadata": {}, "source": [ "## Rotation Gates (RX, RY, RZ)\n", "\n", "Parameterized rotations around the Bloch sphere axes." ] }, { "cell_type": "code", "execution_count": null, "id": "13205e58", "metadata": {}, "outputs": [], "source": [ "# RY sweep: rotate from |0> toward |1>\n", "print(\"RY rotation sweep:\")\n", "for angle_frac in [0, 0.25, 0.5, 0.75, 1.0]:\n", " theta = angle_frac * math.pi\n", " circuit = qs.Circuit(1).ry(0, theta).measure_all()\n", " result = qs.run(circuit, shots=1000, seed=42)\n", " p1 = result.counts.get('1', 0) / 1000\n", " print(f\" RY({angle_frac:.2f}*pi): P(|1>) = {p1:.3f}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "1cd3dc51", "metadata": {}, "outputs": [], "source": [ "# RX sweep\n", "print(\"RX rotation sweep:\")\n", "for angle_frac in [0, 0.25, 0.5, 0.75, 1.0]:\n", " theta = angle_frac * math.pi\n", " circuit = qs.Circuit(1).rx(0, theta).measure_all()\n", " result = qs.run(circuit, shots=1000, seed=42)\n", " p1 = result.counts.get('1', 0) / 1000\n", " print(f\" RX({angle_frac:.2f}*pi): P(|1>) = {p1:.3f}\")" ] }, { "cell_type": "markdown", "id": "2da4a370", "metadata": {}, "source": [ "## General Unitary (U3)\n", "\n", "U3(theta, phi, lambda) is the most general single-qubit gate:\n", "\n", "$$U3(\\theta, \\phi, \\lambda) = \\begin{pmatrix} \\cos(\\theta/2) & -e^{i\\lambda}\\sin(\\theta/2) \\\\ e^{i\\phi}\\sin(\\theta/2) & e^{i(\\phi+\\lambda)}\\cos(\\theta/2) \\end{pmatrix}$$" ] }, { "cell_type": "code", "execution_count": null, "id": "ba1720ec", "metadata": {}, "outputs": [], "source": [ "# U3 can represent any single-qubit gate\n", "# U3(pi, 0, pi) = X gate\n", "circuit = qs.Circuit(1).u3(0, theta=math.pi, phi=0, lam=math.pi).measure_all()\n", "result = qs.run(circuit, shots=100, seed=42)\n", "print(f\"U3(pi,0,pi)|0> = X|0> = {result.counts}\")\n", "\n", "# U3(pi/2, 0, pi) = H gate (up to global phase)\n", "circuit = qs.Circuit(1).u3(0, theta=math.pi/2, phi=0, lam=math.pi).measure_all()\n", "result = qs.run(circuit, shots=1000, seed=42)\n", "print(f\"U3(pi/2,0,pi)|0> ~ H|0> = {result.counts}\")" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }