-
Notifications
You must be signed in to change notification settings - Fork 2
/
utilities.py
266 lines (251 loc) · 10.7 KB
/
utilities.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
from qiskit import QuantumCircuit
import os
def add_controlU_multilayer(circ, pauli_str, number_of_qubits, quantum_register, ancilla_register):
'''Testing cricuits: Adds a controlled Pauli to circ.'''
# print(pauli_str)
c_minus_x = QuantumCircuit.from_qasm_str("""
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
u3(1.0592554e-13,3.288277,2.8116442) q[0];
u3(pi,5.8991791,2.7575864) q[1];
cx q[0],q[1];
u3(2*pi,5.4995268,4.1085153) q[0];
u3(pi,0.38400625,3.5255989) q[1];
""".strip())
c_minus_j_x = QuantumCircuit.from_qasm_str("""
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
u3(6.9759301e-13,4.3371506,0.076820611) q[0];
u3(6.9089241e-09,5.477472,3.947306) q[1];
cx q[0],q[1];
u3(2*pi,0.60772643,2.832284) q[0];
u3(5.5644668e-09,5.4477338,3.9770441) q[1];
""".strip())
c_plus_j_x =QuantumCircuit.from_qasm_str("""
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
u3(pi,4.6477413,6.258122) q[0];
u3(1.7599424,3*pi/2,3*pi/2) q[1];
cx q[0],q[1];
u3(pi,3.1666559,3.2062404) q[0];
u3(4.9015351,3*pi/2,3*pi/2) q[1];
""".strip())
#Check the length of the pauli string. It maybe empty when there were no matches found.
ancilla_qpos=ancilla_register
# ancilla_qpos=ancilla_register
# print(circ)
if len(pauli_str)>number_of_qubits:
phase=pauli_str[:2]
pauli_str=pauli_str[2:]
qubit_pos=number_of_qubits-1
phase_added=False
for _, char in enumerate(pauli_str):
# First case is most common
if phase_added or phase=="+1":
if char=="X":
# print(phase_added, phase, char)
circ.cx(ancilla_qpos, quantum_register[qubit_pos])
elif char=="Y":
# print(phase_added, phase, char)
circ.sdg(quantum_register[qubit_pos])
circ.cx(ancilla_qpos, quantum_register[qubit_pos])
circ.s(quantum_register[qubit_pos])
elif char=="Z":
# print(phase_added, phase, char)
circ.h(quantum_register[qubit_pos])
circ.cx(ancilla_qpos, quantum_register[qubit_pos])
circ.h(quantum_register[qubit_pos])
# -1 phase
else:
if char=="X":
# print(phase_added, phase, char)
circ.compose(c_minus_x, qubits=[ancilla_qpos, quantum_register[qubit_pos]], inplace=True)
elif char=="Y":
# print(phase_added, phase, char)
circ.sdg(quantum_register[qubit_pos])
circ.compose(c_minus_x, qubits=[ancilla_qpos, quantum_register[qubit_pos]], inplace=True)
circ.s(quantum_register[qubit_pos])
elif char=="Z":
# print(phase_added, phase, char)
circ.h(quantum_register[qubit_pos])
circ.compose(c_minus_x, qubits=[ancilla_qpos, quantum_register[qubit_pos]], inplace=True)
circ.h(quantum_register[qubit_pos])
# Note the phase added needs this check because of identity terms in the pauli strings.
# With no check, if there is an identity it will change phase_added to true and we won't get the
# added necessary phase on of the pauli terms.
if char!="I":
phase_added=True
qubit_pos-=1
return circ
def add_controlU_multilayer_clifford(circ, pauli_str, number_of_qubits, quantum_register, ancilla_register):
'''Testing cricuits: Adds a controlled Pauli to circ.'''
# print(pauli_str)
c_minus_x = QuantumCircuit.from_qasm_str("""
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
z q[1];
cx q[0],q[1];
z q[1];
""".strip())
c_minus_j_x = QuantumCircuit.from_qasm_str("""
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
u3(6.9759301e-13,4.3371506,0.076820611) q[0];
u3(6.9089241e-09,5.477472,3.947306) q[1];
cx q[0],q[1];
u3(2*pi,0.60772643,2.832284) q[0];
u3(5.5644668e-09,5.4477338,3.9770441) q[1];
""".strip())
c_plus_j_x =QuantumCircuit.from_qasm_str("""
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
u3(pi,4.6477413,6.258122) q[0];
u3(1.7599424,3*pi/2,3*pi/2) q[1];
cx q[0],q[1];
u3(pi,3.1666559,3.2062404) q[0];
u3(4.9015351,3*pi/2,3*pi/2) q[1];
""".strip())
#Check the length of the pauli string. It maybe empty when there were no matches found.
ancilla_qpos=ancilla_register
# ancilla_qpos=ancilla_register
# print(circ)
if len(pauli_str)>number_of_qubits:
phase=pauli_str[:2]
pauli_str=pauli_str[2:]
qubit_pos=number_of_qubits-1
phase_added=False
for _, char in enumerate(pauli_str):
# First case is most common
if phase_added or phase=="+1":
if char=="X":
# print(phase_added, phase, char)
circ.cx(ancilla_qpos, quantum_register[qubit_pos])
elif char=="Y":
# print(phase_added, phase, char)
circ.sdg(quantum_register[qubit_pos])
circ.cx(ancilla_qpos, quantum_register[qubit_pos])
circ.s(quantum_register[qubit_pos])
elif char=="Z":
# print(phase_added, phase, char)
circ.h(quantum_register[qubit_pos])
circ.cx(ancilla_qpos, quantum_register[qubit_pos])
circ.h(quantum_register[qubit_pos])
# -1 phase
else:
if char=="X":
# print(phase_added, phase, char)
circ.compose(c_minus_x, qubits=[ancilla_qpos, quantum_register[qubit_pos]], inplace=True)
elif char=="Y":
# print(phase_added, phase, char)
circ.sdg(quantum_register[qubit_pos])
circ.compose(c_minus_x, qubits=[ancilla_qpos, quantum_register[qubit_pos]], inplace=True)
circ.s(quantum_register[qubit_pos])
elif char=="Z":
# print(phase_added, phase, char)
circ.h(quantum_register[qubit_pos])
circ.compose(c_minus_x, qubits=[ancilla_qpos, quantum_register[qubit_pos]], inplace=True)
circ.h(quantum_register[qubit_pos])
# Note the phase added needs this check because of identity terms in the pauli strings.
# With no check, if there is an identity it will change phase_added to true and we won't get the
# added necessary phase on of the pauli terms.
if char!="I":
phase_added=True
qubit_pos-=1
return circ
def add_controlU(circ, pauli_str, number_of_qubits, quantum_register, ancilla_register):
'''Testing cricuits: Adds a controlled Pauli to circ.'''
print(pauli_str)
c_minus_x = QuantumCircuit.from_qasm_str("""
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
u3(1.0592554e-13,3.288277,2.8116442) q[0];
u3(pi,5.8991791,2.7575864) q[1];
cx q[0],q[1];
u3(2*pi,5.4995268,4.1085153) q[0];
u3(pi,0.38400625,3.5255989) q[1];
""".strip())
c_minus_j_x = QuantumCircuit.from_qasm_str("""
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
u3(6.9759301e-13,4.3371506,0.076820611) q[0];
u3(6.9089241e-09,5.477472,3.947306) q[1];
cx q[0],q[1];
u3(2*pi,0.60772643,2.832284) q[0];
u3(5.5644668e-09,5.4477338,3.9770441) q[1];
""".strip())
c_plus_j_x =QuantumCircuit.from_qasm_str("""
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
u3(pi,4.6477413,6.258122) q[0];
u3(1.7599424,3*pi/2,3*pi/2) q[1];
cx q[0],q[1];
u3(pi,3.1666559,3.2062404) q[0];
u3(4.9015351,3*pi/2,3*pi/2) q[1];
""".strip())
#Check the length of the pauli string. It maybe empty when there were no matches found.
ancilla_qpos=ancilla_register[0]
if len(pauli_str)>number_of_qubits:
phase=pauli_str[:2]
pauli_str=pauli_str[2:]
qubit_pos=number_of_qubits-1
phase_added=False
for _, char in enumerate(pauli_str):
# First case is most common
if phase_added or phase=="+1":
if char=="X":
# print(phase_added, phase, char)
circ.cx(ancilla_qpos, quantum_register[qubit_pos])
elif char=="Y":
# print(phase_added, phase, char)
circ.sdg(quantum_register[qubit_pos])
circ.cx(ancilla_qpos, quantum_register[qubit_pos])
circ.s(quantum_register[qubit_pos])
elif char=="Z":
# print(phase_added, phase, char)
circ.h(quantum_register[qubit_pos])
circ.cx(ancilla_qpos, quantum_register[qubit_pos])
circ.h(quantum_register[qubit_pos])
# -1 phase
else:
if char=="X":
# print(phase_added, phase, char)
circ.compose(c_minus_x, qubits=[ancilla_qpos, quantum_register[qubit_pos]], inplace=True)
elif char=="Y":
# print(phase_added, phase, char)
circ.sdg(quantum_register[qubit_pos])
circ.compose(c_minus_x, qubits=[ancilla_qpos, quantum_register[qubit_pos]], inplace=True)
circ.s(quantum_register[qubit_pos])
elif char=="Z":
# print(phase_added, phase, char)
circ.h(quantum_register[qubit_pos])
circ.compose(c_minus_x, qubits=[ancilla_qpos, quantum_register[qubit_pos]], inplace=True)
circ.h(quantum_register[qubit_pos])
# Note the phase added needs this check because of identity terms in the pauli strings.
# With no check, if there is an identity it will change phase_added to true and we won't get the
# added necessary phase on of the pauli terms.
if char!="I":
phase_added=True
qubit_pos-=1
return circ
def get_files_from_dir_by_extension(base_path, extension):
'''Extension: should be a string with that looks like ".*"
Return: type list'''
all_files= [file_name for file_name in os.listdir(base_path) if os.path.isfile(os.path.join(base_path, file_name)) ]
found_files=[]
for file_name in all_files:
if file_name.endswith(extension):
found_files.append(file_name)
return found_files
def insert_identity_layer(circ):
'''Inserts a layer of identity gates.'''
for idx in list(range(circ.num_qubits)):
circ.i(idx)