diff --git a/run_part1.py b/run_part1.py new file mode 100644 index 0000000..28ab542 --- /dev/null +++ b/run_part1.py @@ -0,0 +1,138 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "3a8f1303-c991-4af4-9cad-649ae271c00f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(28, 28)\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import qiskit\n", + "\n", + "# Encoder function to convert an image into a quantum circuit\n", + "def encoder(image):\n", + " height, width = image.shape\n", + " print(image.shape)\n", + " qc = qiskit.QuantumCircuit(height * width, height * width)\n", + "\n", + " # Apply Hadamard gates to the qubits\n", + " for i in range(height * width):\n", + " qc.h(i)\n", + "\n", + " # Apply the NEQR encoding\n", + " for row in range(height):\n", + " for col in range(width):\n", + " # Get the pixel value at the current position\n", + " pixel_value = image[row][col]\n", + "\n", + " # Apply controlled-Z gates based on the pixel value\n", + " for i in range(height * width):\n", + " if (i // width == row and i % width == col):\n", + " if pixel_value == 0:\n", + " qc.z(i)\n", + " else:\n", + " qc.cz(i, (row * width) + col)\n", + "\n", + " return qc\n", + "\n", + "# Simulator function to simulate a circuit and get a histogram\n", + "def simulator(circuit):\n", + " # Simulate the circuit using the statevector simulator\n", + " backend = qiskit.Aer.get_backend('statevector_simulator')\n", + " result = qiskit.execute(circuit, backend).result()\n", + " statevector = result.get_statevector()\n", + "\n", + " # Get the probability distribution of the statevector\n", + " probabilities = np.abs(statevector)**2\n", + "\n", + " return probabilities\n", + "\n", + "# Decoder function to convert the histogram into a regenerated image\n", + "def decoder(histogram, image):\n", + " # Reshape the histogram into an image\n", + " height, width = image.shape\n", + " image = np.zeros((height, width))\n", + "\n", + " for i in range(height * width):\n", + " row = i // width\n", + " col = i % width\n", + " image[row][col] = histogram[i]\n", + "\n", + " return image\n", + "\n", + "# Example usage\n", + "if __name__ == \"__main__\":\n", + " # Load the image\n", + " data_path='data'\n", + "#load the actual hackthon data (fashion-mnist)\n", + " image=np.load(data_path+'/images.npy')\n", + " #image = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]])\n", + "\n", + " # Convert the image into a quantum circuit\n", + " circuit = encoder(image[1])\n", + "\n", + " # Simulate the circuit and get a histogram\n", + " histogram = simulator(circuit)\n", + "\n", + " # Convert the histogram into a regenerated image\n", + " regenerated_image = decoder(histogram, image[1])\n", + "\n", + " print(\"Original Image:\")\n", + " print(image)\n", + " print(histogram)\n", + " print(\"Regenerated Image:\")\n", + " print(regenerated_image)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b5acf433-e1d7-4a07-81c7-355ef73c7aaf", + "metadata": {}, + "outputs": [], + "source": [ + "import matplotlib.pyplot as plt\n", + "plt.imshow(image[1])\n", + "plt.imshow(regenerated_image)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a100e8c4-d54d-498d-ae02-9b17636cecbf", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 [IonQ]", + "language": "python", + "name": "python3_ionq_6vdluz" + }, + "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.9.10" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/run_part2.py b/run_part2.py new file mode 100644 index 0000000..8a62f03 --- /dev/null +++ b/run_part2.py @@ -0,0 +1,88 @@ +def encode(image): + q = qiskit.QuantumRegister(3) + qc = qiskit.QuantumCircuit(q) + img = images + image = np.array(images) + if image[0][0]==0: + circuit.rx(np.pi,0) + else: + theta = np.pi # according to line circuit.rx(np.pi,0) + qc.h(0) + qc.h(1) + + qc.barrier() + + + qc.cry(theta,0,2) + qc.cx(0,1) + qc.cry(-theta,1,2) + qc.cx(0,1) + qc.cry(theta,1,2) + + qc.barrier() + + + qc.x(1) + qc.cry(theta,0,2) + qc.cx(0,1) + qc.cry(-theta,1,2) + qc.cx(0,1) + qc.cry(theta,1,2) + + qc.barrier() + + qc.x(1) + qc.x(0) + qc.cry(theta,0,2) + qc.cx(0,1) + qc.cry(-theta,1,2) + qc.cx(0,1) + qc.cry(theta,1,2) + + + qc.barrier() + + qc.x(1) + + qc.cry(theta,0,2) + qc.cx(0,1) + qc.cry(-theta,1,2) + qc.cx(0,1) + qc.cry(theta,1,2) + + qc.measure_all() + + qc.draw() + return qc +def decode(histogram): + if 1 in histogram.keys(): + image=np.array([[0,0],[0,0]]) + else: + image=np.array([[1,1],[1,1]]) + return image + + +def run_part2(image): + + #loade the quantum classifier circuit + classifier=qiskit.QuantumCircuit.from_qasm_file('quantum_classifier.qasm') + + #encode image into circuit + qc=encode(image) + + #append with classifier circuit + nq1 = qc.width() + nq2 = classifier.width() + nq = max(nq1, nq2) + qc = qiskit.QuantumCircuit(nq) + qc.append(qc.to_instruction(), list(range(nq1))) + qc.append(classifier.to_instruction(), list(range(nq2))) + + #simulate circuit + histogram=simulate(qc) + + #convert histogram to category + label=histogram_to_category(histogram) + + return qc,label +#score \ No newline at end of file