├── cat.png ├── Result.png ├── cat_320.png ├── Result_320.png ├── quantum_edge_detection.py ├── README.md ├── utils.py ├── runner_320.py ├── runner.py └── frqi.py /cat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paritoshkc/Quantum-Computing/HEAD/cat.png -------------------------------------------------------------------------------- /Result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paritoshkc/Quantum-Computing/HEAD/Result.png -------------------------------------------------------------------------------- /cat_320.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paritoshkc/Quantum-Computing/HEAD/cat_320.png -------------------------------------------------------------------------------- /Result_320.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paritoshkc/Quantum-Computing/HEAD/Result_320.png -------------------------------------------------------------------------------- /quantum_edge_detection.py: -------------------------------------------------------------------------------- 1 | from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit, execute 2 | from qiskit.aqua.circuits.fourier_transform_circuits import FourierTransformCircuits 3 | from math import pi 4 | from qiskit import Aer 5 | 6 | def quantum_adder(circuit, epsilon): 7 | qubits = circuit.qubits 8 | n_qubits = circuit.n_qubits 9 | FourierTransformCircuits.construct_circuit(circuit, qubits) 10 | for i in range(n_qubits): 11 | circuit.u1(float(2 * pi * epsilon)/2**(i + 1), qubits[n_qubits - i - 1]) 12 | FourierTransformCircuits.construct_circuit(circuit, qubits, inverse=True) 13 | 14 | def quantum_rotate_image(circuit): #gives you the quantum state where you have to measure the ancilla and obtain 0 15 | for i in circuit.qubits: 16 | circuit.x(i) 17 | 18 | def quantum_edge_detection(circuit): #gives you the quantum state where you have to measure the ancilla and obtain 0 19 | qubits = circuit.qubits 20 | ancilla = qubits[0] 21 | circuit.h(ancilla) 22 | quantum_adder(circuit, -1) 23 | circuit.h(ancilla) 24 | circuit.x(ancilla) 25 | 26 | 27 | # circuit.measure(ancilla, clbit) 28 | 29 | # backend = Aer.get_backend('qasm_simulator') 30 | # job_sim = execute(circuit, backend) 31 | # sim_result = job_sim.result() 32 | 33 | # print(sim_result.get_counts(circuit)) 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Quantum-Computing 3 | Quantum Computing has paved its path from being a theory to physical read-to-use machines. This project reflects on the implmentation of Quantum image processing with FRQI image model in Qiskit 4 | 5 | ## Getting Started 6 | 7 | 8 | 9 | ### Prerequisites 10 | 11 | Python 3.5+ , qiskit , matplot and numpy. 12 | Installing Qiskit with visualization can be done using pip 13 | ``` 14 | pip install qiskit[visualization] 15 | ``` 16 | 17 | ## Running the program and seeing the result 18 | 19 | Use runner.py to run the program and generate result. 20 | 21 | ## Selecting images to check 22 | There are 3 options for the image which can be selected from the Utils.py class:- 23 | 1. To select cat image call - util.get_Cat_image() 24 | 2. To select MNIST Image call - util.get_MNIST_data() 25 | 3. To select python generated image call - util.generate_image() 26 | 27 | ## Image transformation 28 | 1. To rotate the image uncomment below line in runner.py 29 | ``` 30 | qed.quantum_rotate_image(qc) 31 | ``` 32 | 2. To generate edge detection uncomment below line in runner.py 33 | ``` 34 | qed.quantum_edge_detection() 35 | ``` 36 | 37 | ## Running the noise model 38 | 39 | To add moise model to the simulation uncomment below lines from the runner.py class 40 | ``` 41 | backend = provider.get_backend('ibmq_16_melbourne') 42 | noise_model = NoiseModel.from_backend(backend) 43 | coupling_map = backend.configuration().coupling_map 44 | basis_gates = noise_model.basis_gates 45 | result = execute(qc, Aer.get_backend('qasm_simulator'), shots=numOfShots,coupling_map=coupling_map, 46 | basis_gates=basis_gates, 47 | noise_model=noise_model).result() 48 | 49 | ``` 50 | 51 | ## Result 52 | Result will be generated in the form of 'Result.png' and saved in the main folder. 53 | 54 | -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from resizeimage import resizeimage 5 | from PIL import Image, ImageOps 6 | 7 | def get_MNIST_data(): 8 | (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() 9 | return x_train 10 | 11 | 12 | 13 | def get_Cat_320_image(): 14 | return Image.open("cat_320.png").convert('LA') 15 | 16 | def get_Cat_image(): 17 | return Image.open("cat.png").convert('LA') 18 | 19 | def large_image_normalization(images,w,h): 20 | image=np.array([]) 21 | 22 | for y in range(h-32,h): 23 | for x in range(w-32,w): 24 | image=np.append(image,images.getpixel((x,y))[0]) 25 | genimg = image.reshape((32,32)) 26 | image = image.flatten() 27 | # change type 28 | image = image.astype('float64') 29 | # Normalization(0~pi/2) 30 | image /= 255.0 31 | generated_image=image 32 | # generated_image = np.arcsin(image) 33 | print(generated_image) 34 | image=images 35 | return generated_image 36 | 37 | 38 | 39 | def image_normalization(image,size,show): 40 | image = resizeimage.resize_cover(image, [size, size]) 41 | w, h = size, size 42 | image = np.array([[image.getpixel((x,y))[0] for x in range(w)] for y in range(h)]) 43 | 44 | # display the image 45 | if show: 46 | genimg = image.reshape((size,size)) 47 | plt.imshow(genimg, cmap='gray', vmin=0, vmax=255) 48 | plt.show() 49 | 50 | image = image.flatten() 51 | image = image.astype('float64') 52 | image /= 255.0 53 | generated_image = np.arcsin(image) 54 | # print(generated_image) 55 | return generated_image 56 | 57 | def generate_image(size): 58 | w, h = size, size 59 | data = np.zeros((h, w, 3), dtype=np.uint8) 60 | w_bound=int(w/2) 61 | h_bound=int(h/2) 62 | data[0:w_bound, 0:h_bound] = [255, 0, 0] # red patch in upper left 63 | data[0:w_bound, h_bound+1:h] = [0, 255, 0] # red patch in upper left 64 | data[w_bound+1:w, 0:h_bound] = [0, 0, 255] # red patch in upper left 65 | data[w_bound+1:w, h_bound+1:h] = [128, 128, 128] # red patch in upper left 66 | 67 | imgq = Image.fromarray(data, 'RGB') 68 | imgq.save('my.png') 69 | return imgq.convert('LA') 70 | 71 | def get_image_pixel_value(image,size): 72 | img_arr= np.array([[image.getpixel((x,y))[0] for x in range(size)] for y in range(size)]) 73 | img_arr = img_arr.flatten() 74 | return img_arr 75 | 76 | def get_count_of_pixel(arr1,arr2): 77 | same=0 78 | notsame=0 79 | for i in range(len(arr1)): 80 | if arr1[i]==arr2[i]: 81 | same+=1 82 | else: 83 | notsame+=1 84 | return (same,notsame) 85 | 86 | 87 | -------------------------------------------------------------------------------- /runner_320.py: -------------------------------------------------------------------------------- 1 | 2 | # frqi circuit from https://github.com/Shedka/citiesatnight 3 | 4 | 5 | import utils 6 | from qiskit import IBMQ, QuantumCircuit, ClassicalRegister, QuantumRegister 7 | from qiskit import execute, QuantumRegister 8 | from qiskit.qasm import pi 9 | from qiskit.tools.visualization import plot_histogram, circuit_drawer 10 | from qiskit.visualization import plot_state_city, plot_bloch_multivector 11 | from qiskit.visualization import plot_state_paulivec, plot_state_hinton 12 | from qiskit.visualization import plot_state_qsphere 13 | from qiskit.visualization import plot_histogram, plot_gate_map, plot_circuit_layout 14 | from qiskit import execute, Aer, BasicAer 15 | from qiskit.providers.aer.noise import NoiseModel 16 | import numpy as np 17 | import matplotlib.pyplot as plt 18 | from resizeimage import resizeimage 19 | from PIL import Image, ImageOps 20 | import frqi 21 | # import quantum_edge_detection as qed 22 | 23 | 24 | 25 | # Insert API key generated after registring in IBM Quantum Experience 26 | # IBMQ.save_account('API KEY') 27 | 28 | IBMQ.load_account() 29 | provider = IBMQ.get_provider( group='open', project='main') 30 | 31 | 32 | # dimensions of the image 33 | size=32 34 | 35 | #target image 36 | images=utils.get_Cat_320_image() 37 | 38 | # New blank image 39 | new_im = Image.new('RGB', (320, 320)) 40 | 41 | for k in range(10): 42 | for j in range(10): 43 | normalized_image=utils.large_image_normalization(images,32+(32*k),32+(32*j)) 44 | genimg= np.array([]) 45 | anc = QuantumRegister(1, "anc") 46 | img = QuantumRegister(11, "img") 47 | anc2 = QuantumRegister(1, "anc2") 48 | c = ClassicalRegister(12) 49 | qc = QuantumCircuit(anc, img, anc2, c) 50 | 51 | for i in range(1, len(img)): 52 | qc.h(img[i]) 53 | 54 | 55 | for i in range(len(normalized_image)): 56 | if normalized_image[i] != 0: 57 | frqi.c10mary(qc, 2 * normalized_image[i], format(i, '010b'), img[0], anc2[0], [img[j] for j in range(1,len(img))]) 58 | qc.measure(img, c[1:12]) 59 | print(qc.depth()) 60 | numOfShots = 1000000 61 | result = execute(qc, Aer.get_backend('qasm_simulator'), shots=numOfShots).result() 62 | for i in range(len(normalized_image)): 63 | try: 64 | genimg = np.append(genimg,[np.sqrt(result.get_counts(qc)[format(i, '010b')+'10']/numOfShots)]) 65 | except KeyError: 66 | genimg = np.append(genimg,[0.0]) 67 | 68 | genimg *= 32.0 * 255.0 69 | genimg = genimg.astype('int') 70 | genimg = genimg.reshape((32,32)) 71 | im=Image.fromarray(genimg) 72 | 73 | new_im.paste(im,(32*k,32*j)) 74 | new_im.show() 75 | new_im.save('Result_320.png') 76 | 77 | -------------------------------------------------------------------------------- /runner.py: -------------------------------------------------------------------------------- 1 | import utils 2 | from qiskit import IBMQ, QuantumCircuit, ClassicalRegister, QuantumRegister 3 | from qiskit import execute, QuantumRegister 4 | from qiskit.qasm import pi 5 | from qiskit.tools.visualization import plot_histogram, circuit_drawer 6 | from qiskit.visualization import plot_state_city, plot_bloch_multivector 7 | from qiskit.visualization import plot_state_paulivec, plot_state_hinton 8 | from qiskit.visualization import plot_state_qsphere 9 | from qiskit.visualization import plot_histogram, plot_gate_map, plot_circuit_layout 10 | from qiskit import execute, Aer, BasicAer 11 | from qiskit.providers.aer.noise import NoiseModel 12 | import numpy as np 13 | import matplotlib.pyplot as plt 14 | from resizeimage import resizeimage 15 | from PIL import Image, ImageOps 16 | import frqi 17 | import quantum_edge_detection as qed 18 | # import quantum_edge_detection as qed 19 | 20 | 21 | 22 | # Insert API key generated after registring in IBM Quantum Experience 23 | # IBMQ.save_account('API KEY') 24 | 25 | IBMQ.load_account() 26 | provider = IBMQ.get_provider( group='open', project='main') 27 | 28 | 29 | # dimensions of the image 30 | size=32 31 | 32 | #target image 33 | image=utils.get_Cat_image() 34 | 35 | #normalized image 36 | normalized_image=utils.image_normalization(image,32,True) 37 | 38 | #get target image pixel values for comparison with output image 39 | img_arr=utils.get_image_pixel_value(image,32) 40 | 41 | 42 | # initialize qubits and classical registers for building the circuit 43 | anc = QuantumRegister(1, "anc") 44 | img = QuantumRegister(11, "img") 45 | anc2 = QuantumRegister(1, "anc2") 46 | c = ClassicalRegister(12) 47 | 48 | # create circuit 49 | qc = QuantumCircuit(anc, img, anc2, c) 50 | 51 | # apply hadamard gates 52 | for i in range(1, len(img)): 53 | qc.h(img[i]) 54 | 55 | # frqi circuit from https://github.com/Shedka/citiesatnight 56 | for i in range(len(normalized_image)): 57 | if normalized_image[i] != 0: 58 | frqi.c10mary(qc, 2 * normalized_image[i], format(i, '010b'), img[0], anc2[0], [img[j] for j in range(1,len(img))]) 59 | 60 | #rotate the image 180 deg 61 | # qed.quantum_rotate_image(qc) 62 | 63 | #Edge Detection 64 | # qed.quantum_edge_detection() 65 | 66 | qc.measure(img, c[1:12]) 67 | print(qc.depth()) 68 | numOfShots = 1000000 69 | 70 | 71 | #To add noise on the simulation UNCOMMENT BELOW LINES 72 | 73 | # backend = provider.get_backend('ibmq_16_melbourne') 74 | # noise_model = NoiseModel.from_backend(backend) 75 | # # Get coupling map from backend 76 | # coupling_map = backend.configuration().coupling_map 77 | # # Get basis gates from noise model 78 | # basis_gates = noise_model.basis_gates 79 | 80 | # To run without noise UNCOMMENT BELOW LINES 81 | # result = execute(qc, Aer.get_backend('qasm_simulator'), shots=numOfShots,coupling_map=coupling_map, 82 | # basis_gates=basis_gates, 83 | # noise_model=noise_model).result() 84 | 85 | # To run without noise UNCOMMENT BELOW LINES 86 | result = execute(qc, Aer.get_backend('qasm_simulator'), shots=numOfShots).result() 87 | 88 | # Image retrieval from quantum state to pixels 89 | 90 | genimg = np.array([]) 91 | 92 | #### decode 93 | for i in range(len(normalized_image)): 94 | try: 95 | genimg = np.append(genimg,[np.sqrt(result.get_counts(qc)[format(i, '010b')+'10']/numOfShots)]) 96 | except KeyError: 97 | genimg = np.append(genimg,[0.0]) 98 | 99 | 100 | 101 | # inverse nomalization 102 | genimg *= size * 255.0 103 | # genimg = np.sin(genimg) 104 | 105 | same,notSame= utils.get_count_of_pixel(img_arr,genimg) 106 | print(same,notSame) 107 | percentage= (same/1024)*100 108 | print ("Total image recovered "+ str(percentage)) 109 | 110 | # convert type 111 | genimg = genimg.astype('int') 112 | genimg = genimg.reshape((size,size)) 113 | plt.imshow(genimg, cmap='gray', vmin=0, vmax=255) 114 | plt.savefig('Result'+'.png') 115 | plt.show() -------------------------------------------------------------------------------- /frqi.py: -------------------------------------------------------------------------------- 1 | from qiskit import IBMQ, QuantumCircuit, ClassicalRegister, QuantumRegister 2 | from qiskit import execute, QuantumRegister 3 | from qiskit.qasm import pi 4 | from qiskit.tools.visualization import plot_histogram, circuit_drawer 5 | from qiskit import execute, Aer, BasicAer 6 | import numpy as np 7 | import random 8 | import keras 9 | from keras.models import Sequential 10 | from keras.layers import Dense, Activation 11 | from keras.datasets import mnist 12 | import matplotlib.pyplot as plt 13 | from sklearn.metrics import mean_squared_error, mean_absolute_error, mutual_info_score, r2_score 14 | from resizeimage import resizeimage 15 | from PIL import Image 16 | 17 | def margolus(circ, t, c0, c1): 18 | circ.ry(np.pi/4,t) 19 | circ.cx(c0, t) 20 | circ.ry(np.pi/4,t) 21 | circ.cx(c1, t) 22 | circ.ry(-np.pi/4,t) 23 | circ.cx(c0, t) 24 | circ.ry(-np.pi/4,t) 25 | 26 | def rccx(circ, t, c0, c1): 27 | circ.h(t) 28 | circ.t(t) 29 | circ.cx(c0, t) 30 | circ.tdg(t) 31 | circ.cx(c1, t) 32 | circ.t(t) 33 | circ.cx(c0, t) 34 | circ.tdg(t) 35 | circ.h(t) 36 | 37 | def rcccx(circ, t, c0, c1, c2): 38 | circ.h(t) 39 | circ.t(t) 40 | circ.cx(c0, t) 41 | circ.tdg(t) 42 | circ.h(t) 43 | circ.cx(c1, t) 44 | circ.t(t) 45 | circ.cx(c2, t) 46 | circ.tdg(t) 47 | circ.cx(c1, t) 48 | circ.t(t) 49 | circ.cx(c2, t) 50 | circ.tdg(t) 51 | circ.h(t) 52 | circ.t(t) 53 | circ.cx(c0, t) 54 | circ.tdg(t) 55 | circ.h(t) 56 | 57 | 58 | def ccry(circ, angle, t, c0, c1): 59 | circ.cu3(angle/2, 0, 0, c1, t) 60 | circ.cx(c1, c0) 61 | circ.cu3(-angle/2, 0, 0, c0, t) 62 | circ.cx(c1, c0) 63 | circ.cu3(angle/2, 0, 0, c0, t) 64 | 65 | def mary(circ, angle, t, c0, c1): 66 | circ.ry(angle/4,t) 67 | circ.cx(c0, t) 68 | circ.ry(-angle/4,t) 69 | circ.cx(c1, t) 70 | circ.ry(angle/4,t) 71 | circ.cx(c0, t) 72 | circ.ry(-angle/4,t) 73 | circ.cx(c1, t) 74 | 75 | def cccry(circ, angle, t, a, c0, c1, c2): 76 | margolus(circ, a, c1, c2) 77 | mary(circ, angle, t, a, c0) 78 | margolus(circ, a, c1, c2) 79 | 80 | def mary_4(circ, angle, t, c0, c1, c2): 81 | circ.h(t) 82 | circ.t(t) 83 | circ.cx(c0,t) 84 | circ.tdg(t) 85 | circ.h(t) 86 | circ.cx(c1,t) 87 | circ.rz(angle/4,t) 88 | circ.cx(c2,t) 89 | circ.rz(-angle/4,t) 90 | circ.cx(c1,t) 91 | circ.rz(angle/4,t) 92 | circ.cx(c2,t) 93 | circ.rz(-angle/4,t) 94 | circ.h(t) 95 | circ.t(t) 96 | circ.cx(c0,t) 97 | circ.tdg(t) 98 | circ.h(t) 99 | 100 | def mary_8(circ, angle, t, c0, c1, c2, c3, c4, c5, c6): 101 | circ.h(t) 102 | circ.t(t) 103 | rccx(circ, t, c0, c1) 104 | circ.tdg(t) 105 | circ.h(t) 106 | rccx(circ, t, c2, c3) 107 | circ.rz(angle/4,t) 108 | rcccx(circ, t, c4, c5, c6) 109 | circ.rz(-angle/4,t) 110 | rccx(circ, t, c2, c3) 111 | circ.rz(angle/4,t) 112 | rcccx(circ, t, c4, c5, c6) 113 | circ.rz(-angle/4,t) 114 | circ.h(t) 115 | circ.t(t) 116 | rccx(circ, t, c0, c1) 117 | circ.tdg(t) 118 | circ.h(t) 119 | 120 | 121 | def c10ry(circ, angle, bin, target, anc, controls): 122 | # c10mary(qc, 2 * x_train[img_num][i], format(i, '010b'), 0, 1, [i for i in range(2,12)]) 123 | print(bin) 124 | clist = [] 125 | 126 | for i in bin: 127 | clist.append(int(i)) 128 | 129 | for i in range(len(clist)): 130 | if clist[i] == 0: 131 | circ.x(controls[-i-1]) 132 | 133 | margolus(circ, anc, controls[0], controls[1]) 134 | circ.x(controls[0]) 135 | circ.x(controls[1]) 136 | margolus(circ, controls[1], controls[2], controls[3]) 137 | circ.x(controls[2]) 138 | circ.x(controls[3]) 139 | margolus(circ, controls[3], controls[4], controls[5]) 140 | circ.x(controls[4]) 141 | circ.x(controls[5]) 142 | 143 | margolus(circ, controls[5], controls[8], controls[9]) 144 | margolus(circ, controls[4], controls[6], controls[7]) 145 | margolus(circ, controls[2], controls[4], controls[5]) 146 | margolus(circ, controls[0], controls[2], controls[3]) 147 | 148 | mary_4(circ, angle, target, anc, controls[0], controls[1]) 149 | 150 | margolus(circ, controls[0], controls[2], controls[3]) 151 | margolus(circ, controls[2], controls[4], controls[5]) 152 | margolus(circ, controls[4], controls[6], controls[7]) 153 | margolus(circ, controls[5], controls[8], controls[9]) 154 | 155 | circ.x(controls[5]) 156 | circ.x(controls[4]) 157 | margolus(circ, controls[3], controls[4], controls[5]) 158 | circ.x(controls[3]) 159 | circ.x(controls[2]) 160 | margolus(circ, controls[1], controls[2], controls[3]) 161 | circ.x(controls[1]) 162 | circ.x(controls[0]) 163 | margolus(circ, anc, controls[0], controls[1]) 164 | 165 | for i in range(len(clist)): 166 | if clist[i] == 0: 167 | circ.x(controls[-i-1]) 168 | 169 | def c10mary(circ, angle, bin, target, anc, controls): 170 | 171 | # c10mary(qc, 2 * x_train[img_num][i], format(i, '010b'), 0, 1, [i for i in range(2,12)]) 172 | clist = [] 173 | 174 | for i in bin: 175 | clist.append(int(i)) 176 | # print("angle", angle) 177 | # print("clist - bin",clist) 178 | 179 | for i in range(len(clist)): 180 | if clist[i] == 0: 181 | circ.x(controls[-i-1]) 182 | 183 | rccx(circ, anc, controls[4], controls[5]) 184 | # circuit_drawer(circ,output='mpl', filename='my_circuit_rccx.png') 185 | circ.x(controls[4]) 186 | circ.x(controls[5]) 187 | rccx(circ, controls[4], controls[6], controls[7]) 188 | rccx(circ, controls[5], controls[8], controls[9]) 189 | 190 | 191 | mary_8(circ, angle, target, anc, controls[0], controls[1], controls[2], controls[3], controls[4], controls[5]) 192 | 193 | rccx(circ, controls[5], controls[8], controls[9]) 194 | rccx(circ, controls[4], controls[6], controls[7]) 195 | circ.x(controls[5]) 196 | circ.x(controls[4]) 197 | rccx(circ, anc, controls[4], controls[5]) 198 | 199 | for i in range(len(clist)): 200 | if clist[i] == 0: 201 | circ.x(controls[-i-1]) 202 | # for i in range(len(clist)): 203 | # circ.x(controls[i]) 204 | 205 | 206 | 207 | def image_normalization(image): 208 | image = resizeimage.resize_cover(image, [32, 32]) 209 | w, h = 32, 32 210 | image = np.array([[image.getpixel((x,y))[0] for x in range(w)] for y in range(h)]) 211 | 212 | # 2-dimentional data convert to 1-dimentional array 213 | image = image.flatten() 214 | # change type 215 | image = image.astype('float64') 216 | # Normalization(0~pi/2) 217 | image /= 255.0 218 | generated_image = np.arcsin(image) 219 | 220 | return generated_image 221 | 222 | 223 | if __name__ == '__main__': 224 | # (x_train, y_train), (x_test, y_test) = mnist.load_data() 225 | # img_num = 1 226 | 227 | # #show original image 228 | # plt.imshow(x_train[img_num], cmap='gray') 229 | # #plt.savefig('mnistimg'+str(img_num)+'.png') 230 | # plt.show() 231 | 232 | # # 2-dimentional data convert to 1-dimentional array 233 | # x_train = x_train.reshape(60000, 784) 234 | # # change type 235 | # x_train = x_train.astype('float64') 236 | # # Normalization(0~pi/2) 237 | # x_train /= 255.0 238 | # x_train = np.arcsin(x_train) 239 | x_train=image_normalization(Image.open("cat.png").convert('LA')) 240 | 241 | backends = Aer.backends() 242 | 243 | #print("Aer backends:",backends) 244 | 245 | qubit = 12 246 | qc = QuantumCircuit(qubit,qubit) 247 | 248 | 249 | # apply hadamard gates 250 | qc.h(range(2,qubit)) 251 | # image1 = image_normalization(image1) 252 | 253 | 254 | # apply c10Ry gates (representing color data) 255 | for i in range(len(x_train)): 256 | if x_train[i] != 0: 257 | c10mary(qc, 2 * x_train[i], format(i, '010b'), 0, 1, [i for i in range(2,12)]) 258 | 259 | # qc.x(range(2,qubit)) 260 | qc.measure(range(qubit),range(qubit)) 261 | 262 | backend_sim = Aer.get_backend('qasm_simulator') 263 | #print(qc.depth()) 264 | numOfShots = 1000000 265 | result = execute(qc, backend_sim, shots=numOfShots).result() 266 | #circuit_drawer(qc).show() 267 | #plot_histogram(result.get_counts(qc)) 268 | 269 | print(result.get_counts(qc)) 270 | 271 | # generated image 272 | genimg = np.array([]) 273 | 274 | #### decode 275 | for i in range(len(x_train)): 276 | try: 277 | genimg = np.append(genimg,[np.sqrt(result.get_counts(qc)[format(i, '010b')+'01']/numOfShots)]) 278 | except KeyError: 279 | genimg = np.append(genimg,[0.0]) 280 | 281 | # inverse nomalization 282 | genimg *= 32.0 * 255.0 283 | x_train = np.sin(x_train) 284 | x_train *= 255.0 285 | 286 | # convert type 287 | genimg = genimg.astype('int') 288 | 289 | # back to 2-dimentional data 290 | genimg = genimg.reshape((32,32)) 291 | 292 | plt.imshow(genimg, cmap='gray', vmin=0, vmax=255) 293 | # plt.savefig('gen_'+str(img_num)+'.png') 294 | plt.show() --------------------------------------------------------------------------------