Use this file to discover all available pages before exploring further.
View on GitHub
Open this notebook in GitHub to run it yourself
Based on the paper: “Edge Detection Quantumized: A Novel Quantum Algorithm for Image Processing” https://arxiv.org/html/2404.06889v1 This notebook demonstrates:
QPIE (Quantum Probability Image Encoding) encoding
import mathfrom typing import Listimport matplotlib.pyplot as pltimport numpy as npfrom classiq import *
# originalphoto = []photo = plt.imread("Marina Bay Sands 128.png")plt.imshow(photo)
Output:
<matplotlib.image.AxesImage at 0x168cc3e90>
segments = [i / 255 for i in [0, 30, 60, 90, 120, 150, 180, 210, 255]]image = np.array(photo)# print(segments)# print(image)for i in range(1, len(image)): for j in range(len(image[i])): pixel = image[i][j][0] for s in segments: if pixel >= s: image[i][j][0] = s image[i][j][1] = s image[i][j][2] = s# print("AFTER THE UPDATE")# print(image)plt.imshow(image)image = np.array(photo)
Convert an image into valid QPIE probability amplitudes.The image is converted to grayscale if needed, made non-negative, and L2-normalized so the sum of squared values equals
The result is stored as an n×n array IMAGE_DATA.
def image_to_qpie_amplitudes(image: np.ndarray) -> np.ndarray: """ Convert an image to QPIE probability amplitudes as an n×n array. |img⟩ = Σ(x,y) (I_xy / √(Σ I_xy²)) |xy⟩ """ if len(image.shape) == 3: image = np.mean(image, axis=2) n = image.shape[0] assert image.shape == (n, n), "Image must be square" image = np.abs(image) norm = np.sqrt(np.sum(image**2)) if norm == 0: return np.ones((n, n)) / n return (image / norm).T
We define an ImagePixel QStruct with separate x and y registers, and load the image via lookup_table — the amplitudes are computed classically from the pixel coordinates and loaded with prepare_amplitudes.The QHED algorithm detects edges by:
Adding auxiliary qubits in ∣+⟩ state
Controlled shifts of x (horizontal) and y (vertical) by −1
The model is synthesized with a 20-qubit width limit and a long timeout, and finally exported as quantum_image_edge_detection with 15-digit numeric precision.
print(f"\n{n}x{n} Image Circuit Statistics:")print(f" - Number of qubits: {qprog.data.width}")print(f" - Circuit depth: {qprog.transpiled_circuit.depth}")print( f" - Number of gates: {qprog.transpiled_circuit.count_ops if hasattr(qprog.transpiled_circuit, 'count_ops') else 'N/A'}")
Output:
128x128 Image Circuit Statistics:- Number of qubits: 17- Circuit depth: 32813- Number of gates: {'u': 16617, 'cx': 16584}
show(qprog)
Output:
Quantum program link: https://platform.classiq.io/circuit/3AlGcDNufmf2h6AwpUBoej5fGk8
If edge_aux == 1 then it is marked as an edge pixel.The new amplitude is calculated based on the number of shots measured for that pixel, normalized by the total number of shots.