-
Notifications
You must be signed in to change notification settings - Fork 0
/
qft_test.py
146 lines (123 loc) · 4.61 KB
/
qft_test.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import numpy as np
from qiskit import QuantumCircuit
from qtealeaves.observables import TNState2File, TNObservables
from qtealeaves.emulator import MPS
from qtealeaves.convergence_parameters import TNConvergenceParameters
from qmatchatea import run_simulation, QCIO
from qmatchatea.utils.qk_utils import GHZ_qiskit
from qmatchatea.utils.utils import print_state
import os
from PIL import Image
from scipy.fft import dct, fft, idct
import pickle
def qft_rotations(circuit, n):
if n == 0: # Exit function if circuit is empty
return circuit
n -= 1 # Indexes start from 0
circuit.h(n) # Apply the H-gate to the most significant qubit
for qubit in range(n):
circuit.cp(np.pi/2**(n-qubit), qubit, n)
qft_rotations(circuit, n)
def swap_registers(circuit, n):
for quit in range(n//2):
circuit.swap(quit, n-quit-1)
return circuit
def build_qc(num_qubits):
qc = QuantumCircuit(num_qubits)
qft_rotations(qc, num_qubits)
swap_registers(qc, num_qubits)
return qc
def build_qc_inverse(num_qubits):
qc = QuantumCircuit(num_qubits)
qft_rotations(qc, num_qubits)
swap_registers(qc, num_qubits)
return qc.inverse()
def run_qm_circuit(qc, mps):
# Write down the quantum circuit. This GHZ state given by the qmatcha library is the "best"
# for MPS tensor networks, since it uses a linear connectivity
#_ = GHZ_qiskit(qc)
io_info = QCIO(inPATH="data1/in/", outPATH="data1/out/", initial_state=mps)
observables = TNObservables()
save_state = TNState2File(name="my_tn_state", formatting="U")
observables += save_state
results = run_simulation(qc, observables=observables, io_info=io_info)
results.load_state()
state = results.observables["tn_state_path"]
#print("-" * 30, "Observables results", "-" * 30)
#print(f"The state is saved in {state}, expected is data/out/my_tn_state.pklmps")
#print(f"Class of the saved TN result is {results.tn_state}")
#print("The resulting statevector is:")
#print_state(results.tn_state.to_statevector(qiskit_order=True).elem )
#print()
comp_time = np.round(results.computational_time, 3)
meas_time = np.round(results.observables.get("measurement_time", None), 3)
memory = np.round(np.max(results.observables.get("memory", [0])), 4)
#print("-" * 30, "Runtime statistics", "-" * 30)
#print(f"Datetime of the simulation: {results.date_time}")
#print(f"Computational time: {comp_time} s")
#print(f"Measurement time: {meas_time} s")
#print(f"Maximum memory used: {memory} GB")
#print(
# f"Lower bound on the fidelity F of the state: {results.fidelity}, i.e. {results.fidelity}≤F≤1"
#)
return results
def ket_encoding_vector(img):
return np.ndarray.flatten(img)
def ket_encoding_zigzag(img):
blocksize = int(np.sqrt(img.size))
block = img.reshape(blocksize, blocksize)
l=[]
s=1
for i in range(2*blocksize-1):
if s==1:
x=0
y=i
while y>-1:
if x<blocksize:
if y<blocksize:
l.append(block[y,x])
y-=1
x+=1
if s==-1:
y=0
x=i
while x>-1:
if y<blocksize:
if x<blocksize:
l.append(block[y,x])
x-=1
y+=1
s*=-1
l = np.array(l)
return l
def ket_encoding_halfing(img):
pass
def ket_to_mps(ket):
n =int(np.log2(len(ket)))
conv_params = TNConvergenceParameters(max_bond_dimension=64)
#ket = ket.reshape([2]*n).reshape(-1, order="F")
mps = MPS.from_statevector(ket, conv_params=conv_params)
return mps
def qft_vector_encode(block):
bsize = block.shape[0]
ket = ket_encoding_vector(block).astype(complex)
norm_value = np.sqrt(np.sum(ket**2))
#print(norm_value)
norm_ket = ket / norm_value
num_qubits = np.log2(np.shape(ket)[0])
qc = build_qc(int(num_qubits))
mps = ket_to_mps(norm_ket)
results = run_qm_circuit(qc, mps)
results = results.tn_state.to_statevector(qiskit_order=False).elem * norm_value
return results.reshape(bsize,bsize)
def qft_vector_decode(block):
bsize = block.shape[0]
ket = ket_encoding_vector(block).astype(complex)
norm_value = np.sqrt(np.sum(ket**2))
norm_ket = ket / norm_value
num_qubits = np.log2(np.shape(ket)[0])
qc = build_qc_inverse(int(num_qubits))
mps = ket_to_mps(norm_ket)
results = run_qm_circuit(qc, mps)
results = results.tn_state.to_statevector(qiskit_order=False).elem.real * norm_value
return results.reshape(bsize,bsize)