-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQPIE.py
58 lines (35 loc) · 1.11 KB
/
QPIE.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
from qiskit import QuantumCircuit, Aer, execute
def qpie_circuit(im): #for square image 2^n * 2^n
n = int(np.log2(len(im)))
vals = im.flatten()
vals = vals/np.linalg.norm(vals)
qc = QuantumCircuit(2*n)
qc.initialize(vals)
return qc
def decode_out(qc, norm, shot=10000, fourier=False):
n = qc.num_qubits
shots = shot
backend = Aer.get_backend('qasm_simulator')
results = execute(qc, backend=backend, shots=shots).result()
answer = results.get_counts()
outim = np.zeros((int(2**(n/2)), int(2**(n/2))))
b = 0
for i,j in np.ndindex(outim.shape):
bit = format(b, '0' + str(n) + 'b')
p_i = answer.get(bit,0)/shots
if fourier:
pix_val = np.sqrt(p_i) * norm * 2**(n/2)
else:
pix_val = np.sqrt(p_i) * norm
outim[i,j] = (pix_val)
b+=1
return outim
def diff_rel(im1,im2):
im = np.abs(im1 - im2)
s = np.sum(im.flatten())/(len(im1)**2)
return s * 100
def MSE(im1,im2):
im = (im1-im2)**2
s = np.sum(im.flatten())/(len(im1)**2)
return s