{ "cells": [ { "cell_type": "markdown", "id": "c9f485e4", "metadata": {}, "source": [ "# Quantum Image Encoding using PiQture (INEQR)\n", "\n", "This notebook demonstrates how to encode an image using the Improved Novel Enhanced Quantum Representation (INEQR) method provided by the PiQture library, integrated into our `quantum_algo_microscopy` package." ] }, { "cell_type": "code", "execution_count": 2, "id": "532b6e8c", "metadata": {}, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'src'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[2], line 7\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;66;03m# Adjust the path if running from a different location or if installed\u001b[39;00m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;66;03m# import sys\u001b[39;00m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;66;03m# sys.path.append('..') # Add root directory to path\u001b[39;00m\n\u001b[0;32m----> 7\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msrc\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mquantum_algo_microscopy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mimage_processing\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m preprocess_image\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msrc\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mquantum_algo_microscopy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mqml\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m encode_image_ineqr\n\u001b[1;32m 10\u001b[0m \u001b[38;5;66;03m# Ensure plots are displayed inline\u001b[39;00m\n", "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'src'" ] } ], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "# Adjust the path if running from a different location or if installed\n", "import sys\n", "sys.path.append('..') # Add root directory to path\n", "\n", "from src.quantum_algo_microscopy.image_processing import preprocess_image\n", "from src.quantum_algo_microscopy.qml import encode_image_ineqr\n", "\n", "# Ensure plots are displayed inline\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "id": "4277fae3", "metadata": {}, "source": [ "## 1. Load and Preprocess Image\n", "\n", "First, we load the image and preprocess it. Preprocessing involves converting to grayscale, resizing (e.g., to 8x8 pixels for manageable quantum circuits), and normalizing pixel values to the [0, 1] range." ] }, { "cell_type": "code", "execution_count": null, "id": "cfb6da7a", "metadata": {}, "outputs": [], "source": [ "# Define image path (relative to the notebook directory)\n", "# Make sure 'duck_image.jpeg' is in the root directory or update path\n", "image_path = '../duck_image.jpeg'\n", "\n", "# Define target size for resizing\n", "image_size = (8, 8)\n", "\n", "# Preprocess the image\n", "img_array_normalized = preprocess_image(image_path, size=image_size)\n", "\n", "print(f\"Image loaded and preprocessed to size: {img_array_normalized.shape}\")" ] }, { "cell_type": "markdown", "id": "4964ecab", "metadata": {}, "source": [ "## 2. Display Preprocessed Image (Optional)" ] }, { "cell_type": "code", "execution_count": null, "id": "bcb960a1", "metadata": {}, "outputs": [], "source": [ "plt.imshow(img_array_normalized, cmap='gray')\n", "plt.title(f\"Preprocessed Image ({image_size[0]}x{image_size[1]})\")\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "3b88e2af", "metadata": {}, "source": [ "## 3. Encode Image using INEQR\n", "\n", "Now, we use the [encode_image_ineqr](cci:1://file:///Users/robertoreis/Documents/codes/quantum_algo_microscopy/src/quantum_algo_microscopy/qml/image_encoding.py:7:0-32:20) function from our QML module, which utilizes PiQture's INEQR implementation." ] }, { "cell_type": "code", "execution_count": null, "id": "b2b93a3e", "metadata": {}, "outputs": [], "source": [ "# Encode the preprocessed image\n", "try:\n", " ineqr_circuit = encode_image_ineqr(img_array_normalized)\n", " print(f\"Image encoded into INEQR quantum circuit.\")\n", " print(f\"Number of qubits: {ineqr_circuit.num_qubits}\")\n", "except Exception as e:\n", " print(f\"An error occurred during encoding: {e}\")\n", " ineqr_circuit = None # Set to None if encoding failed" ] }, { "cell_type": "markdown", "id": "509d6101", "metadata": {}, "source": [ "## 4. Draw the Quantum Circuit\n", "\n", "Finally, we visualize the generated quantum circuit. Note that INEQR circuits can be quite complex and might take time to render." ] }, { "cell_type": "code", "execution_count": null, "id": "ed06467a", "metadata": {}, "outputs": [], "source": [ "# Draw the circuit only if encoding was successful\n", "if ineqr_circuit:\n", " print(\"Drawing circuit... (this might take a moment)\")\n", " try:\n", " # Draw the circuit (might produce a large image)\n", " circuit_drawing = ineqr_circuit.draw(output='mpl', fold=-1) # fold=-1 prevents line wrapping\n", "\n", " # Display the drawing if it was generated\n", " if circuit_drawing:\n", " # Increase figure size for better readability\n", " fig = circuit_drawing.get_figure()\n", " fig.set_size_inches(25, 20) # Adjust size as needed\n", " plt.show()\n", " else:\n", " print(\"Circuit drawing could not be generated.\")\n", " \n", " except Exception as e:\n", " print(f\"An error occurred during circuit drawing: {e}\")\n", " print(\"Try drawing with 'text' output:\")\n", " print(ineqr_circuit.draw(output='text', fold=-1))\n", "else:\n", " print(\"Skipping circuit drawing because encoding failed.\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "213d01d3", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "cwq", "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.4" } }, "nbformat": 4, "nbformat_minor": 5 }