{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pennylane as pl\n", "from pennylane import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 1: The QFT matrix" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n_qubits = 4\n", "dev = pl.device(\"default.qubit\", wires=n_qubits)\n", "wires = list(range(n_qubits))\n", "\n", "def matrix():\n", " N = 2 ** n_qubits\n", " m = np.empty([N,N]).astype(complex)\n", " # define the QFT matrix\n", " return m\n", "\n", "print(matrix())\n", "\n", "@pl.qnode(dev)\n", "def circuit(k):\n", " bits = [int(x) for x in np.binary_repr(k, width=n_qubits)]\n", " print(bits)\n", " pl.BasisStatePreparation(bits, wires=wires)\n", " pl.QubitUnitary(matrix(), wires=wires)\n", " return pl.state()\n", "\n", "results = pl.snapshots(circuit)(3)\n", "# plot the results\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 2: The 2-qubit QFT circuit" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n_qubits = 2\n", "dev = pl.device(\"default.qubit\", wires=n_qubits)\n", "wires = list(range(n_qubits))\n", "\n", "@pl.qnode(dev)\n", "def circuit(k):\n", " bits = [int(x) for x in np.binary_repr(k, width=n_qubits)]\n", " pl.BasisStatePreparation(bits, wires=wires)\n", " # build the 2-qubit QFT circuit\n", " return pl.state()\n", "\n", "pl.draw_mpl(circuit, style=\"black_white\")(1);\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "results = pl.snapshots(circuit)(1)\n", "# plot the results" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 3: The recursive QFT circuit" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n_qubits = 4\n", "dev = pl.device(\"default.qubit\", wires=n_qubits)\n", "wires = list(range(n_qubits))\n", "\n", "def subcircuit(wires):\n", " # build the subcircuit on the given wires\n", "\n", "def QFT():\n", " # put all the subcircuits together\n", "\n", "@pl.qnode(dev)\n", "def circuit(k):\n", " bits = [int(x) for x in np.binary_repr(k, width=n_qubits)]\n", " pl.BasisStatePreparation(bits, wires=wires)\n", " QFT()\n", " return pl.state()\n", "\n", "pl.draw_mpl(circuit, style=\"black_white\", decimals=2)(3);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "results = pl.snapshots(circuit)(1)\n", "# plot the results" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 4: Using the QFT for period-finding" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n_qubits = 6\n", "period = 10\n", "dev = pl.device(\"default.qubit\", wires=n_qubits)\n", "wires = list(range(n_qubits))\n", "\n", "def periodicstatecircuit():\n", " # set up a circuit that output a periodic state, using pl.ControlledSequence and pl.PhaseShift\n", "\n", "@pl.qnode(dev)\n", "def periodicstate():\n", " periodicstatecircuit()\n", " return pl.state()\n", "\n", "state = periodicstate().real[:2**(n_qubits-1)]\n", "# plot the results" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "@pl.qnode(dev)\n", "def circuit():\n", " periodicstatecircuit()\n", " pl.QFT(wires=wires)\n", " return pl.probs(wires=wires)\n", "\n", "state = circuit()[:2**(n_qubits-1)]\n", "# plot the results\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Find the most probable answer and translate that in the the most likely period\n", "print(2**(n_qubits)/6)" ] } ], "metadata": { "kernelspec": { "display_name": "pennylane", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.7" } }, "nbformat": 4, "nbformat_minor": 2 }