Quantum Image Encoding using PiQture (INEQR)

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.

[2]:
import matplotlib.pyplot as plt
import numpy as np
# Adjust the path if running from a different location or if installed
import sys
sys.path.append('..') # Add root directory to path

from src.quantum_algo_microscopy.image_processing import preprocess_image
from src.quantum_algo_microscopy.qml import encode_image_ineqr

# Ensure plots are displayed inline
%matplotlib inline
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[2], line 7
      2 import numpy as np
      3 # Adjust the path if running from a different location or if installed
      4 # import sys
      5 # sys.path.append('..') # Add root directory to path
----> 7 from src.quantum_algo_microscopy.image_processing import preprocess_image
      8 from src.quantum_algo_microscopy.qml import encode_image_ineqr
     10 # Ensure plots are displayed inline

ModuleNotFoundError: No module named 'src'

1. Load and Preprocess Image

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.

[ ]:
# Define image path (relative to the notebook directory)
# Make sure 'duck_image.jpeg' is in the root directory or update path
image_path = '../duck_image.jpeg'

# Define target size for resizing
image_size = (8, 8)

# Preprocess the image
img_array_normalized = preprocess_image(image_path, size=image_size)

print(f"Image loaded and preprocessed to size: {img_array_normalized.shape}")

2. Display Preprocessed Image (Optional)

[ ]:
plt.imshow(img_array_normalized, cmap='gray')
plt.title(f"Preprocessed Image ({image_size[0]}x{image_size[1]})")
plt.show()

3. Encode Image using INEQR

Now, we use the encode_image_ineqr function from our QML module, which utilizes PiQture’s INEQR implementation.

[ ]:
# Encode the preprocessed image
try:
    ineqr_circuit = encode_image_ineqr(img_array_normalized)
    print(f"Image encoded into INEQR quantum circuit.")
    print(f"Number of qubits: {ineqr_circuit.num_qubits}")
except Exception as e:
    print(f"An error occurred during encoding: {e}")
    ineqr_circuit = None # Set to None if encoding failed

4. Draw the Quantum Circuit

Finally, we visualize the generated quantum circuit. Note that INEQR circuits can be quite complex and might take time to render.

[ ]:
# Draw the circuit only if encoding was successful
if ineqr_circuit:
    print("Drawing circuit... (this might take a moment)")
    try:
        # Draw the circuit (might produce a large image)
        circuit_drawing = ineqr_circuit.draw(output='mpl', fold=-1) # fold=-1 prevents line wrapping

        # Display the drawing if it was generated
        if circuit_drawing:
            # Increase figure size for better readability
            fig = circuit_drawing.get_figure()
            fig.set_size_inches(25, 20) # Adjust size as needed
            plt.show()
        else:
            print("Circuit drawing could not be generated.")

    except Exception as e:
        print(f"An error occurred during circuit drawing: {e}")
        print("Try drawing with 'text' output:")
        print(ineqr_circuit.draw(output='text', fold=-1))
else:
    print("Skipping circuit drawing because encoding failed.")

[ ]: