Spaces:
Runtime error
Runtime error
Amit
commited on
Commit
·
951488e
1
Parent(s):
8e22481
Translator and toolchain added.
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- qasm_parser/braket/qasm_braket_parser.py +186 -0
- qasm_parser/cirq/qasm_cirq_parser.py +205 -0
- qasm_parser/cudaq/qasm_cudaq_parser.py +177 -0
- qasm_parser/cuquantum_qiskit/qasm_qiskit_parser_gpu_2pt0_new.py +163 -0
- qasm_parser/cuquantum_qiskit/qasm_qiskit_parser_ngpu_2pt0_new.py +163 -0
- qasm_parser/cuquantum_qsimcirq/qasm_qsimcirq_gpu_parser.py +210 -0
- qasm_parser/cuquantum_qsimcirq/qasm_qsimcirq_ngpu_parser.py +210 -0
- qasm_parser/hiq/qasm_hiq_parser.py +188 -0
- qasm_parser/hybridq/qasm_hybridq_gpu_parser.py +197 -0
- qasm_parser/hybridq/qasm_hybridq_mt_parser.py +175 -0
- qasm_parser/hybridq/qasm_hybridq_parser.py +199 -0
- qasm_parser/intel_qs/qasm_intel_mt_parser.py +213 -0
- qasm_parser/intel_qs/qasm_intel_parser.py +252 -0
- qasm_parser/intel_qs/qasm_intel_parser_mpi.py +245 -0
- qasm_parser/intel_qs/qasm_intel_parser_sp.py +253 -0
- qasm_parser/myqlm/qasm_myqlm_cpp_parser.py +210 -0
- qasm_parser/myqlm/qasm_myqlm_py_parser.py +201 -0
- qasm_parser/pennylane/qasm_pennylane_gpu_parser.py +198 -0
- qasm_parser/pennylane/qasm_pennylane_l_parser.py +185 -0
- qasm_parser/pennylane/qasm_pennylane_parser.py +184 -0
- qasm_parser/projectq/qasm_projectq_mt_parser.py +163 -0
- qasm_parser/projectq/qasm_projectq_parser.py +185 -0
- qasm_parser/qcgpu/qasm_gcgpu_parser.py +198 -0
- qasm_parser/qibo/qasm_qibo_parser.py +150 -0
- qasm_parser/qibo/qasm_qibojit_gpu_parser.py +167 -0
- qasm_parser/qibo/qasm_qibojit_parser.py +151 -0
- qasm_parser/qibo_ngpu/qasm_qibojit_gpu_parser.py +183 -0
- qasm_parser/qiskit/qasm_qiskit_parser_2pt0_new.py +200 -0
- qasm_parser/qiskit/qasm_qiskit_parser_gpu_2pt0_new.py +186 -0
- qasm_parser/qiskit/qasm_qiskit_parser_mt_2pt0_new.py +187 -0
- qasm_parser/qiskit_cont_ngpu/qasm_qiskit_parser_nvidia_container_2pt0.py +173 -0
- qasm_parser/qpanda/qasm_qpanda_parser.py +174 -0
- qasm_parser/qpp/qasm_qpp_parser.py +199 -0
- qasm_parser/qrack/qasm_qrack_opt_gpu_parser.py +179 -0
- qasm_parser/qrack/qasm_qrack_opt_parser.py +179 -0
- qasm_parser/qrack/qasm_qrack_sch_gpu_parser.py +179 -0
- qasm_parser/qrack/qasm_qrack_sch_parser.py +179 -0
- qasm_parser/qsimcirq/qasm_qsimcirq_a100_parser.py +211 -0
- qasm_parser/qsimcirq/qasm_qsimcirq_gpu_parser.py +210 -0
- qasm_parser/qsimcirq/qasm_qsimcirq_mt_parser.py +184 -0
- qasm_parser/qsimcirq/qasm_qsimcirq_parser.py +213 -0
- qasm_parser/qsimcirq_cont_ngpu/qasm_qsimcirq_mnvidia_parser.py +191 -0
- qasm_parser/quest/qasm_quest_parser.py +214 -0
- qasm_parser/quest/qasm_quest_parser_sp.py +214 -0
- qasm_parser/qulacs/qasm_qulacs_gpu_new_parser.py +200 -0
- qasm_parser/qulacs/qasm_qulacs_new_parser.py +200 -0
- qasm_parser/svsim/qasm_svsim_gpu_a100_parser.py +152 -0
- qasm_parser/svsim/qasm_svsim_gpu_ma100_parser.py +152 -0
- qasm_parser/svsim/qasm_svsim_gpu_parser.py +152 -0
- qasm_parser/svsim/qasm_svsim_mt_parser.py +152 -0
qasm_parser/braket/qasm_braket_parser.py
ADDED
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import numpy as np\n')
|
11 |
+
f.write('from braket.circuits import Circuit\n')
|
12 |
+
f.write('from braket.devices import LocalSimulator\n')
|
13 |
+
f.write('from braket.circuits.gates import Unitary\n')
|
14 |
+
f.write('import os\n')
|
15 |
+
f.write('from time import process_time, perf_counter\n')
|
16 |
+
|
17 |
+
f.write('def U2(p, l):\n')
|
18 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
19 |
+
f.write(' return np.array(mat)\n\n')
|
20 |
+
|
21 |
+
f.write('def U3(t, p, l):\n')
|
22 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
23 |
+
f.write(' return np.array(mat)\n\n')
|
24 |
+
|
25 |
+
f.write('t_sp = process_time()\n')
|
26 |
+
f.write('t_s = perf_counter()\n')
|
27 |
+
|
28 |
+
f.write('qc = Circuit()\n')
|
29 |
+
|
30 |
+
with open(input_filename, "r") as ifile:
|
31 |
+
lines = ifile.readlines()
|
32 |
+
|
33 |
+
lc = 0
|
34 |
+
cirq_c = 0
|
35 |
+
for line in lines:
|
36 |
+
|
37 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
38 |
+
|
39 |
+
if s is None:
|
40 |
+
continue
|
41 |
+
|
42 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
43 |
+
lc = lc + 1
|
44 |
+
f.write('# {}\n'.format(s.group()))
|
45 |
+
continue
|
46 |
+
|
47 |
+
elif s.group() == 'cirq':
|
48 |
+
lc = lc + 1
|
49 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
50 |
+
cirq_c = cirq_c + 1
|
51 |
+
continue
|
52 |
+
|
53 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'h':
|
54 |
+
lc = lc + 1
|
55 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
56 |
+
t_qbit = int(m_i[0].strip('[]'))
|
57 |
+
f.write('qc.{}({})\n'.format(s.group(), t_qbit))
|
58 |
+
continue
|
59 |
+
|
60 |
+
elif s.group() == 'sx':
|
61 |
+
lc = lc + 1
|
62 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
63 |
+
t_qbit = int(m_i[0].strip('[]'))
|
64 |
+
f.write('qc.v({})\n'.format(t_qbit))
|
65 |
+
|
66 |
+
elif s.group() == 'sxdg':
|
67 |
+
lc = lc + 1
|
68 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
69 |
+
t_qbit = int(m_i[0].strip('[]'))
|
70 |
+
f.write('qc.vi({})\n'.format(t_qbit))
|
71 |
+
continue
|
72 |
+
|
73 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
74 |
+
lc = lc + 1
|
75 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
76 |
+
# print(m_r)
|
77 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
78 |
+
sp_str = m_r[0].split('e-')
|
79 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
80 |
+
elif 'pi' in m_r[0]:
|
81 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
82 |
+
# print(m_r)
|
83 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
84 |
+
t_qbit = int(m_i[0].strip('[]'))
|
85 |
+
# f.write('qc.add_{}gate({}, np.pi*{})\n'.format(s.group().upper(), t_qbit, float(m_r[0][0])))
|
86 |
+
f.write('qc.{}({}, np.pi*{})\n'.format(s.group(), t_qbit, float(m_r[0][0])))
|
87 |
+
continue
|
88 |
+
|
89 |
+
elif s.group() == 'cx':
|
90 |
+
lc = lc + 1
|
91 |
+
match = re.findall(r'\[\d\d*\]', line)
|
92 |
+
c_qbit = int(match[0].strip('[]'))
|
93 |
+
t_qbit = int(match[1].strip('[]'))
|
94 |
+
f.write('qc.cnot({}, {})\n'.format(c_qbit, t_qbit))
|
95 |
+
continue
|
96 |
+
|
97 |
+
elif s.group() == 'u2':
|
98 |
+
lc = lc + 1
|
99 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
100 |
+
m_r = m_r[0].split(',')
|
101 |
+
for i, m in enumerate(m_r):
|
102 |
+
if 'pi' in m and 'e' in m:
|
103 |
+
sp_str = m_r[i].split('e-')
|
104 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
105 |
+
elif 'pi' in m:
|
106 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
107 |
+
else:
|
108 |
+
m_r[i] = '0'
|
109 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
110 |
+
t_qbit = int(m_i[0].strip('[]'))
|
111 |
+
f.write('qc.unitary([{}], U2(np.pi*{}, np.pi*{}))\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
112 |
+
continue
|
113 |
+
|
114 |
+
elif s.group() == 'u3':
|
115 |
+
lc = lc + 1
|
116 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
117 |
+
m_r = m_r[0].split(',')
|
118 |
+
for i, m in enumerate(m_r):
|
119 |
+
if 'pi' in m and 'e' in m:
|
120 |
+
sp_str = m_r[i].split('e-')
|
121 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
122 |
+
elif 'pi' in m:
|
123 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
124 |
+
else:
|
125 |
+
m_r[i] = '0'
|
126 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
127 |
+
t_qbit = int(m_i[0].strip('[]'))
|
128 |
+
f.write('qc.unitary([{}], U3(np.pi*{}, np.pi*{}, np.pi*{}))\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
129 |
+
continue
|
130 |
+
|
131 |
+
|
132 |
+
f.write('qc.state_vector()\n')
|
133 |
+
f.write('device = LocalSimulator()\n')
|
134 |
+
f.write('f_state = device.run(qc).result().values[0]\n')
|
135 |
+
|
136 |
+
f.write('t_e = perf_counter()\n')
|
137 |
+
f.write('t_ep = process_time()\n')
|
138 |
+
f.write('print(t_e - t_s)\n')
|
139 |
+
f.write('print(t_ep - t_sp)\n')
|
140 |
+
|
141 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
142 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
143 |
+
|
144 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
145 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
146 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
147 |
+
print(int(lc_res) == lc)
|
148 |
+
# print(lc)
|
149 |
+
|
150 |
+
# parse_qasm_to_package_gen(12, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "n12_qulacs.py")
|
151 |
+
|
152 |
+
task = 'hdyn'
|
153 |
+
sim_pack = 'braket'
|
154 |
+
com_cap = 'st'
|
155 |
+
prec = 'dp'
|
156 |
+
|
157 |
+
for N in range(6, 38, 2):
|
158 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
159 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
160 |
+
|
161 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
162 |
+
|
163 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
164 |
+
|
165 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
166 |
+
parse_qasm_to_package_gen(N, input_file, output_file, save_file_path)
|
167 |
+
|
168 |
+
|
169 |
+
"""
|
170 |
+
for N in range(12, 42, 2):
|
171 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
172 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
173 |
+
|
174 |
+
output_file_path = '/data/user/gangap_a/rqc/qulacs/qulacs_run_files/'
|
175 |
+
output_file = output_file_path + 'qulacs_rqc_n{}.py'.format(N)
|
176 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
177 |
+
|
178 |
+
for N in range(41, 51):
|
179 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
180 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
181 |
+
|
182 |
+
output_file_path = '/data/user/gangap_a/rqc/qulacs/qulacs_run_files/'
|
183 |
+
output_file = output_file_path + 'qulacs_rqc_n{}.py'.format(N)
|
184 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
185 |
+
|
186 |
+
"""
|
qasm_parser/cirq/qasm_cirq_parser.py
ADDED
@@ -0,0 +1,205 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
import os
|
6 |
+
|
7 |
+
def parse_qasm_to_package_gen(N, precision, input_filename, output_filename, save_data_path):
|
8 |
+
|
9 |
+
with open(output_filename, 'w') as f:
|
10 |
+
|
11 |
+
f.write('import os\n')
|
12 |
+
f.write('from time import process_time, perf_counter\n')
|
13 |
+
f.write('import numpy as np\n\n')
|
14 |
+
f.write('import cirq\n')
|
15 |
+
|
16 |
+
f.write('class U2(cirq.Gate):\n')
|
17 |
+
f.write(' def __init__(self, phi, lam):\n')
|
18 |
+
f.write(' super(U2, self)\n')
|
19 |
+
f.write(' self._p = phi\n')
|
20 |
+
f.write(' self._l = lam\n\n')
|
21 |
+
|
22 |
+
f.write(' def _num_qubits_(self):\n')
|
23 |
+
f.write(' return 1\n\n')
|
24 |
+
|
25 |
+
f.write(' def _unitary_(self):\n')
|
26 |
+
f.write(' mat = ((np.cos(0.5*(self._p + self._l)) - 1j*np.sin(0.5*(self._p + self._l)))/np.sqrt(2))*np.array([[1, -np.cos(self._l)-1j*np.sin(self._l)], [np.cos(self._p) + 1j*np.sin(self._p), np.cos(self._l + self._p) + 1j*np.sin(self._l+self._p)]])\n')
|
27 |
+
f.write(' return mat\n\n')
|
28 |
+
|
29 |
+
f.write('class U3(cirq.Gate):\n')
|
30 |
+
f.write(' def __init__(self, theta, phi, lam):\n')
|
31 |
+
f.write(' super(U3, self)\n')
|
32 |
+
f.write(' self._t = theta\n')
|
33 |
+
f.write(' self._p = phi\n')
|
34 |
+
f.write(' self._l = lam\n\n')
|
35 |
+
|
36 |
+
f.write(' def _num_qubits_(self):\n')
|
37 |
+
f.write(' return 1\n\n')
|
38 |
+
|
39 |
+
f.write(' def _unitary_(self):\n')
|
40 |
+
f.write(' mat = (np.cos(0.5*(self._p + self._l)) - 1j*np.sin(0.5*(self._p + self._l)))*np.array([[np.cos(self._t/2.), -np.sin(self._t/2.)*(np.cos(self._l) + 1j*np.sin(self._l))], [np.sin(self._t/2.)*(np.cos(self._p) + 1j*np.sin(self._p)), np.cos(self._t/2.)*(np.cos(self._l + self._p) + 1j*np.sin(self._l + self._p))]])\n')
|
41 |
+
f.write(' return mat\n\n')
|
42 |
+
|
43 |
+
f.write('t_sp = process_time()\n')
|
44 |
+
f.write('t_s = perf_counter()\n')
|
45 |
+
|
46 |
+
f.write('q = cirq.LineQubit.range({})\n'.format(N))
|
47 |
+
|
48 |
+
f.write('cir = cirq.Circuit()\n')
|
49 |
+
|
50 |
+
with open(input_filename, "r") as ifile:
|
51 |
+
lines = ifile.readlines()
|
52 |
+
|
53 |
+
lc = 0
|
54 |
+
cirq_c = 0
|
55 |
+
for line in lines:
|
56 |
+
|
57 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
58 |
+
|
59 |
+
if s is None:
|
60 |
+
continue
|
61 |
+
|
62 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
63 |
+
lc = lc + 1
|
64 |
+
f.write('# {}\n'.format(s.group()))
|
65 |
+
continue
|
66 |
+
|
67 |
+
elif s.group() == 'cirq':
|
68 |
+
lc = lc + 1
|
69 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
70 |
+
cirq_c = cirq_c + 1
|
71 |
+
continue
|
72 |
+
|
73 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'h':
|
74 |
+
lc = lc + 1
|
75 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
76 |
+
t_qbit = int(m_i[0].strip('[]'))
|
77 |
+
f.write('cir.append(cirq.{}(q[{}]))\n'.format(s.group().upper(), t_qbit))
|
78 |
+
continue
|
79 |
+
|
80 |
+
elif s.group() == 'sx':
|
81 |
+
lc = lc + 1
|
82 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
83 |
+
t_qbit = int(m_i[0].strip('[]'))
|
84 |
+
f.write('cir.append(cirq.XPowGate(exponent=0.5).on(q[{}]))\n'.format(t_qbit))
|
85 |
+
|
86 |
+
elif s.group() == 'sxdg':
|
87 |
+
lc = lc + 1
|
88 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
89 |
+
t_qbit = int(m_i[0].strip('[]'))
|
90 |
+
f.write('cir.append(cirq.XPowGate(exponent=-0.5).on(q[{}]))\n'.format(t_qbit))
|
91 |
+
|
92 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
93 |
+
lc = lc + 1
|
94 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
95 |
+
# print(m_r)
|
96 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
97 |
+
sp_str = m_r[0].split('e-')
|
98 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
99 |
+
elif 'pi' in m_r[0]:
|
100 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
101 |
+
# print(m_r)
|
102 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
103 |
+
t_qbit = int(m_i[0].strip('[]'))
|
104 |
+
f.write('cir.append(cirq.{}(np.pi*{}).on(q[{}]))\n'.format(s.group(), float(m_r[0][0]), t_qbit))
|
105 |
+
continue
|
106 |
+
|
107 |
+
elif s.group() == 'cx':
|
108 |
+
lc = lc + 1
|
109 |
+
match = re.findall(r'\[\d\d*\]', line)
|
110 |
+
c_qbit = int(match[0].strip('[]'))
|
111 |
+
t_qbit = int(match[1].strip('[]'))
|
112 |
+
# f.write('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit))
|
113 |
+
f.write('cir.append(cirq.CNOT(q[{}], q[{}]))\n'.format(c_qbit, t_qbit))
|
114 |
+
continue
|
115 |
+
|
116 |
+
elif s.group() == 'u2':
|
117 |
+
lc = lc + 1
|
118 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
119 |
+
m_r = m_r[0].split(',')
|
120 |
+
for i, m in enumerate(m_r):
|
121 |
+
if 'pi' in m and 'e' in m:
|
122 |
+
sp_str = m_r[i].split('e-')
|
123 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
124 |
+
elif 'pi' in m:
|
125 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
126 |
+
else:
|
127 |
+
m_r[i] = '0'
|
128 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
129 |
+
t_qbit = int(m_i[0].strip('[]'))
|
130 |
+
f.write('cir.append(U2(np.pi*{}, np.pi*{}).on(q[{}]))\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
131 |
+
continue
|
132 |
+
|
133 |
+
elif s.group() == 'u3':
|
134 |
+
lc = lc + 1
|
135 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
136 |
+
m_r = m_r[0].split(',')
|
137 |
+
for i, m in enumerate(m_r):
|
138 |
+
if 'pi' in m and 'e' in m:
|
139 |
+
sp_str = m_r[i].split('e-')
|
140 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
141 |
+
elif 'pi' in m:
|
142 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
143 |
+
else:
|
144 |
+
m_r[i] = '0'
|
145 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
146 |
+
t_qbit = int(m_i[0].strip('[]'))
|
147 |
+
f.write('cir.append(U3(np.pi*{}, np.pi*{}, np.pi*{}).on(q[{}]))\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
148 |
+
continue
|
149 |
+
|
150 |
+
if precision == 'single':
|
151 |
+
f.write('simulator = cirq.Simulator()\n')
|
152 |
+
elif precision == 'double':
|
153 |
+
f.write('simulator = cirq.Simulator(dtype=np.complex128)\n')
|
154 |
+
f.write('result = simulator.simulate(cir)\n')
|
155 |
+
f.write('t_e = perf_counter()\n')
|
156 |
+
f.write('t_ep = process_time()\n')
|
157 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
158 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
159 |
+
f.write('print(t_e - t_s)\n')
|
160 |
+
f.write('print(t_ep - t_sp)\n')
|
161 |
+
# f.write('print(result.final_state_vector)\n')
|
162 |
+
|
163 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
164 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
165 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
166 |
+
print(int(lc_res) == lc)
|
167 |
+
# print(lc)
|
168 |
+
|
169 |
+
# parse_qasm_to_package_gen(12, "../qasm_test/qasm_rqc_test.qasm", "test_cirq.py")
|
170 |
+
# parse_qasm_to_package_gen(12, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_cirq.py")
|
171 |
+
|
172 |
+
r"""
|
173 |
+
for N in range(12, 42, 2):
|
174 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
175 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
176 |
+
|
177 |
+
output_file_path = '/data/user/gangap_a/rqc/cirq/cirq_run_files/'
|
178 |
+
output_file = output_file_path + 'cirq_rqc_n{}.py'.format(N)
|
179 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
180 |
+
|
181 |
+
for N in range(41, 51):
|
182 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
183 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
184 |
+
|
185 |
+
output_file_path = '/data/user/gangap_a/rqc/cirq/cirq_run_files/'
|
186 |
+
output_file = output_file_path + 'cirq_rqc_n{}.py'.format(N)
|
187 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
188 |
+
"""
|
189 |
+
|
190 |
+
task = 'qft'
|
191 |
+
sim_pack = 'cirq'
|
192 |
+
com_cap = 'st'
|
193 |
+
prec = 'sp'
|
194 |
+
|
195 |
+
for N in range(6, 40, 2):
|
196 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
197 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
198 |
+
|
199 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
200 |
+
|
201 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
202 |
+
|
203 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
204 |
+
parse_qasm_to_package_gen(N, 'single', input_file, output_file, save_file_path)
|
205 |
+
|
qasm_parser/cudaq/qasm_cudaq_parser.py
ADDED
@@ -0,0 +1,177 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, comp_cap, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
f.write('import sys\n')
|
12 |
+
f.write('import numpy as np\n')
|
13 |
+
f.write('from time import process_time, perf_counter\n')
|
14 |
+
f.write('import cudaq\n')
|
15 |
+
if comp_cap == 'st' or comp_cap == 'mt':
|
16 |
+
f.write('cudaq.set_qpu(\'qpp\')\n')
|
17 |
+
elif comp_cap == 'gpu':
|
18 |
+
f.write('cudaq.set_qpu(\'cuquantum\')\n')
|
19 |
+
|
20 |
+
f.write('t_sp = process_time()\n')
|
21 |
+
f.write('t_s = perf_counter()\n')
|
22 |
+
|
23 |
+
f.write('cir = cudaq.make_kernel()\n'.format(N))
|
24 |
+
f.write('q = cir.qalloc({})\n'.format(N))
|
25 |
+
|
26 |
+
with open(input_filename, "r") as ifile:
|
27 |
+
lines = ifile.readlines()
|
28 |
+
|
29 |
+
lc = 0
|
30 |
+
cirq_c = 0
|
31 |
+
for line in lines:
|
32 |
+
|
33 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
34 |
+
|
35 |
+
if s is None:
|
36 |
+
continue
|
37 |
+
|
38 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
39 |
+
lc = lc + 1
|
40 |
+
f.write('# {}\n'.format(s.group()))
|
41 |
+
continue
|
42 |
+
|
43 |
+
elif s.group() == 'cirq':
|
44 |
+
lc = lc + 1
|
45 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
46 |
+
cirq_c = cirq_c + 1
|
47 |
+
continue
|
48 |
+
|
49 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'h':
|
50 |
+
lc = lc + 1
|
51 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
52 |
+
t_qbit = int(m_i[0].strip('[]'))
|
53 |
+
f.write('cir.{}(q[{}])\n'.format(s.group(), t_qbit))
|
54 |
+
continue
|
55 |
+
|
56 |
+
elif s.group() == 'sx':
|
57 |
+
lc = lc + 1
|
58 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
59 |
+
t_qbit = int(m_i[0].strip('[]'))
|
60 |
+
f.write('cir.rx(0.5*np.pi, q[{}])\n'.format(t_qbit))
|
61 |
+
|
62 |
+
elif s.group() == 'sxdg':
|
63 |
+
lc = lc + 1
|
64 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
65 |
+
t_qbit = int(m_i[0].strip('[]'))
|
66 |
+
f.write('cir.rx(-0.5*np.pi, q[{}])\n'.format(t_qbit))
|
67 |
+
continue
|
68 |
+
|
69 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
70 |
+
lc = lc + 1
|
71 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
72 |
+
# print(m_r)
|
73 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
74 |
+
sp_str = m_r[0].split('e-')
|
75 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
76 |
+
elif 'pi' in m_r[0]:
|
77 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
78 |
+
# print(m_r)
|
79 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
80 |
+
t_qbit = int(m_i[0].strip('[]'))
|
81 |
+
f.write('cir.{}(np.pi*{}, q[{}])\n'.format(s.group(), float(m_r[0][0]), t_qbit))
|
82 |
+
continue
|
83 |
+
|
84 |
+
elif s.group() == 'cx':
|
85 |
+
lc = lc + 1
|
86 |
+
match = re.findall(r'\[\d\d*\]', line)
|
87 |
+
c_qbit = int(match[0].strip('[]'))
|
88 |
+
t_qbit = int(match[1].strip('[]'))
|
89 |
+
f.write('cir.cx(q[{}], q[{}])\n'.format(c_qbit, t_qbit))
|
90 |
+
continue
|
91 |
+
|
92 |
+
elif s.group() == 'u2':
|
93 |
+
lc = lc + 1
|
94 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
95 |
+
m_r = m_r[0].split(',')
|
96 |
+
for i, m in enumerate(m_r):
|
97 |
+
if 'pi' in m and 'e' in m:
|
98 |
+
sp_str = m_r[i].split('e-')
|
99 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
100 |
+
elif 'pi' in m:
|
101 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
102 |
+
else:
|
103 |
+
m_r[i] = '0'
|
104 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
105 |
+
t_qbit = int(m_i[0].strip('[]'))
|
106 |
+
f.write('cir.U2(np.pi*{}, np.pi*{}, q[{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
107 |
+
continue
|
108 |
+
|
109 |
+
elif s.group() == 'u3':
|
110 |
+
lc = lc + 1
|
111 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
112 |
+
m_r = m_r[0].split(',')
|
113 |
+
for i, m in enumerate(m_r):
|
114 |
+
if 'pi' in m and 'e' in m:
|
115 |
+
sp_str = m_r[i].split('e-')
|
116 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
117 |
+
elif 'pi' in m:
|
118 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
119 |
+
else:
|
120 |
+
m_r[i] = '0'
|
121 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
122 |
+
t_qbit = int(m_i[0].strip('[]'))
|
123 |
+
f.write('cir.U3(np.pi*{}, np.pi*{}, np.pi*{}, q[{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
124 |
+
continue
|
125 |
+
|
126 |
+
f.write('state = cudaq.get_state(cir)\n')
|
127 |
+
|
128 |
+
f.write('t_e = perf_counter()\n')
|
129 |
+
f.write('t_ep = process_time()\n')
|
130 |
+
f.write('print(t_e - t_s)\n')
|
131 |
+
f.write('print(t_ep - t_sp)\n')
|
132 |
+
|
133 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
134 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
135 |
+
|
136 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
137 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
138 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
139 |
+
print(int(lc_res) == lc)
|
140 |
+
# print(lc)
|
141 |
+
|
142 |
+
|
143 |
+
task = 'rqc'
|
144 |
+
sim_pack = 'cudaq'
|
145 |
+
com_cap = 'st'
|
146 |
+
prec = 'sp'
|
147 |
+
|
148 |
+
for N in range(12, 38, 2):
|
149 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
150 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
151 |
+
|
152 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
153 |
+
|
154 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
155 |
+
|
156 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
157 |
+
parse_qasm_to_package_gen(N, com_cap, input_file, output_file, save_file_path)
|
158 |
+
|
159 |
+
|
160 |
+
r"""
|
161 |
+
for N in range(12, 42, 2):
|
162 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
163 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
164 |
+
|
165 |
+
output_file_path = '/data/user/gangap_a/rqc/svsim/svsim_run_files/'
|
166 |
+
output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N)
|
167 |
+
parse_qasm_to_package_gen(N, 1, input_file, output_file)
|
168 |
+
|
169 |
+
for N in range(41, 51):
|
170 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
171 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
172 |
+
|
173 |
+
output_file_path = '/data/user/gangap_a/rqc/svsim/svsim_run_files/'
|
174 |
+
output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N)
|
175 |
+
parse_qasm_to_package_gen(N, 1, input_file, output_file)
|
176 |
+
|
177 |
+
"""
|
qasm_parser/cuquantum_qiskit/qasm_qiskit_parser_gpu_2pt0_new.py
ADDED
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
import os
|
6 |
+
|
7 |
+
def parse_qasm_to_package_gen(N, f_precision, input_filename, output_filename, save_data_path):
|
8 |
+
|
9 |
+
with open(output_filename, 'w') as f:
|
10 |
+
|
11 |
+
f.write('import os\n')
|
12 |
+
f.write('from qiskit import QuantumRegister, QuantumCircuit, transpile, ClassicalRegister\n')
|
13 |
+
f.write('from qiskit.quantum_info.operators import Operator\n')
|
14 |
+
f.write('from qiskit.providers.aer import QasmSimulator\n')
|
15 |
+
f.write('from qiskit import Aer\n')
|
16 |
+
f.write('from time import process_time, perf_counter\n')
|
17 |
+
f.write('import numpy as np\n\n')
|
18 |
+
|
19 |
+
f.write('def U2(p, l):\n')
|
20 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
21 |
+
f.write(' return Operator(mat)\n\n')
|
22 |
+
|
23 |
+
f.write('def U3(t, p, l):\n')
|
24 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
25 |
+
f.write(' return Operator(mat)\n\n')
|
26 |
+
|
27 |
+
f.write('t_sp = process_time()\n')
|
28 |
+
f.write('t_s = perf_counter()\n')
|
29 |
+
|
30 |
+
# f.write('backend = Aer.get_backend(\'statevector_simulator\')\n')#, max_parallel_threads={}, statevector_parallel_threshold={}, precision=\'{}\')\n'.format(mpt, spt, f_precision))
|
31 |
+
f.write('backend = Aer.get_backend(\'aer_simulator_statevector\')\n')
|
32 |
+
f.write('opt = backend.options\n')
|
33 |
+
f.write('opt.device=\'GPU\'\n')
|
34 |
+
f.write('opt.precision=\'{}\'\n'.format(f_precision))
|
35 |
+
|
36 |
+
f.write('cir = QuantumCircuit({})\n'.format(N))
|
37 |
+
with open(input_filename, "r") as ifile:
|
38 |
+
lines = ifile.readlines()
|
39 |
+
|
40 |
+
lc = 0
|
41 |
+
cirq_c = 0
|
42 |
+
for line in lines:
|
43 |
+
|
44 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
45 |
+
|
46 |
+
if s is None:
|
47 |
+
continue
|
48 |
+
|
49 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
50 |
+
lc = lc + 1
|
51 |
+
f.write('# {}\n'.format(s.group()))
|
52 |
+
continue
|
53 |
+
|
54 |
+
elif s.group() == 'cirq':
|
55 |
+
lc = lc + 1
|
56 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
57 |
+
cirq_c = cirq_c + 1
|
58 |
+
continue
|
59 |
+
|
60 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'sx' or s.group() == 'sxdg' or s.group() == 'h':
|
61 |
+
lc = lc + 1
|
62 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
63 |
+
t_qbit = int(m_i[0].strip('[]'))
|
64 |
+
f.write('cir.{}({})\n'.format(s.group(), t_qbit))
|
65 |
+
continue
|
66 |
+
|
67 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
68 |
+
lc = lc + 1
|
69 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
70 |
+
# print(m_r)
|
71 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
72 |
+
sp_str = m_r[0].split('e-')
|
73 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
74 |
+
elif 'pi' in m_r[0]:
|
75 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
76 |
+
# print(m_r)
|
77 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
78 |
+
t_qbit = int(m_i[0].strip('[]'))
|
79 |
+
f.write('cir.{}(np.pi*{}, {})\n'.format(s.group(), float(m_r[0][0]), t_qbit))
|
80 |
+
continue
|
81 |
+
|
82 |
+
elif s.group() == 'cx':
|
83 |
+
lc = lc + 1
|
84 |
+
match = re.findall(r'\[\d\d*\]', line)
|
85 |
+
c_qbit = int(match[0].strip('[]'))
|
86 |
+
t_qbit = int(match[1].strip('[]'))
|
87 |
+
f.write('cir.cx({}, {})\n'.format(c_qbit, t_qbit))
|
88 |
+
continue
|
89 |
+
|
90 |
+
elif s.group() == 'u2':
|
91 |
+
lc = lc + 1
|
92 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
93 |
+
m_r = m_r[0].split(',')
|
94 |
+
for i, m in enumerate(m_r):
|
95 |
+
if 'pi' in m and 'e' in m:
|
96 |
+
sp_str = m_r[i].split('e-')
|
97 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
98 |
+
elif 'pi' in m:
|
99 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
100 |
+
else:
|
101 |
+
m_r[i] = '0'
|
102 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
103 |
+
t_qbit = int(m_i[0].strip('[]'))
|
104 |
+
# f.write('cir.u(np.pi/2., np.pi*{}, np.pi*{}, {})\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
105 |
+
f.write('cir.append(U2(np.pi*{}, np.pi*{}), [{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
106 |
+
continue
|
107 |
+
|
108 |
+
elif s.group() == 'u3':
|
109 |
+
lc = lc + 1
|
110 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
111 |
+
m_r = m_r[0].split(',')
|
112 |
+
for i, m in enumerate(m_r):
|
113 |
+
if 'pi' in m and 'e' in m:
|
114 |
+
sp_str = m_r[i].split('e-')
|
115 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
116 |
+
elif 'pi' in m:
|
117 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
118 |
+
else:
|
119 |
+
m_r[i] = '0'
|
120 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
121 |
+
t_qbit = int(m_i[0].strip('[]'))
|
122 |
+
# f.write('cir.u(np.pi*{}, np.pi*{}, np.pi*{}, {})\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
123 |
+
f.write('cir.append(U3(np.pi*{}, np.pi*{}, np.pi*{}), [{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
124 |
+
continue
|
125 |
+
|
126 |
+
f.write('job = backend.run(transpile(cir, backend))\n')
|
127 |
+
f.write('result = job.result()\n')
|
128 |
+
# f.write('outputstate = result.get_statevector(cir)\n')
|
129 |
+
f.write('print(f\'backend: {result.backend_name}\')\n')
|
130 |
+
|
131 |
+
f.write('t_e = perf_counter()\n')
|
132 |
+
f.write('t_ep = process_time()\n')
|
133 |
+
|
134 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
135 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
136 |
+
f.write('print(t_e - t_s)\n')
|
137 |
+
f.write('print(t_ep - t_sp)\n')
|
138 |
+
# f.write('print(outputstate)')
|
139 |
+
|
140 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
141 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
142 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
143 |
+
print(int(lc_res) == lc)
|
144 |
+
# print(lc)
|
145 |
+
|
146 |
+
# parse_qasm_to_package_gen(12, 1, 42, "../qasm_test/qasm_rqc_test.qasm", "test_qiskit.py")
|
147 |
+
# parse_qasm_to_package_gen(12, 1, 42, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_qiskit_2.py")
|
148 |
+
|
149 |
+
|
150 |
+
task = 'hdyn'
|
151 |
+
sim_pack = 'cuquantum_qiskit'
|
152 |
+
com_cap = 'gpu1'
|
153 |
+
prec = 'sp'
|
154 |
+
|
155 |
+
for N in range(6, 40, 2):
|
156 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
157 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
158 |
+
|
159 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
160 |
+
|
161 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
162 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
163 |
+
parse_qasm_to_package_gen(N, "single", input_file, output_file, save_file_path)
|
qasm_parser/cuquantum_qiskit/qasm_qiskit_parser_ngpu_2pt0_new.py
ADDED
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
import os
|
6 |
+
|
7 |
+
def parse_qasm_to_package_gen(N, f_precision, input_filename, output_filename, save_data_path):
|
8 |
+
|
9 |
+
with open(output_filename, 'w') as f:
|
10 |
+
|
11 |
+
f.write('import os\n')
|
12 |
+
f.write('from qiskit import QuantumRegister, QuantumCircuit, transpile, ClassicalRegister\n')
|
13 |
+
f.write('from qiskit.quantum_info.operators import Operator\n')
|
14 |
+
f.write('from qiskit.providers.aer import QasmSimulator\n')
|
15 |
+
f.write('from qiskit import Aer\n')
|
16 |
+
f.write('from time import process_time, perf_counter\n')
|
17 |
+
f.write('import numpy as np\n\n')
|
18 |
+
|
19 |
+
f.write('def U2(p, l):\n')
|
20 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
21 |
+
f.write(' return Operator(mat)\n\n')
|
22 |
+
|
23 |
+
f.write('def U3(t, p, l):\n')
|
24 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
25 |
+
f.write(' return Operator(mat)\n\n')
|
26 |
+
|
27 |
+
f.write('t_sp = process_time()\n')
|
28 |
+
f.write('t_s = perf_counter()\n')
|
29 |
+
|
30 |
+
# f.write('backend = Aer.get_backend(\'statevector_simulator\')\n')#, max_parallel_threads={}, statevector_parallel_threshold={}, precision=\'{}\')\n'.format(mpt, spt, f_precision))
|
31 |
+
f.write('backend = Aer.get_backend(\'aer_simulator_statevector\')\n')
|
32 |
+
f.write('opt = backend.options\n')
|
33 |
+
f.write('opt.device=\'GPU\'\n')
|
34 |
+
f.write('opt.precision=\'{}\'\n'.format(f_precision))
|
35 |
+
|
36 |
+
f.write('cir = QuantumCircuit({})\n'.format(N))
|
37 |
+
with open(input_filename, "r") as ifile:
|
38 |
+
lines = ifile.readlines()
|
39 |
+
|
40 |
+
lc = 0
|
41 |
+
cirq_c = 0
|
42 |
+
for line in lines:
|
43 |
+
|
44 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
45 |
+
|
46 |
+
if s is None:
|
47 |
+
continue
|
48 |
+
|
49 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
50 |
+
lc = lc + 1
|
51 |
+
f.write('# {}\n'.format(s.group()))
|
52 |
+
continue
|
53 |
+
|
54 |
+
elif s.group() == 'cirq':
|
55 |
+
lc = lc + 1
|
56 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
57 |
+
cirq_c = cirq_c + 1
|
58 |
+
continue
|
59 |
+
|
60 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'sx' or s.group() == 'sxdg' or s.group() == 'h':
|
61 |
+
lc = lc + 1
|
62 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
63 |
+
t_qbit = int(m_i[0].strip('[]'))
|
64 |
+
f.write('cir.{}({})\n'.format(s.group(), t_qbit))
|
65 |
+
continue
|
66 |
+
|
67 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
68 |
+
lc = lc + 1
|
69 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
70 |
+
# print(m_r)
|
71 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
72 |
+
sp_str = m_r[0].split('e-')
|
73 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
74 |
+
elif 'pi' in m_r[0]:
|
75 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
76 |
+
# print(m_r)
|
77 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
78 |
+
t_qbit = int(m_i[0].strip('[]'))
|
79 |
+
f.write('cir.{}(np.pi*{}, {})\n'.format(s.group(), float(m_r[0][0]), t_qbit))
|
80 |
+
continue
|
81 |
+
|
82 |
+
elif s.group() == 'cx':
|
83 |
+
lc = lc + 1
|
84 |
+
match = re.findall(r'\[\d\d*\]', line)
|
85 |
+
c_qbit = int(match[0].strip('[]'))
|
86 |
+
t_qbit = int(match[1].strip('[]'))
|
87 |
+
f.write('cir.cx({}, {})\n'.format(c_qbit, t_qbit))
|
88 |
+
continue
|
89 |
+
|
90 |
+
elif s.group() == 'u2':
|
91 |
+
lc = lc + 1
|
92 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
93 |
+
m_r = m_r[0].split(',')
|
94 |
+
for i, m in enumerate(m_r):
|
95 |
+
if 'pi' in m and 'e' in m:
|
96 |
+
sp_str = m_r[i].split('e-')
|
97 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
98 |
+
elif 'pi' in m:
|
99 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
100 |
+
else:
|
101 |
+
m_r[i] = '0'
|
102 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
103 |
+
t_qbit = int(m_i[0].strip('[]'))
|
104 |
+
# f.write('cir.u(np.pi/2., np.pi*{}, np.pi*{}, {})\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
105 |
+
f.write('cir.append(U2(np.pi*{}, np.pi*{}), [{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
106 |
+
continue
|
107 |
+
|
108 |
+
elif s.group() == 'u3':
|
109 |
+
lc = lc + 1
|
110 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
111 |
+
m_r = m_r[0].split(',')
|
112 |
+
for i, m in enumerate(m_r):
|
113 |
+
if 'pi' in m and 'e' in m:
|
114 |
+
sp_str = m_r[i].split('e-')
|
115 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
116 |
+
elif 'pi' in m:
|
117 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
118 |
+
else:
|
119 |
+
m_r[i] = '0'
|
120 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
121 |
+
t_qbit = int(m_i[0].strip('[]'))
|
122 |
+
# f.write('cir.u(np.pi*{}, np.pi*{}, np.pi*{}, {})\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
123 |
+
f.write('cir.append(U3(np.pi*{}, np.pi*{}, np.pi*{}), [{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
124 |
+
continue
|
125 |
+
|
126 |
+
f.write('job = backend.run(transpile(cir, backend))\n')
|
127 |
+
f.write('result = job.result()\n')
|
128 |
+
# f.write('outputstate = result.get_statevector(cir)\n')
|
129 |
+
f.write('print(f\'backend: {result.backend_name}\')\n')
|
130 |
+
|
131 |
+
f.write('t_e = perf_counter()\n')
|
132 |
+
f.write('t_ep = process_time()\n')
|
133 |
+
|
134 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
135 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
136 |
+
f.write('print(t_e - t_s)\n')
|
137 |
+
# f.write('print(t_ep - t_sp)\n')
|
138 |
+
# f.write('print(outputstate)')
|
139 |
+
|
140 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
141 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
142 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
143 |
+
print(int(lc_res) == lc)
|
144 |
+
# print(lc)
|
145 |
+
|
146 |
+
# parse_qasm_to_package_gen(12, 1, 42, "../qasm_test/qasm_rqc_test.qasm", "test_qiskit.py")
|
147 |
+
# parse_qasm_to_package_gen(12, 1, 42, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_qiskit_2.py")
|
148 |
+
|
149 |
+
|
150 |
+
task = 'qft'
|
151 |
+
sim_pack = 'cuquantum_qiskit'
|
152 |
+
com_cap = 'gpu8'
|
153 |
+
prec = 'dp'
|
154 |
+
|
155 |
+
for N in range(6, 38, 2):
|
156 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
157 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
158 |
+
|
159 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
160 |
+
|
161 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
162 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
163 |
+
parse_qasm_to_package_gen(N, "double", input_file, output_file, save_file_path)
|
qasm_parser/cuquantum_qsimcirq/qasm_qsimcirq_gpu_parser.py
ADDED
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
# f.write('os.chdir(\'/sim_lib/qsimcirq/qsim/\')\n')
|
12 |
+
# f.write('cwd = os.getcwd()\n')
|
13 |
+
# f.write('import sys\n')
|
14 |
+
# f.write('sys.path.insert(0, \'\')\n')
|
15 |
+
|
16 |
+
f.write('from time import process_time, perf_counter\n')
|
17 |
+
f.write('import numpy as np\n\n')
|
18 |
+
f.write('import cirq\n')
|
19 |
+
f.write('import qsimcirq\n')
|
20 |
+
|
21 |
+
f.write('class U2(cirq.Gate):\n')
|
22 |
+
f.write(' def __init__(self, phi, lam):\n')
|
23 |
+
f.write(' super(U2, self)\n')
|
24 |
+
f.write(' self._p = phi\n')
|
25 |
+
f.write(' self._l = lam\n\n')
|
26 |
+
|
27 |
+
f.write(' def _num_qubits_(self):\n')
|
28 |
+
f.write(' return 1\n\n')
|
29 |
+
|
30 |
+
f.write(' def _unitary_(self):\n')
|
31 |
+
f.write(' mat = ((np.cos(0.5*(self._p + self._l)) - 1j*np.sin(0.5*(self._p + self._l)))/np.sqrt(2))*np.array([[1, -np.cos(self._l)-1j*np.sin(self._l)], [np.cos(self._p) + 1j*np.sin(self._p), np.cos(self._l + self._p) + 1j*np.sin(self._l+self._p)]])\n')
|
32 |
+
f.write(' return mat\n\n')
|
33 |
+
|
34 |
+
f.write('class U3(cirq.Gate):\n')
|
35 |
+
f.write(' def __init__(self, theta, phi, lam):\n')
|
36 |
+
f.write(' super(U3, self)\n')
|
37 |
+
f.write(' self._t = theta\n')
|
38 |
+
f.write(' self._p = phi\n')
|
39 |
+
f.write(' self._l = lam\n\n')
|
40 |
+
|
41 |
+
f.write(' def _num_qubits_(self):\n')
|
42 |
+
f.write(' return 1\n\n')
|
43 |
+
|
44 |
+
f.write(' def _unitary_(self):\n')
|
45 |
+
f.write(' mat = (np.cos(0.5*(self._p + self._l)) - 1j*np.sin(0.5*(self._p + self._l)))*np.array([[np.cos(self._t/2.), -np.sin(self._t/2.)*(np.cos(self._l) + 1j*np.sin(self._l))], [np.sin(self._t/2.)*(np.cos(self._p) + 1j*np.sin(self._p)), np.cos(self._t/2.)*(np.cos(self._l + self._p) + 1j*np.sin(self._l + self._p))]])\n')
|
46 |
+
f.write(' return mat\n\n')
|
47 |
+
|
48 |
+
f.write('t_sp = process_time()\n')
|
49 |
+
f.write('t_s = perf_counter()\n')
|
50 |
+
|
51 |
+
f.write('q = cirq.LineQubit.range({})\n'.format(N))
|
52 |
+
|
53 |
+
f.write('cir = cirq.Circuit()\n')
|
54 |
+
|
55 |
+
with open(input_filename, "r") as ifile:
|
56 |
+
lines = ifile.readlines()
|
57 |
+
|
58 |
+
lc = 0
|
59 |
+
cirq_c = 0
|
60 |
+
for line in lines:
|
61 |
+
|
62 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
63 |
+
|
64 |
+
if s is None:
|
65 |
+
continue
|
66 |
+
|
67 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
68 |
+
lc = lc + 1
|
69 |
+
f.write('# {}\n'.format(s.group()))
|
70 |
+
continue
|
71 |
+
|
72 |
+
elif s.group() == 'cirq':
|
73 |
+
lc = lc + 1
|
74 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
75 |
+
cirq_c = cirq_c + 1
|
76 |
+
continue
|
77 |
+
|
78 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'h':
|
79 |
+
lc = lc + 1
|
80 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
81 |
+
t_qbit = int(m_i[0].strip('[]'))
|
82 |
+
f.write('cir.append(cirq.{}(q[{}]))\n'.format(s.group().upper(), t_qbit))
|
83 |
+
continue
|
84 |
+
|
85 |
+
elif s.group() == 'sx':
|
86 |
+
lc = lc + 1
|
87 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
88 |
+
t_qbit = int(m_i[0].strip('[]'))
|
89 |
+
f.write('cir.append(cirq.XPowGate(exponent=0.5).on(q[{}]))\n'.format(t_qbit))
|
90 |
+
|
91 |
+
elif s.group() == 'sxdg':
|
92 |
+
lc = lc + 1
|
93 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
94 |
+
t_qbit = int(m_i[0].strip('[]'))
|
95 |
+
f.write('cir.append(cirq.XPowGate(exponent=-0.5).on(q[{}]))\n'.format(t_qbit))
|
96 |
+
|
97 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
98 |
+
lc = lc + 1
|
99 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
100 |
+
# print(m_r)
|
101 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
102 |
+
sp_str = m_r[0].split('e-')
|
103 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
104 |
+
elif 'pi' in m_r[0]:
|
105 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
106 |
+
# print(m_r)
|
107 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
108 |
+
t_qbit = int(m_i[0].strip('[]'))
|
109 |
+
f.write('cir.append(cirq.{}(np.pi*{}).on(q[{}]))\n'.format(s.group(), float(m_r[0][0]), t_qbit))
|
110 |
+
continue
|
111 |
+
|
112 |
+
elif s.group() == 'cx':
|
113 |
+
lc = lc + 1
|
114 |
+
match = re.findall(r'\[\d\d*\]', line)
|
115 |
+
c_qbit = int(match[0].strip('[]'))
|
116 |
+
t_qbit = int(match[1].strip('[]'))
|
117 |
+
# f.write('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit))
|
118 |
+
f.write('cir.append(cirq.CNOT(q[{}], q[{}]))\n'.format(c_qbit, t_qbit))
|
119 |
+
continue
|
120 |
+
|
121 |
+
elif s.group() == 'u2':
|
122 |
+
lc = lc + 1
|
123 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
124 |
+
m_r = m_r[0].split(',')
|
125 |
+
for i, m in enumerate(m_r):
|
126 |
+
if 'pi' in m and 'e' in m:
|
127 |
+
sp_str = m_r[i].split('e-')
|
128 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
129 |
+
elif 'pi' in m:
|
130 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
131 |
+
else:
|
132 |
+
m_r[i] = '0'
|
133 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
134 |
+
t_qbit = int(m_i[0].strip('[]'))
|
135 |
+
f.write('cir.append(U2(np.pi*{}, np.pi*{}).on(q[{}]))\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
136 |
+
continue
|
137 |
+
|
138 |
+
elif s.group() == 'u3':
|
139 |
+
lc = lc + 1
|
140 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
141 |
+
m_r = m_r[0].split(',')
|
142 |
+
for i, m in enumerate(m_r):
|
143 |
+
if 'pi' in m and 'e' in m:
|
144 |
+
sp_str = m_r[i].split('e-')
|
145 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
146 |
+
elif 'pi' in m:
|
147 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
148 |
+
else:
|
149 |
+
m_r[i] = '0'
|
150 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
151 |
+
t_qbit = int(m_i[0].strip('[]'))
|
152 |
+
f.write('cir.append(U3(np.pi*{}, np.pi*{}, np.pi*{}).on(q[{}]))\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
153 |
+
continue
|
154 |
+
|
155 |
+
|
156 |
+
f.write('gpu_options = qsimcirq.QSimOptions(gpu_mode=1)\n')
|
157 |
+
f.write('qsim_simulator = qsimcirq.QSimSimulator(qsim_options=gpu_options)\n')
|
158 |
+
f.write('result = qsim_simulator.simulate(cir)\n')
|
159 |
+
|
160 |
+
f.write('t_e = perf_counter()\n')
|
161 |
+
f.write('t_ep = process_time()\n')
|
162 |
+
f.write('print(t_e - t_s)\n')
|
163 |
+
f.write('print(t_ep - t_sp)\n')
|
164 |
+
# f.write('print(result.final_state_vector)\n')
|
165 |
+
|
166 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
167 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
168 |
+
|
169 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
170 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
171 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
172 |
+
print(int(lc_res) == lc)
|
173 |
+
# print(lc)
|
174 |
+
|
175 |
+
# parse_qasm_to_package_gen(12, "../qasm_test/qasm_rqc_test.qasm", "test_cirq.py")
|
176 |
+
# parse_qasm_to_package_gen(12, 1, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_qsimcirq.py")
|
177 |
+
|
178 |
+
task = 'hdyn'
|
179 |
+
sim_pack = 'cuquantum_qsimcirq'
|
180 |
+
com_cap = 'gpu1'
|
181 |
+
prec = 'sp'
|
182 |
+
|
183 |
+
for N in range(6, 40, 2):
|
184 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
185 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
186 |
+
|
187 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
188 |
+
|
189 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
190 |
+
|
191 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
192 |
+
parse_qasm_to_package_gen(N, input_file, output_file, save_file_path)
|
193 |
+
|
194 |
+
r"""
|
195 |
+
for N in range(12, 42, 2):
|
196 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
197 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
198 |
+
|
199 |
+
output_file_path = '/data/user/gangap_a/rqc/qsimcirq/qsimcirq_gpu_run_files/'
|
200 |
+
output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N)
|
201 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
202 |
+
|
203 |
+
for N in range(41, 51):
|
204 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
205 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
206 |
+
|
207 |
+
output_file_path = '/data/user/gangap_a/rqc/qsimcirq/qsimcirq_gpu_run_files/'
|
208 |
+
output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N)
|
209 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
210 |
+
"""
|
qasm_parser/cuquantum_qsimcirq/qasm_qsimcirq_ngpu_parser.py
ADDED
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, input_filename, output_filename, save_data_path, cc):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
# f.write('os.chdir(\'/sim_lib/qsimcirq/qsim/\')\n')
|
12 |
+
# f.write('cwd = os.getcwd()\n')
|
13 |
+
# f.write('import sys\n')
|
14 |
+
# f.write('sys.path.insert(0, \'\')\n')
|
15 |
+
|
16 |
+
f.write('from time import process_time, perf_counter\n')
|
17 |
+
f.write('import numpy as np\n\n')
|
18 |
+
f.write('import cirq\n')
|
19 |
+
f.write('import qsimcirq\n')
|
20 |
+
|
21 |
+
f.write('class U2(cirq.Gate):\n')
|
22 |
+
f.write(' def __init__(self, phi, lam):\n')
|
23 |
+
f.write(' super(U2, self)\n')
|
24 |
+
f.write(' self._p = phi\n')
|
25 |
+
f.write(' self._l = lam\n\n')
|
26 |
+
|
27 |
+
f.write(' def _num_qubits_(self):\n')
|
28 |
+
f.write(' return 1\n\n')
|
29 |
+
|
30 |
+
f.write(' def _unitary_(self):\n')
|
31 |
+
f.write(' mat = ((np.cos(0.5*(self._p + self._l)) - 1j*np.sin(0.5*(self._p + self._l)))/np.sqrt(2))*np.array([[1, -np.cos(self._l)-1j*np.sin(self._l)], [np.cos(self._p) + 1j*np.sin(self._p), np.cos(self._l + self._p) + 1j*np.sin(self._l+self._p)]])\n')
|
32 |
+
f.write(' return mat\n\n')
|
33 |
+
|
34 |
+
f.write('class U3(cirq.Gate):\n')
|
35 |
+
f.write(' def __init__(self, theta, phi, lam):\n')
|
36 |
+
f.write(' super(U3, self)\n')
|
37 |
+
f.write(' self._t = theta\n')
|
38 |
+
f.write(' self._p = phi\n')
|
39 |
+
f.write(' self._l = lam\n\n')
|
40 |
+
|
41 |
+
f.write(' def _num_qubits_(self):\n')
|
42 |
+
f.write(' return 1\n\n')
|
43 |
+
|
44 |
+
f.write(' def _unitary_(self):\n')
|
45 |
+
f.write(' mat = (np.cos(0.5*(self._p + self._l)) - 1j*np.sin(0.5*(self._p + self._l)))*np.array([[np.cos(self._t/2.), -np.sin(self._t/2.)*(np.cos(self._l) + 1j*np.sin(self._l))], [np.sin(self._t/2.)*(np.cos(self._p) + 1j*np.sin(self._p)), np.cos(self._t/2.)*(np.cos(self._l + self._p) + 1j*np.sin(self._l + self._p))]])\n')
|
46 |
+
f.write(' return mat\n\n')
|
47 |
+
|
48 |
+
f.write('t_sp = process_time()\n')
|
49 |
+
f.write('t_s = perf_counter()\n')
|
50 |
+
|
51 |
+
f.write('q = cirq.LineQubit.range({})\n'.format(N))
|
52 |
+
|
53 |
+
f.write('cir = cirq.Circuit()\n')
|
54 |
+
|
55 |
+
with open(input_filename, "r") as ifile:
|
56 |
+
lines = ifile.readlines()
|
57 |
+
|
58 |
+
lc = 0
|
59 |
+
cirq_c = 0
|
60 |
+
for line in lines:
|
61 |
+
|
62 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
63 |
+
|
64 |
+
if s is None:
|
65 |
+
continue
|
66 |
+
|
67 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
68 |
+
lc = lc + 1
|
69 |
+
f.write('# {}\n'.format(s.group()))
|
70 |
+
continue
|
71 |
+
|
72 |
+
elif s.group() == 'cirq':
|
73 |
+
lc = lc + 1
|
74 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
75 |
+
cirq_c = cirq_c + 1
|
76 |
+
continue
|
77 |
+
|
78 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'h':
|
79 |
+
lc = lc + 1
|
80 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
81 |
+
t_qbit = int(m_i[0].strip('[]'))
|
82 |
+
f.write('cir.append(cirq.{}(q[{}]))\n'.format(s.group().upper(), t_qbit))
|
83 |
+
continue
|
84 |
+
|
85 |
+
elif s.group() == 'sx':
|
86 |
+
lc = lc + 1
|
87 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
88 |
+
t_qbit = int(m_i[0].strip('[]'))
|
89 |
+
f.write('cir.append(cirq.XPowGate(exponent=0.5).on(q[{}]))\n'.format(t_qbit))
|
90 |
+
|
91 |
+
elif s.group() == 'sxdg':
|
92 |
+
lc = lc + 1
|
93 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
94 |
+
t_qbit = int(m_i[0].strip('[]'))
|
95 |
+
f.write('cir.append(cirq.XPowGate(exponent=-0.5).on(q[{}]))\n'.format(t_qbit))
|
96 |
+
|
97 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
98 |
+
lc = lc + 1
|
99 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
100 |
+
# print(m_r)
|
101 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
102 |
+
sp_str = m_r[0].split('e-')
|
103 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
104 |
+
elif 'pi' in m_r[0]:
|
105 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
106 |
+
# print(m_r)
|
107 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
108 |
+
t_qbit = int(m_i[0].strip('[]'))
|
109 |
+
f.write('cir.append(cirq.{}(np.pi*{}).on(q[{}]))\n'.format(s.group(), float(m_r[0][0]), t_qbit))
|
110 |
+
continue
|
111 |
+
|
112 |
+
elif s.group() == 'cx':
|
113 |
+
lc = lc + 1
|
114 |
+
match = re.findall(r'\[\d\d*\]', line)
|
115 |
+
c_qbit = int(match[0].strip('[]'))
|
116 |
+
t_qbit = int(match[1].strip('[]'))
|
117 |
+
# f.write('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit))
|
118 |
+
f.write('cir.append(cirq.CNOT(q[{}], q[{}]))\n'.format(c_qbit, t_qbit))
|
119 |
+
continue
|
120 |
+
|
121 |
+
elif s.group() == 'u2':
|
122 |
+
lc = lc + 1
|
123 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
124 |
+
m_r = m_r[0].split(',')
|
125 |
+
for i, m in enumerate(m_r):
|
126 |
+
if 'pi' in m and 'e' in m:
|
127 |
+
sp_str = m_r[i].split('e-')
|
128 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
129 |
+
elif 'pi' in m:
|
130 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
131 |
+
else:
|
132 |
+
m_r[i] = '0'
|
133 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
134 |
+
t_qbit = int(m_i[0].strip('[]'))
|
135 |
+
f.write('cir.append(U2(np.pi*{}, np.pi*{}).on(q[{}]))\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
136 |
+
continue
|
137 |
+
|
138 |
+
elif s.group() == 'u3':
|
139 |
+
lc = lc + 1
|
140 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
141 |
+
m_r = m_r[0].split(',')
|
142 |
+
for i, m in enumerate(m_r):
|
143 |
+
if 'pi' in m and 'e' in m:
|
144 |
+
sp_str = m_r[i].split('e-')
|
145 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
146 |
+
elif 'pi' in m:
|
147 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
148 |
+
else:
|
149 |
+
m_r[i] = '0'
|
150 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
151 |
+
t_qbit = int(m_i[0].strip('[]'))
|
152 |
+
f.write('cir.append(U3(np.pi*{}, np.pi*{}, np.pi*{}).on(q[{}]))\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
153 |
+
continue
|
154 |
+
|
155 |
+
|
156 |
+
f.write('gpu_options = qsimcirq.QSimOptions(gpu_mode={})\n'.format(int(cc[-1])))
|
157 |
+
f.write('qsim_simulator = qsimcirq.QSimSimulator(qsim_options=gpu_options)\n')
|
158 |
+
f.write('result = qsim_simulator.simulate(cir)\n')
|
159 |
+
|
160 |
+
f.write('t_e = perf_counter()\n')
|
161 |
+
f.write('t_ep = process_time()\n')
|
162 |
+
f.write('print(t_e - t_s)\n')
|
163 |
+
f.write('print(t_ep - t_sp)\n')
|
164 |
+
# f.write('print(result.final_state_vector)\n')
|
165 |
+
|
166 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
167 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
168 |
+
|
169 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
170 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
171 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
172 |
+
print(int(lc_res) == lc)
|
173 |
+
# print(lc)
|
174 |
+
|
175 |
+
# parse_qasm_to_package_gen(12, "../qasm_test/qasm_rqc_test.qasm", "test_cirq.py")
|
176 |
+
# parse_qasm_to_package_gen(12, 1, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_qsimcirq.py")
|
177 |
+
|
178 |
+
task = 'qft'
|
179 |
+
sim_pack = 'cuquantum_qsimcirq'
|
180 |
+
com_cap = 'gpu8'
|
181 |
+
prec = 'sp'
|
182 |
+
|
183 |
+
for N in range(6, 38, 2):
|
184 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
185 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
186 |
+
|
187 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
188 |
+
|
189 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
190 |
+
|
191 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
192 |
+
parse_qasm_to_package_gen(N, input_file, output_file, save_file_path, com_cap)
|
193 |
+
|
194 |
+
r"""
|
195 |
+
for N in range(12, 42, 2):
|
196 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
197 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
198 |
+
|
199 |
+
output_file_path = '/data/user/gangap_a/rqc/qsimcirq/qsimcirq_gpu_run_files/'
|
200 |
+
output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N)
|
201 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
202 |
+
|
203 |
+
for N in range(41, 51):
|
204 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
205 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
206 |
+
|
207 |
+
output_file_path = '/data/user/gangap_a/rqc/qsimcirq/qsimcirq_gpu_run_files/'
|
208 |
+
output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N)
|
209 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
210 |
+
"""
|
qasm_parser/hiq/qasm_hiq_parser.py
ADDED
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
f.write('from time import process_time, perf_counter\n')
|
12 |
+
f.write('from projectq.backends import Simulator\n')
|
13 |
+
f.write('from projectq import MainEngine\n')
|
14 |
+
f.write('from projectq.meta import Compute, Control, Loop, Uncompute\n')
|
15 |
+
f.write('from projectq.ops import All, H, Measure, X, Z, Rx, Rz, Ry, CNOT, SGate, SqrtXGate, QubitOperator, MatrixGate, get_inverse\n')
|
16 |
+
f.write('import numpy as np\n\n')
|
17 |
+
|
18 |
+
f.write('from hiq.projectq.cengines import GreedyScheduler, HiQMainEngine\n')
|
19 |
+
f.write('from hiq.projectq.backends import SimulatorMPI\n')
|
20 |
+
f.write('import projectq.setups.decompositions\n')
|
21 |
+
|
22 |
+
f.write('def U2(p, l):\n')
|
23 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
24 |
+
f.write(' return MatrixGate(mat)\n\n')
|
25 |
+
|
26 |
+
f.write('def U3(t, p, l):\n')
|
27 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
28 |
+
f.write(' return MatrixGate(mat)\n\n')
|
29 |
+
|
30 |
+
f.write('t_sp = process_time()\n')
|
31 |
+
f.write('t_s = perf_counter()\n')
|
32 |
+
|
33 |
+
f.write('backend = SimulatorMPI(num_local_qubits={})\n'.format(N))
|
34 |
+
f.write('eng = HiQMainEngine(backend, [GreedyScheduler()])\n')
|
35 |
+
f.write('qureg = eng.allocate_qureg({})\n'.format(N))
|
36 |
+
|
37 |
+
with open(input_filename, "r") as ifile:
|
38 |
+
lines = ifile.readlines()
|
39 |
+
|
40 |
+
lc = 0
|
41 |
+
for line in lines:
|
42 |
+
|
43 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
44 |
+
|
45 |
+
if s is None:
|
46 |
+
continue
|
47 |
+
|
48 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits', 'cirq']:
|
49 |
+
lc = lc + 1
|
50 |
+
f.write('# {}\n'.format(s.group()))
|
51 |
+
continue
|
52 |
+
|
53 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 'h':
|
54 |
+
lc = lc + 1
|
55 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
56 |
+
t_qbit = int(m_i[0].strip('[]'))
|
57 |
+
f.write('{} | qureg[{}]\n'.format(s.group().upper(), t_qbit))
|
58 |
+
continue
|
59 |
+
|
60 |
+
elif s.group() == 'sx':
|
61 |
+
lc = lc + 1
|
62 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
63 |
+
t_qbit = int(m_i[0].strip('[]'))
|
64 |
+
f.write('SqrtXGate() | qureg[{}]\n'.format(t_qbit))
|
65 |
+
|
66 |
+
elif s.group() == 'sxdg':
|
67 |
+
lc = lc + 1
|
68 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
69 |
+
t_qbit = int(m_i[0].strip('[]'))
|
70 |
+
f.write('get_inverse(SqrtXGate()) | qureg[{}]\n'.format(t_qbit))
|
71 |
+
|
72 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
73 |
+
lc = lc + 1
|
74 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
75 |
+
# print(m_r)
|
76 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
77 |
+
sp_str = m_r[0].split('e-')
|
78 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
79 |
+
elif 'pi' in m_r[0]:
|
80 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
81 |
+
# print(m_r)
|
82 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
83 |
+
t_qbit = int(m_i[0].strip('[]'))
|
84 |
+
f.write('R{}(np.pi*{}) | qureg[{}]\n'.format(s.group()[1], float(m_r[0][0]), t_qbit))
|
85 |
+
continue
|
86 |
+
|
87 |
+
elif s.group() == "s":
|
88 |
+
lc = lc + 1
|
89 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
90 |
+
t_qbit = int(m_i[0].strip('[]'))
|
91 |
+
f.write('SGate() | qureg[{}]\n'.format(t_qbit))
|
92 |
+
continue
|
93 |
+
|
94 |
+
elif s.group() == 'cx':
|
95 |
+
lc = lc + 1
|
96 |
+
match = re.findall(r'\[\d\d*\]', line)
|
97 |
+
c_qbit = int(match[0].strip('[]'))
|
98 |
+
t_qbit = int(match[1].strip('[]'))
|
99 |
+
f.write('CNOT | (qureg[{}], qureg[{}])\n'.format(c_qbit, t_qbit))
|
100 |
+
continue
|
101 |
+
|
102 |
+
elif s.group() == 'u2':
|
103 |
+
lc = lc + 1
|
104 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
105 |
+
m_r = m_r[0].split(',')
|
106 |
+
for i, m in enumerate(m_r):
|
107 |
+
if 'pi' in m and 'e' in m:
|
108 |
+
sp_str = m_r[i].split('e-')
|
109 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
110 |
+
elif 'pi' in m:
|
111 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
112 |
+
else:
|
113 |
+
m_r[i] = '0'
|
114 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
115 |
+
t_qbit = int(m_i[0].strip('[]'))
|
116 |
+
f.write('U2(np.pi*{}, np.pi*{}) | qureg[{}]\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
117 |
+
continue
|
118 |
+
|
119 |
+
elif s.group() == 'u3':
|
120 |
+
lc = lc + 1
|
121 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
122 |
+
m_r = m_r[0].split(',')
|
123 |
+
for i, m in enumerate(m_r):
|
124 |
+
if 'pi' in m and 'e' in m:
|
125 |
+
sp_str = m_r[i].split('e-')
|
126 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
127 |
+
elif 'pi' in m:
|
128 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
129 |
+
else:
|
130 |
+
m_r[i] = '0'
|
131 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
132 |
+
t_qbit = int(m_i[0].strip('[]'))
|
133 |
+
f.write('U3(np.pi*{}, np.pi*{}, np.pi*{}) | qureg[{}]\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
134 |
+
continue
|
135 |
+
|
136 |
+
f.write('eng.flush()\n')
|
137 |
+
f.write('All(Measure) | qureg\n')
|
138 |
+
|
139 |
+
f.write('t_e = perf_counter()\n')
|
140 |
+
f.write('t_ep = process_time()\n')
|
141 |
+
f.write('print(t_e - t_s)\n')
|
142 |
+
f.write('print(t_ep - t_sp)\n')
|
143 |
+
|
144 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
145 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
146 |
+
|
147 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
148 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
149 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
150 |
+
print(int(lc_res) == lc)
|
151 |
+
# print(lc)
|
152 |
+
|
153 |
+
# parse_qasm_to_package_gen(12, "qasm_ex.qasm", "n12_projectq.py")
|
154 |
+
|
155 |
+
task = 'qft'
|
156 |
+
sim_pack = 'hiq'
|
157 |
+
com_cap = 'mt'
|
158 |
+
prec = 'dp'
|
159 |
+
|
160 |
+
for N in range(6, 40, 2):
|
161 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
162 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
163 |
+
|
164 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
165 |
+
|
166 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
167 |
+
|
168 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
169 |
+
parse_qasm_to_package_gen(N, input_file, output_file, save_file_path)
|
170 |
+
|
171 |
+
r"""
|
172 |
+
for N in range(12, 42, 2):
|
173 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
174 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
175 |
+
|
176 |
+
output_file_path = '/data/user/gangap_a/rqc/hiq_sim/hiq_run_files/'
|
177 |
+
output_file = output_file_path + 'hiq_rqc_n{}.py'.format(N)
|
178 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
179 |
+
|
180 |
+
for N in range(41, 51):
|
181 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
182 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
183 |
+
|
184 |
+
output_file_path = '/data/user/gangap_a/rqc/hiq_sim/hiq_run_files/'
|
185 |
+
output_file = output_file_path + 'hiq_rqc_n{}.py'.format(N)
|
186 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
187 |
+
|
188 |
+
"""
|
qasm_parser/hybridq/qasm_hybridq_gpu_parser.py
ADDED
@@ -0,0 +1,197 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
f.write('from time import process_time, perf_counter\n')
|
12 |
+
f.write('from hybridq.gate import Gate\n')
|
13 |
+
f.write('from hybridq.circuit import Circuit\n')
|
14 |
+
f.write('from hybridq.gate import MatrixGate\n')
|
15 |
+
f.write('from hybridq.circuit.simulation import simulate\n')
|
16 |
+
f.write('import numpy as np\n')
|
17 |
+
|
18 |
+
f.write('def SqrtXdg(q0):\n')
|
19 |
+
f.write(' mat = [[(1-1j)/2., (1+1j)/2.], [(1+1j)/2., (1-1j)/2.]]\n')
|
20 |
+
f.write(' return MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n')
|
21 |
+
|
22 |
+
f.write('def S(q0):\n')
|
23 |
+
f.write(' mat = [[1, 0], [0, 1j]]\n')
|
24 |
+
f.write(' return MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n')
|
25 |
+
|
26 |
+
f.write('def U2(p, l, q0):\n')
|
27 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
28 |
+
f.write(' return MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n')
|
29 |
+
|
30 |
+
f.write('def U3(t, p, l, q0):\n')
|
31 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
32 |
+
f.write(' return MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n')
|
33 |
+
|
34 |
+
f.write('t_sp = process_time()\n')
|
35 |
+
f.write('t_s = perf_counter()\n')
|
36 |
+
|
37 |
+
f.write('init_state = \'0\'*{}\n'.format(N))
|
38 |
+
|
39 |
+
f.write('cir_arr = []\n')
|
40 |
+
|
41 |
+
with open(input_filename, "r") as ifile:
|
42 |
+
lines = ifile.readlines()
|
43 |
+
|
44 |
+
lc = 0
|
45 |
+
cirq_c = 0
|
46 |
+
for line in lines:
|
47 |
+
|
48 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
49 |
+
|
50 |
+
if s is None:
|
51 |
+
continue
|
52 |
+
|
53 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
54 |
+
lc = lc + 1
|
55 |
+
f.write('# {}\n'.format(s.group()))
|
56 |
+
continue
|
57 |
+
|
58 |
+
elif s.group() == 'cirq':
|
59 |
+
lc = lc + 1
|
60 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
61 |
+
cirq_c = cirq_c + 1
|
62 |
+
continue
|
63 |
+
|
64 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 'h':
|
65 |
+
lc = lc + 1
|
66 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
67 |
+
t_qbit = int(m_i[0].strip('[]'))
|
68 |
+
f.write('cir_arr.append(Gate(\'{}\', qubits=[{}]))\n'.format(s.group().upper(), t_qbit))
|
69 |
+
continue
|
70 |
+
|
71 |
+
elif s.group() == 'sx':
|
72 |
+
lc = lc + 1
|
73 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
74 |
+
t_qbit = int(m_i[0].strip('[]'))
|
75 |
+
f.write('cir_arr.append(Gate(\'SQRT_X\', qubits=[{}]))\n'.format(t_qbit))
|
76 |
+
|
77 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
78 |
+
lc = lc + 1
|
79 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
80 |
+
# print(m_r)
|
81 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
82 |
+
sp_str = m_r[0].split('e-')
|
83 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
84 |
+
elif 'pi' in m_r[0]:
|
85 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
86 |
+
# print(m_r)
|
87 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
88 |
+
t_qbit = int(m_i[0].strip('[]'))
|
89 |
+
f.write('cir_arr.append(Gate(\'{}\', qubits=[{}], params=[np.pi*{}]))\n'.format(s.group().upper(), t_qbit, float(m_r[0][0])))
|
90 |
+
continue
|
91 |
+
|
92 |
+
elif s.group() == 'sxdg':
|
93 |
+
lc = lc + 1
|
94 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
95 |
+
t_qbit = int(m_i[0].strip('[]'))
|
96 |
+
f.write('cir_arr.append(SqrtXdg({}))\n'.format(t_qbit))
|
97 |
+
continue
|
98 |
+
|
99 |
+
elif s.group() == "s":
|
100 |
+
lc = lc + 1
|
101 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
102 |
+
t_qbit = int(m_i[0].strip('[]'))
|
103 |
+
f.write('cir_arr.append(S({}))\n'.format(t_qbit))
|
104 |
+
continue
|
105 |
+
|
106 |
+
elif s.group() == 'cx':
|
107 |
+
lc = lc + 1
|
108 |
+
match = re.findall(r'\[\d\d*\]', line)
|
109 |
+
c_qbit = int(match[0].strip('[]'))
|
110 |
+
t_qbit = int(match[1].strip('[]'))
|
111 |
+
f.write('cir_arr.append(Gate(\'CX\', qubits=[{}, {}]))\n'.format(c_qbit, t_qbit))
|
112 |
+
continue
|
113 |
+
|
114 |
+
elif s.group() == 'u2':
|
115 |
+
lc = lc + 1
|
116 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
117 |
+
m_r = m_r[0].split(',')
|
118 |
+
for i, m in enumerate(m_r):
|
119 |
+
if 'pi' in m and 'e' in m:
|
120 |
+
sp_str = m_r[i].split('e-')
|
121 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
122 |
+
elif 'pi' in m:
|
123 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
124 |
+
else:
|
125 |
+
m_r[i] = '0'
|
126 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
127 |
+
t_qbit = int(m_i[0].strip('[]'))
|
128 |
+
f.write('cir_arr.append(U2(np.pi*{}, np.pi*{}, {}))\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
129 |
+
continue
|
130 |
+
|
131 |
+
elif s.group() == 'u3':
|
132 |
+
lc = lc + 1
|
133 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
134 |
+
m_r = m_r[0].split(',')
|
135 |
+
for i, m in enumerate(m_r):
|
136 |
+
if 'pi' in m and 'e' in m:
|
137 |
+
sp_str = m_r[i].split('e-')
|
138 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
139 |
+
elif 'pi' in m:
|
140 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
141 |
+
else:
|
142 |
+
m_r[i] = '0'
|
143 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
144 |
+
t_qbit = int(m_i[0].strip('[]'))
|
145 |
+
f.write('cir_arr.append(U3(np.pi*{}, np.pi*{}, np.pi*{}, {}))\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
146 |
+
continue
|
147 |
+
|
148 |
+
# f.write('psi = simulate(cir_arr, initial_state=init_state, simplify=False, optimize=\'evolution-einsum\', backend=\'jax\', max_largest_intermediate=2**40)\n')
|
149 |
+
f.write('psi = simulate(cir_arr, initial_state=init_state, optimize=\'evolution-einsum\', backend=\'jax\', max_largest_intermediate=2**40)\n')
|
150 |
+
f.write('t_e = perf_counter()\n')
|
151 |
+
f.write('t_ep = process_time()\n')
|
152 |
+
f.write('print(t_e - t_s)\n')
|
153 |
+
f.write('print(t_ep - t_sp)\n')
|
154 |
+
|
155 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
156 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
157 |
+
|
158 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
159 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
160 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
161 |
+
print(int(lc_res) == lc)
|
162 |
+
# print(lc)
|
163 |
+
|
164 |
+
task = 'rqc'
|
165 |
+
sim_pack = 'hybridq'
|
166 |
+
com_cap = 'gpu'
|
167 |
+
prec = 'sp'
|
168 |
+
|
169 |
+
for N in range(12, 36, 2):
|
170 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
171 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
172 |
+
|
173 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
174 |
+
|
175 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
176 |
+
|
177 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
178 |
+
parse_qasm_to_package_gen(N, input_file, output_file, save_file_path)
|
179 |
+
|
180 |
+
r"""
|
181 |
+
for N in range(12, 42, 2):
|
182 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
183 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
184 |
+
|
185 |
+
output_file_path = '/data/user/gangap_a/rqc/hybridq/hybridq_gpu_run_files/'
|
186 |
+
output_file = output_file_path + 'hybridq_rqc_n{}.py'.format(N)
|
187 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
188 |
+
|
189 |
+
for N in range(41, 51):
|
190 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
191 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
192 |
+
|
193 |
+
output_file_path = '/data/user/gangap_a/rqc/hybridq/hybridq_gpu_run_files/'
|
194 |
+
output_file = output_file_path + 'hybridq_rqc_n{}.py'.format(N)
|
195 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
196 |
+
|
197 |
+
"""
|
qasm_parser/hybridq/qasm_hybridq_mt_parser.py
ADDED
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
f.write('from time import process_time, perf_counter\n')
|
12 |
+
f.write('from hybridq.gate import Gate\n')
|
13 |
+
f.write('from hybridq.circuit import Circuit\n')
|
14 |
+
f.write('from hybridq.gate import MatrixGate\n')
|
15 |
+
f.write('from hybridq.circuit.simulation import simulate\n')
|
16 |
+
f.write('import numpy as np\n')
|
17 |
+
|
18 |
+
f.write('def SqrtXdg(q0):\n')
|
19 |
+
f.write(' mat = [[(1-1j)/2., (1+1j)/2.], [(1+1j)/2., (1-1j)/2.]]\n')
|
20 |
+
f.write(' return MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n')
|
21 |
+
|
22 |
+
f.write('def S(q0):\n')
|
23 |
+
f.write(' mat = [[1, 0], [0, 1j]]\n')
|
24 |
+
f.write(' return MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n')
|
25 |
+
|
26 |
+
f.write('def U2(p, l, q0):\n')
|
27 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
28 |
+
f.write(' return MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n')
|
29 |
+
|
30 |
+
f.write('def U3(t, p, l, q0):\n')
|
31 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
32 |
+
f.write(' return MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n')
|
33 |
+
|
34 |
+
f.write('t_sp = process_time()\n')
|
35 |
+
f.write('t_s = perf_counter()\n')
|
36 |
+
|
37 |
+
f.write('init_state = \'0\'*{}\n'.format(N))
|
38 |
+
|
39 |
+
f.write('cir_arr = []\n')
|
40 |
+
|
41 |
+
with open(input_filename, "r") as ifile:
|
42 |
+
lines = ifile.readlines()
|
43 |
+
|
44 |
+
lc = 0
|
45 |
+
cirq_c = 0
|
46 |
+
for line in lines:
|
47 |
+
|
48 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
49 |
+
|
50 |
+
if s is None:
|
51 |
+
continue
|
52 |
+
|
53 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
54 |
+
lc = lc + 1
|
55 |
+
f.write('# {}\n'.format(s.group()))
|
56 |
+
continue
|
57 |
+
|
58 |
+
elif s.group() == 'cirq':
|
59 |
+
lc = lc + 1
|
60 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
61 |
+
cirq_c = cirq_c + 1
|
62 |
+
continue
|
63 |
+
|
64 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z':
|
65 |
+
lc = lc + 1
|
66 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
67 |
+
t_qbit = int(m_i[0].strip('[]'))
|
68 |
+
f.write('cir_arr.append(Gate(\'{}\', qubits=[{}]))\n'.format(s.group().upper(), t_qbit))
|
69 |
+
continue
|
70 |
+
|
71 |
+
elif s.group() == 'sx':
|
72 |
+
lc = lc + 1
|
73 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
74 |
+
t_qbit = int(m_i[0].strip('[]'))
|
75 |
+
f.write('cir_arr.append(Gate(\'SQRT_X\', qubits=[{}]))\n'.format(t_qbit))
|
76 |
+
|
77 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
78 |
+
lc = lc + 1
|
79 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
80 |
+
# print(m_r)
|
81 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
82 |
+
sp_str = m_r[0].split('e-')
|
83 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
84 |
+
elif 'pi' in m_r[0]:
|
85 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
86 |
+
# print(m_r)
|
87 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
88 |
+
t_qbit = int(m_i[0].strip('[]'))
|
89 |
+
f.write('cir_arr.append(Gate(\'{}\', qubits=[{}], params=[np.pi*{}]))\n'.format(s.group().upper(), t_qbit, float(m_r[0][0])))
|
90 |
+
continue
|
91 |
+
|
92 |
+
elif s.group() == 'sxdg':
|
93 |
+
lc = lc + 1
|
94 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
95 |
+
t_qbit = int(m_i[0].strip('[]'))
|
96 |
+
f.write('cir_arr.append(SqrtXdg({}))\n'.format(t_qbit))
|
97 |
+
continue
|
98 |
+
|
99 |
+
elif s.group() == "s":
|
100 |
+
lc = lc + 1
|
101 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
102 |
+
t_qbit = int(m_i[0].strip('[]'))
|
103 |
+
f.write('cir_arr.append(S({}))\n'.format(t_qbit))
|
104 |
+
continue
|
105 |
+
|
106 |
+
elif s.group() == 'cx':
|
107 |
+
lc = lc + 1
|
108 |
+
match = re.findall(r'\[\d\d*\]', line)
|
109 |
+
c_qbit = int(match[0].strip('[]'))
|
110 |
+
t_qbit = int(match[1].strip('[]'))
|
111 |
+
f.write('cir_arr.append(Gate(\'CX\', qubits=[{}, {}]))\n'.format(c_qbit, t_qbit))
|
112 |
+
continue
|
113 |
+
|
114 |
+
elif s.group() == 'u2':
|
115 |
+
lc = lc + 1
|
116 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
117 |
+
m_r = m_r[0].split(',')
|
118 |
+
for i, m in enumerate(m_r):
|
119 |
+
if 'pi' in m and 'e' in m:
|
120 |
+
sp_str = m_r[i].split('e-')
|
121 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
122 |
+
elif 'pi' in m:
|
123 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
124 |
+
else:
|
125 |
+
m_r[i] = '0'
|
126 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
127 |
+
t_qbit = int(m_i[0].strip('[]'))
|
128 |
+
f.write('cir_arr.append(U2(np.pi*{}, np.pi*{}, {}))\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
129 |
+
continue
|
130 |
+
|
131 |
+
elif s.group() == 'u3':
|
132 |
+
lc = lc + 1
|
133 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
134 |
+
m_r = m_r[0].split(',')
|
135 |
+
for i, m in enumerate(m_r):
|
136 |
+
if 'pi' in m and 'e' in m:
|
137 |
+
sp_str = m_r[i].split('e-')
|
138 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
139 |
+
elif 'pi' in m:
|
140 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
141 |
+
else:
|
142 |
+
m_r[i] = '0'
|
143 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
144 |
+
t_qbit = int(m_i[0].strip('[]'))
|
145 |
+
f.write('cir_arr.append(U3(np.pi*{}, np.pi*{}, np.pi*{}, {}))\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
146 |
+
continue
|
147 |
+
|
148 |
+
f.write('psi = simulate(cir_arr, initial_state=init_state, simplify=False, optimize=\'evolution-hybridq\', parallel=84, max_largest_intermediate=2**40)\n')
|
149 |
+
f.write('t_e = perf_counter()\n')
|
150 |
+
f.write('t_ep = process_time()\n')
|
151 |
+
f.write('print(t_e - t_s)\n')
|
152 |
+
f.write('print(t_ep - t_sp)\n')
|
153 |
+
|
154 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
155 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
156 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
157 |
+
print(int(lc_res) == lc)
|
158 |
+
# print(lc)
|
159 |
+
|
160 |
+
for N in range(12, 42, 2):
|
161 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
162 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
163 |
+
|
164 |
+
output_file_path = '/data/user/gangap_a/rqc/hybridq/hybridq_mt_run_files/'
|
165 |
+
output_file = output_file_path + 'hybridq_rqc_n{}.py'.format(N)
|
166 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
167 |
+
|
168 |
+
for N in range(41, 51):
|
169 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
170 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
171 |
+
|
172 |
+
output_file_path = '/data/user/gangap_a/rqc/hybridq/hybridq_mt_run_files/'
|
173 |
+
output_file = output_file_path + 'hybridq_rqc_n{}.py'.format(N)
|
174 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
175 |
+
|
qasm_parser/hybridq/qasm_hybridq_parser.py
ADDED
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, n_threads, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
f.write('from time import process_time, perf_counter\n')
|
12 |
+
f.write('from hybridq.gate import Gate\n')
|
13 |
+
f.write('from hybridq.circuit import Circuit\n')
|
14 |
+
f.write('from hybridq.gate import MatrixGate\n')
|
15 |
+
f.write('from hybridq.circuit.simulation import simulate\n')
|
16 |
+
f.write('import numpy as np\n')
|
17 |
+
|
18 |
+
f.write('def SqrtXdg(q0):\n')
|
19 |
+
f.write(' mat = [[(1-1j)/2., (1+1j)/2.], [(1+1j)/2., (1-1j)/2.]]\n')
|
20 |
+
f.write(' return MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n')
|
21 |
+
|
22 |
+
f.write('def S(q0):\n')
|
23 |
+
f.write(' mat = [[1, 0], [0, 1j]]\n')
|
24 |
+
f.write(' return MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n')
|
25 |
+
|
26 |
+
f.write('def U2(p, l, q0):\n')
|
27 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
28 |
+
f.write(' return MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n')
|
29 |
+
|
30 |
+
f.write('def U3(t, p, l, q0):\n')
|
31 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
32 |
+
f.write(' return MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n')
|
33 |
+
|
34 |
+
f.write('t_sp = process_time()\n')
|
35 |
+
f.write('t_s = perf_counter()\n')
|
36 |
+
|
37 |
+
f.write('init_state = \'0\'*{}\n'.format(N))
|
38 |
+
|
39 |
+
f.write('cir_arr = []\n')
|
40 |
+
|
41 |
+
with open(input_filename, "r") as ifile:
|
42 |
+
lines = ifile.readlines()
|
43 |
+
|
44 |
+
lc = 0
|
45 |
+
cirq_c = 0
|
46 |
+
for line in lines:
|
47 |
+
|
48 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
49 |
+
|
50 |
+
if s is None:
|
51 |
+
continue
|
52 |
+
|
53 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
54 |
+
lc = lc + 1
|
55 |
+
f.write('# {}\n'.format(s.group()))
|
56 |
+
continue
|
57 |
+
|
58 |
+
elif s.group() == 'cirq':
|
59 |
+
lc = lc + 1
|
60 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
61 |
+
cirq_c = cirq_c + 1
|
62 |
+
continue
|
63 |
+
|
64 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 'h':
|
65 |
+
lc = lc + 1
|
66 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
67 |
+
t_qbit = int(m_i[0].strip('[]'))
|
68 |
+
f.write('cir_arr.append(Gate(\'{}\', qubits=[{}]))\n'.format(s.group().upper(), t_qbit))
|
69 |
+
continue
|
70 |
+
|
71 |
+
elif s.group() == 'sx':
|
72 |
+
lc = lc + 1
|
73 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
74 |
+
t_qbit = int(m_i[0].strip('[]'))
|
75 |
+
f.write('cir_arr.append(Gate(\'SQRT_X\', qubits=[{}]))\n'.format(t_qbit))
|
76 |
+
|
77 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
78 |
+
lc = lc + 1
|
79 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
80 |
+
# print(m_r)
|
81 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
82 |
+
sp_str = m_r[0].split('e-')
|
83 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
84 |
+
elif 'pi' in m_r[0]:
|
85 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
86 |
+
# print(m_r)
|
87 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
88 |
+
t_qbit = int(m_i[0].strip('[]'))
|
89 |
+
f.write('cir_arr.append(Gate(\'{}\', qubits=[{}], params=[np.pi*{}]))\n'.format(s.group().upper(), t_qbit, float(m_r[0][0])))
|
90 |
+
continue
|
91 |
+
|
92 |
+
elif s.group() == 'sxdg':
|
93 |
+
lc = lc + 1
|
94 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
95 |
+
t_qbit = int(m_i[0].strip('[]'))
|
96 |
+
f.write('cir_arr.append(SqrtXdg({}))\n'.format(t_qbit))
|
97 |
+
continue
|
98 |
+
|
99 |
+
elif s.group() == "s":
|
100 |
+
lc = lc + 1
|
101 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
102 |
+
t_qbit = int(m_i[0].strip('[]'))
|
103 |
+
f.write('cir_arr.append(S({}))\n'.format(t_qbit))
|
104 |
+
continue
|
105 |
+
|
106 |
+
elif s.group() == 'cx':
|
107 |
+
lc = lc + 1
|
108 |
+
match = re.findall(r'\[\d\d*\]', line)
|
109 |
+
c_qbit = int(match[0].strip('[]'))
|
110 |
+
t_qbit = int(match[1].strip('[]'))
|
111 |
+
f.write('cir_arr.append(Gate(\'CX\', qubits=[{}, {}]))\n'.format(c_qbit, t_qbit))
|
112 |
+
continue
|
113 |
+
|
114 |
+
elif s.group() == 'u2':
|
115 |
+
lc = lc + 1
|
116 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
117 |
+
m_r = m_r[0].split(',')
|
118 |
+
for i, m in enumerate(m_r):
|
119 |
+
if 'pi' in m and 'e' in m:
|
120 |
+
sp_str = m_r[i].split('e-')
|
121 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
122 |
+
elif 'pi' in m:
|
123 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
124 |
+
else:
|
125 |
+
m_r[i] = '0'
|
126 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
127 |
+
t_qbit = int(m_i[0].strip('[]'))
|
128 |
+
f.write('cir_arr.append(U2(np.pi*{}, np.pi*{}, {}))\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
129 |
+
continue
|
130 |
+
|
131 |
+
elif s.group() == 'u3':
|
132 |
+
lc = lc + 1
|
133 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
134 |
+
m_r = m_r[0].split(',')
|
135 |
+
for i, m in enumerate(m_r):
|
136 |
+
if 'pi' in m and 'e' in m:
|
137 |
+
sp_str = m_r[i].split('e-')
|
138 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
139 |
+
elif 'pi' in m:
|
140 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
141 |
+
else:
|
142 |
+
m_r[i] = '0'
|
143 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
144 |
+
t_qbit = int(m_i[0].strip('[]'))
|
145 |
+
f.write('cir_arr.append(U3(np.pi*{}, np.pi*{}, np.pi*{}, {}))\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
146 |
+
continue
|
147 |
+
|
148 |
+
# f.write('psi = simulate(cir_arr, initial_state=init_state, simplify=False, optimize=\'evolution-hybridq\', parallel=n_threads, max_largest_intermediate=2**40)\n')
|
149 |
+
f.write('psi = simulate(cir_arr, initial_state=init_state, optimize=\'evolution-hybridq\', parallel={}, max_largest_intermediate=2**40)\n'.format(n_threads))
|
150 |
+
f.write('t_e = perf_counter()\n')
|
151 |
+
f.write('t_ep = process_time()\n')
|
152 |
+
f.write('print(t_e - t_s)\n')
|
153 |
+
f.write('print(t_ep - t_sp)\n')
|
154 |
+
|
155 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
156 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
157 |
+
|
158 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
159 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
160 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
161 |
+
print(int(lc_res) == lc)
|
162 |
+
# print(lc)
|
163 |
+
|
164 |
+
|
165 |
+
task = 'rqc'
|
166 |
+
sim_pack = 'hybridq'
|
167 |
+
com_cap = 'st'
|
168 |
+
prec = 'sp'
|
169 |
+
|
170 |
+
for N in range(12, 40, 2):
|
171 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
172 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
173 |
+
|
174 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
175 |
+
|
176 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
177 |
+
|
178 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
179 |
+
parse_qasm_to_package_gen(N, 1, input_file, output_file, save_file_path)
|
180 |
+
|
181 |
+
|
182 |
+
r"""
|
183 |
+
for N in range(12, 42, 2):
|
184 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
185 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
186 |
+
|
187 |
+
output_file_path = '/data/user/gangap_a/rqc/hybridq/hybridq_run_files/'
|
188 |
+
output_file = output_file_path + 'hybridq_rqc_n{}.py'.format(N)
|
189 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
190 |
+
|
191 |
+
for N in range(41, 51):
|
192 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
193 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
194 |
+
|
195 |
+
output_file_path = '/data/user/gangap_a/rqc/hybridq/hybridq_run_files/'
|
196 |
+
output_file = output_file_path + 'hybridq_rqc_n{}.py'.format(N)
|
197 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
198 |
+
|
199 |
+
"""
|
qasm_parser/intel_qs/qasm_intel_mt_parser.py
ADDED
@@ -0,0 +1,213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, _nt, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('#include <time.h>\n')
|
11 |
+
f.write('#include <complex.h>\n')
|
12 |
+
|
13 |
+
f.write('#define BILLION 1E9\n')
|
14 |
+
|
15 |
+
f.write('#include "../../include/qureg.hpp"')
|
16 |
+
|
17 |
+
f.write('\n')
|
18 |
+
|
19 |
+
f.write('template<typename Type>\n')
|
20 |
+
f.write('void ApplyU2(iqs::QubitRegister<Type> &state, int q0, double p, double l) {\n')
|
21 |
+
f.write('TM2x2<ComplexDP> D;\n')
|
22 |
+
f.write('D(0, 0) = Type(std::cos(0.5*(p+l))/sqrt(2), -std::sin(0.5*(p+l))/sqrt(2));\n')
|
23 |
+
f.write('D(0, 1) = Type(-std::cos(0.5*(p-l))/sqrt(2), std::sin(0.5*(p-l))/sqrt(2));\n')
|
24 |
+
f.write('D(1, 0) = Type(std::cos(0.5*(p-l))/sqrt(2), std::sin(0.5*(p-l))/sqrt(2));\n')
|
25 |
+
f.write('D(1, 1) = Type(std::cos(0.5*(p+l))/sqrt(2), std::sin(0.5*(p+l))/sqrt(2));\n')
|
26 |
+
|
27 |
+
f.write('state.Apply1QubitGate(q0, D);\n')
|
28 |
+
f.write('}\n\n')
|
29 |
+
|
30 |
+
f.write('template<typename Type>\n')
|
31 |
+
f.write('void ApplyU3(iqs::QubitRegister<Type> &state, int q0, double t, double p, double l) {\n')
|
32 |
+
f.write('TM2x2<ComplexDP> D;\n')
|
33 |
+
f.write('D(0, 0) = Type(std::cos(0.5*(p+l))*std::cos(0.5*t), -std::sin(0.5*(p+l))*std::cos(0.5*t));\n')
|
34 |
+
f.write('D(0, 1) = Type(-std::cos(0.5*(p-l))*std::sin(0.5*t), std::sin(0.5*(p-l))*std::sin(0.5*t));\n')
|
35 |
+
f.write('D(1, 0) = Type(std::cos(0.5*(p-l))*std::sin(0.5*t), std::sin(0.5*(p-l))*std::sin(0.5*t));\n')
|
36 |
+
f.write('D(1, 1) = Type(std::cos(0.5*(p+l))*std::cos(0.5*t), std::sin(0.5*(p+l))*std::cos(0.5*t));\n')
|
37 |
+
f.write('state.Apply1QubitGate(q0, D);\n')
|
38 |
+
f.write('}\n\n')
|
39 |
+
|
40 |
+
f.write('template<typename Type>\n')
|
41 |
+
f.write('void ApplyS(iqs::QubitRegister<Type> &state, int q0) {\n')
|
42 |
+
f.write('TM2x2<ComplexDP> D;\n')
|
43 |
+
f.write('D(0, 0) = (1, 0);\n')
|
44 |
+
f.write('D(0, 1) = (0, 0);\n')
|
45 |
+
f.write('D(1, 0) = (0, 0);\n')
|
46 |
+
f.write('D(1, 1) = (0, 1);\n')
|
47 |
+
f.write('state.Apply1QubitGate(q0, D);\n')
|
48 |
+
f.write('}\n\n')
|
49 |
+
|
50 |
+
f.write('template<typename Type>\n')
|
51 |
+
f.write('void ApplySXDG(iqs::QubitRegister<Type> &state, int q0) {\n')
|
52 |
+
f.write('TM2x2<ComplexDP> D;\n')
|
53 |
+
f.write('D(0, 0) = (0.5, -0.5);\n')
|
54 |
+
f.write('D(0, 1) = (0.5, 0.5);\n')
|
55 |
+
f.write('D(1, 0) = (0.5, 0.5);\n')
|
56 |
+
f.write('D(1, 1) = (0.5, -0.5);\n')
|
57 |
+
f.write('state.Apply1QubitGate(q0, D);\n')
|
58 |
+
f.write('}\n\n')
|
59 |
+
|
60 |
+
f.write('int main(int argc, char **argv){\n')
|
61 |
+
|
62 |
+
f.write('\t#ifndef INTELQS_HAS_MPI\n')
|
63 |
+
f.write('\t#endif\n')
|
64 |
+
|
65 |
+
f.write('\tiqs::mpi::Environment env(argc, argv);\n')
|
66 |
+
f.write('\tif (env.IsUsefulRank() == false) return 0;\n')
|
67 |
+
f.write('\tassert(env.GetNumStates()==1);\n')
|
68 |
+
f.write('\tint my_rank = env.GetStateRank();\n')
|
69 |
+
f.write('\tint num_ranks = env.GetStateSize();\n')
|
70 |
+
f.write('\tint num_threads = {};\n\n'.format(_nt))
|
71 |
+
|
72 |
+
f.write('\t#ifdef _OPENMP\n')
|
73 |
+
f.write('\t#pragma omp parallel\n')
|
74 |
+
f.write('\tnum_threads=omp_get_num_threads();\n')
|
75 |
+
f.write('\t#endif\n\n')
|
76 |
+
|
77 |
+
f.write('\tiqs::QubitRegister<ComplexDP> state ({});\n'.format(N))
|
78 |
+
f.write('\tstd::size_t index = 0;\n')
|
79 |
+
f.write('\tstate.Initialize("base", index);\n')
|
80 |
+
|
81 |
+
f.write('\tstruct timespec requestStart, requestEnd;\n')
|
82 |
+
f.write('\tiqs::mpi::StateBarrier();\n')
|
83 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestStart);\n')
|
84 |
+
|
85 |
+
lc = 0
|
86 |
+
|
87 |
+
with open(input_filename, "r") as ifile:
|
88 |
+
lines = ifile.readlines()
|
89 |
+
|
90 |
+
for line in lines:
|
91 |
+
|
92 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
93 |
+
|
94 |
+
if s is None:
|
95 |
+
continue
|
96 |
+
|
97 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits', 'cirq']:
|
98 |
+
lc = lc + 1
|
99 |
+
f.write('// {}\n'.format(s.group()))
|
100 |
+
continue
|
101 |
+
|
102 |
+
# this can be done better by using key value pairs
|
103 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z':
|
104 |
+
lc = lc + 1
|
105 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
106 |
+
t_qbit = int(m_i[0].strip('[]'))
|
107 |
+
f.write('\tstate.ApplyPauli{}({});\n'.format(s.group().upper(), t_qbit))
|
108 |
+
continue
|
109 |
+
|
110 |
+
elif s.group() == 'sx':
|
111 |
+
lc = lc + 1
|
112 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
113 |
+
t_qbit = int(m_i[0].strip('[]'))
|
114 |
+
f.write('\tstate.ApplyPauliSqrt{}({});\n'.format(s.group()[1].upper(), t_qbit))
|
115 |
+
continue
|
116 |
+
|
117 |
+
elif s.group() == "s" or s.group() == 'sxdg':
|
118 |
+
lc = lc + 1
|
119 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
120 |
+
t_qbit = int(m_i[0].strip('[]'))
|
121 |
+
f.write('\tApply{}(state, {});\n'.format(s.group().upper(), t_qbit))
|
122 |
+
continue
|
123 |
+
|
124 |
+
#################
|
125 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
126 |
+
lc = lc + 1
|
127 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
128 |
+
# print(m_r)
|
129 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
130 |
+
sp_str = m_r[0].split('e-')
|
131 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
132 |
+
elif 'pi' in m_r[0]:
|
133 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
134 |
+
# print(m_r)
|
135 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
136 |
+
t_qbit = int(m_i[0].strip('[]'))
|
137 |
+
f.write('\tstate.ApplyRotation{}({}, M_PI*{});\n'.format(s.group()[1].upper(), t_qbit, float(m_r[0][0])))
|
138 |
+
continue
|
139 |
+
|
140 |
+
elif s.group() == 'cx':
|
141 |
+
lc = lc + 1
|
142 |
+
match = re.findall(r'\[\d\d*\]', line)
|
143 |
+
c_qbit = int(match[0].strip('[]'))
|
144 |
+
t_qbit = int(match[1].strip('[]'))
|
145 |
+
f.write('\tstate.ApplyCPauliX({}, {});\n'.format(c_qbit, t_qbit))
|
146 |
+
continue
|
147 |
+
|
148 |
+
elif s.group() == 'u2':
|
149 |
+
lc = lc + 1
|
150 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
151 |
+
m_r = m_r[0].split(',')
|
152 |
+
for i, m in enumerate(m_r):
|
153 |
+
if 'pi' in m and 'e' in m:
|
154 |
+
sp_str = m_r[i].split('e-')
|
155 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
156 |
+
elif 'pi' in m:
|
157 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
158 |
+
else:
|
159 |
+
m_r[i] = '0'
|
160 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
161 |
+
t_qbit = int(m_i[0].strip('[]'))
|
162 |
+
f.write('\tApplyU2(state, {}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
163 |
+
continue
|
164 |
+
|
165 |
+
elif s.group() == 'u3':
|
166 |
+
lc = lc + 1
|
167 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
168 |
+
m_r = m_r[0].split(',')
|
169 |
+
for i, m in enumerate(m_r):
|
170 |
+
if 'pi' in m and 'e' in m:
|
171 |
+
sp_str = m_r[i].split('e-')
|
172 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
173 |
+
elif 'pi' in m:
|
174 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
175 |
+
else:
|
176 |
+
m_r[i] = '0'
|
177 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
178 |
+
t_qbit = int(m_i[0].strip('[]'))
|
179 |
+
f.write('\tApplyU3(state, {}, M_PI*{}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
180 |
+
continue
|
181 |
+
|
182 |
+
f.write('\tiqs::mpi::StateBarrier();\n')
|
183 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestEnd);\n')
|
184 |
+
|
185 |
+
f.write('\tdouble accum = (requestEnd.tv_sec - requestStart.tv_sec) + (requestEnd.tv_nsec - requestStart.tv_nsec)/BILLION;\n')
|
186 |
+
f.write('\tprintf( "%lf, ", accum );\n')
|
187 |
+
f.write('\treturn 0;\n')
|
188 |
+
f.write('}')
|
189 |
+
|
190 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
191 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
192 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
193 |
+
print(int(lc_res) == lc)
|
194 |
+
# print(lc)
|
195 |
+
|
196 |
+
for N in range(12, 42, 2):
|
197 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
198 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
199 |
+
|
200 |
+
output_file_path = '/data/user/gangap_a/rqc/intel_qs_cpp/intel-qs/rqc_mt/intel_mt_run_files/'
|
201 |
+
output_file = output_file_path + 'intel_rqc_n{}.cpp'.format(N)
|
202 |
+
parse_qasm_to_package_gen(N, 84, input_file, output_file)
|
203 |
+
|
204 |
+
r"""
|
205 |
+
for N in range(41, 51):
|
206 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
207 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
208 |
+
|
209 |
+
output_file_path = '/data/user/gangap_a/rqc/intel_qs_cpp/intel-qs/rqc/intel_mt_run_files/'
|
210 |
+
output_file = output_file_path + 'intel_rqc_n{}.cpp'.format(N)
|
211 |
+
parse_qasm_to_package_gen(N, 84, input_file, output_file)
|
212 |
+
|
213 |
+
"""
|
qasm_parser/intel_qs/qasm_intel_parser.py
ADDED
@@ -0,0 +1,252 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, _nt, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('#include <time.h>\n')
|
11 |
+
f.write('#include <complex.h>\n')
|
12 |
+
|
13 |
+
f.write('#define BILLION 1E9\n')
|
14 |
+
|
15 |
+
f.write('#include "/sim_lib/intel_qs_cpp/intel-qs/include/qureg.hpp"')
|
16 |
+
|
17 |
+
f.write('\n')
|
18 |
+
|
19 |
+
f.write('template<typename Type>\n')
|
20 |
+
f.write('void ApplyU2(iqs::QubitRegister<Type> &state, int q0, double p, double l) {\n')
|
21 |
+
f.write('TM2x2<ComplexDP> D;\n')
|
22 |
+
f.write('D(0, 0) = Type(std::cos(0.5*(p+l))/sqrt(2), -std::sin(0.5*(p+l))/sqrt(2));\n')
|
23 |
+
f.write('D(0, 1) = Type(-std::cos(0.5*(p-l))/sqrt(2), std::sin(0.5*(p-l))/sqrt(2));\n')
|
24 |
+
f.write('D(1, 0) = Type(std::cos(0.5*(p-l))/sqrt(2), std::sin(0.5*(p-l))/sqrt(2));\n')
|
25 |
+
f.write('D(1, 1) = Type(std::cos(0.5*(p+l))/sqrt(2), std::sin(0.5*(p+l))/sqrt(2));\n')
|
26 |
+
|
27 |
+
f.write('state.Apply1QubitGate(q0, D);\n')
|
28 |
+
f.write('}\n\n')
|
29 |
+
|
30 |
+
f.write('template<typename Type>\n')
|
31 |
+
f.write('void ApplyU3(iqs::QubitRegister<Type> &state, int q0, double t, double p, double l) {\n')
|
32 |
+
f.write('TM2x2<ComplexDP> D;\n')
|
33 |
+
f.write('D(0, 0) = Type(std::cos(0.5*(p+l))*std::cos(0.5*t), -std::sin(0.5*(p+l))*std::cos(0.5*t));\n')
|
34 |
+
f.write('D(0, 1) = Type(-std::cos(0.5*(p-l))*std::sin(0.5*t), std::sin(0.5*(p-l))*std::sin(0.5*t));\n')
|
35 |
+
f.write('D(1, 0) = Type(std::cos(0.5*(p-l))*std::sin(0.5*t), std::sin(0.5*(p-l))*std::sin(0.5*t));\n')
|
36 |
+
f.write('D(1, 1) = Type(std::cos(0.5*(p+l))*std::cos(0.5*t), std::sin(0.5*(p+l))*std::cos(0.5*t));\n')
|
37 |
+
f.write('state.Apply1QubitGate(q0, D);\n')
|
38 |
+
f.write('}\n\n')
|
39 |
+
|
40 |
+
f.write('template<typename Type>\n')
|
41 |
+
f.write('void ApplyS(iqs::QubitRegister<Type> &state, int q0) {\n')
|
42 |
+
f.write('TM2x2<ComplexDP> D;\n')
|
43 |
+
f.write('D(0, 0) = Type(1, 0);\n')
|
44 |
+
f.write('D(0, 1) = Type(0, 0);\n')
|
45 |
+
f.write('D(1, 0) = Type(0, 0);\n')
|
46 |
+
f.write('D(1, 1) = Type(0, 1);\n')
|
47 |
+
f.write('state.Apply1QubitGate(q0, D);\n')
|
48 |
+
f.write('}\n\n')
|
49 |
+
|
50 |
+
f.write('template<typename Type>\n')
|
51 |
+
f.write('void ApplySXDG(iqs::QubitRegister<Type> &state, int q0) {\n')
|
52 |
+
f.write('TM2x2<ComplexDP> D;\n')
|
53 |
+
f.write('D(0, 0) = Type(0.5, -0.5);\n')
|
54 |
+
f.write('D(0, 1) = Type(0.5, 0.5);\n')
|
55 |
+
f.write('D(1, 0) = Type(0.5, 0.5);\n')
|
56 |
+
f.write('D(1, 1) = Type(0.5, -0.5);\n')
|
57 |
+
f.write('state.Apply1QubitGate(q0, D);\n')
|
58 |
+
f.write('}\n\n')
|
59 |
+
|
60 |
+
|
61 |
+
f.write('int main(int argc, char **argv){\n')
|
62 |
+
|
63 |
+
f.write('\t#ifndef INTELQS_HAS_MPI\n')
|
64 |
+
f.write('\t#endif\n')
|
65 |
+
|
66 |
+
f.write('\tiqs::mpi::Environment env(argc, argv);\n')
|
67 |
+
f.write('\tif (env.IsUsefulRank() == false) return 0;\n')
|
68 |
+
f.write('\tassert(env.GetNumStates()==1);\n')
|
69 |
+
f.write('\tint my_rank = env.GetStateRank();\n')
|
70 |
+
f.write('\tint num_ranks = env.GetStateSize();\n')
|
71 |
+
f.write('\tint num_threads = {};\n\n'.format(_nt))
|
72 |
+
|
73 |
+
f.write('\t#ifdef _OPENMP\n')
|
74 |
+
f.write('\t#pragma omp parallel\n')
|
75 |
+
# if com_cap == 'st':
|
76 |
+
# f.write('\tassert(num_threads==omp_get_num_threads());\n')
|
77 |
+
# elif com_cap == 'mt':
|
78 |
+
f.write('\tnum_threads=omp_get_num_threads();\n')
|
79 |
+
f.write('\t#endif\n\n')
|
80 |
+
|
81 |
+
f.write('\tiqs::mpi::StateBarrier();\n')
|
82 |
+
|
83 |
+
f.write('\tiqs::QubitRegister<ComplexDP> psi ({});\n'.format(N))
|
84 |
+
f.write('\tstd::size_t index = 0;\n')
|
85 |
+
f.write('\tpsi.Initialize("base", index);\n')
|
86 |
+
|
87 |
+
f.write('\tstruct timespec requestStart, requestEnd;\n')
|
88 |
+
# f.write('\tiqs::mpi::StateBarrier();\n')
|
89 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestStart);\n')
|
90 |
+
|
91 |
+
lc = 0
|
92 |
+
|
93 |
+
with open(input_filename, "r") as ifile:
|
94 |
+
lines = ifile.readlines()
|
95 |
+
|
96 |
+
for line in lines:
|
97 |
+
|
98 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
99 |
+
|
100 |
+
if s is None:
|
101 |
+
continue
|
102 |
+
|
103 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits', 'cirq']:
|
104 |
+
lc = lc + 1
|
105 |
+
f.write('// {}\n'.format(s.group()))
|
106 |
+
continue
|
107 |
+
|
108 |
+
# this can be done better by using key value pairs
|
109 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z':
|
110 |
+
lc = lc + 1
|
111 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
112 |
+
t_qbit = int(m_i[0].strip('[]'))
|
113 |
+
f.write('\tpsi.ApplyPauli{}({});\n'.format(s.group().upper(), t_qbit))
|
114 |
+
continue
|
115 |
+
|
116 |
+
elif s.group() == 'h':
|
117 |
+
lc = lc + 1
|
118 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
119 |
+
t_qbit = int(m_i[0].strip('[]'))
|
120 |
+
f.write('\tpsi.ApplyHadamard({});\n'.format(t_qbit))
|
121 |
+
continue
|
122 |
+
|
123 |
+
elif s.group() == 'sx':
|
124 |
+
lc = lc + 1
|
125 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
126 |
+
t_qbit = int(m_i[0].strip('[]'))
|
127 |
+
f.write('\tpsi.ApplyPauliSqrt{}({});\n'.format(s.group()[1].upper(), t_qbit))
|
128 |
+
continue
|
129 |
+
|
130 |
+
elif s.group() == "s" or s.group() == 'sxdg':
|
131 |
+
lc = lc + 1
|
132 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
133 |
+
t_qbit = int(m_i[0].strip('[]'))
|
134 |
+
f.write('\tApply{}(psi, {});\n'.format(s.group().upper(), t_qbit))
|
135 |
+
continue
|
136 |
+
|
137 |
+
#################
|
138 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
139 |
+
lc = lc + 1
|
140 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
141 |
+
# print(m_r)
|
142 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
143 |
+
sp_str = m_r[0].split('e-')
|
144 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
145 |
+
elif 'pi' in m_r[0]:
|
146 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
147 |
+
# print(m_r)
|
148 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
149 |
+
t_qbit = int(m_i[0].strip('[]'))
|
150 |
+
f.write('\tpsi.ApplyRotation{}({}, M_PI*{});\n'.format(s.group()[1].upper(), t_qbit, float(m_r[0][0])))
|
151 |
+
continue
|
152 |
+
|
153 |
+
elif s.group() == 'cx':
|
154 |
+
lc = lc + 1
|
155 |
+
match = re.findall(r'\[\d\d*\]', line)
|
156 |
+
c_qbit = int(match[0].strip('[]'))
|
157 |
+
t_qbit = int(match[1].strip('[]'))
|
158 |
+
f.write('\tpsi.ApplyCPauliX({}, {});\n'.format(c_qbit, t_qbit))
|
159 |
+
continue
|
160 |
+
|
161 |
+
elif s.group() == 'u2':
|
162 |
+
lc = lc + 1
|
163 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
164 |
+
m_r = m_r[0].split(',')
|
165 |
+
for i, m in enumerate(m_r):
|
166 |
+
if 'pi' in m and 'e' in m:
|
167 |
+
sp_str = m_r[i].split('e-')
|
168 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
169 |
+
elif 'pi' in m:
|
170 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
171 |
+
else:
|
172 |
+
m_r[i] = '0'
|
173 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
174 |
+
t_qbit = int(m_i[0].strip('[]'))
|
175 |
+
f.write('\tApplyU2(psi, {}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
176 |
+
continue
|
177 |
+
|
178 |
+
elif s.group() == 'u3':
|
179 |
+
lc = lc + 1
|
180 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
181 |
+
m_r = m_r[0].split(',')
|
182 |
+
for i, m in enumerate(m_r):
|
183 |
+
if 'pi' in m and 'e' in m:
|
184 |
+
sp_str = m_r[i].split('e-')
|
185 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
186 |
+
elif 'pi' in m:
|
187 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
188 |
+
else:
|
189 |
+
m_r[i] = '0'
|
190 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
191 |
+
t_qbit = int(m_i[0].strip('[]'))
|
192 |
+
f.write('\tApplyU3(psi, {}, M_PI*{}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
193 |
+
continue
|
194 |
+
|
195 |
+
f.write('\tiqs::mpi::StateBarrier();\n')
|
196 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestEnd);\n')
|
197 |
+
|
198 |
+
f.write('\tdouble accum = (requestEnd.tv_sec - requestStart.tv_sec) + (requestEnd.tv_nsec - requestStart.tv_nsec)/BILLION;\n')
|
199 |
+
f.write('\tprintf( "%lf, ", accum );\n')
|
200 |
+
f.write('\treturn 0;\n')
|
201 |
+
f.write('}')
|
202 |
+
|
203 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
204 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
205 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
206 |
+
print(int(lc_res) == lc)
|
207 |
+
# print(lc)
|
208 |
+
|
209 |
+
|
210 |
+
task = 'qft'
|
211 |
+
sim_pack = 'intel_qs_cpp'
|
212 |
+
com_cap = 'mt'
|
213 |
+
prec = 'dp'
|
214 |
+
|
215 |
+
for N in range(6, 40, 2):
|
216 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
217 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
218 |
+
|
219 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
220 |
+
|
221 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
222 |
+
|
223 |
+
output_file = output_file_path + '{}_{}_{}_{}_n{}.cpp'.format(sim_pack, task, com_cap, prec, N)
|
224 |
+
parse_qasm_to_package_gen(N, 84, input_file, output_file)
|
225 |
+
|
226 |
+
r"""
|
227 |
+
for N in range(16, 18, 2):
|
228 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
229 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
230 |
+
|
231 |
+
output_file_path = '/data/user/gangap_a/rqc/intel_qs_cpp/intel-qs/rqc/intel_run_files/'
|
232 |
+
output_file = output_file_path + 'intel_rqc_n{}.cpp'.format(N)
|
233 |
+
parse_qasm_to_package_gen(N, 1, input_file, output_file)
|
234 |
+
|
235 |
+
for N in range(41, 51):
|
236 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
237 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
238 |
+
|
239 |
+
output_file_path = '/data/user/gangap_a/rqc/intel_qs_cpp/intel-qs/rqc/intel_run_files/'
|
240 |
+
output_file = output_file_path + 'intel_rqc_n{}.cpp'.format(N)
|
241 |
+
parse_qasm_to_package_gen(N, 1, input_file, output_file)
|
242 |
+
|
243 |
+
|
244 |
+
|
245 |
+
for N in range(16, 18, 2):
|
246 |
+
input_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/data_files/'
|
247 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
248 |
+
|
249 |
+
output_file_path = '/data/user/gangap_a/rqc/intel_qs_cpp/intel-qs/rqc/intel_run_files/'
|
250 |
+
output_file = output_file_path + 'intel_rqc_n{}.py'.format(N)
|
251 |
+
parse_qasm_to_package_gen(N, 1, input_file, output_file)
|
252 |
+
"""
|
qasm_parser/intel_qs/qasm_intel_parser_mpi.py
ADDED
@@ -0,0 +1,245 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, _nt, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('#include <time.h>\n')
|
11 |
+
f.write('#include <complex.h>\n')
|
12 |
+
|
13 |
+
f.write('#define BILLION 1E9\n')
|
14 |
+
|
15 |
+
f.write('#include "/data/user/gangap_a/sim_lib_mpi/intel-qs/include/qureg.hpp"')
|
16 |
+
|
17 |
+
f.write('\n')
|
18 |
+
|
19 |
+
f.write('template<typename Type>\n')
|
20 |
+
f.write('void ApplyU2(iqs::QubitRegister<Type> &state, int q0, double p, double l) {\n')
|
21 |
+
f.write('TM2x2<ComplexDP> D;\n')
|
22 |
+
f.write('D(0, 0) = Type(std::cos(0.5*(p+l))/sqrt(2), -std::sin(0.5*(p+l))/sqrt(2));\n')
|
23 |
+
f.write('D(0, 1) = Type(-std::cos(0.5*(p-l))/sqrt(2), std::sin(0.5*(p-l))/sqrt(2));\n')
|
24 |
+
f.write('D(1, 0) = Type(std::cos(0.5*(p-l))/sqrt(2), std::sin(0.5*(p-l))/sqrt(2));\n')
|
25 |
+
f.write('D(1, 1) = Type(std::cos(0.5*(p+l))/sqrt(2), std::sin(0.5*(p+l))/sqrt(2));\n')
|
26 |
+
|
27 |
+
f.write('state.Apply1QubitGate(q0, D);\n')
|
28 |
+
f.write('}\n\n')
|
29 |
+
|
30 |
+
f.write('template<typename Type>\n')
|
31 |
+
f.write('void ApplyU3(iqs::QubitRegister<Type> &state, int q0, double t, double p, double l) {\n')
|
32 |
+
f.write('TM2x2<ComplexDP> D;\n')
|
33 |
+
f.write('D(0, 0) = Type(std::cos(0.5*(p+l))*std::cos(0.5*t), -std::sin(0.5*(p+l))*std::cos(0.5*t));\n')
|
34 |
+
f.write('D(0, 1) = Type(-std::cos(0.5*(p-l))*std::sin(0.5*t), std::sin(0.5*(p-l))*std::sin(0.5*t));\n')
|
35 |
+
f.write('D(1, 0) = Type(std::cos(0.5*(p-l))*std::sin(0.5*t), std::sin(0.5*(p-l))*std::sin(0.5*t));\n')
|
36 |
+
f.write('D(1, 1) = Type(std::cos(0.5*(p+l))*std::cos(0.5*t), std::sin(0.5*(p+l))*std::cos(0.5*t));\n')
|
37 |
+
f.write('state.Apply1QubitGate(q0, D);\n')
|
38 |
+
f.write('}\n\n')
|
39 |
+
|
40 |
+
f.write('template<typename Type>\n')
|
41 |
+
f.write('void ApplyS(iqs::QubitRegister<Type> &state, int q0) {\n')
|
42 |
+
f.write('TM2x2<ComplexDP> D;\n')
|
43 |
+
f.write('D(0, 0) = (1, 0);\n')
|
44 |
+
f.write('D(0, 1) = (0, 0);\n')
|
45 |
+
f.write('D(1, 0) = (0, 0);\n')
|
46 |
+
f.write('D(1, 1) = (0, 1);\n')
|
47 |
+
f.write('state.Apply1QubitGate(q0, D);\n')
|
48 |
+
f.write('}\n\n')
|
49 |
+
|
50 |
+
f.write('template<typename Type>\n')
|
51 |
+
f.write('void ApplySXDG(iqs::QubitRegister<Type> &state, int q0) {\n')
|
52 |
+
f.write('TM2x2<ComplexDP> D;\n')
|
53 |
+
f.write('D(0, 0) = (0.5, -0.5);\n')
|
54 |
+
f.write('D(0, 1) = (0.5, 0.5);\n')
|
55 |
+
f.write('D(1, 0) = (0.5, 0.5);\n')
|
56 |
+
f.write('D(1, 1) = (0.5, -0.5);\n')
|
57 |
+
f.write('state.Apply1QubitGate(q0, D);\n')
|
58 |
+
f.write('}\n\n')
|
59 |
+
|
60 |
+
|
61 |
+
f.write('int main(int argc, char **argv){\n')
|
62 |
+
|
63 |
+
f.write('\t#ifndef INTELQS_HAS_MPI\n')
|
64 |
+
f.write('\t#endif\n')
|
65 |
+
|
66 |
+
f.write('\tiqs::mpi::Environment env(argc, argv);\n')
|
67 |
+
f.write('\tif (env.IsUsefulRank() == false) return 0;\n')
|
68 |
+
f.write('\tassert(env.GetNumStates()==1);\n')
|
69 |
+
f.write('\tint my_rank = env.GetStateRank();\n')
|
70 |
+
f.write('\tint num_ranks = env.GetStateSize();\n')
|
71 |
+
f.write('\tint num_threads = {};\n\n'.format(_nt))
|
72 |
+
|
73 |
+
f.write('\t#ifdef _OPENMP\n')
|
74 |
+
f.write('\t#pragma omp parallel\n')
|
75 |
+
# if com_cap == 'st':
|
76 |
+
# f.write('\tassert(num_threads==omp_get_num_threads());\n')
|
77 |
+
# elif com_cap == 'mt':
|
78 |
+
f.write('\tnum_threads=omp_get_num_threads();\n')
|
79 |
+
f.write('\t#endif\n\n')
|
80 |
+
|
81 |
+
f.write('\tiqs::mpi::StateBarrier();\n')
|
82 |
+
|
83 |
+
f.write('\tiqs::QubitRegister<ComplexDP> psi ({});\n'.format(N))
|
84 |
+
f.write('\tstd::size_t index = 0;\n')
|
85 |
+
f.write('\tpsi.Initialize("base", index);\n')
|
86 |
+
|
87 |
+
f.write('\tstruct timespec requestStart, requestEnd;\n')
|
88 |
+
# f.write('\tiqs::mpi::StateBarrier();\n')
|
89 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestStart);\n')
|
90 |
+
|
91 |
+
lc = 0
|
92 |
+
|
93 |
+
with open(input_filename, "r") as ifile:
|
94 |
+
lines = ifile.readlines()
|
95 |
+
|
96 |
+
for line in lines:
|
97 |
+
|
98 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
99 |
+
|
100 |
+
if s is None:
|
101 |
+
continue
|
102 |
+
|
103 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits', 'cirq']:
|
104 |
+
lc = lc + 1
|
105 |
+
f.write('// {}\n'.format(s.group()))
|
106 |
+
continue
|
107 |
+
|
108 |
+
# this can be done better by using key value pairs
|
109 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z':
|
110 |
+
lc = lc + 1
|
111 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
112 |
+
t_qbit = int(m_i[0].strip('[]'))
|
113 |
+
f.write('\tpsi.ApplyPauli{}({});\n'.format(s.group().upper(), t_qbit))
|
114 |
+
continue
|
115 |
+
|
116 |
+
elif s.group() == 'sx':
|
117 |
+
lc = lc + 1
|
118 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
119 |
+
t_qbit = int(m_i[0].strip('[]'))
|
120 |
+
f.write('\tpsi.ApplyPauliSqrt{}({});\n'.format(s.group()[1].upper(), t_qbit))
|
121 |
+
continue
|
122 |
+
|
123 |
+
elif s.group() == "s" or s.group() == 'sxdg':
|
124 |
+
lc = lc + 1
|
125 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
126 |
+
t_qbit = int(m_i[0].strip('[]'))
|
127 |
+
f.write('\tApply{}(psi, {});\n'.format(s.group().upper(), t_qbit))
|
128 |
+
continue
|
129 |
+
|
130 |
+
#################
|
131 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
132 |
+
lc = lc + 1
|
133 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
134 |
+
# print(m_r)
|
135 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
136 |
+
sp_str = m_r[0].split('e-')
|
137 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
138 |
+
elif 'pi' in m_r[0]:
|
139 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
140 |
+
# print(m_r)
|
141 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
142 |
+
t_qbit = int(m_i[0].strip('[]'))
|
143 |
+
f.write('\tpsi.ApplyRotation{}({}, M_PI*{});\n'.format(s.group()[1].upper(), t_qbit, float(m_r[0][0])))
|
144 |
+
continue
|
145 |
+
|
146 |
+
elif s.group() == 'cx':
|
147 |
+
lc = lc + 1
|
148 |
+
match = re.findall(r'\[\d\d*\]', line)
|
149 |
+
c_qbit = int(match[0].strip('[]'))
|
150 |
+
t_qbit = int(match[1].strip('[]'))
|
151 |
+
f.write('\tpsi.ApplyCPauliX({}, {});\n'.format(c_qbit, t_qbit))
|
152 |
+
continue
|
153 |
+
|
154 |
+
elif s.group() == 'u2':
|
155 |
+
lc = lc + 1
|
156 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
157 |
+
m_r = m_r[0].split(',')
|
158 |
+
for i, m in enumerate(m_r):
|
159 |
+
if 'pi' in m and 'e' in m:
|
160 |
+
sp_str = m_r[i].split('e-')
|
161 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
162 |
+
elif 'pi' in m:
|
163 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
164 |
+
else:
|
165 |
+
m_r[i] = '0'
|
166 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
167 |
+
t_qbit = int(m_i[0].strip('[]'))
|
168 |
+
f.write('\tApplyU2(psi, {}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
169 |
+
continue
|
170 |
+
|
171 |
+
elif s.group() == 'u3':
|
172 |
+
lc = lc + 1
|
173 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
174 |
+
m_r = m_r[0].split(',')
|
175 |
+
for i, m in enumerate(m_r):
|
176 |
+
if 'pi' in m and 'e' in m:
|
177 |
+
sp_str = m_r[i].split('e-')
|
178 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
179 |
+
elif 'pi' in m:
|
180 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
181 |
+
else:
|
182 |
+
m_r[i] = '0'
|
183 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
184 |
+
t_qbit = int(m_i[0].strip('[]'))
|
185 |
+
f.write('\tApplyU3(psi, {}, M_PI*{}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
186 |
+
continue
|
187 |
+
|
188 |
+
f.write('\tiqs::mpi::StateBarrier();\n')
|
189 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestEnd);\n')
|
190 |
+
|
191 |
+
f.write('\tdouble accum = (requestEnd.tv_sec - requestStart.tv_sec) + (requestEnd.tv_nsec - requestStart.tv_nsec)/BILLION;\n')
|
192 |
+
f.write('\tprintf( "%lf, ", accum );\n')
|
193 |
+
f.write('\treturn 0;\n')
|
194 |
+
f.write('}')
|
195 |
+
|
196 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
197 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
198 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
199 |
+
print(int(lc_res) == lc)
|
200 |
+
# print(lc)
|
201 |
+
|
202 |
+
|
203 |
+
task = 'hdyn'
|
204 |
+
sim_pack = 'intel_qs_cpp'
|
205 |
+
com_cap = 'mpi'
|
206 |
+
prec = 'dp'
|
207 |
+
|
208 |
+
for N in range(12, 36, 2):
|
209 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
210 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
211 |
+
|
212 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
213 |
+
|
214 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
215 |
+
|
216 |
+
output_file = output_file_path + '{}_{}_{}_{}_n{}.cpp'.format(sim_pack, task, com_cap, prec, N)
|
217 |
+
parse_qasm_to_package_gen(N, 1, input_file, output_file)
|
218 |
+
|
219 |
+
r"""
|
220 |
+
for N in range(16, 18, 2):
|
221 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
222 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
223 |
+
|
224 |
+
output_file_path = '/data/user/gangap_a/rqc/intel_qs_cpp/intel-qs/rqc/intel_run_files/'
|
225 |
+
output_file = output_file_path + 'intel_rqc_n{}.cpp'.format(N)
|
226 |
+
parse_qasm_to_package_gen(N, 1, input_file, output_file)
|
227 |
+
|
228 |
+
for N in range(41, 51):
|
229 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
230 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
231 |
+
|
232 |
+
output_file_path = '/data/user/gangap_a/rqc/intel_qs_cpp/intel-qs/rqc/intel_run_files/'
|
233 |
+
output_file = output_file_path + 'intel_rqc_n{}.cpp'.format(N)
|
234 |
+
parse_qasm_to_package_gen(N, 1, input_file, output_file)
|
235 |
+
|
236 |
+
|
237 |
+
|
238 |
+
for N in range(16, 18, 2):
|
239 |
+
input_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/data_files/'
|
240 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
241 |
+
|
242 |
+
output_file_path = '/data/user/gangap_a/rqc/intel_qs_cpp/intel-qs/rqc/intel_run_files/'
|
243 |
+
output_file = output_file_path + 'intel_rqc_n{}.py'.format(N)
|
244 |
+
parse_qasm_to_package_gen(N, 1, input_file, output_file)
|
245 |
+
"""
|
qasm_parser/intel_qs/qasm_intel_parser_sp.py
ADDED
@@ -0,0 +1,253 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, _nt, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('#include <time.h>\n')
|
11 |
+
f.write('#include <complex.h>\n')
|
12 |
+
|
13 |
+
f.write('#define BILLION 1E9\n')
|
14 |
+
|
15 |
+
f.write('#include "/sim_lib/intel_qs_cpp/intel-qs/include/qureg.hpp"')
|
16 |
+
|
17 |
+
f.write('\n')
|
18 |
+
|
19 |
+
f.write('template<typename Type>\n')
|
20 |
+
f.write('void ApplyU2(iqs::QubitRegister<Type> &state, int q0, float p, float l) {\n')
|
21 |
+
f.write('TM2x2<ComplexDP> D;\n')
|
22 |
+
f.write('D(0, 0) = Type(std::cos(0.5*(p+l))/sqrt(2), -std::sin(0.5*(p+l))/sqrt(2));\n')
|
23 |
+
f.write('D(0, 1) = Type(-std::cos(0.5*(p-l))/sqrt(2), std::sin(0.5*(p-l))/sqrt(2));\n')
|
24 |
+
f.write('D(1, 0) = Type(std::cos(0.5*(p-l))/sqrt(2), std::sin(0.5*(p-l))/sqrt(2));\n')
|
25 |
+
f.write('D(1, 1) = Type(std::cos(0.5*(p+l))/sqrt(2), std::sin(0.5*(p+l))/sqrt(2));\n')
|
26 |
+
|
27 |
+
f.write('state.Apply1QubitGate(q0, D);\n')
|
28 |
+
f.write('}\n\n')
|
29 |
+
|
30 |
+
f.write('template<typename Type>\n')
|
31 |
+
f.write('void ApplyU3(iqs::QubitRegister<Type> &state, int q0, float t, float p, float l) {\n')
|
32 |
+
f.write('TM2x2<ComplexDP> D;\n')
|
33 |
+
f.write('D(0, 0) = Type(std::cos(0.5*(p+l))*std::cos(0.5*t), -std::sin(0.5*(p+l))*std::cos(0.5*t));\n')
|
34 |
+
f.write('D(0, 1) = Type(-std::cos(0.5*(p-l))*std::sin(0.5*t), std::sin(0.5*(p-l))*std::sin(0.5*t));\n')
|
35 |
+
f.write('D(1, 0) = Type(std::cos(0.5*(p-l))*std::sin(0.5*t), std::sin(0.5*(p-l))*std::sin(0.5*t));\n')
|
36 |
+
f.write('D(1, 1) = Type(std::cos(0.5*(p+l))*std::cos(0.5*t), std::sin(0.5*(p+l))*std::cos(0.5*t));\n')
|
37 |
+
f.write('state.Apply1QubitGate(q0, D);\n')
|
38 |
+
f.write('}\n\n')
|
39 |
+
|
40 |
+
f.write('template<typename Type>\n')
|
41 |
+
f.write('void ApplyS(iqs::QubitRegister<Type> &state, int q0) {\n')
|
42 |
+
f.write('TM2x2<ComplexDP> D;\n')
|
43 |
+
f.write('D(0, 0) = Type(1, 0);\n')
|
44 |
+
f.write('D(0, 1) = Type(0, 0);\n')
|
45 |
+
f.write('D(1, 0) = Type(0, 0);\n')
|
46 |
+
f.write('D(1, 1) = Type(0, 1);\n')
|
47 |
+
f.write('state.Apply1QubitGate(q0, D);\n')
|
48 |
+
f.write('}\n\n')
|
49 |
+
|
50 |
+
f.write('template<typename Type>\n')
|
51 |
+
f.write('void ApplySXDG(iqs::QubitRegister<Type> &state, int q0) {\n')
|
52 |
+
f.write('TM2x2<ComplexDP> D;\n')
|
53 |
+
f.write('D(0, 0) = Type(0.5, -0.5);\n')
|
54 |
+
f.write('D(0, 1) = Type(0.5, 0.5);\n')
|
55 |
+
f.write('D(1, 0) = Type(0.5, 0.5);\n')
|
56 |
+
f.write('D(1, 1) = Type(0.5, -0.5);\n')
|
57 |
+
f.write('state.Apply1QubitGate(q0, D);\n')
|
58 |
+
f.write('}\n\n')
|
59 |
+
|
60 |
+
|
61 |
+
f.write('int main(int argc, char **argv){\n')
|
62 |
+
|
63 |
+
f.write('\t#ifndef INTELQS_HAS_MPI\n')
|
64 |
+
f.write('\t#endif\n')
|
65 |
+
|
66 |
+
f.write('\tiqs::mpi::Environment env(argc, argv);\n')
|
67 |
+
f.write('\tif (env.IsUsefulRank() == false) return 0;\n')
|
68 |
+
f.write('\tassert(env.GetNumStates()==1);\n')
|
69 |
+
f.write('\tint my_rank = env.GetStateRank();\n')
|
70 |
+
f.write('\tint num_ranks = env.GetStateSize();\n')
|
71 |
+
f.write('\tint num_threads = {};\n\n'.format(_nt))
|
72 |
+
|
73 |
+
f.write('\t#ifdef _OPENMP\n')
|
74 |
+
f.write('\t#pragma omp parallel\n')
|
75 |
+
# if com_cap == 'st':
|
76 |
+
# f.write('\tassert(num_threads==omp_get_num_threads());\n')
|
77 |
+
# elif com_cap == 'mt':
|
78 |
+
f.write('\tnum_threads=omp_get_num_threads();\n')
|
79 |
+
f.write('\t#endif\n\n')
|
80 |
+
|
81 |
+
f.write('\tiqs::mpi::StateBarrier();\n')
|
82 |
+
|
83 |
+
f.write('\tiqs::QubitRegister<ComplexSP> psi ({});\n'.format(N))
|
84 |
+
f.write('\tstd::size_t index = 0;\n')
|
85 |
+
f.write('\tpsi.Initialize("base", index);\n')
|
86 |
+
|
87 |
+
f.write('\tstruct timespec requestStart, requestEnd;\n')
|
88 |
+
# f.write('\tiqs::mpi::StateBarrier();\n')
|
89 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestStart);\n')
|
90 |
+
|
91 |
+
lc = 0
|
92 |
+
|
93 |
+
with open(input_filename, "r") as ifile:
|
94 |
+
lines = ifile.readlines()
|
95 |
+
|
96 |
+
for line in lines:
|
97 |
+
|
98 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
99 |
+
|
100 |
+
if s is None:
|
101 |
+
continue
|
102 |
+
|
103 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits', 'cirq']:
|
104 |
+
lc = lc + 1
|
105 |
+
f.write('// {}\n'.format(s.group()))
|
106 |
+
continue
|
107 |
+
|
108 |
+
# this can be done better by using key value pairs
|
109 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z':
|
110 |
+
lc = lc + 1
|
111 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
112 |
+
t_qbit = int(m_i[0].strip('[]'))
|
113 |
+
f.write('\tpsi.ApplyPauli{}({});\n'.format(s.group().upper(), t_qbit))
|
114 |
+
continue
|
115 |
+
|
116 |
+
elif s.group() == 'h':
|
117 |
+
lc = lc + 1
|
118 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
119 |
+
t_qbit = int(m_i[0].strip('[]'))
|
120 |
+
f.write('\tpsi.ApplyHadamard({});\n'.format(t_qbit))
|
121 |
+
continue
|
122 |
+
|
123 |
+
|
124 |
+
elif s.group() == 'sx':
|
125 |
+
lc = lc + 1
|
126 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
127 |
+
t_qbit = int(m_i[0].strip('[]'))
|
128 |
+
f.write('\tpsi.ApplyPauliSqrt{}({});\n'.format(s.group()[1].upper(), t_qbit))
|
129 |
+
continue
|
130 |
+
|
131 |
+
elif s.group() == "s" or s.group() == 'sxdg':
|
132 |
+
lc = lc + 1
|
133 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
134 |
+
t_qbit = int(m_i[0].strip('[]'))
|
135 |
+
f.write('\tApply{}(psi, {});\n'.format(s.group().upper(), t_qbit))
|
136 |
+
continue
|
137 |
+
|
138 |
+
#################
|
139 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
140 |
+
lc = lc + 1
|
141 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
142 |
+
# print(m_r)
|
143 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
144 |
+
sp_str = m_r[0].split('e-')
|
145 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
146 |
+
elif 'pi' in m_r[0]:
|
147 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
148 |
+
# print(m_r)
|
149 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
150 |
+
t_qbit = int(m_i[0].strip('[]'))
|
151 |
+
f.write('\tpsi.ApplyRotation{}({}, M_PI*{});\n'.format(s.group()[1].upper(), t_qbit, float(m_r[0][0])))
|
152 |
+
continue
|
153 |
+
|
154 |
+
elif s.group() == 'cx':
|
155 |
+
lc = lc + 1
|
156 |
+
match = re.findall(r'\[\d\d*\]', line)
|
157 |
+
c_qbit = int(match[0].strip('[]'))
|
158 |
+
t_qbit = int(match[1].strip('[]'))
|
159 |
+
f.write('\tpsi.ApplyCPauliX({}, {});\n'.format(c_qbit, t_qbit))
|
160 |
+
continue
|
161 |
+
|
162 |
+
elif s.group() == 'u2':
|
163 |
+
lc = lc + 1
|
164 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
165 |
+
m_r = m_r[0].split(',')
|
166 |
+
for i, m in enumerate(m_r):
|
167 |
+
if 'pi' in m and 'e' in m:
|
168 |
+
sp_str = m_r[i].split('e-')
|
169 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
170 |
+
elif 'pi' in m:
|
171 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
172 |
+
else:
|
173 |
+
m_r[i] = '0'
|
174 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
175 |
+
t_qbit = int(m_i[0].strip('[]'))
|
176 |
+
f.write('\tApplyU2(psi, {}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
177 |
+
continue
|
178 |
+
|
179 |
+
elif s.group() == 'u3':
|
180 |
+
lc = lc + 1
|
181 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
182 |
+
m_r = m_r[0].split(',')
|
183 |
+
for i, m in enumerate(m_r):
|
184 |
+
if 'pi' in m and 'e' in m:
|
185 |
+
sp_str = m_r[i].split('e-')
|
186 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
187 |
+
elif 'pi' in m:
|
188 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
189 |
+
else:
|
190 |
+
m_r[i] = '0'
|
191 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
192 |
+
t_qbit = int(m_i[0].strip('[]'))
|
193 |
+
f.write('\tApplyU3(psi, {}, M_PI*{}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
194 |
+
continue
|
195 |
+
|
196 |
+
f.write('\tiqs::mpi::StateBarrier();\n')
|
197 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestEnd);\n')
|
198 |
+
|
199 |
+
f.write('\tdouble accum = (requestEnd.tv_sec - requestStart.tv_sec) + (requestEnd.tv_nsec - requestStart.tv_nsec)/BILLION;\n')
|
200 |
+
f.write('\tprintf( "%lf, ", accum );\n')
|
201 |
+
f.write('\treturn 0;\n')
|
202 |
+
f.write('}')
|
203 |
+
|
204 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
205 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
206 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
207 |
+
print(int(lc_res) == lc)
|
208 |
+
# print(lc)
|
209 |
+
|
210 |
+
|
211 |
+
task = 'rqc'
|
212 |
+
sim_pack = 'intel_qs_cpp'
|
213 |
+
com_cap = 'mt'
|
214 |
+
prec = 'sp'
|
215 |
+
|
216 |
+
for N in range(12, 38, 2):
|
217 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
218 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
219 |
+
|
220 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
221 |
+
|
222 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
223 |
+
|
224 |
+
output_file = output_file_path + '{}_{}_{}_{}_n{}.cpp'.format(sim_pack, task, com_cap, prec, N)
|
225 |
+
parse_qasm_to_package_gen(N, 84, input_file, output_file)
|
226 |
+
|
227 |
+
r"""
|
228 |
+
for N in range(16, 18, 2):
|
229 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
230 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
231 |
+
|
232 |
+
output_file_path = '/data/user/gangap_a/rqc/intel_qs_cpp/intel-qs/rqc/intel_run_files/'
|
233 |
+
output_file = output_file_path + 'intel_rqc_n{}.cpp'.format(N)
|
234 |
+
parse_qasm_to_package_gen(N, 1, input_file, output_file)
|
235 |
+
|
236 |
+
for N in range(41, 51):
|
237 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
238 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
239 |
+
|
240 |
+
output_file_path = '/data/user/gangap_a/rqc/intel_qs_cpp/intel-qs/rqc/intel_run_files/'
|
241 |
+
output_file = output_file_path + 'intel_rqc_n{}.cpp'.format(N)
|
242 |
+
parse_qasm_to_package_gen(N, 1, input_file, output_file)
|
243 |
+
|
244 |
+
|
245 |
+
|
246 |
+
for N in range(16, 18, 2):
|
247 |
+
input_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/data_files/'
|
248 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
249 |
+
|
250 |
+
output_file_path = '/data/user/gangap_a/rqc/intel_qs_cpp/intel-qs/rqc/intel_run_files/'
|
251 |
+
output_file = output_file_path + 'intel_rqc_n{}.py'.format(N)
|
252 |
+
parse_qasm_to_package_gen(N, 1, input_file, output_file)
|
253 |
+
"""
|
qasm_parser/myqlm/qasm_myqlm_cpp_parser.py
ADDED
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, cc, pr, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import numpy as np\n')
|
11 |
+
f.write('from qat.lang.AQASM import Program, X, Y, Z, H, S, T, CNOT, RX, RY, RZ\n')
|
12 |
+
# f.write('from qat.qpus import get_default_qpu\n')
|
13 |
+
f.write('from qat.clinalg import CLinalg\n')
|
14 |
+
f.write('from qat.lang.AQASM import AbstractGate\n')
|
15 |
+
f.write('import os\n')
|
16 |
+
f.write('from time import process_time, perf_counter\n')
|
17 |
+
|
18 |
+
f.write('SX = AbstractGate("SX", [], arity=1)\n')#, matrix_generator=np.array([[(1+1j)/2., (1-1j)/2.], [(1-1j)/2., (1+1j)/2.]]))\n')
|
19 |
+
f.write('SX.set_matrix_generator(lambda: np.array([[(1+1j)/2., (1-1j)/2.], [(1-1j)/2., (1+1j)/2.]]))\n')
|
20 |
+
|
21 |
+
f.write('SXDG = AbstractGate("SXDG", [], arity=1)\n')#, matrix_generator=np.array([[(1-1j)/2., (1+1j)/2.], [(1+1j)/2., (1-1j)/2.]]))\n')
|
22 |
+
f.write('SXDG.set_matrix_generator(lambda: np.array([[(1-1j)/2., (1+1j)/2.], [(1+1j)/2., (1-1j)/2.]]))\n')
|
23 |
+
|
24 |
+
f.write('def U2_generator(p, l):\n')
|
25 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
26 |
+
f.write(' return np.array(mat)\n\n')
|
27 |
+
|
28 |
+
f.write('U2 = AbstractGate("U2", [float, float], arity=1, matrix_generator=U2_generator)\n')
|
29 |
+
|
30 |
+
f.write('def U3_generator(t, p, l):\n')
|
31 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
32 |
+
f.write(' return np.array(mat)\n\n')
|
33 |
+
|
34 |
+
f.write('U3 = AbstractGate("U3", [float, float, float], arity=1, matrix_generator=U3_generator)\n')
|
35 |
+
|
36 |
+
f.write('t_sp = process_time()\n')
|
37 |
+
f.write('t_s = perf_counter()\n')
|
38 |
+
|
39 |
+
f.write('prog = Program()\n')
|
40 |
+
f.write('qbits = prog.qalloc({})\n'.format(N))
|
41 |
+
|
42 |
+
with open(input_filename, "r") as ifile:
|
43 |
+
lines = ifile.readlines()
|
44 |
+
|
45 |
+
lc = 0
|
46 |
+
cirq_c = 0
|
47 |
+
for line in lines:
|
48 |
+
|
49 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
50 |
+
|
51 |
+
if s is None:
|
52 |
+
continue
|
53 |
+
|
54 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
55 |
+
lc = lc + 1
|
56 |
+
f.write('# {}\n'.format(s.group()))
|
57 |
+
continue
|
58 |
+
|
59 |
+
elif s.group() == 'cirq':
|
60 |
+
lc = lc + 1
|
61 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
62 |
+
cirq_c = cirq_c + 1
|
63 |
+
continue
|
64 |
+
|
65 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'h':
|
66 |
+
lc = lc + 1
|
67 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
68 |
+
t_qbit = int(m_i[0].strip('[]'))
|
69 |
+
f.write('{}(qbits[{}])\n'.format(s.group().upper(), t_qbit))
|
70 |
+
continue
|
71 |
+
|
72 |
+
elif s.group() == 'sx':
|
73 |
+
lc = lc + 1
|
74 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
75 |
+
t_qbit = int(m_i[0].strip('[]'))
|
76 |
+
f.write('prog.apply(SX(), qbits[{}])\n'.format(t_qbit))
|
77 |
+
|
78 |
+
elif s.group() == 'sxdg':
|
79 |
+
lc = lc + 1
|
80 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
81 |
+
t_qbit = int(m_i[0].strip('[]'))
|
82 |
+
f.write('prog.apply(SXDG(), qbits[{}])\n'.format(t_qbit))
|
83 |
+
continue
|
84 |
+
|
85 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
86 |
+
lc = lc + 1
|
87 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
88 |
+
# print(m_r)
|
89 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
90 |
+
sp_str = m_r[0].split('e-')
|
91 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
92 |
+
elif 'pi' in m_r[0]:
|
93 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
94 |
+
# print(m_r)
|
95 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
96 |
+
t_qbit = int(m_i[0].strip('[]'))
|
97 |
+
# f.write('qc.add_{}gate({}, np.pi*{})\n'.format(s.group().upper(), t_qbit, float(m_r[0][0])))
|
98 |
+
f.write('{}(np.pi*{})(qbits[{}])\n'.format(s.group().upper(), float(m_r[0][0]), t_qbit))
|
99 |
+
continue
|
100 |
+
|
101 |
+
elif s.group() == 'cx':
|
102 |
+
lc = lc + 1
|
103 |
+
match = re.findall(r'\[\d\d*\]', line)
|
104 |
+
c_qbit = int(match[0].strip('[]'))
|
105 |
+
t_qbit = int(match[1].strip('[]'))
|
106 |
+
f.write('CNOT(qbits[{}], qbits[{}])\n'.format(c_qbit, t_qbit))
|
107 |
+
continue
|
108 |
+
|
109 |
+
elif s.group() == 'u2':
|
110 |
+
lc = lc + 1
|
111 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
112 |
+
m_r = m_r[0].split(',')
|
113 |
+
for i, m in enumerate(m_r):
|
114 |
+
if 'pi' in m and 'e' in m:
|
115 |
+
sp_str = m_r[i].split('e-')
|
116 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
117 |
+
elif 'pi' in m:
|
118 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
119 |
+
else:
|
120 |
+
m_r[i] = '0'
|
121 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
122 |
+
t_qbit = int(m_i[0].strip('[]'))
|
123 |
+
f.write('prog.apply(U2(np.pi*{}, np.pi*{}), qbits[{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
124 |
+
continue
|
125 |
+
|
126 |
+
elif s.group() == 'u3':
|
127 |
+
lc = lc + 1
|
128 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
129 |
+
m_r = m_r[0].split(',')
|
130 |
+
for i, m in enumerate(m_r):
|
131 |
+
if 'pi' in m and 'e' in m:
|
132 |
+
sp_str = m_r[i].split('e-')
|
133 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
134 |
+
elif 'pi' in m:
|
135 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
136 |
+
else:
|
137 |
+
m_r[i] = '0'
|
138 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
139 |
+
t_qbit = int(m_i[0].strip('[]'))
|
140 |
+
f.write('prog.apply(U3(np.pi*{}, np.pi*{}, np.pi*{}), qbits[{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]), float(m_r[2][0]), t_qbit))
|
141 |
+
continue
|
142 |
+
|
143 |
+
|
144 |
+
# f.write('qpu = get_default_qpu()\n')
|
145 |
+
if (com_cap == 'st' or com_cap == 'mt') and pr == 'sp':
|
146 |
+
f.write('qpu = CLinalg(use_nbthreads_heuristic=True, precision=1)\n')
|
147 |
+
elif (com_cap == 'st' or com_cap == 'mt') and pr == 'dp':
|
148 |
+
f.write('qpu = CLinalg(use_nbthreads_heuristic=True, precision=2)\n')
|
149 |
+
elif com_cap == 'gpu' and pr == 'sp':
|
150 |
+
f.write('qpu = CLinalg(use_gpu=True, precision=1)\n')
|
151 |
+
elif com_cap == 'gpu' and pr == 'dp':
|
152 |
+
f.write('qpu = CLinalg(use_gpu=True, precision=2)\n')
|
153 |
+
|
154 |
+
|
155 |
+
f.write('circuit = prog.to_circ()\n')
|
156 |
+
f.write('job = circuit.to_job()\n')
|
157 |
+
f.write('result = qpu.submit(job)\n')
|
158 |
+
# f.write('f_state = device.run(qc).result().values[0]\n')
|
159 |
+
|
160 |
+
f.write('t_e = perf_counter()\n')
|
161 |
+
f.write('t_ep = process_time()\n')
|
162 |
+
f.write('print(t_e - t_s)\n')
|
163 |
+
f.write('print(t_ep - t_sp)\n')
|
164 |
+
|
165 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
166 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
167 |
+
|
168 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
169 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
170 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
171 |
+
print(int(lc_res) == lc)
|
172 |
+
# print(lc)
|
173 |
+
|
174 |
+
# parse_qasm_to_package_gen(12, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "n12_qulacs.py")
|
175 |
+
|
176 |
+
task = 'qft'
|
177 |
+
sim_pack = 'myqlm_cpp'
|
178 |
+
com_cap = 'mt'
|
179 |
+
prec = 'dp'
|
180 |
+
|
181 |
+
for N in range(6, 38, 2):
|
182 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
183 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
184 |
+
|
185 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
186 |
+
|
187 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
188 |
+
|
189 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
190 |
+
parse_qasm_to_package_gen(N, com_cap, prec, input_file, output_file, save_file_path)
|
191 |
+
|
192 |
+
|
193 |
+
"""
|
194 |
+
for N in range(12, 42, 2):
|
195 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
196 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
197 |
+
|
198 |
+
output_file_path = '/data/user/gangap_a/rqc/qulacs/qulacs_run_files/'
|
199 |
+
output_file = output_file_path + 'qulacs_rqc_n{}.py'.format(N)
|
200 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
201 |
+
|
202 |
+
for N in range(41, 51):
|
203 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
204 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
205 |
+
|
206 |
+
output_file_path = '/data/user/gangap_a/rqc/qulacs/qulacs_run_files/'
|
207 |
+
output_file = output_file_path + 'qulacs_rqc_n{}.py'.format(N)
|
208 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
209 |
+
|
210 |
+
"""
|
qasm_parser/myqlm/qasm_myqlm_py_parser.py
ADDED
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import numpy as np\n')
|
11 |
+
f.write('from qat.lang.AQASM import Program, X, Y, Z, H, S, T, CNOT, RX, RY, RZ\n')
|
12 |
+
# f.write('from qat.qpus import get_default_qpu\n')
|
13 |
+
f.write('from qat.pylinalg import PyLinalg\n')
|
14 |
+
f.write('from qat.lang.AQASM import AbstractGate\n')
|
15 |
+
f.write('import os\n')
|
16 |
+
f.write('from time import process_time, perf_counter\n')
|
17 |
+
|
18 |
+
f.write('SX = AbstractGate("SX", [], arity=1)\n')#, matrix_generator=np.array([[(1+1j)/2., (1-1j)/2.], [(1-1j)/2., (1+1j)/2.]]))\n')
|
19 |
+
f.write('SX.set_matrix_generator(lambda: np.array([[(1+1j)/2., (1-1j)/2.], [(1-1j)/2., (1+1j)/2.]]))\n')
|
20 |
+
|
21 |
+
f.write('SXDG = AbstractGate("SXDG", [], arity=1)\n')#, matrix_generator=np.array([[(1-1j)/2., (1+1j)/2.], [(1+1j)/2., (1-1j)/2.]]))\n')
|
22 |
+
f.write('SXDG.set_matrix_generator(lambda: np.array([[(1-1j)/2., (1+1j)/2.], [(1+1j)/2., (1-1j)/2.]]))\n')
|
23 |
+
|
24 |
+
f.write('def U2_generator(p, l):\n')
|
25 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
26 |
+
f.write(' return np.array(mat)\n\n')
|
27 |
+
|
28 |
+
f.write('U2 = AbstractGate("U2", [float, float], arity=1, matrix_generator=U2_generator)\n')
|
29 |
+
|
30 |
+
f.write('def U3_generator(t, p, l):\n')
|
31 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
32 |
+
f.write(' return np.array(mat)\n\n')
|
33 |
+
|
34 |
+
f.write('U3 = AbstractGate("U3", [float, float, float], arity=1, matrix_generator=U3_generator)\n')
|
35 |
+
|
36 |
+
f.write('t_sp = process_time()\n')
|
37 |
+
f.write('t_s = perf_counter()\n')
|
38 |
+
|
39 |
+
f.write('prog = Program()\n')
|
40 |
+
f.write('qbits = prog.qalloc({})\n'.format(N))
|
41 |
+
|
42 |
+
with open(input_filename, "r") as ifile:
|
43 |
+
lines = ifile.readlines()
|
44 |
+
|
45 |
+
lc = 0
|
46 |
+
cirq_c = 0
|
47 |
+
for line in lines:
|
48 |
+
|
49 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
50 |
+
|
51 |
+
if s is None:
|
52 |
+
continue
|
53 |
+
|
54 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
55 |
+
lc = lc + 1
|
56 |
+
f.write('# {}\n'.format(s.group()))
|
57 |
+
continue
|
58 |
+
|
59 |
+
elif s.group() == 'cirq':
|
60 |
+
lc = lc + 1
|
61 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
62 |
+
cirq_c = cirq_c + 1
|
63 |
+
continue
|
64 |
+
|
65 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'h':
|
66 |
+
lc = lc + 1
|
67 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
68 |
+
t_qbit = int(m_i[0].strip('[]'))
|
69 |
+
f.write('{}(qbits[{}])\n'.format(s.group().upper(), t_qbit))
|
70 |
+
continue
|
71 |
+
|
72 |
+
elif s.group() == 'sx':
|
73 |
+
lc = lc + 1
|
74 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
75 |
+
t_qbit = int(m_i[0].strip('[]'))
|
76 |
+
f.write('prog.apply(SX(), qbits[{}])\n'.format(t_qbit))
|
77 |
+
|
78 |
+
elif s.group() == 'sxdg':
|
79 |
+
lc = lc + 1
|
80 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
81 |
+
t_qbit = int(m_i[0].strip('[]'))
|
82 |
+
f.write('prog.apply(SXDG(), qbits[{}])\n'.format(t_qbit))
|
83 |
+
continue
|
84 |
+
|
85 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
86 |
+
lc = lc + 1
|
87 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
88 |
+
# print(m_r)
|
89 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
90 |
+
sp_str = m_r[0].split('e-')
|
91 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
92 |
+
elif 'pi' in m_r[0]:
|
93 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
94 |
+
# print(m_r)
|
95 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
96 |
+
t_qbit = int(m_i[0].strip('[]'))
|
97 |
+
# f.write('qc.add_{}gate({}, np.pi*{})\n'.format(s.group().upper(), t_qbit, float(m_r[0][0])))
|
98 |
+
f.write('{}(np.pi*{})(qbits[{}])\n'.format(s.group().upper(), float(m_r[0][0]), t_qbit))
|
99 |
+
continue
|
100 |
+
|
101 |
+
elif s.group() == 'cx':
|
102 |
+
lc = lc + 1
|
103 |
+
match = re.findall(r'\[\d\d*\]', line)
|
104 |
+
c_qbit = int(match[0].strip('[]'))
|
105 |
+
t_qbit = int(match[1].strip('[]'))
|
106 |
+
f.write('CNOT(qbits[{}], qbits[{}])\n'.format(c_qbit, t_qbit))
|
107 |
+
continue
|
108 |
+
|
109 |
+
elif s.group() == 'u2':
|
110 |
+
lc = lc + 1
|
111 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
112 |
+
m_r = m_r[0].split(',')
|
113 |
+
for i, m in enumerate(m_r):
|
114 |
+
if 'pi' in m and 'e' in m:
|
115 |
+
sp_str = m_r[i].split('e-')
|
116 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
117 |
+
elif 'pi' in m:
|
118 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
119 |
+
else:
|
120 |
+
m_r[i] = '0'
|
121 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
122 |
+
t_qbit = int(m_i[0].strip('[]'))
|
123 |
+
f.write('prog.apply(U2(np.pi*{}, np.pi*{}), qbits[{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
124 |
+
continue
|
125 |
+
|
126 |
+
elif s.group() == 'u3':
|
127 |
+
lc = lc + 1
|
128 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
129 |
+
m_r = m_r[0].split(',')
|
130 |
+
for i, m in enumerate(m_r):
|
131 |
+
if 'pi' in m and 'e' in m:
|
132 |
+
sp_str = m_r[i].split('e-')
|
133 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
134 |
+
elif 'pi' in m:
|
135 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
136 |
+
else:
|
137 |
+
m_r[i] = '0'
|
138 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
139 |
+
t_qbit = int(m_i[0].strip('[]'))
|
140 |
+
f.write('prog.apply(U3(np.pi*{}, np.pi*{}, np.pi*{}), qbits[{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]), float(m_r[2][0]), t_qbit))
|
141 |
+
continue
|
142 |
+
|
143 |
+
|
144 |
+
# f.write('qpu = get_default_qpu()\n')
|
145 |
+
f.write('qpu = PyLinalg()\n')
|
146 |
+
f.write('circuit = prog.to_circ()\n')
|
147 |
+
f.write('job = circuit.to_job()\n')
|
148 |
+
f.write('result = qpu.submit(job)\n')
|
149 |
+
# f.write('f_state = device.run(qc).result().values[0]\n')
|
150 |
+
|
151 |
+
f.write('t_e = perf_counter()\n')
|
152 |
+
f.write('t_ep = process_time()\n')
|
153 |
+
f.write('print(t_e - t_s)\n')
|
154 |
+
f.write('print(t_ep - t_sp)\n')
|
155 |
+
|
156 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
157 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
158 |
+
|
159 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
160 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
161 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
162 |
+
print(int(lc_res) == lc)
|
163 |
+
# print(lc)
|
164 |
+
|
165 |
+
# parse_qasm_to_package_gen(12, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "n12_qulacs.py")
|
166 |
+
|
167 |
+
task = 'qft'
|
168 |
+
sim_pack = 'myqlm'
|
169 |
+
com_cap = 'mt'
|
170 |
+
prec = 'dp'
|
171 |
+
|
172 |
+
for N in range(6, 38, 2):
|
173 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
174 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
175 |
+
|
176 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
177 |
+
|
178 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
179 |
+
|
180 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
181 |
+
parse_qasm_to_package_gen(N, input_file, output_file, save_file_path)
|
182 |
+
|
183 |
+
|
184 |
+
"""
|
185 |
+
for N in range(12, 42, 2):
|
186 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
187 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
188 |
+
|
189 |
+
output_file_path = '/data/user/gangap_a/rqc/qulacs/qulacs_run_files/'
|
190 |
+
output_file = output_file_path + 'qulacs_rqc_n{}.py'.format(N)
|
191 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
192 |
+
|
193 |
+
for N in range(41, 51):
|
194 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
195 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
196 |
+
|
197 |
+
output_file_path = '/data/user/gangap_a/rqc/qulacs/qulacs_run_files/'
|
198 |
+
output_file = output_file_path + 'qulacs_rqc_n{}.py'.format(N)
|
199 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
200 |
+
|
201 |
+
"""
|
qasm_parser/pennylane/qasm_pennylane_gpu_parser.py
ADDED
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, precision, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
f.write('from time import process_time, perf_counter\n')
|
12 |
+
f.write('import numpy as np\n\n')
|
13 |
+
f.write('import pennylane as qml\n')
|
14 |
+
f.write('from pennylane import numpy as np\n')
|
15 |
+
|
16 |
+
f.write('def U2(p, l):\n')
|
17 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
18 |
+
f.write(' return mat\n\n')
|
19 |
+
|
20 |
+
f.write('def U3(t, p, l):\n')
|
21 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
22 |
+
f.write(' return mat\n\n')
|
23 |
+
|
24 |
+
f.write('dev = qml.device("lightning.gpu", shots=None, wires={}, c_dtype={})\n'.format(N, precision))
|
25 |
+
f.write('@qml.qnode(dev)\n')
|
26 |
+
f.write('def run_rqc():\n')
|
27 |
+
|
28 |
+
with open(input_filename, "r") as ifile:
|
29 |
+
lines = ifile.readlines()
|
30 |
+
|
31 |
+
lc = 0
|
32 |
+
cirq_c = 0
|
33 |
+
for line in lines:
|
34 |
+
|
35 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
36 |
+
|
37 |
+
if s is None:
|
38 |
+
continue
|
39 |
+
|
40 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
41 |
+
lc = lc + 1
|
42 |
+
f.write('\t# {}\n'.format(s.group()))
|
43 |
+
continue
|
44 |
+
|
45 |
+
elif s.group() == 'cirq':
|
46 |
+
lc = lc + 1
|
47 |
+
f.write('\t# {} {}\n'.format(s.group(), cirq_c))
|
48 |
+
cirq_c = cirq_c + 1
|
49 |
+
continue
|
50 |
+
|
51 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z':
|
52 |
+
lc = lc + 1
|
53 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
54 |
+
t_qbit = int(m_i[0].strip('[]'))
|
55 |
+
f.write('\tqml.Pauli{}(wires={})\n'.format(s.group().upper(), t_qbit))
|
56 |
+
continue
|
57 |
+
|
58 |
+
elif s.group() == 'h':
|
59 |
+
lc = lc + 1
|
60 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
61 |
+
t_qbit = int(m_i[0].strip('[]'))
|
62 |
+
f.write('\tqml.Hadamard(wires={})\n'.format(t_qbit))
|
63 |
+
continue
|
64 |
+
|
65 |
+
elif s.group() == 's':
|
66 |
+
lc = lc + 1
|
67 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
68 |
+
t_qbit = int(m_i[0].strip('[]'))
|
69 |
+
f.write('\tqml.S(wires={})\n'.format(t_qbit))
|
70 |
+
continue
|
71 |
+
|
72 |
+
elif s.group() == 'sx':
|
73 |
+
lc = lc + 1
|
74 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
75 |
+
t_qbit = int(m_i[0].strip('[]'))
|
76 |
+
f.write('\tqml.SX(wires={})\n'.format(t_qbit))
|
77 |
+
|
78 |
+
elif s.group() == 'sxdg':
|
79 |
+
lc = lc + 1
|
80 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
81 |
+
t_qbit = int(m_i[0].strip('[]'))
|
82 |
+
f.write('\tqml.SX(wires={}).inv()\n'.format(t_qbit))
|
83 |
+
|
84 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
85 |
+
lc = lc + 1
|
86 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
87 |
+
# print(m_r)
|
88 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
89 |
+
sp_str = m_r[0].split('e-')
|
90 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
91 |
+
elif 'pi' in m_r[0]:
|
92 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
93 |
+
# print(m_r)
|
94 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
95 |
+
t_qbit = int(m_i[0].strip('[]'))
|
96 |
+
f.write('\tqml.{}(np.pi*{}, wires={})\n'.format(s.group().upper(), float(m_r[0][0]), t_qbit))
|
97 |
+
continue
|
98 |
+
|
99 |
+
elif s.group() == 'cx':
|
100 |
+
lc = lc + 1
|
101 |
+
match = re.findall(r'\[\d\d*\]', line)
|
102 |
+
c_qbit = int(match[0].strip('[]'))
|
103 |
+
t_qbit = int(match[1].strip('[]'))
|
104 |
+
# f.write('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit))
|
105 |
+
f.write('\tqml.CNOT(wires=[{}, {}])\n'.format(c_qbit, t_qbit))
|
106 |
+
continue
|
107 |
+
|
108 |
+
elif s.group() == 'u2':
|
109 |
+
lc = lc + 1
|
110 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
111 |
+
m_r = m_r[0].split(',')
|
112 |
+
for i, m in enumerate(m_r):
|
113 |
+
if 'pi' in m and 'e' in m:
|
114 |
+
sp_str = m_r[i].split('e-')
|
115 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
116 |
+
elif 'pi' in m:
|
117 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
118 |
+
else:
|
119 |
+
m_r[i] = '0'
|
120 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
121 |
+
t_qbit = int(m_i[0].strip('[]'))
|
122 |
+
f.write('\tqml.QubitUnitary(U2(np.pi*{}, np.pi*{}), wires={})\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
123 |
+
continue
|
124 |
+
|
125 |
+
elif s.group() == 'u3':
|
126 |
+
lc = lc + 1
|
127 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
128 |
+
m_r = m_r[0].split(',')
|
129 |
+
for i, m in enumerate(m_r):
|
130 |
+
if 'pi' in m and 'e' in m:
|
131 |
+
sp_str = m_r[i].split('e-')
|
132 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
133 |
+
elif 'pi' in m:
|
134 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
135 |
+
else:
|
136 |
+
m_r[i] = '0'
|
137 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
138 |
+
t_qbit = int(m_i[0].strip('[]'))
|
139 |
+
f.write('\tqml.QubitUnitary(U3(np.pi*{}, np.pi*{}, np.pi*{}), wires={})\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
140 |
+
continue
|
141 |
+
|
142 |
+
f.write('\treturn qml.state()\n')
|
143 |
+
|
144 |
+
f.write('t_sp = process_time()\n')
|
145 |
+
f.write('t_s = perf_counter()\n')
|
146 |
+
f.write('fs=run_rqc()\n')
|
147 |
+
f.write('t_e = perf_counter()\n')
|
148 |
+
f.write('t_ep = process_time()\n')
|
149 |
+
f.write('print(t_e - t_s)\n')
|
150 |
+
f.write('print(t_ep - t_sp)\n')
|
151 |
+
|
152 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
153 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
154 |
+
|
155 |
+
# f.write('print(result.final_state_vector)\n')
|
156 |
+
|
157 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
158 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
159 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
160 |
+
print(int(lc_res) == lc)
|
161 |
+
# print(lc)
|
162 |
+
|
163 |
+
task = 'qft'
|
164 |
+
sim_pack = 'pennylane_l'
|
165 |
+
com_cap = 'gpu'
|
166 |
+
prec = 'dp'
|
167 |
+
|
168 |
+
for N in range(6, 40, 2):
|
169 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
170 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
171 |
+
|
172 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
173 |
+
|
174 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
175 |
+
|
176 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
177 |
+
parse_qasm_to_package_gen(N, 'np.complex128', input_file, output_file, save_file_path)
|
178 |
+
|
179 |
+
|
180 |
+
# parse_qasm_to_package_gen(12, "../qasm_test/qasm_rqc_test.qasm", "test_cirq.py")
|
181 |
+
# parse_qasm_to_package_gen(12, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_pennylane.py")
|
182 |
+
r"""
|
183 |
+
for N in range(12, 42, 2):
|
184 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
185 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
186 |
+
|
187 |
+
output_file_path = '/data/user/gangap_a/rqc/pennylane/pennylane_gpu_sp_run_files/'
|
188 |
+
output_file = output_file_path + 'pennylane_rqc_n{}.py'.format(N)
|
189 |
+
parse_qasm_to_package_gen(N, 'np.complex64', input_file, output_file)
|
190 |
+
|
191 |
+
for N in range(41, 51):
|
192 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
193 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
194 |
+
|
195 |
+
output_file_path = '/data/user/gangap_a/rqc/pennylane/pennylane_gpu_sp_run_files/'
|
196 |
+
output_file = output_file_path + 'pennylane_rqc_n{}.py'.format(N)
|
197 |
+
parse_qasm_to_package_gen(N, 'np.complex64', input_file, output_file)
|
198 |
+
"""
|
qasm_parser/pennylane/qasm_pennylane_l_parser.py
ADDED
@@ -0,0 +1,185 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, precision, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
f.write('from time import process_time, perf_counter\n')
|
12 |
+
f.write('import numpy as np\n\n')
|
13 |
+
f.write('import pennylane as qml\n')
|
14 |
+
f.write('from pennylane import numpy as np\n')
|
15 |
+
|
16 |
+
f.write('def U2(p, l):\n')
|
17 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
18 |
+
f.write(' return mat\n\n')
|
19 |
+
|
20 |
+
f.write('def U3(t, p, l):\n')
|
21 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
22 |
+
f.write(' return mat\n\n')
|
23 |
+
|
24 |
+
if precision == 'single':
|
25 |
+
f.write('dev = qml.device("lightning.qubit", shots=None, wires={}, c_dtype=np.complex64)\n'.format(N))
|
26 |
+
elif precision == 'double':
|
27 |
+
f.write('dev = qml.device("lightning.qubit", shots=None, wires={}, c_dtype=np.complex128)\n'.format(N))
|
28 |
+
|
29 |
+
f.write('@qml.qnode(dev)\n')
|
30 |
+
f.write('def run_rqc():\n')
|
31 |
+
|
32 |
+
with open(input_filename, "r") as ifile:
|
33 |
+
lines = ifile.readlines()
|
34 |
+
|
35 |
+
lc = 0
|
36 |
+
cirq_c = 0
|
37 |
+
for line in lines:
|
38 |
+
|
39 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
40 |
+
|
41 |
+
if s is None:
|
42 |
+
continue
|
43 |
+
|
44 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
45 |
+
lc = lc + 1
|
46 |
+
f.write('\t# {}\n'.format(s.group()))
|
47 |
+
continue
|
48 |
+
|
49 |
+
elif s.group() == 'cirq':
|
50 |
+
lc = lc + 1
|
51 |
+
f.write('\t# {} {}\n'.format(s.group(), cirq_c))
|
52 |
+
cirq_c = cirq_c + 1
|
53 |
+
continue
|
54 |
+
|
55 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z':
|
56 |
+
lc = lc + 1
|
57 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
58 |
+
t_qbit = int(m_i[0].strip('[]'))
|
59 |
+
f.write('\tqml.Pauli{}(wires={})\n'.format(s.group().upper(), t_qbit))
|
60 |
+
continue
|
61 |
+
|
62 |
+
elif s.group() == 'h':
|
63 |
+
lc = lc + 1
|
64 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
65 |
+
t_qbit = int(m_i[0].strip('[]'))
|
66 |
+
f.write('\tqml.Hadamard(wires={})\n'.format(t_qbit))
|
67 |
+
continue
|
68 |
+
|
69 |
+
|
70 |
+
elif s.group() == 's':
|
71 |
+
lc = lc + 1
|
72 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
73 |
+
t_qbit = int(m_i[0].strip('[]'))
|
74 |
+
f.write('\tqml.S(wires={})\n'.format(t_qbit))
|
75 |
+
continue
|
76 |
+
|
77 |
+
elif s.group() == 'sx':
|
78 |
+
lc = lc + 1
|
79 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
80 |
+
t_qbit = int(m_i[0].strip('[]'))
|
81 |
+
f.write('\tqml.SX(wires={})\n'.format(t_qbit))
|
82 |
+
|
83 |
+
elif s.group() == 'sxdg':
|
84 |
+
lc = lc + 1
|
85 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
86 |
+
t_qbit = int(m_i[0].strip('[]'))
|
87 |
+
f.write('\tqml.SX(wires={}).inv()\n'.format(t_qbit))
|
88 |
+
|
89 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
90 |
+
lc = lc + 1
|
91 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
92 |
+
# print(m_r)
|
93 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
94 |
+
sp_str = m_r[0].split('e-')
|
95 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
96 |
+
elif 'pi' in m_r[0]:
|
97 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
98 |
+
# print(m_r)
|
99 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
100 |
+
t_qbit = int(m_i[0].strip('[]'))
|
101 |
+
f.write('\tqml.{}(np.pi*{}, wires={})\n'.format(s.group().upper(), float(m_r[0][0]), t_qbit))
|
102 |
+
continue
|
103 |
+
|
104 |
+
elif s.group() == 'cx':
|
105 |
+
lc = lc + 1
|
106 |
+
match = re.findall(r'\[\d\d*\]', line)
|
107 |
+
c_qbit = int(match[0].strip('[]'))
|
108 |
+
t_qbit = int(match[1].strip('[]'))
|
109 |
+
# f.write('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit))
|
110 |
+
f.write('\tqml.CNOT(wires=[{}, {}])\n'.format(c_qbit, t_qbit))
|
111 |
+
continue
|
112 |
+
|
113 |
+
elif s.group() == 'u2':
|
114 |
+
lc = lc + 1
|
115 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
116 |
+
m_r = m_r[0].split(',')
|
117 |
+
for i, m in enumerate(m_r):
|
118 |
+
if 'pi' in m and 'e' in m:
|
119 |
+
sp_str = m_r[i].split('e-')
|
120 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
121 |
+
elif 'pi' in m:
|
122 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
123 |
+
else:
|
124 |
+
m_r[i] = '0'
|
125 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
126 |
+
t_qbit = int(m_i[0].strip('[]'))
|
127 |
+
f.write('\tqml.QubitUnitary(U2(np.pi*{}, np.pi*{}), wires={})\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
128 |
+
continue
|
129 |
+
|
130 |
+
elif s.group() == 'u3':
|
131 |
+
lc = lc + 1
|
132 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
133 |
+
m_r = m_r[0].split(',')
|
134 |
+
for i, m in enumerate(m_r):
|
135 |
+
if 'pi' in m and 'e' in m:
|
136 |
+
sp_str = m_r[i].split('e-')
|
137 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
138 |
+
elif 'pi' in m:
|
139 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
140 |
+
else:
|
141 |
+
m_r[i] = '0'
|
142 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
143 |
+
t_qbit = int(m_i[0].strip('[]'))
|
144 |
+
f.write('\tqml.QubitUnitary(U3(np.pi*{}, np.pi*{}, np.pi*{}), wires={})\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
145 |
+
continue
|
146 |
+
|
147 |
+
f.write('\treturn qml.state()\n')
|
148 |
+
|
149 |
+
f.write('t_sp = process_time()\n')
|
150 |
+
f.write('t_s = perf_counter()\n')
|
151 |
+
f.write('fs=run_rqc()\n')
|
152 |
+
f.write('t_e = perf_counter()\n')
|
153 |
+
f.write('t_ep = process_time()\n')
|
154 |
+
f.write('print(t_e - t_s)\n')
|
155 |
+
f.write('print(t_ep - t_sp)\n')
|
156 |
+
|
157 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
158 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
159 |
+
|
160 |
+
# f.write('print(result.final_state_vector)\n')
|
161 |
+
|
162 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
163 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
164 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
165 |
+
print(int(lc_res) == lc)
|
166 |
+
# print(lc)
|
167 |
+
|
168 |
+
# parse_qasm_to_package_gen(12, "../qasm_test/qasm_rqc_test.qasm", "test_cirq.py")
|
169 |
+
# parse_qasm_to_package_gen(12, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_pennylane_l.py")
|
170 |
+
|
171 |
+
task ='qft'
|
172 |
+
sim_pack = 'pennylane_l'
|
173 |
+
com_cap = 'mt'
|
174 |
+
prec = 'dp'
|
175 |
+
|
176 |
+
for N in range(6, 40, 2):
|
177 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
178 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
179 |
+
|
180 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
181 |
+
|
182 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
183 |
+
|
184 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
185 |
+
parse_qasm_to_package_gen(N, 'double', input_file, output_file, save_file_path)
|
qasm_parser/pennylane/qasm_pennylane_parser.py
ADDED
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, precision, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
f.write('from time import process_time, perf_counter\n')
|
12 |
+
f.write('import numpy as np\n\n')
|
13 |
+
f.write('import pennylane as qml\n')
|
14 |
+
f.write('from pennylane import numpy as np\n')
|
15 |
+
|
16 |
+
f.write('def U2(p, l):\n')
|
17 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
18 |
+
f.write(' return mat\n\n')
|
19 |
+
|
20 |
+
f.write('def U3(t, p, l):\n')
|
21 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
22 |
+
f.write(' return mat\n\n')
|
23 |
+
|
24 |
+
if precision == 'single':
|
25 |
+
f.write('dev = qml.device("default.qubit", shots=None, wires={}, r_dtype=np.float32, c_dtype=np.complex64)\n'.format(N))
|
26 |
+
elif precision == 'double':
|
27 |
+
f.write('dev = qml.device("default.qubit", shots=None, wires={}, r_dtype=np.float64, c_dtype=np.complex128)\n'.format(N))
|
28 |
+
|
29 |
+
f.write('@qml.qnode(dev)\n')
|
30 |
+
f.write('def run_rqc():\n')
|
31 |
+
|
32 |
+
with open(input_filename, "r") as ifile:
|
33 |
+
lines = ifile.readlines()
|
34 |
+
|
35 |
+
lc = 0
|
36 |
+
cirq_c = 0
|
37 |
+
for line in lines:
|
38 |
+
|
39 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
40 |
+
|
41 |
+
if s is None:
|
42 |
+
continue
|
43 |
+
|
44 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
45 |
+
lc = lc + 1
|
46 |
+
f.write('\t# {}\n'.format(s.group()))
|
47 |
+
continue
|
48 |
+
|
49 |
+
elif s.group() == 'cirq':
|
50 |
+
lc = lc + 1
|
51 |
+
f.write('\t# {} {}\n'.format(s.group(), cirq_c))
|
52 |
+
cirq_c = cirq_c + 1
|
53 |
+
continue
|
54 |
+
|
55 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z':
|
56 |
+
lc = lc + 1
|
57 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
58 |
+
t_qbit = int(m_i[0].strip('[]'))
|
59 |
+
f.write('\tqml.Pauli{}(wires={})\n'.format(s.group().upper(), t_qbit))
|
60 |
+
continue
|
61 |
+
|
62 |
+
elif s.group() == 'h':
|
63 |
+
lc = lc + 1
|
64 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
65 |
+
t_qbit = int(m_i[0].strip('[]'))
|
66 |
+
f.write('\tqml.Hadamard(wires={})\n'.format(t_qbit))
|
67 |
+
continue
|
68 |
+
|
69 |
+
elif s.group() == 's':
|
70 |
+
lc = lc + 1
|
71 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
72 |
+
t_qbit = int(m_i[0].strip('[]'))
|
73 |
+
f.write('\tqml.S(wires={})\n'.format(t_qbit))
|
74 |
+
continue
|
75 |
+
|
76 |
+
elif s.group() == 'sx':
|
77 |
+
lc = lc + 1
|
78 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
79 |
+
t_qbit = int(m_i[0].strip('[]'))
|
80 |
+
f.write('\tqml.SX(wires={})\n'.format(t_qbit))
|
81 |
+
|
82 |
+
elif s.group() == 'sxdg':
|
83 |
+
lc = lc + 1
|
84 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
85 |
+
t_qbit = int(m_i[0].strip('[]'))
|
86 |
+
f.write('\tqml.SX(wires={}).inv()\n'.format(t_qbit))
|
87 |
+
|
88 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
89 |
+
lc = lc + 1
|
90 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
91 |
+
# print(m_r)
|
92 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
93 |
+
sp_str = m_r[0].split('e-')
|
94 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
95 |
+
elif 'pi' in m_r[0]:
|
96 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
97 |
+
# print(m_r)
|
98 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
99 |
+
t_qbit = int(m_i[0].strip('[]'))
|
100 |
+
f.write('\tqml.{}(np.pi*{}, wires={})\n'.format(s.group().upper(), float(m_r[0][0]), t_qbit))
|
101 |
+
continue
|
102 |
+
|
103 |
+
elif s.group() == 'cx':
|
104 |
+
lc = lc + 1
|
105 |
+
match = re.findall(r'\[\d\d*\]', line)
|
106 |
+
c_qbit = int(match[0].strip('[]'))
|
107 |
+
t_qbit = int(match[1].strip('[]'))
|
108 |
+
# f.write('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit))
|
109 |
+
f.write('\tqml.CNOT(wires=[{}, {}])\n'.format(c_qbit, t_qbit))
|
110 |
+
continue
|
111 |
+
|
112 |
+
elif s.group() == 'u2':
|
113 |
+
lc = lc + 1
|
114 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
115 |
+
m_r = m_r[0].split(',')
|
116 |
+
for i, m in enumerate(m_r):
|
117 |
+
if 'pi' in m and 'e' in m:
|
118 |
+
sp_str = m_r[i].split('e-')
|
119 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
120 |
+
elif 'pi' in m:
|
121 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
122 |
+
else:
|
123 |
+
m_r[i] = '0'
|
124 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
125 |
+
t_qbit = int(m_i[0].strip('[]'))
|
126 |
+
f.write('\tqml.QubitUnitary(U2(np.pi*{}, np.pi*{}), wires={})\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
127 |
+
continue
|
128 |
+
|
129 |
+
elif s.group() == 'u3':
|
130 |
+
lc = lc + 1
|
131 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
132 |
+
m_r = m_r[0].split(',')
|
133 |
+
for i, m in enumerate(m_r):
|
134 |
+
if 'pi' in m and 'e' in m:
|
135 |
+
sp_str = m_r[i].split('e-')
|
136 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
137 |
+
elif 'pi' in m:
|
138 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
139 |
+
else:
|
140 |
+
m_r[i] = '0'
|
141 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
142 |
+
t_qbit = int(m_i[0].strip('[]'))
|
143 |
+
f.write('\tqml.QubitUnitary(U3(np.pi*{}, np.pi*{}, np.pi*{}), wires={})\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
144 |
+
continue
|
145 |
+
|
146 |
+
f.write('\treturn qml.state()\n')
|
147 |
+
|
148 |
+
f.write('t_sp = process_time()\n')
|
149 |
+
f.write('t_s = perf_counter()\n')
|
150 |
+
f.write('fs=run_rqc()\n')
|
151 |
+
f.write('t_e = perf_counter()\n')
|
152 |
+
f.write('t_ep = process_time()\n')
|
153 |
+
f.write('print(t_e - t_s)\n')
|
154 |
+
f.write('print(t_ep - t_sp)\n')
|
155 |
+
|
156 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
157 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
158 |
+
|
159 |
+
# f.write('print(result.final_state_vector)\n')
|
160 |
+
|
161 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
162 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
163 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
164 |
+
print(int(lc_res) == lc)
|
165 |
+
# print(lc)
|
166 |
+
|
167 |
+
# parse_qasm_to_package_gen(12, "../qasm_test/qasm_rqc_test.qasm", "test_cirq.py")
|
168 |
+
# parse_qasm_to_package_gen(12, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_pennylane.py")
|
169 |
+
|
170 |
+
task = 'qft'
|
171 |
+
sim_pack = 'pennylane'
|
172 |
+
com_cap = 'st'
|
173 |
+
prec = 'sp'
|
174 |
+
|
175 |
+
for N in range(6, 40, 2):
|
176 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
177 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
178 |
+
|
179 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
180 |
+
|
181 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
182 |
+
|
183 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
184 |
+
parse_qasm_to_package_gen(N, 'single', input_file, output_file, save_file_path)
|
qasm_parser/projectq/qasm_projectq_mt_parser.py
ADDED
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
f.write('from time import process_time, perf_counter\n')
|
12 |
+
f.write('from projectq.backends import Simulator\n')
|
13 |
+
f.write('from projectq import MainEngine\n')
|
14 |
+
f.write('from projectq.meta import Compute, Control, Loop, Uncompute\n')
|
15 |
+
f.write('from projectq.ops import All, H, Measure, X, Z, Rx, Rz, Ry, CNOT, SGate, SqrtXGate, QubitOperator, MatrixGate, get_inverse\n')
|
16 |
+
f.write('import numpy as np\n\n')
|
17 |
+
|
18 |
+
f.write('def U2(p, l):\n')
|
19 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
20 |
+
f.write(' return MatrixGate(mat)\n\n')
|
21 |
+
|
22 |
+
f.write('def U3(t, p, l):\n')
|
23 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
24 |
+
f.write(' return MatrixGate(mat)\n\n')
|
25 |
+
|
26 |
+
f.write('t_sp = process_time()\n')
|
27 |
+
f.write('t_s = perf_counter()\n')
|
28 |
+
|
29 |
+
f.write('eng = MainEngine(backend=Simulator(gate_fusion=True))\n')
|
30 |
+
f.write('qureg = eng.allocate_qureg({})\n'.format(N))
|
31 |
+
|
32 |
+
with open(input_filename, "r") as ifile:
|
33 |
+
lines = ifile.readlines()
|
34 |
+
|
35 |
+
lc = 0
|
36 |
+
for line in lines:
|
37 |
+
|
38 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
39 |
+
|
40 |
+
if s is None:
|
41 |
+
continue
|
42 |
+
|
43 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits', 'cirq']:
|
44 |
+
lc = lc + 1
|
45 |
+
f.write('# {}\n'.format(s.group()))
|
46 |
+
continue
|
47 |
+
|
48 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z':
|
49 |
+
lc = lc + 1
|
50 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
51 |
+
t_qbit = int(m_i[0].strip('[]'))
|
52 |
+
f.write('{} | qureg[{}]\n'.format(s.group().upper(), t_qbit))
|
53 |
+
continue
|
54 |
+
|
55 |
+
elif s.group() == 'sx':
|
56 |
+
lc = lc + 1
|
57 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
58 |
+
t_qbit = int(m_i[0].strip('[]'))
|
59 |
+
f.write('SqrtXGate() | qureg[{}]\n'.format(t_qbit))
|
60 |
+
|
61 |
+
elif s.group() == 'sxdg':
|
62 |
+
lc = lc + 1
|
63 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
64 |
+
t_qbit = int(m_i[0].strip('[]'))
|
65 |
+
f.write('get_inverse(SqrtXGate()) | qureg[{}]\n'.format(t_qbit))
|
66 |
+
|
67 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
68 |
+
lc = lc + 1
|
69 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
70 |
+
# print(m_r)
|
71 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
72 |
+
sp_str = m_r[0].split('e-')
|
73 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
74 |
+
elif 'pi' in m_r[0]:
|
75 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
76 |
+
# print(m_r)
|
77 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
78 |
+
t_qbit = int(m_i[0].strip('[]'))
|
79 |
+
f.write('R{}(np.pi*{}) | qureg[{}]\n'.format(s.group()[1], float(m_r[0][0]), t_qbit))
|
80 |
+
continue
|
81 |
+
|
82 |
+
elif s.group() == "s":
|
83 |
+
lc = lc + 1
|
84 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
85 |
+
t_qbit = int(m_i[0].strip('[]'))
|
86 |
+
f.write('SGate() | qureg[{}]\n'.format(t_qbit))
|
87 |
+
continue
|
88 |
+
|
89 |
+
elif s.group() == 'cx':
|
90 |
+
lc = lc + 1
|
91 |
+
match = re.findall(r'\[\d\d*\]', line)
|
92 |
+
c_qbit = int(match[0].strip('[]'))
|
93 |
+
t_qbit = int(match[1].strip('[]'))
|
94 |
+
f.write('CNOT | (qureg[{}], qureg[{}])\n'.format(c_qbit, t_qbit))
|
95 |
+
continue
|
96 |
+
|
97 |
+
elif s.group() == 'u2':
|
98 |
+
lc = lc + 1
|
99 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
100 |
+
m_r = m_r[0].split(',')
|
101 |
+
for i, m in enumerate(m_r):
|
102 |
+
if 'pi' in m and 'e' in m:
|
103 |
+
sp_str = m_r[i].split('e-')
|
104 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
105 |
+
elif 'pi' in m:
|
106 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
107 |
+
else:
|
108 |
+
m_r[i] = '0'
|
109 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
110 |
+
t_qbit = int(m_i[0].strip('[]'))
|
111 |
+
f.write('U2(np.pi*{}, np.pi*{}) | qureg[{}]\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
112 |
+
continue
|
113 |
+
|
114 |
+
elif s.group() == 'u3':
|
115 |
+
lc = lc + 1
|
116 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
117 |
+
m_r = m_r[0].split(',')
|
118 |
+
for i, m in enumerate(m_r):
|
119 |
+
if 'pi' in m and 'e' in m:
|
120 |
+
sp_str = m_r[i].split('e-')
|
121 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
122 |
+
elif 'pi' in m:
|
123 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
124 |
+
else:
|
125 |
+
m_r[i] = '0'
|
126 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
127 |
+
t_qbit = int(m_i[0].strip('[]'))
|
128 |
+
f.write('U3(np.pi*{}, np.pi*{}, np.pi*{}) | qureg[{}]\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
129 |
+
continue
|
130 |
+
|
131 |
+
f.write('eng.flush()\n')
|
132 |
+
f.write('All(Measure) | qureg\n')
|
133 |
+
|
134 |
+
f.write('t_e = perf_counter()\n')
|
135 |
+
f.write('t_ep = process_time()\n')
|
136 |
+
f.write('print(t_e - t_s)\n')
|
137 |
+
f.write('print(t_ep - t_sp)\n')
|
138 |
+
|
139 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
140 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
141 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
142 |
+
print(int(lc_res) == lc)
|
143 |
+
# print(lc)
|
144 |
+
|
145 |
+
# parse_qasm_to_package_gen(12, "qasm_ex.qasm", "n12_projectq.py")
|
146 |
+
|
147 |
+
|
148 |
+
for N in range(12, 42, 2):
|
149 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
150 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
151 |
+
|
152 |
+
output_file_path = '/data/user/gangap_a/rqc/projectq/projectq_mt_run_files/'
|
153 |
+
output_file = output_file_path + 'projectq_rqc_n{}.py'.format(N)
|
154 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
155 |
+
|
156 |
+
for N in range(41, 51):
|
157 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
158 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
159 |
+
|
160 |
+
output_file_path = '/data/user/gangap_a/rqc/projectq/projectq_mt_run_files/'
|
161 |
+
output_file = output_file_path + 'projectq_rqc_n{}.py'.format(N)
|
162 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
163 |
+
|
qasm_parser/projectq/qasm_projectq_parser.py
ADDED
@@ -0,0 +1,185 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
f.write('from time import process_time, perf_counter\n')
|
12 |
+
f.write('from projectq.backends import Simulator\n')
|
13 |
+
f.write('from projectq import MainEngine\n')
|
14 |
+
f.write('from projectq.meta import Compute, Control, Loop, Uncompute\n')
|
15 |
+
f.write('from projectq.ops import All, H, Measure, X, Z, Rx, Rz, Ry, CNOT, SGate, SqrtXGate, QubitOperator, MatrixGate, get_inverse\n')
|
16 |
+
f.write('import numpy as np\n\n')
|
17 |
+
|
18 |
+
f.write('def U2(p, l):\n')
|
19 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
20 |
+
f.write(' return MatrixGate(mat)\n\n')
|
21 |
+
|
22 |
+
f.write('def U3(t, p, l):\n')
|
23 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
24 |
+
f.write(' return MatrixGate(mat)\n\n')
|
25 |
+
|
26 |
+
f.write('t_sp = process_time()\n')
|
27 |
+
f.write('t_s = perf_counter()\n')
|
28 |
+
|
29 |
+
# f.write('eng = MainEngine(backend=Simulator(gate_fusion=True))\n')
|
30 |
+
f.write('eng = MainEngine(backend=Simulator())\n')
|
31 |
+
f.write('qureg = eng.allocate_qureg({})\n'.format(N))
|
32 |
+
|
33 |
+
with open(input_filename, "r") as ifile:
|
34 |
+
lines = ifile.readlines()
|
35 |
+
|
36 |
+
lc = 0
|
37 |
+
for line in lines:
|
38 |
+
|
39 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
40 |
+
|
41 |
+
if s is None:
|
42 |
+
continue
|
43 |
+
|
44 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits', 'cirq']:
|
45 |
+
lc = lc + 1
|
46 |
+
f.write('# {}\n'.format(s.group()))
|
47 |
+
continue
|
48 |
+
|
49 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 'h':
|
50 |
+
lc = lc + 1
|
51 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
52 |
+
t_qbit = int(m_i[0].strip('[]'))
|
53 |
+
f.write('{} | qureg[{}]\n'.format(s.group().upper(), t_qbit))
|
54 |
+
continue
|
55 |
+
|
56 |
+
elif s.group() == 'sx':
|
57 |
+
lc = lc + 1
|
58 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
59 |
+
t_qbit = int(m_i[0].strip('[]'))
|
60 |
+
f.write('SqrtXGate() | qureg[{}]\n'.format(t_qbit))
|
61 |
+
|
62 |
+
elif s.group() == 'sxdg':
|
63 |
+
lc = lc + 1
|
64 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
65 |
+
t_qbit = int(m_i[0].strip('[]'))
|
66 |
+
f.write('get_inverse(SqrtXGate()) | qureg[{}]\n'.format(t_qbit))
|
67 |
+
|
68 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
69 |
+
lc = lc + 1
|
70 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
71 |
+
# print(m_r)
|
72 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
73 |
+
sp_str = m_r[0].split('e-')
|
74 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
75 |
+
elif 'pi' in m_r[0]:
|
76 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
77 |
+
# print(m_r)
|
78 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
79 |
+
t_qbit = int(m_i[0].strip('[]'))
|
80 |
+
f.write('R{}(np.pi*{}) | qureg[{}]\n'.format(s.group()[1], float(m_r[0][0]), t_qbit))
|
81 |
+
continue
|
82 |
+
|
83 |
+
elif s.group() == "s":
|
84 |
+
lc = lc + 1
|
85 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
86 |
+
t_qbit = int(m_i[0].strip('[]'))
|
87 |
+
f.write('SGate() | qureg[{}]\n'.format(t_qbit))
|
88 |
+
continue
|
89 |
+
|
90 |
+
elif s.group() == 'cx':
|
91 |
+
lc = lc + 1
|
92 |
+
match = re.findall(r'\[\d\d*\]', line)
|
93 |
+
c_qbit = int(match[0].strip('[]'))
|
94 |
+
t_qbit = int(match[1].strip('[]'))
|
95 |
+
f.write('CNOT | (qureg[{}], qureg[{}])\n'.format(c_qbit, t_qbit))
|
96 |
+
continue
|
97 |
+
|
98 |
+
elif s.group() == 'u2':
|
99 |
+
lc = lc + 1
|
100 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
101 |
+
m_r = m_r[0].split(',')
|
102 |
+
for i, m in enumerate(m_r):
|
103 |
+
if 'pi' in m and 'e' in m:
|
104 |
+
sp_str = m_r[i].split('e-')
|
105 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
106 |
+
elif 'pi' in m:
|
107 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
108 |
+
else:
|
109 |
+
m_r[i] = '0'
|
110 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
111 |
+
t_qbit = int(m_i[0].strip('[]'))
|
112 |
+
f.write('U2(np.pi*{}, np.pi*{}) | qureg[{}]\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
113 |
+
continue
|
114 |
+
|
115 |
+
elif s.group() == 'u3':
|
116 |
+
lc = lc + 1
|
117 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
118 |
+
m_r = m_r[0].split(',')
|
119 |
+
for i, m in enumerate(m_r):
|
120 |
+
if 'pi' in m and 'e' in m:
|
121 |
+
sp_str = m_r[i].split('e-')
|
122 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
123 |
+
elif 'pi' in m:
|
124 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
125 |
+
else:
|
126 |
+
m_r[i] = '0'
|
127 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
128 |
+
t_qbit = int(m_i[0].strip('[]'))
|
129 |
+
f.write('U3(np.pi*{}, np.pi*{}, np.pi*{}) | qureg[{}]\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
130 |
+
continue
|
131 |
+
|
132 |
+
f.write('eng.flush()\n')
|
133 |
+
f.write('All(Measure) | qureg\n')
|
134 |
+
|
135 |
+
f.write('t_e = perf_counter()\n')
|
136 |
+
f.write('t_ep = process_time()\n')
|
137 |
+
f.write('print(t_e - t_s)\n')
|
138 |
+
f.write('print(t_ep - t_sp)\n')
|
139 |
+
|
140 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
141 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
142 |
+
|
143 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
144 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
145 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
146 |
+
print(int(lc_res) == lc)
|
147 |
+
# print(lc)
|
148 |
+
|
149 |
+
# parse_qasm_to_package_gen(12, "qasm_ex.qasm", "n12_projectq.py")
|
150 |
+
|
151 |
+
|
152 |
+
task = 'qft'
|
153 |
+
sim_pack = 'projectq'
|
154 |
+
com_cap = 'mt'
|
155 |
+
prec = 'dp'
|
156 |
+
|
157 |
+
for N in range(6, 40, 2):
|
158 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
159 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
160 |
+
|
161 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
162 |
+
|
163 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
164 |
+
|
165 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
166 |
+
parse_qasm_to_package_gen(N, input_file, output_file, save_file_path)
|
167 |
+
|
168 |
+
r"""
|
169 |
+
for N in range(12, 42, 2):
|
170 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
171 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
172 |
+
|
173 |
+
output_file_path = '/data/user/gangap_a/rqc/projectq/projectq_run_files/'
|
174 |
+
output_file = output_file_path + 'projectq_rqc_n{}.py'.format(N)
|
175 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
176 |
+
|
177 |
+
for N in range(41, 51):
|
178 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
179 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
180 |
+
|
181 |
+
output_file_path = '/data/user/gangap_a/rqc/projectq/projectq_run_files/'
|
182 |
+
output_file = output_file_path + 'projectq_rqc_n{}.py'.format(N)
|
183 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
184 |
+
|
185 |
+
"""
|
qasm_parser/qcgpu/qasm_gcgpu_parser.py
ADDED
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
|
11 |
+
f.write('from time import process_time\n')
|
12 |
+
f.write('import qcgpu\n')
|
13 |
+
f.write('import numpy as np\n')
|
14 |
+
f.write('import os\n')
|
15 |
+
f.write('from time import process_time, perf_counter\n')
|
16 |
+
|
17 |
+
f.write('def SqrtX():\n')
|
18 |
+
f.write(' mat = [[(1+1j)/2., (1-1j)/2.], [(1-1j)/2., (1+1j)/2.]]\n')
|
19 |
+
f.write(' return qcgpu.Gate(mat)\n\n')
|
20 |
+
|
21 |
+
f.write('def SqrtXdg():\n')
|
22 |
+
f.write(' mat = [[(1-1j)/2., (1+1j)/2.], [(1+1j)/2., (1-1j)/2.]]\n')
|
23 |
+
f.write(' return qcgpu.Gate(mat)\n\n')
|
24 |
+
|
25 |
+
f.write('def Rx(t):\n')
|
26 |
+
f.write(' mat = [[np.cos(0.5*t), -1j*np.sin(0.5*t)], [-1j*np.sin(0.5*t), np.cos(0.5*t)]]\n')
|
27 |
+
f.write(' return qcgpu.Gate(mat)\n\n')
|
28 |
+
|
29 |
+
f.write('def Ry(t):\n')
|
30 |
+
f.write(' mat = [[np.cos(0.5*t), -np.sin(0.5*t)], [np.sin(0.5*t), np.cos(0.5*t)]]\n')
|
31 |
+
f.write(' return qcgpu.Gate(mat)\n\n')
|
32 |
+
|
33 |
+
f.write('def Rz(t):\n')
|
34 |
+
f.write(' mat = [[np.exp(-0.5j*t), 0.], [0., np.exp(0.5j*t)]]\n')
|
35 |
+
f.write(' return qcgpu.Gate(mat)\n\n')
|
36 |
+
|
37 |
+
f.write('def U2(p, l):\n')
|
38 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
39 |
+
f.write(' return qcgpu.Gate(mat)\n\n')
|
40 |
+
|
41 |
+
f.write('def U3(t, p, l):\n')
|
42 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
43 |
+
f.write(' return qcgpu.Gate(mat)\n\n')
|
44 |
+
|
45 |
+
f.write('t_sp = process_time()\n')
|
46 |
+
f.write('t_s = perf_counter()\n')
|
47 |
+
|
48 |
+
f.write('reg = qcgpu.State({})\n'.format(N))
|
49 |
+
|
50 |
+
with open(input_filename, "r") as ifile:
|
51 |
+
lines = ifile.readlines()
|
52 |
+
|
53 |
+
lc = 0
|
54 |
+
cirq_c = 0
|
55 |
+
for line in lines:
|
56 |
+
|
57 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
58 |
+
|
59 |
+
if s is None:
|
60 |
+
continue
|
61 |
+
|
62 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
63 |
+
lc = lc + 1
|
64 |
+
f.write('# {}\n'.format(s.group()))
|
65 |
+
continue
|
66 |
+
|
67 |
+
elif s.group() == 'cirq':
|
68 |
+
lc = lc + 1
|
69 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
70 |
+
cirq_c = cirq_c + 1
|
71 |
+
continue
|
72 |
+
|
73 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'h':
|
74 |
+
lc = lc + 1
|
75 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
76 |
+
t_qbit = int(m_i[0].strip('[]'))
|
77 |
+
f.write('reg.{}({})\n'.format(s.group(), t_qbit))
|
78 |
+
continue
|
79 |
+
|
80 |
+
elif s.group() == 'sx':
|
81 |
+
lc = lc + 1
|
82 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
83 |
+
t_qbit = int(m_i[0].strip('[]'))
|
84 |
+
f.write('reg.apply_gate(SqrtX(), {})\n'.format(t_qbit))
|
85 |
+
|
86 |
+
elif s.group() == 'sxdg':
|
87 |
+
lc = lc + 1
|
88 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
89 |
+
t_qbit = int(m_i[0].strip('[]'))
|
90 |
+
f.write('reg.apply_gate(SqrtXdg(), {})\n'.format(t_qbit))
|
91 |
+
continue
|
92 |
+
|
93 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
94 |
+
lc = lc + 1
|
95 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
96 |
+
# print(m_r)
|
97 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
98 |
+
sp_str = m_r[0].split('e-')
|
99 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
100 |
+
elif 'pi' in m_r[0]:
|
101 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
102 |
+
# print(m_r)
|
103 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
104 |
+
t_qbit = int(m_i[0].strip('[]'))
|
105 |
+
f.write('reg.apply_gate(R{}(np.pi*{}), {})\n'.format(s.group()[1], float(m_r[0][0]), t_qbit))
|
106 |
+
continue
|
107 |
+
|
108 |
+
elif s.group() == 'cx':
|
109 |
+
lc = lc + 1
|
110 |
+
match = re.findall(r'\[\d\d*\]', line)
|
111 |
+
c_qbit = int(match[0].strip('[]'))
|
112 |
+
t_qbit = int(match[1].strip('[]'))
|
113 |
+
f.write('reg.cx({}, {})\n'.format(c_qbit, t_qbit))
|
114 |
+
continue
|
115 |
+
|
116 |
+
elif s.group() == 'u2':
|
117 |
+
lc = lc + 1
|
118 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
119 |
+
m_r = m_r[0].split(',')
|
120 |
+
for i, m in enumerate(m_r):
|
121 |
+
if 'pi' in m and 'e' in m:
|
122 |
+
sp_str = m_r[i].split('e-')
|
123 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
124 |
+
elif 'pi' in m:
|
125 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
126 |
+
else:
|
127 |
+
m_r[i] = '0'
|
128 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
129 |
+
t_qbit = int(m_i[0].strip('[]'))
|
130 |
+
f.write('reg.apply_gate(U2(np.pi*{}, np.pi*{}), {})\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
131 |
+
continue
|
132 |
+
|
133 |
+
elif s.group() == 'u3':
|
134 |
+
lc = lc + 1
|
135 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
136 |
+
m_r = m_r[0].split(',')
|
137 |
+
for i, m in enumerate(m_r):
|
138 |
+
if 'pi' in m and 'e' in m:
|
139 |
+
sp_str = m_r[i].split('e-')
|
140 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
141 |
+
elif 'pi' in m:
|
142 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
143 |
+
else:
|
144 |
+
m_r[i] = '0'
|
145 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
146 |
+
t_qbit = int(m_i[0].strip('[]'))
|
147 |
+
f.write('reg.apply_gate(U3(np.pi*{}, np.pi*{}, np.pi*{}), {})\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
148 |
+
continue
|
149 |
+
|
150 |
+
f.write('t_e = perf_counter()\n')
|
151 |
+
f.write('t_ep = process_time()\n')
|
152 |
+
f.write('print(t_e - t_s)\n')
|
153 |
+
f.write('print(t_ep - t_sp)\n')
|
154 |
+
|
155 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
156 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
157 |
+
|
158 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
159 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
160 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
161 |
+
print(int(lc_res) == lc)
|
162 |
+
# print(lc)
|
163 |
+
|
164 |
+
# parse_qasm_to_package_gen(12, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "n12_qulacs.py")
|
165 |
+
|
166 |
+
task = 'qft'
|
167 |
+
sim_pack = 'qcgpu'
|
168 |
+
com_cap = 'gpu'
|
169 |
+
prec = 'sp'
|
170 |
+
|
171 |
+
for N in range(6, 40, 2):
|
172 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
173 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
174 |
+
|
175 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
176 |
+
|
177 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
178 |
+
|
179 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
180 |
+
parse_qasm_to_package_gen(N, input_file, output_file, save_file_path)
|
181 |
+
|
182 |
+
r"""
|
183 |
+
for N in range(12, 42, 2):
|
184 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
185 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
186 |
+
|
187 |
+
output_file_path = '/data/user/gangap_a/rqc/qcgpu/qcgpu_run_files/'
|
188 |
+
output_file = output_file_path + 'qcgpu_rqc_n{}.py'.format(N)
|
189 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
190 |
+
|
191 |
+
for N in range(41, 51):
|
192 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
193 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
194 |
+
|
195 |
+
output_file_path = '/data/user/gangap_a/rqc/qcgpu/qcgpu_run_files/'
|
196 |
+
output_file = output_file_path + 'qcgpu_rqc_n{}.py'.format(N)
|
197 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
198 |
+
"""
|
qasm_parser/qibo/qasm_qibo_parser.py
ADDED
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, precision, cpu_threads, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import numpy as np\n')
|
11 |
+
f.write('import qibo as qb\n')
|
12 |
+
|
13 |
+
if cpu_threads == 1:
|
14 |
+
f.write('qb.set_threads({})\n'.format(cpu_threads))
|
15 |
+
f.write('qb.set_backend("numpy")\n')
|
16 |
+
|
17 |
+
f.write('qb.set_precision(\'{}\')\n'.format(precision))
|
18 |
+
|
19 |
+
f.write('import os\n')
|
20 |
+
f.write('from time import process_time, perf_counter\n')
|
21 |
+
|
22 |
+
f.write('t_sp = process_time()\n')
|
23 |
+
f.write('t_s = perf_counter()\n')
|
24 |
+
|
25 |
+
f.write('cir = qb.models.Circuit({})\n'.format(N))
|
26 |
+
|
27 |
+
with open(input_filename, "r") as ifile:
|
28 |
+
lines = ifile.readlines()
|
29 |
+
|
30 |
+
lc = 0
|
31 |
+
cirq_c = 0
|
32 |
+
for line in lines:
|
33 |
+
|
34 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
35 |
+
|
36 |
+
if s is None:
|
37 |
+
continue
|
38 |
+
|
39 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
40 |
+
lc = lc + 1
|
41 |
+
f.write('# {}\n'.format(s.group()))
|
42 |
+
continue
|
43 |
+
|
44 |
+
elif s.group() == 'cirq':
|
45 |
+
lc = lc + 1
|
46 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
47 |
+
cirq_c = cirq_c + 1
|
48 |
+
continue
|
49 |
+
|
50 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'sx' or s.group() == 'sxdg' or s.group() == 'h':
|
51 |
+
lc = lc + 1
|
52 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
53 |
+
t_qbit = int(m_i[0].strip('[]'))
|
54 |
+
f.write('cir.add(qb.gates.{}({}))\n'.format(s.group().upper(), t_qbit))
|
55 |
+
continue
|
56 |
+
|
57 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
58 |
+
lc = lc + 1
|
59 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
60 |
+
# print(m_r)
|
61 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
62 |
+
sp_str = m_r[0].split('e-')
|
63 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
64 |
+
elif 'pi' in m_r[0]:
|
65 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
66 |
+
# print(m_r)
|
67 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
68 |
+
t_qbit = int(m_i[0].strip('[]'))
|
69 |
+
f.write('cir.add(qb.gates.{}({}, theta=np.pi*{}, trainable=False))\n'.format(s.group().upper(), t_qbit, float(m_r[0][0])))
|
70 |
+
continue
|
71 |
+
|
72 |
+
elif s.group() == 'cx':
|
73 |
+
lc = lc + 1
|
74 |
+
match = re.findall(r'\[\d\d*\]', line)
|
75 |
+
c_qbit = int(match[0].strip('[]'))
|
76 |
+
t_qbit = int(match[1].strip('[]'))
|
77 |
+
# f.write('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit))
|
78 |
+
f.write('cir.add(qb.gates.CNOT({}, {}))\n'.format(c_qbit, t_qbit))
|
79 |
+
continue
|
80 |
+
|
81 |
+
elif s.group() == 'u2':
|
82 |
+
lc = lc + 1
|
83 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
84 |
+
m_r = m_r[0].split(',')
|
85 |
+
for i, m in enumerate(m_r):
|
86 |
+
if 'pi' in m and 'e' in m:
|
87 |
+
sp_str = m_r[i].split('e-')
|
88 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
89 |
+
elif 'pi' in m:
|
90 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
91 |
+
else:
|
92 |
+
m_r[i] = '0'
|
93 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
94 |
+
t_qbit = int(m_i[0].strip('[]'))
|
95 |
+
f.write('cir.add(qb.gates.U2({}, phi=np.pi*{}, lam=np.pi*{}, trainable=False))\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
96 |
+
continue
|
97 |
+
|
98 |
+
elif s.group() == 'u3':
|
99 |
+
lc = lc + 1
|
100 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
101 |
+
m_r = m_r[0].split(',')
|
102 |
+
for i, m in enumerate(m_r):
|
103 |
+
if 'pi' in m and 'e' in m:
|
104 |
+
sp_str = m_r[i].split('e-')
|
105 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
106 |
+
elif 'pi' in m:
|
107 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
108 |
+
else:
|
109 |
+
m_r[i] = '0'
|
110 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
111 |
+
t_qbit = int(m_i[0].strip('[]'))
|
112 |
+
f.write('cir.add(qb.gates.U3({}, theta=np.pi*{}, phi=np.pi*{}, lam=np.pi*{}))\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
113 |
+
continue
|
114 |
+
|
115 |
+
f.write('result = cir()\n')
|
116 |
+
|
117 |
+
f.write('t_e = perf_counter()\n')
|
118 |
+
f.write('t_ep = process_time()\n')
|
119 |
+
f.write('print(t_e - t_s)\n')
|
120 |
+
f.write('print(t_ep - t_sp)\n')
|
121 |
+
|
122 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
123 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
124 |
+
|
125 |
+
# f.write('print(result.final_state_vector)\n')
|
126 |
+
|
127 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
128 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
129 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
130 |
+
print(int(lc_res) == lc)
|
131 |
+
# print(lc)
|
132 |
+
|
133 |
+
# parse_qasm_to_package_gen(12, "../qasm_test/qasm_rqc_test.qasm", "test_cirq.py")
|
134 |
+
# parse_qasm_to_package_gen(12, 1, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_qibo.py")
|
135 |
+
|
136 |
+
task = 'qft'
|
137 |
+
sim_pack = 'qibo'
|
138 |
+
com_cap = 'mt'
|
139 |
+
prec = 'sp'
|
140 |
+
|
141 |
+
for N in range(6, 40, 2):
|
142 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
143 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
144 |
+
|
145 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
146 |
+
|
147 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
148 |
+
|
149 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
150 |
+
parse_qasm_to_package_gen(N, 'single', 84, input_file, output_file, save_file_path)
|
qasm_parser/qibo/qasm_qibojit_gpu_parser.py
ADDED
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, precision, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import numpy as np\n')
|
11 |
+
f.write('import qibo as qb\n')
|
12 |
+
|
13 |
+
f.write('qb.set_backend("qibojit")\n')
|
14 |
+
# f.write('qb.set_threads({})\n'.format(cpu_threads))
|
15 |
+
|
16 |
+
f.write('qb.set_precision(\'{}\')\n'.format(precision))
|
17 |
+
|
18 |
+
f.write('import os\n')
|
19 |
+
f.write('from time import process_time, perf_counter\n')
|
20 |
+
|
21 |
+
f.write('t_sp = process_time()\n')
|
22 |
+
f.write('t_s = perf_counter()\n')
|
23 |
+
|
24 |
+
f.write('cir = qb.models.Circuit({})\n'.format(N))
|
25 |
+
|
26 |
+
with open(input_filename, "r") as ifile:
|
27 |
+
lines = ifile.readlines()
|
28 |
+
|
29 |
+
lc = 0
|
30 |
+
cirq_c = 0
|
31 |
+
for line in lines:
|
32 |
+
|
33 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
34 |
+
|
35 |
+
if s is None:
|
36 |
+
continue
|
37 |
+
|
38 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
39 |
+
lc = lc + 1
|
40 |
+
f.write('# {}\n'.format(s.group()))
|
41 |
+
continue
|
42 |
+
|
43 |
+
elif s.group() == 'cirq':
|
44 |
+
lc = lc + 1
|
45 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
46 |
+
cirq_c = cirq_c + 1
|
47 |
+
continue
|
48 |
+
|
49 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'sx' or s.group() == 'sxdg' or s.group() == 'h':
|
50 |
+
lc = lc + 1
|
51 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
52 |
+
t_qbit = int(m_i[0].strip('[]'))
|
53 |
+
f.write('cir.add(qb.gates.{}({}))\n'.format(s.group().upper(), t_qbit))
|
54 |
+
continue
|
55 |
+
|
56 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
57 |
+
lc = lc + 1
|
58 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
59 |
+
# print(m_r)
|
60 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
61 |
+
sp_str = m_r[0].split('e-')
|
62 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
63 |
+
elif 'pi' in m_r[0]:
|
64 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
65 |
+
# print(m_r)
|
66 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
67 |
+
t_qbit = int(m_i[0].strip('[]'))
|
68 |
+
f.write('cir.add(qb.gates.{}({}, theta=np.pi*{}, trainable=False))\n'.format(s.group().upper(), t_qbit, float(m_r[0][0])))
|
69 |
+
continue
|
70 |
+
|
71 |
+
elif s.group() == 'cx':
|
72 |
+
lc = lc + 1
|
73 |
+
match = re.findall(r'\[\d\d*\]', line)
|
74 |
+
c_qbit = int(match[0].strip('[]'))
|
75 |
+
t_qbit = int(match[1].strip('[]'))
|
76 |
+
# f.write('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit))
|
77 |
+
f.write('cir.add(qb.gates.CNOT({}, {}))\n'.format(c_qbit, t_qbit))
|
78 |
+
continue
|
79 |
+
|
80 |
+
elif s.group() == 'u2':
|
81 |
+
lc = lc + 1
|
82 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
83 |
+
m_r = m_r[0].split(',')
|
84 |
+
for i, m in enumerate(m_r):
|
85 |
+
if 'pi' in m and 'e' in m:
|
86 |
+
sp_str = m_r[i].split('e-')
|
87 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
88 |
+
elif 'pi' in m:
|
89 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
90 |
+
else:
|
91 |
+
m_r[i] = '0'
|
92 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
93 |
+
t_qbit = int(m_i[0].strip('[]'))
|
94 |
+
f.write('cir.add(qb.gates.U2({}, phi=np.pi*{}, lam=np.pi*{}, trainable=False))\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
95 |
+
continue
|
96 |
+
|
97 |
+
elif s.group() == 'u3':
|
98 |
+
lc = lc + 1
|
99 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
100 |
+
m_r = m_r[0].split(',')
|
101 |
+
for i, m in enumerate(m_r):
|
102 |
+
if 'pi' in m and 'e' in m:
|
103 |
+
sp_str = m_r[i].split('e-')
|
104 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
105 |
+
elif 'pi' in m:
|
106 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
107 |
+
else:
|
108 |
+
m_r[i] = '0'
|
109 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
110 |
+
t_qbit = int(m_i[0].strip('[]'))
|
111 |
+
f.write('cir.add(qb.gates.U3({}, theta=np.pi*{}, phi=np.pi*{}, lam=np.pi*{}))\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
112 |
+
continue
|
113 |
+
|
114 |
+
f.write('result = cir()\n')
|
115 |
+
|
116 |
+
f.write('t_e = perf_counter()\n')
|
117 |
+
f.write('t_ep = process_time()\n')
|
118 |
+
f.write('print(t_e - t_s)\n')
|
119 |
+
f.write('print(t_ep - t_sp)\n')
|
120 |
+
# f.write('print(result.final_state_vector)\n')
|
121 |
+
|
122 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
123 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
124 |
+
|
125 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
126 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
127 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
128 |
+
print(int(lc_res) == lc)
|
129 |
+
# print(lc)
|
130 |
+
|
131 |
+
# parse_qasm_to_package_gen(12, "../qasm_test/qasm_rqc_test.qasm", "test_cirq.py")
|
132 |
+
# parse_qasm_to_package_gen(12, 1, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_qibojit.py")
|
133 |
+
|
134 |
+
task = 'rqc'
|
135 |
+
sim_pack = 'qibojit'
|
136 |
+
com_cap = 'gpu'
|
137 |
+
prec = 'dp'
|
138 |
+
|
139 |
+
for N in range(12, 40, 2):
|
140 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
141 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
142 |
+
|
143 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
144 |
+
|
145 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
146 |
+
|
147 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
148 |
+
parse_qasm_to_package_gen(N, 'double', input_file, output_file, save_file_path)
|
149 |
+
|
150 |
+
r"""
|
151 |
+
for N in range(12, 42, 2):
|
152 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
153 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
154 |
+
|
155 |
+
output_file_path = '/data/user/gangap_a/rqc/qibo/qibojit_gpu_sp_run_files/'
|
156 |
+
output_file = output_file_path + 'qibojit_rqc_n{}.py'.format(N)
|
157 |
+
parse_qasm_to_package_gen(N, 'single', input_file, output_file)
|
158 |
+
|
159 |
+
for N in range(41, 51):
|
160 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
161 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
162 |
+
|
163 |
+
output_file_path = '/data/user/gangap_a/rqc/qibo/qibojit_gpu_sp_run_files/'
|
164 |
+
output_file = output_file_path + 'qibojit_rqc_n{}.py'.format(N)
|
165 |
+
parse_qasm_to_package_gen(N, 'single', input_file, output_file)
|
166 |
+
|
167 |
+
"""
|
qasm_parser/qibo/qasm_qibojit_parser.py
ADDED
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, precision, cpu_threads, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import numpy as np\n')
|
11 |
+
f.write('import qibo as qb\n')
|
12 |
+
|
13 |
+
f.write('qb.set_backend("qibojit")\n')
|
14 |
+
|
15 |
+
if cpu_threads == 1:
|
16 |
+
f.write('qb.set_threads({})\n'.format(cpu_threads))
|
17 |
+
|
18 |
+
f.write('qb.set_precision(\'{}\')\n'.format(precision))
|
19 |
+
|
20 |
+
f.write('import os\n')
|
21 |
+
f.write('from time import process_time, perf_counter\n')
|
22 |
+
|
23 |
+
f.write('t_sp = process_time()\n')
|
24 |
+
f.write('t_s = perf_counter()\n')
|
25 |
+
|
26 |
+
f.write('cir = qb.models.Circuit({})\n'.format(N))
|
27 |
+
|
28 |
+
with open(input_filename, "r") as ifile:
|
29 |
+
lines = ifile.readlines()
|
30 |
+
|
31 |
+
lc = 0
|
32 |
+
cirq_c = 0
|
33 |
+
for line in lines:
|
34 |
+
|
35 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
36 |
+
|
37 |
+
if s is None:
|
38 |
+
continue
|
39 |
+
|
40 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
41 |
+
lc = lc + 1
|
42 |
+
f.write('# {}\n'.format(s.group()))
|
43 |
+
continue
|
44 |
+
|
45 |
+
elif s.group() == 'cirq':
|
46 |
+
lc = lc + 1
|
47 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
48 |
+
cirq_c = cirq_c + 1
|
49 |
+
continue
|
50 |
+
|
51 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'sx' or s.group() == 'sxdg' or s.group() == 'h':
|
52 |
+
lc = lc + 1
|
53 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
54 |
+
t_qbit = int(m_i[0].strip('[]'))
|
55 |
+
f.write('cir.add(qb.gates.{}({}))\n'.format(s.group().upper(), t_qbit))
|
56 |
+
continue
|
57 |
+
|
58 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
59 |
+
lc = lc + 1
|
60 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
61 |
+
# print(m_r)
|
62 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
63 |
+
sp_str = m_r[0].split('e-')
|
64 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
65 |
+
elif 'pi' in m_r[0]:
|
66 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
67 |
+
# print(m_r)
|
68 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
69 |
+
t_qbit = int(m_i[0].strip('[]'))
|
70 |
+
f.write('cir.add(qb.gates.{}({}, theta=np.pi*{}, trainable=False))\n'.format(s.group().upper(), t_qbit, float(m_r[0][0])))
|
71 |
+
continue
|
72 |
+
|
73 |
+
elif s.group() == 'cx':
|
74 |
+
lc = lc + 1
|
75 |
+
match = re.findall(r'\[\d\d*\]', line)
|
76 |
+
c_qbit = int(match[0].strip('[]'))
|
77 |
+
t_qbit = int(match[1].strip('[]'))
|
78 |
+
# f.write('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit))
|
79 |
+
f.write('cir.add(qb.gates.CNOT({}, {}))\n'.format(c_qbit, t_qbit))
|
80 |
+
continue
|
81 |
+
|
82 |
+
elif s.group() == 'u2':
|
83 |
+
lc = lc + 1
|
84 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
85 |
+
m_r = m_r[0].split(',')
|
86 |
+
for i, m in enumerate(m_r):
|
87 |
+
if 'pi' in m and 'e' in m:
|
88 |
+
sp_str = m_r[i].split('e-')
|
89 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
90 |
+
elif 'pi' in m:
|
91 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
92 |
+
else:
|
93 |
+
m_r[i] = '0'
|
94 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
95 |
+
t_qbit = int(m_i[0].strip('[]'))
|
96 |
+
f.write('cir.add(qb.gates.U2({}, phi=np.pi*{}, lam=np.pi*{}, trainable=False))\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
97 |
+
continue
|
98 |
+
|
99 |
+
elif s.group() == 'u3':
|
100 |
+
lc = lc + 1
|
101 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
102 |
+
m_r = m_r[0].split(',')
|
103 |
+
for i, m in enumerate(m_r):
|
104 |
+
if 'pi' in m and 'e' in m:
|
105 |
+
sp_str = m_r[i].split('e-')
|
106 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
107 |
+
elif 'pi' in m:
|
108 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
109 |
+
else:
|
110 |
+
m_r[i] = '0'
|
111 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
112 |
+
t_qbit = int(m_i[0].strip('[]'))
|
113 |
+
f.write('cir.add(qb.gates.U3({}, theta=np.pi*{}, phi=np.pi*{}, lam=np.pi*{}))\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
114 |
+
continue
|
115 |
+
|
116 |
+
f.write('result = cir()\n')
|
117 |
+
|
118 |
+
f.write('t_e = perf_counter()\n')
|
119 |
+
f.write('t_ep = process_time()\n')
|
120 |
+
f.write('print(t_e - t_s)\n')
|
121 |
+
f.write('print(t_ep - t_sp)\n')
|
122 |
+
|
123 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
124 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
125 |
+
|
126 |
+
# f.write('print(result.final_state_vector)\n')
|
127 |
+
|
128 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
129 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
130 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
131 |
+
print(int(lc_res) == lc)
|
132 |
+
# print(lc)
|
133 |
+
|
134 |
+
# parse_qasm_to_package_gen(12, "../qasm_test/qasm_rqc_test.qasm", "test_cirq.py")
|
135 |
+
# parse_qasm_to_package_gen(12, 1, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_qibojit.py")
|
136 |
+
|
137 |
+
task = 'qft'
|
138 |
+
sim_pack = 'qibojit'
|
139 |
+
com_cap = 'mt'
|
140 |
+
prec = 'sp'
|
141 |
+
|
142 |
+
for N in range(6, 40, 2):
|
143 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
144 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
145 |
+
|
146 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
147 |
+
|
148 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
149 |
+
|
150 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
151 |
+
parse_qasm_to_package_gen(N, 'single', 84, input_file, output_file, save_file_path)
|
qasm_parser/qibo_ngpu/qasm_qibojit_gpu_parser.py
ADDED
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, ngpus, precision, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import numpy as np\n')
|
11 |
+
f.write('import qibo as qb\n')
|
12 |
+
|
13 |
+
f.write('qb.set_backend("qibojit")\n')
|
14 |
+
# f.write('qb.set_threads({})\n'.format(cpu_threads))
|
15 |
+
|
16 |
+
f.write('qb.set_precision(\'{}\')\n'.format(precision))
|
17 |
+
|
18 |
+
f.write('import os\n')
|
19 |
+
f.write('from time import process_time, perf_counter\n')
|
20 |
+
|
21 |
+
f.write('t_sp = process_time()\n')
|
22 |
+
f.write('t_s = perf_counter()\n')
|
23 |
+
|
24 |
+
gs_key_arr = []
|
25 |
+
|
26 |
+
if ngpus==1:
|
27 |
+
f.write('cir = qb.models.Circuit({})\n'.format(N))
|
28 |
+
else:
|
29 |
+
gs_key_arr = []
|
30 |
+
|
31 |
+
for gpu in range(ngpus):
|
32 |
+
gs_key_arr.append('/GPU:' + str(gpu))
|
33 |
+
|
34 |
+
gs_val_arr = [1 for _ in range(ngpus)]
|
35 |
+
|
36 |
+
gpu_dict = dict(zip(gs_key_arr, gs_val_arr))
|
37 |
+
|
38 |
+
f.write('cir = qb.models.Circuit({}, {})\n'.format(N, gpu_dict))
|
39 |
+
|
40 |
+
with open(input_filename, "r") as ifile:
|
41 |
+
lines = ifile.readlines()
|
42 |
+
|
43 |
+
lc = 0
|
44 |
+
cirq_c = 0
|
45 |
+
for line in lines:
|
46 |
+
|
47 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
48 |
+
|
49 |
+
if s is None:
|
50 |
+
continue
|
51 |
+
|
52 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
53 |
+
lc = lc + 1
|
54 |
+
f.write('# {}\n'.format(s.group()))
|
55 |
+
continue
|
56 |
+
|
57 |
+
elif s.group() == 'cirq':
|
58 |
+
lc = lc + 1
|
59 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
60 |
+
cirq_c = cirq_c + 1
|
61 |
+
continue
|
62 |
+
|
63 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'sx' or s.group() == 'sxdg' or s.group() == 'h':
|
64 |
+
lc = lc + 1
|
65 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
66 |
+
t_qbit = int(m_i[0].strip('[]'))
|
67 |
+
f.write('cir.add(qb.gates.{}({}))\n'.format(s.group().upper(), t_qbit))
|
68 |
+
continue
|
69 |
+
|
70 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
71 |
+
lc = lc + 1
|
72 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
73 |
+
# print(m_r)
|
74 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
75 |
+
sp_str = m_r[0].split('e-')
|
76 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
77 |
+
elif 'pi' in m_r[0]:
|
78 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
79 |
+
# print(m_r)
|
80 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
81 |
+
t_qbit = int(m_i[0].strip('[]'))
|
82 |
+
f.write('cir.add(qb.gates.{}({}, theta=np.pi*{}, trainable=False))\n'.format(s.group().upper(), t_qbit, float(m_r[0][0])))
|
83 |
+
continue
|
84 |
+
|
85 |
+
elif s.group() == 'cx':
|
86 |
+
lc = lc + 1
|
87 |
+
match = re.findall(r'\[\d\d*\]', line)
|
88 |
+
c_qbit = int(match[0].strip('[]'))
|
89 |
+
t_qbit = int(match[1].strip('[]'))
|
90 |
+
# f.write('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit))
|
91 |
+
f.write('cir.add(qb.gates.CNOT({}, {}))\n'.format(c_qbit, t_qbit))
|
92 |
+
continue
|
93 |
+
|
94 |
+
elif s.group() == 'u2':
|
95 |
+
lc = lc + 1
|
96 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
97 |
+
m_r = m_r[0].split(',')
|
98 |
+
for i, m in enumerate(m_r):
|
99 |
+
if 'pi' in m and 'e' in m:
|
100 |
+
sp_str = m_r[i].split('e-')
|
101 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
102 |
+
elif 'pi' in m:
|
103 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
104 |
+
else:
|
105 |
+
m_r[i] = '0'
|
106 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
107 |
+
t_qbit = int(m_i[0].strip('[]'))
|
108 |
+
f.write('cir.add(qb.gates.U2({}, phi=np.pi*{}, lam=np.pi*{}, trainable=False))\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
109 |
+
continue
|
110 |
+
|
111 |
+
elif s.group() == 'u3':
|
112 |
+
lc = lc + 1
|
113 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
114 |
+
m_r = m_r[0].split(',')
|
115 |
+
for i, m in enumerate(m_r):
|
116 |
+
if 'pi' in m and 'e' in m:
|
117 |
+
sp_str = m_r[i].split('e-')
|
118 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
119 |
+
elif 'pi' in m:
|
120 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
121 |
+
else:
|
122 |
+
m_r[i] = '0'
|
123 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
124 |
+
t_qbit = int(m_i[0].strip('[]'))
|
125 |
+
f.write('cir.add(qb.gates.U3({}, theta=np.pi*{}, phi=np.pi*{}, lam=np.pi*{}))\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
126 |
+
continue
|
127 |
+
|
128 |
+
f.write('result = cir()\n')
|
129 |
+
|
130 |
+
f.write('t_e = perf_counter()\n')
|
131 |
+
f.write('t_ep = process_time()\n')
|
132 |
+
f.write('print(t_e - t_s)\n')
|
133 |
+
f.write('print(t_ep - t_sp)\n')
|
134 |
+
|
135 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
136 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
137 |
+
|
138 |
+
# f.write('print(result.final_state_vector)\n')
|
139 |
+
|
140 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
141 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
142 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
143 |
+
print(int(lc_res) == lc)
|
144 |
+
# print(lc)
|
145 |
+
|
146 |
+
# parse_qasm_to_package_gen(12, "../qasm_test/qasm_rqc_test.qasm", "test_cirq.py")
|
147 |
+
# parse_qasm_to_package_gen(12, 1, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_qibojit.py")
|
148 |
+
|
149 |
+
task = 'qft'
|
150 |
+
sim_pack = 'qibojit'
|
151 |
+
com_cap = 'gpu8'
|
152 |
+
prec = 'dp'
|
153 |
+
|
154 |
+
for N in range(6, 40, 2):
|
155 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
156 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
157 |
+
|
158 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
159 |
+
|
160 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
161 |
+
|
162 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
163 |
+
parse_qasm_to_package_gen(N, 8, 'double', input_file, output_file, save_file_path)
|
164 |
+
|
165 |
+
|
166 |
+
r"""
|
167 |
+
for N in range(12, 42, 2):
|
168 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
169 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
170 |
+
|
171 |
+
output_file_path = '/data/user/gangap_a/rqc/qibojit_na100/qibojit_n1_run_files/'
|
172 |
+
output_file = output_file_path + 'qibojit_rqc_n{}.py'.format(N)
|
173 |
+
parse_qasm_to_package_gen(N, 1, 'double', input_file, output_file)
|
174 |
+
|
175 |
+
for N in range(41, 51):
|
176 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
177 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
178 |
+
|
179 |
+
output_file_path = '/data/user/gangap_a/rqc/qibojit_na100/qibojit_n1_run_files/'
|
180 |
+
output_file = output_file_path + 'qibojit_rqc_n{}.py'.format(N)
|
181 |
+
parse_qasm_to_package_gen(N, 1, 'double', input_file, output_file)
|
182 |
+
|
183 |
+
"""
|
qasm_parser/qiskit/qasm_qiskit_parser_2pt0_new.py
ADDED
@@ -0,0 +1,200 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
import os
|
6 |
+
|
7 |
+
def parse_qasm_to_package_gen(N, mpt, spt, f_precision, input_filename, output_filename, save_data_path):
|
8 |
+
|
9 |
+
with open(output_filename, 'w') as f:
|
10 |
+
|
11 |
+
f.write('import os\n')
|
12 |
+
f.write('from qiskit import QuantumRegister, QuantumCircuit, transpile, ClassicalRegister\n')
|
13 |
+
f.write('from qiskit.quantum_info.operators import Operator\n')
|
14 |
+
f.write('from qiskit.providers.aer import QasmSimulator\n')
|
15 |
+
f.write('from qiskit import Aer\n')
|
16 |
+
f.write('from time import process_time, perf_counter\n')
|
17 |
+
f.write('import numpy as np\n\n')
|
18 |
+
|
19 |
+
f.write('from qiskit.quantum_info import Statevector, shannon_entropy\n')
|
20 |
+
|
21 |
+
f.write('def U2(p, l):\n')
|
22 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
23 |
+
f.write(' return Operator(mat)\n\n')
|
24 |
+
|
25 |
+
f.write('def U3(t, p, l):\n')
|
26 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
27 |
+
f.write(' return Operator(mat)\n\n')
|
28 |
+
|
29 |
+
f.write('t_sp = process_time()\n')
|
30 |
+
f.write('t_s = perf_counter()\n')
|
31 |
+
|
32 |
+
f.write('backend = Aer.get_backend(\'statevector_simulator\')#, max_parallel_threads={}, statevector_parallel_threshold={}, precision=\'{}\')\n'.format(mpt, spt, f_precision))
|
33 |
+
f.write('opt = backend.options\n')
|
34 |
+
f.write('opt.max_parallel_threads={}\n'.format(mpt))
|
35 |
+
f.write('opt.statevector_parallel_threshold={}\n'.format(spt))
|
36 |
+
f.write('opt.precision=\'{}\'\n'.format(f_precision))
|
37 |
+
|
38 |
+
f.write('cir = QuantumCircuit({})\n'.format(N))
|
39 |
+
with open(input_filename, "r") as ifile:
|
40 |
+
lines = ifile.readlines()
|
41 |
+
|
42 |
+
lc = 0
|
43 |
+
cirq_c = 0
|
44 |
+
for line in lines:
|
45 |
+
|
46 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
47 |
+
|
48 |
+
if s is None:
|
49 |
+
continue
|
50 |
+
|
51 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
52 |
+
lc = lc + 1
|
53 |
+
f.write('# {}\n'.format(s.group()))
|
54 |
+
continue
|
55 |
+
|
56 |
+
elif s.group() == 'cirq':
|
57 |
+
lc = lc + 1
|
58 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
59 |
+
cirq_c = cirq_c + 1
|
60 |
+
continue
|
61 |
+
|
62 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'sx' or s.group() == 'sxdg' or s.group() == 'h':
|
63 |
+
lc = lc + 1
|
64 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
65 |
+
t_qbit = int(m_i[0].strip('[]'))
|
66 |
+
f.write('cir.{}({})\n'.format(s.group(), t_qbit))
|
67 |
+
continue
|
68 |
+
|
69 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
70 |
+
lc = lc + 1
|
71 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
72 |
+
# print(m_r)
|
73 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
74 |
+
sp_str = m_r[0].split('e-')
|
75 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
76 |
+
elif 'pi' in m_r[0]:
|
77 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
78 |
+
# print(m_r)
|
79 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
80 |
+
t_qbit = int(m_i[0].strip('[]'))
|
81 |
+
f.write('cir.{}(np.pi*{}, {})\n'.format(s.group(), float(m_r[0][0]), t_qbit))
|
82 |
+
continue
|
83 |
+
|
84 |
+
elif s.group() == 'cx':
|
85 |
+
lc = lc + 1
|
86 |
+
match = re.findall(r'\[\d\d*\]', line)
|
87 |
+
c_qbit = int(match[0].strip('[]'))
|
88 |
+
t_qbit = int(match[1].strip('[]'))
|
89 |
+
f.write('cir.cx({}, {})\n'.format(c_qbit, t_qbit))
|
90 |
+
continue
|
91 |
+
|
92 |
+
elif s.group() == 'u2':
|
93 |
+
lc = lc + 1
|
94 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
95 |
+
m_r = m_r[0].split(',')
|
96 |
+
for i, m in enumerate(m_r):
|
97 |
+
if 'pi' in m and 'e' in m:
|
98 |
+
sp_str = m_r[i].split('e-')
|
99 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
100 |
+
elif 'pi' in m:
|
101 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
102 |
+
else:
|
103 |
+
m_r[i] = '0'
|
104 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
105 |
+
t_qbit = int(m_i[0].strip('[]'))
|
106 |
+
# f.write('cir.u(np.pi/2., np.pi*{}, np.pi*{}, {})\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
107 |
+
f.write('cir.append(U2(np.pi*{}, np.pi*{}), [{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
108 |
+
continue
|
109 |
+
|
110 |
+
elif s.group() == 'u3':
|
111 |
+
lc = lc + 1
|
112 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
113 |
+
m_r = m_r[0].split(',')
|
114 |
+
for i, m in enumerate(m_r):
|
115 |
+
if 'pi' in m and 'e' in m:
|
116 |
+
sp_str = m_r[i].split('e-')
|
117 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
118 |
+
elif 'pi' in m:
|
119 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
120 |
+
else:
|
121 |
+
m_r[i] = '0'
|
122 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
123 |
+
t_qbit = int(m_i[0].strip('[]'))
|
124 |
+
# f.write('cir.u(np.pi*{}, np.pi*{}, np.pi*{}, {})\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
125 |
+
f.write('cir.append(U3(np.pi*{}, np.pi*{}, np.pi*{}), [{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
126 |
+
continue
|
127 |
+
|
128 |
+
f.write('job = backend.run(transpile(cir, backend))\n')
|
129 |
+
f.write('result = job.result()\n')
|
130 |
+
f.write('outputstate = result.get_statevector(cir)\n')
|
131 |
+
|
132 |
+
f.write('probs = Statevector(outputstate).probabilities()\n')
|
133 |
+
f.write('print(sum(probs))\n')
|
134 |
+
|
135 |
+
f.write('ent = shannon_entropy(probs)\n')
|
136 |
+
f.write('print(ent)\n')
|
137 |
+
|
138 |
+
# f.write('t_e = perf_counter()\n')
|
139 |
+
# f.write('t_ep = process_time()\n')
|
140 |
+
|
141 |
+
# f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
142 |
+
# f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
143 |
+
# f.write('print(t_e - t_s)\n')
|
144 |
+
# f.write('print(t_ep - t_sp)\n')
|
145 |
+
|
146 |
+
|
147 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
148 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
149 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
150 |
+
print(int(lc_res) == lc)
|
151 |
+
# print(lc)
|
152 |
+
|
153 |
+
# parse_qasm_to_package_gen(12, 1, 42, "../qasm_test/qasm_rqc_test.qasm", "test_qiskit.py")
|
154 |
+
# parse_qasm_to_package_gen(12, 1, 42, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_qiskit_2.py")
|
155 |
+
|
156 |
+
r"""
|
157 |
+
for N in range(12, 42, 2):
|
158 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
159 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
160 |
+
|
161 |
+
output_file_path = '/data/user/gangap_a/rqc/qiskit/qiskit_sp_run_files/'
|
162 |
+
output_file = output_file_path + 'qiskit_rqc_n{}.py'.format(N)
|
163 |
+
parse_qasm_to_package_gen(N, 1, 42, 'single', input_file, output_file)
|
164 |
+
|
165 |
+
for N in range(41, 51):
|
166 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
167 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
168 |
+
|
169 |
+
output_file_path = '/data/user/gangap_a/rqc/qiskit/qiskit_sp_run_files/'
|
170 |
+
output_file = output_file_path + 'qiskit_rqc_n{}.py'.format(N)
|
171 |
+
parse_qasm_to_package_gen(N, 1, 42, 'single', input_file, output_file)
|
172 |
+
|
173 |
+
for N in range(6, 38, 2):
|
174 |
+
input_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/data_files/'
|
175 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
176 |
+
|
177 |
+
output_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/qiskit/qiskit_run_files/'
|
178 |
+
output_file = output_file_path + 'h_dyn_n{}.py'.format(N)
|
179 |
+
parse_qasm_to_package_gen(N, 1, 42, 'double', input_file, output_file)
|
180 |
+
"""
|
181 |
+
|
182 |
+
task = 'hdyn'
|
183 |
+
sim_pack = 'qiskit'
|
184 |
+
com_cap = 'st'
|
185 |
+
prec = 'sp'
|
186 |
+
|
187 |
+
for N in range(6, 40, 2):
|
188 |
+
input_file_path = '/home/amit/Documents/PSI_m/project_files_final/{}_singular/qasm_files/'.format(task)
|
189 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
190 |
+
|
191 |
+
# save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
192 |
+
|
193 |
+
# output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
194 |
+
|
195 |
+
save_file_path = None
|
196 |
+
|
197 |
+
output_file_path = '/home/amit/Documents/PSI_m/project_files_final/{}_ent/{}/'.format(task, sim_pack)
|
198 |
+
|
199 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
200 |
+
parse_qasm_to_package_gen(N, 1, 40, "single", input_file, output_file, save_file_path)
|
qasm_parser/qiskit/qasm_qiskit_parser_gpu_2pt0_new.py
ADDED
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
import os
|
6 |
+
|
7 |
+
def parse_qasm_to_package_gen(N, f_precision, input_filename, output_filename, save_data_path):
|
8 |
+
|
9 |
+
with open(output_filename, 'w') as f:
|
10 |
+
|
11 |
+
f.write('import os\n')
|
12 |
+
f.write('from qiskit import QuantumRegister, QuantumCircuit, transpile, ClassicalRegister\n')
|
13 |
+
f.write('from qiskit.quantum_info.operators import Operator\n')
|
14 |
+
f.write('from qiskit.providers.aer import QasmSimulator\n')
|
15 |
+
f.write('from qiskit import Aer\n')
|
16 |
+
f.write('from time import process_time, perf_counter\n')
|
17 |
+
f.write('import numpy as np\n\n')
|
18 |
+
|
19 |
+
f.write('def U2(p, l):\n')
|
20 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
21 |
+
f.write(' return Operator(mat)\n\n')
|
22 |
+
|
23 |
+
f.write('def U3(t, p, l):\n')
|
24 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
25 |
+
f.write(' return Operator(mat)\n\n')
|
26 |
+
|
27 |
+
f.write('t_sp = process_time()\n')
|
28 |
+
f.write('t_s = perf_counter()\n')
|
29 |
+
|
30 |
+
f.write('backend = Aer.get_backend(\'statevector_simulator\')\n')#, max_parallel_threads={}, statevector_parallel_threshold={}, precision=\'{}\')\n'.format(mpt, spt, f_precision))
|
31 |
+
f.write('opt = backend.options\n')
|
32 |
+
f.write('opt.device=\'GPU\'\n')
|
33 |
+
f.write('opt.precision=\'{}\'\n'.format(f_precision))
|
34 |
+
|
35 |
+
f.write('cir = QuantumCircuit({})\n'.format(N))
|
36 |
+
with open(input_filename, "r") as ifile:
|
37 |
+
lines = ifile.readlines()
|
38 |
+
|
39 |
+
lc = 0
|
40 |
+
cirq_c = 0
|
41 |
+
for line in lines:
|
42 |
+
|
43 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
44 |
+
|
45 |
+
if s is None:
|
46 |
+
continue
|
47 |
+
|
48 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
49 |
+
lc = lc + 1
|
50 |
+
f.write('# {}\n'.format(s.group()))
|
51 |
+
continue
|
52 |
+
|
53 |
+
elif s.group() == 'cirq':
|
54 |
+
lc = lc + 1
|
55 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
56 |
+
cirq_c = cirq_c + 1
|
57 |
+
continue
|
58 |
+
|
59 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'sx' or s.group() == 'sxdg' or s.group() == 'h':
|
60 |
+
lc = lc + 1
|
61 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
62 |
+
t_qbit = int(m_i[0].strip('[]'))
|
63 |
+
f.write('cir.{}({})\n'.format(s.group(), t_qbit))
|
64 |
+
continue
|
65 |
+
|
66 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
67 |
+
lc = lc + 1
|
68 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
69 |
+
# print(m_r)
|
70 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
71 |
+
sp_str = m_r[0].split('e-')
|
72 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
73 |
+
elif 'pi' in m_r[0]:
|
74 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
75 |
+
# print(m_r)
|
76 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
77 |
+
t_qbit = int(m_i[0].strip('[]'))
|
78 |
+
f.write('cir.{}(np.pi*{}, {})\n'.format(s.group(), float(m_r[0][0]), t_qbit))
|
79 |
+
continue
|
80 |
+
|
81 |
+
elif s.group() == 'cx':
|
82 |
+
lc = lc + 1
|
83 |
+
match = re.findall(r'\[\d\d*\]', line)
|
84 |
+
c_qbit = int(match[0].strip('[]'))
|
85 |
+
t_qbit = int(match[1].strip('[]'))
|
86 |
+
f.write('cir.cx({}, {})\n'.format(c_qbit, t_qbit))
|
87 |
+
continue
|
88 |
+
|
89 |
+
elif s.group() == 'u2':
|
90 |
+
lc = lc + 1
|
91 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
92 |
+
m_r = m_r[0].split(',')
|
93 |
+
for i, m in enumerate(m_r):
|
94 |
+
if 'pi' in m and 'e' in m:
|
95 |
+
sp_str = m_r[i].split('e-')
|
96 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
97 |
+
elif 'pi' in m:
|
98 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
99 |
+
else:
|
100 |
+
m_r[i] = '0'
|
101 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
102 |
+
t_qbit = int(m_i[0].strip('[]'))
|
103 |
+
# f.write('cir.u(np.pi/2., np.pi*{}, np.pi*{}, {})\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
104 |
+
f.write('cir.append(U2(np.pi*{}, np.pi*{}), [{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
105 |
+
continue
|
106 |
+
|
107 |
+
elif s.group() == 'u3':
|
108 |
+
lc = lc + 1
|
109 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
110 |
+
m_r = m_r[0].split(',')
|
111 |
+
for i, m in enumerate(m_r):
|
112 |
+
if 'pi' in m and 'e' in m:
|
113 |
+
sp_str = m_r[i].split('e-')
|
114 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
115 |
+
elif 'pi' in m:
|
116 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
117 |
+
else:
|
118 |
+
m_r[i] = '0'
|
119 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
120 |
+
t_qbit = int(m_i[0].strip('[]'))
|
121 |
+
# f.write('cir.u(np.pi*{}, np.pi*{}, np.pi*{}, {})\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
122 |
+
f.write('cir.append(U3(np.pi*{}, np.pi*{}, np.pi*{}), [{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
123 |
+
continue
|
124 |
+
|
125 |
+
f.write('job = backend.run(transpile(cir, backend))\n')
|
126 |
+
f.write('result = job.result()\n')
|
127 |
+
f.write('outputstate = result.get_statevector(cir)\n')
|
128 |
+
|
129 |
+
f.write('t_e = perf_counter()\n')
|
130 |
+
f.write('t_ep = process_time()\n')
|
131 |
+
|
132 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
133 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
134 |
+
f.write('print(t_e - t_s)\n')
|
135 |
+
f.write('print(t_ep - t_sp)\n')
|
136 |
+
# f.write('print(outputstate)')
|
137 |
+
|
138 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
139 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
140 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
141 |
+
print(int(lc_res) == lc)
|
142 |
+
# print(lc)
|
143 |
+
|
144 |
+
# parse_qasm_to_package_gen(12, 1, 42, "../qasm_test/qasm_rqc_test.qasm", "test_qiskit.py")
|
145 |
+
# parse_qasm_to_package_gen(12, 1, 42, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_qiskit_2.py")
|
146 |
+
|
147 |
+
r"""
|
148 |
+
for N in range(12, 42, 2):
|
149 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
150 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
151 |
+
|
152 |
+
output_file_path = '/data/user/gangap_a/rqc/qiskit/qiskit_sp_run_files/'
|
153 |
+
output_file = output_file_path + 'qiskit_rqc_n{}.py'.format(N)
|
154 |
+
parse_qasm_to_package_gen(N, 1, 42, 'single', input_file, output_file)
|
155 |
+
|
156 |
+
for N in range(41, 51):
|
157 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
158 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
159 |
+
|
160 |
+
output_file_path = '/data/user/gangap_a/rqc/qiskit/qiskit_sp_run_files/'
|
161 |
+
output_file = output_file_path + 'qiskit_rqc_n{}.py'.format(N)
|
162 |
+
parse_qasm_to_package_gen(N, 1, 42, 'single', input_file, output_file)
|
163 |
+
|
164 |
+
for N in range(6, 38, 2):
|
165 |
+
input_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/data_files/'
|
166 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
167 |
+
|
168 |
+
output_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/qiskit/qiskit_run_files/'
|
169 |
+
output_file = output_file_path + 'h_dyn_n{}.py'.format(N)
|
170 |
+
parse_qasm_to_package_gen(N, 1, 42, 'double', input_file, output_file)
|
171 |
+
"""
|
172 |
+
|
173 |
+
task = 'qft'
|
174 |
+
sim_pack = 'qiskit'
|
175 |
+
com_cap = 'gpu'
|
176 |
+
prec = 'sp'
|
177 |
+
|
178 |
+
for N in range(6, 40, 2):
|
179 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
180 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
181 |
+
|
182 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
183 |
+
|
184 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
185 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
186 |
+
parse_qasm_to_package_gen(N, "single", input_file, output_file, save_file_path)
|
qasm_parser/qiskit/qasm_qiskit_parser_mt_2pt0_new.py
ADDED
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
import os
|
6 |
+
|
7 |
+
def parse_qasm_to_package_gen(N, mpt, spt, f_precision, input_filename, output_filename, save_data_path):
|
8 |
+
|
9 |
+
with open(output_filename, 'w') as f:
|
10 |
+
|
11 |
+
f.write('import os\n')
|
12 |
+
f.write('from qiskit import QuantumRegister, QuantumCircuit, transpile, ClassicalRegister\n')
|
13 |
+
f.write('from qiskit.quantum_info.operators import Operator\n')
|
14 |
+
f.write('from qiskit.providers.aer import QasmSimulator\n')
|
15 |
+
f.write('from qiskit import Aer\n')
|
16 |
+
f.write('from time import process_time, perf_counter\n')
|
17 |
+
f.write('import numpy as np\n\n')
|
18 |
+
|
19 |
+
f.write('def U2(p, l):\n')
|
20 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
21 |
+
f.write(' return Operator(mat)\n\n')
|
22 |
+
|
23 |
+
f.write('def U3(t, p, l):\n')
|
24 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
25 |
+
f.write(' return Operator(mat)\n\n')
|
26 |
+
|
27 |
+
f.write('t_sp = process_time()\n')
|
28 |
+
f.write('t_s = perf_counter()\n')
|
29 |
+
|
30 |
+
f.write('backend = Aer.get_backend(\'statevector_simulator\')\n')#, max_parallel_threads={}, statevector_parallel_threshold={}, precision=\'{}\')\n'.format(mpt, spt, f_precision))
|
31 |
+
f.write('opt = backend.options\n')
|
32 |
+
f.write('opt.max_parallel_threads={}\n'.format(mpt))
|
33 |
+
f.write('opt.statevector_parallel_threshold={}\n'.format(spt))
|
34 |
+
f.write('opt.precision=\'{}\'\n'.format(f_precision))
|
35 |
+
|
36 |
+
f.write('cir = QuantumCircuit({})\n'.format(N))
|
37 |
+
with open(input_filename, "r") as ifile:
|
38 |
+
lines = ifile.readlines()
|
39 |
+
|
40 |
+
lc = 0
|
41 |
+
cirq_c = 0
|
42 |
+
for line in lines:
|
43 |
+
|
44 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
45 |
+
|
46 |
+
if s is None:
|
47 |
+
continue
|
48 |
+
|
49 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
50 |
+
lc = lc + 1
|
51 |
+
f.write('# {}\n'.format(s.group()))
|
52 |
+
continue
|
53 |
+
|
54 |
+
elif s.group() == 'cirq':
|
55 |
+
lc = lc + 1
|
56 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
57 |
+
cirq_c = cirq_c + 1
|
58 |
+
continue
|
59 |
+
|
60 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'sx' or s.group() == 'sxdg':
|
61 |
+
lc = lc + 1
|
62 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
63 |
+
t_qbit = int(m_i[0].strip('[]'))
|
64 |
+
f.write('cir.{}({})\n'.format(s.group(), t_qbit))
|
65 |
+
continue
|
66 |
+
|
67 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
68 |
+
lc = lc + 1
|
69 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
70 |
+
# print(m_r)
|
71 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
72 |
+
sp_str = m_r[0].split('e-')
|
73 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
74 |
+
elif 'pi' in m_r[0]:
|
75 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
76 |
+
# print(m_r)
|
77 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
78 |
+
t_qbit = int(m_i[0].strip('[]'))
|
79 |
+
f.write('cir.{}(np.pi*{}, {})\n'.format(s.group(), float(m_r[0][0]), t_qbit))
|
80 |
+
continue
|
81 |
+
|
82 |
+
elif s.group() == 'cx':
|
83 |
+
lc = lc + 1
|
84 |
+
match = re.findall(r'\[\d\d*\]', line)
|
85 |
+
c_qbit = int(match[0].strip('[]'))
|
86 |
+
t_qbit = int(match[1].strip('[]'))
|
87 |
+
f.write('cir.cx({}, {})\n'.format(c_qbit, t_qbit))
|
88 |
+
continue
|
89 |
+
|
90 |
+
elif s.group() == 'u2':
|
91 |
+
lc = lc + 1
|
92 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
93 |
+
m_r = m_r[0].split(',')
|
94 |
+
for i, m in enumerate(m_r):
|
95 |
+
if 'pi' in m and 'e' in m:
|
96 |
+
sp_str = m_r[i].split('e-')
|
97 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
98 |
+
elif 'pi' in m:
|
99 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
100 |
+
else:
|
101 |
+
m_r[i] = '0'
|
102 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
103 |
+
t_qbit = int(m_i[0].strip('[]'))
|
104 |
+
# f.write('cir.u(np.pi/2., np.pi*{}, np.pi*{}, {})\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
105 |
+
f.write('cir.append(U2(np.pi*{}, np.pi*{}), [{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
106 |
+
continue
|
107 |
+
|
108 |
+
elif s.group() == 'u3':
|
109 |
+
lc = lc + 1
|
110 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
111 |
+
m_r = m_r[0].split(',')
|
112 |
+
for i, m in enumerate(m_r):
|
113 |
+
if 'pi' in m and 'e' in m:
|
114 |
+
sp_str = m_r[i].split('e-')
|
115 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
116 |
+
elif 'pi' in m:
|
117 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
118 |
+
else:
|
119 |
+
m_r[i] = '0'
|
120 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
121 |
+
t_qbit = int(m_i[0].strip('[]'))
|
122 |
+
# f.write('cir.u(np.pi*{}, np.pi*{}, np.pi*{}, {})\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
123 |
+
f.write('cir.append(U3(np.pi*{}, np.pi*{}, np.pi*{}), [{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
124 |
+
continue
|
125 |
+
|
126 |
+
f.write('job = backend.run(transpile(cir, backend))\n')
|
127 |
+
f.write('result = job.result()\n')
|
128 |
+
f.write('outputstate = result.get_statevector(cir)\n')
|
129 |
+
|
130 |
+
f.write('t_e = perf_counter()\n')
|
131 |
+
f.write('t_ep = process_time()\n')
|
132 |
+
|
133 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
134 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
135 |
+
f.write('print(t_e - t_s)\n')
|
136 |
+
f.write('print(t_ep - t_sp)\n')
|
137 |
+
# f.write('print(outputstate)')
|
138 |
+
|
139 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
140 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
141 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
142 |
+
print(int(lc_res) == lc)
|
143 |
+
# print(lc)
|
144 |
+
|
145 |
+
# parse_qasm_to_package_gen(12, 1, 42, "../qasm_test/qasm_rqc_test.qasm", "test_qiskit.py")
|
146 |
+
# parse_qasm_to_package_gen(12, 1, 42, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_qiskit_2.py")
|
147 |
+
|
148 |
+
r"""
|
149 |
+
for N in range(12, 42, 2):
|
150 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
151 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
152 |
+
|
153 |
+
output_file_path = '/data/user/gangap_a/rqc/qiskit/qiskit_sp_run_files/'
|
154 |
+
output_file = output_file_path + 'qiskit_rqc_n{}.py'.format(N)
|
155 |
+
parse_qasm_to_package_gen(N, 1, 42, 'single', input_file, output_file)
|
156 |
+
|
157 |
+
for N in range(41, 51):
|
158 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
159 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
160 |
+
|
161 |
+
output_file_path = '/data/user/gangap_a/rqc/qiskit/qiskit_sp_run_files/'
|
162 |
+
output_file = output_file_path + 'qiskit_rqc_n{}.py'.format(N)
|
163 |
+
parse_qasm_to_package_gen(N, 1, 42, 'single', input_file, output_file)
|
164 |
+
|
165 |
+
for N in range(6, 38, 2):
|
166 |
+
input_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/data_files/'
|
167 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
168 |
+
|
169 |
+
output_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/qiskit/qiskit_run_files/'
|
170 |
+
output_file = output_file_path + 'h_dyn_n{}.py'.format(N)
|
171 |
+
parse_qasm_to_package_gen(N, 1, 42, 'double', input_file, output_file)
|
172 |
+
"""
|
173 |
+
|
174 |
+
task = 'hdyn'
|
175 |
+
sim_pack = 'qiskit'
|
176 |
+
com_cap = 'mt'
|
177 |
+
prec = 'sp'
|
178 |
+
|
179 |
+
for N in range(6, 38, 2):
|
180 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
181 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
182 |
+
|
183 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
184 |
+
|
185 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
186 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
187 |
+
parse_qasm_to_package_gen(N, 84, 5, "single", input_file, output_file, save_file_path)
|
qasm_parser/qiskit_cont_ngpu/qasm_qiskit_parser_nvidia_container_2pt0.py
ADDED
@@ -0,0 +1,173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
import math
|
6 |
+
|
7 |
+
def parse_qasm_to_package_gen(N, ngpus, mpi_on, f_precision, input_filename, output_filename):
|
8 |
+
|
9 |
+
with open(output_filename, 'w') as f:
|
10 |
+
|
11 |
+
f.write('import os\n')
|
12 |
+
f.write('import sys\n')
|
13 |
+
# f.write('sys.path.insert(0, \'/data/user/gangap_a/rqc/qiskit_nvidia/cuquantum/lib\')\n')
|
14 |
+
f.write('from qiskit import QuantumRegister, QuantumCircuit, transpile, ClassicalRegister\n')
|
15 |
+
f.write('from qiskit.quantum_info.operators import Operator\n')
|
16 |
+
# f.write('from cusvaer.backends import StatevectorSimulator\n')
|
17 |
+
f.write('from qiskit import Aer\n')
|
18 |
+
f.write('import math\n')
|
19 |
+
f.write('from time import process_time, perf_counter\n')
|
20 |
+
f.write('import numpy as np\n\n')
|
21 |
+
|
22 |
+
f.write('def U2(p, l):\n')
|
23 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
24 |
+
f.write(' return Operator(mat)\n\n')
|
25 |
+
|
26 |
+
f.write('def U3(t, p, l):\n')
|
27 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
28 |
+
f.write(' return Operator(mat)\n\n')
|
29 |
+
|
30 |
+
|
31 |
+
blocking_e = False
|
32 |
+
blocking_q = None
|
33 |
+
|
34 |
+
if mpi_on == True:
|
35 |
+
blocking_e = True
|
36 |
+
blocking_q = N - int(math.log2(ngpus))
|
37 |
+
|
38 |
+
f.write('t_sp = process_time()\n')
|
39 |
+
f.write('t_s = perf_counter()\n')
|
40 |
+
|
41 |
+
# f.write('backend = Aer.get_backend(\'statevector_simulator\', max_parallel_threads={}, statevector_parallel_threshold={})\n'.format(mpt, spt))
|
42 |
+
f.write('backend = Aer.get_backend(\'statevector_simulator\', device=\'GPU\', cusvaer_enable=False, cuStateVec_enable=False, blocking_enable={}, blocking_qubits={}, precision=\'{}\')\n'.format(blocking_e, blocking_q, f_precision))
|
43 |
+
# f.write('backend.set_options(device=\'GPU\')\n')
|
44 |
+
|
45 |
+
# f.write('backend = StatevectorSimulator()\n')
|
46 |
+
# f.write('backend.set_options(precision=\'{}\')\n'.format(f_precision))
|
47 |
+
# f.write('backend.set_options(cusvaer_global_index_bits=[0, 0])\n')
|
48 |
+
# f.write('backend.set_options(cusvaer_p2p_device_bits=0)\n')
|
49 |
+
|
50 |
+
f.write('cir = QuantumCircuit({})\n'.format(N))
|
51 |
+
with open(input_filename, "r") as ifile:
|
52 |
+
lines = ifile.readlines()
|
53 |
+
|
54 |
+
lc = 0
|
55 |
+
cirq_c = 0
|
56 |
+
for line in lines:
|
57 |
+
|
58 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
59 |
+
|
60 |
+
if s is None:
|
61 |
+
continue
|
62 |
+
|
63 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
64 |
+
lc = lc + 1
|
65 |
+
f.write('# {}\n'.format(s.group()))
|
66 |
+
continue
|
67 |
+
|
68 |
+
elif s.group() == 'cirq':
|
69 |
+
lc = lc + 1
|
70 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
71 |
+
cirq_c = cirq_c + 1
|
72 |
+
continue
|
73 |
+
|
74 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'sx' or s.group() == 'sxdg':
|
75 |
+
lc = lc + 1
|
76 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
77 |
+
t_qbit = int(m_i[0].strip('[]'))
|
78 |
+
f.write('cir.{}({})\n'.format(s.group(), t_qbit))
|
79 |
+
continue
|
80 |
+
|
81 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
82 |
+
lc = lc + 1
|
83 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
84 |
+
# print(m_r)
|
85 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
86 |
+
sp_str = m_r[0].split('e-')
|
87 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
88 |
+
elif 'pi' in m_r[0]:
|
89 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
90 |
+
# print(m_r)
|
91 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
92 |
+
t_qbit = int(m_i[0].strip('[]'))
|
93 |
+
f.write('cir.{}(np.pi*{}, {})\n'.format(s.group(), float(m_r[0][0]), t_qbit))
|
94 |
+
continue
|
95 |
+
|
96 |
+
elif s.group() == 'cx':
|
97 |
+
lc = lc + 1
|
98 |
+
match = re.findall(r'\[\d\d*\]', line)
|
99 |
+
c_qbit = int(match[0].strip('[]'))
|
100 |
+
t_qbit = int(match[1].strip('[]'))
|
101 |
+
f.write('cir.cx({}, {})\n'.format(c_qbit, t_qbit))
|
102 |
+
continue
|
103 |
+
|
104 |
+
elif s.group() == 'u2':
|
105 |
+
lc = lc + 1
|
106 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
107 |
+
m_r = m_r[0].split(',')
|
108 |
+
for i, m in enumerate(m_r):
|
109 |
+
if 'pi' in m and 'e' in m:
|
110 |
+
sp_str = m_r[i].split('e-')
|
111 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
112 |
+
elif 'pi' in m:
|
113 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
114 |
+
else:
|
115 |
+
m_r[i] = '0'
|
116 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
117 |
+
t_qbit = int(m_i[0].strip('[]'))
|
118 |
+
# f.write('cir.u(np.pi/2., np.pi*{}, np.pi*{}, {})\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
119 |
+
f.write('cir.append(U2(np.pi*{}, np.pi*{}), [{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
120 |
+
continue
|
121 |
+
|
122 |
+
elif s.group() == 'u3':
|
123 |
+
lc = lc + 1
|
124 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
125 |
+
m_r = m_r[0].split(',')
|
126 |
+
for i, m in enumerate(m_r):
|
127 |
+
if 'pi' in m and 'e' in m:
|
128 |
+
sp_str = m_r[i].split('e-')
|
129 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
130 |
+
elif 'pi' in m:
|
131 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
132 |
+
else:
|
133 |
+
m_r[i] = '0'
|
134 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
135 |
+
t_qbit = int(m_i[0].strip('[]'))
|
136 |
+
# f.write('cir.u(np.pi*{}, np.pi*{}, np.pi*{}, {})\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
137 |
+
f.write('cir.append(U3(np.pi*{}, np.pi*{}, np.pi*{}), [{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
138 |
+
continue
|
139 |
+
|
140 |
+
f.write('job = backend.run(transpile(cir, backend))\n')
|
141 |
+
f.write('result = job.result()\n')
|
142 |
+
f.write('outputstate = result.get_statevector(cir)\n')
|
143 |
+
|
144 |
+
f.write('t_e = perf_counter()\n')
|
145 |
+
f.write('t_ep = process_time()\n')
|
146 |
+
f.write('print(t_e - t_s)\n')
|
147 |
+
f.write('print(t_ep - t_sp)\n')
|
148 |
+
# f.write('print(outputstate)')
|
149 |
+
|
150 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
151 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
152 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
153 |
+
print(int(lc_res) == lc)
|
154 |
+
# print(lc)
|
155 |
+
|
156 |
+
# parse_qasm_to_package_gen(12, 1, 42, "../qasm_test/qasm_rqc_test.qasm", "test_qiskit.py")
|
157 |
+
# parse_qasm_to_package_gen(12, 1, 42, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_qiskit_2.py")
|
158 |
+
|
159 |
+
for N in range(12, 42, 2):
|
160 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
161 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
162 |
+
|
163 |
+
output_file_path = '/data/user/gangap_a/rqc/qiskit_cont_ngpu/qiskit_gpu_n8_off_run_files/'
|
164 |
+
output_file = output_file_path + 'qiskit_rqc_n{}.py'.format(N)
|
165 |
+
parse_qasm_to_package_gen(N, 8, False, 'double', input_file, output_file)
|
166 |
+
|
167 |
+
for N in range(41, 51):
|
168 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
169 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
170 |
+
|
171 |
+
output_file_path = '/data/user/gangap_a/rqc/qiskit_cont_ngpu/qiskit_gpu_n8_off_run_files/'
|
172 |
+
output_file = output_file_path + 'qiskit_rqc_n{}.py'.format(N)
|
173 |
+
parse_qasm_to_package_gen(N, 8, False, 'double', input_file, output_file)
|
qasm_parser/qpanda/qasm_qpanda_parser.py
ADDED
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
import os
|
6 |
+
|
7 |
+
def parse_qasm_to_package_gen(N, com_cap, input_filename, output_filename, save_data_path):
|
8 |
+
|
9 |
+
with open(output_filename, 'w') as f:
|
10 |
+
|
11 |
+
f.write('import os\n')
|
12 |
+
f.write('from time import process_time, perf_counter\n')
|
13 |
+
f.write('import numpy as np\n\n')
|
14 |
+
f.write('from pyqpanda import *\n')
|
15 |
+
|
16 |
+
f.write('def SX_gate(qp, qb):\n')
|
17 |
+
f.write(' qp << U4(qb, np.pi/4., np.pi/2., -np.pi/2., -np.pi/2.)\n')
|
18 |
+
|
19 |
+
f.write('def SXDG_gate(qp, qb):\n')
|
20 |
+
f.write(' qp << U4(qb, -np.pi/4., np.pi/2., np.pi/2., -np.pi/2.)\n')
|
21 |
+
|
22 |
+
f.write('def U2_gate(qp, qb, p, l):\n')
|
23 |
+
f.write(' qp << U4(qb, 0., p, np.pi/2., l)\n')
|
24 |
+
|
25 |
+
f.write('def U3_gate(qp, qb, t, p, l):\n')
|
26 |
+
f.write(' qp << U4(qb, 0., p, t, l)\n')
|
27 |
+
|
28 |
+
f.write('t_sp = process_time()\n')
|
29 |
+
f.write('t_s = perf_counter()\n')
|
30 |
+
|
31 |
+
if com_cap == 'st':
|
32 |
+
# f.write('init(QMachineType.CPU_SINGLE_THREAD)\n')
|
33 |
+
f.write('qvm = init_quantum_machine(QMachineType.CPU_SINGLE_THREAD)\n')
|
34 |
+
elif com_cap == 'mt':
|
35 |
+
f.write('qvm = CPUQVM()\n')
|
36 |
+
f.write('qvm.init_qvm()\n')
|
37 |
+
elif com_cap == 'gpu':
|
38 |
+
f.write('qvm = init_quantum_machine(QMachineType.GPU)\n')
|
39 |
+
|
40 |
+
f.write('qvm.set_configure({}, {})\n'.format(N, N))
|
41 |
+
f.write('q_bits = qvm.qAlloc_many({})\n'.format(N))
|
42 |
+
f.write('qprog = QProg()\n')
|
43 |
+
|
44 |
+
with open(input_filename, "r") as ifile:
|
45 |
+
lines = ifile.readlines()
|
46 |
+
|
47 |
+
lc = 0
|
48 |
+
cirq_c = 0
|
49 |
+
for line in lines:
|
50 |
+
|
51 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
52 |
+
|
53 |
+
if s is None:
|
54 |
+
continue
|
55 |
+
|
56 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
57 |
+
lc = lc + 1
|
58 |
+
f.write('# {}\n'.format(s.group()))
|
59 |
+
continue
|
60 |
+
|
61 |
+
elif s.group() == 'cirq':
|
62 |
+
lc = lc + 1
|
63 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
64 |
+
cirq_c = cirq_c + 1
|
65 |
+
continue
|
66 |
+
|
67 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'h':
|
68 |
+
lc = lc + 1
|
69 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
70 |
+
t_qbit = int(m_i[0].strip('[]'))
|
71 |
+
f.write('qprog << {}(q_bits[{}])\n'.format(s.group().upper(), t_qbit))
|
72 |
+
continue
|
73 |
+
|
74 |
+
elif s.group() == 'sx':
|
75 |
+
lc = lc + 1
|
76 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
77 |
+
t_qbit = int(m_i[0].strip('[]'))
|
78 |
+
f.write('SX_gate(qprog, q_bits[{}])\n'.format(t_qbit))
|
79 |
+
|
80 |
+
elif s.group() == 'sxdg':
|
81 |
+
lc = lc + 1
|
82 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
83 |
+
t_qbit = int(m_i[0].strip('[]'))
|
84 |
+
f.write('SXDG_gate(qprog, q_bits[{}])\n'.format(t_qbit))
|
85 |
+
|
86 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
87 |
+
lc = lc + 1
|
88 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
89 |
+
# print(m_r)
|
90 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
91 |
+
sp_str = m_r[0].split('e-')
|
92 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
93 |
+
elif 'pi' in m_r[0]:
|
94 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
95 |
+
# print(m_r)
|
96 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
97 |
+
t_qbit = int(m_i[0].strip('[]'))
|
98 |
+
f.write('qprog << {}(q_bits[{}], np.pi*{})\n'.format(s.group().upper(), t_qbit, float(m_r[0][0])))
|
99 |
+
continue
|
100 |
+
|
101 |
+
elif s.group() == 'cx':
|
102 |
+
lc = lc + 1
|
103 |
+
match = re.findall(r'\[\d\d*\]', line)
|
104 |
+
c_qbit = int(match[0].strip('[]'))
|
105 |
+
t_qbit = int(match[1].strip('[]'))
|
106 |
+
# f.write('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit))
|
107 |
+
f.write('qprog << CNOT(q_bits[{}], q_bits[{}])\n'.format(c_qbit, t_qbit))
|
108 |
+
continue
|
109 |
+
|
110 |
+
elif s.group() == 'u2':
|
111 |
+
lc = lc + 1
|
112 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
113 |
+
m_r = m_r[0].split(',')
|
114 |
+
for i, m in enumerate(m_r):
|
115 |
+
if 'pi' in m and 'e' in m:
|
116 |
+
sp_str = m_r[i].split('e-')
|
117 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
118 |
+
elif 'pi' in m:
|
119 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
120 |
+
else:
|
121 |
+
m_r[i] = '0'
|
122 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
123 |
+
t_qbit = int(m_i[0].strip('[]'))
|
124 |
+
f.write('U2_gate(qprog, q_bits[{}], np.pi*{}, np.pi*{})\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
125 |
+
continue
|
126 |
+
|
127 |
+
elif s.group() == 'u3':
|
128 |
+
lc = lc + 1
|
129 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
130 |
+
m_r = m_r[0].split(',')
|
131 |
+
for i, m in enumerate(m_r):
|
132 |
+
if 'pi' in m and 'e' in m:
|
133 |
+
sp_str = m_r[i].split('e-')
|
134 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
135 |
+
elif 'pi' in m:
|
136 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
137 |
+
else:
|
138 |
+
m_r[i] = '0'
|
139 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
140 |
+
t_qbit = int(m_i[0].strip('[]'))
|
141 |
+
f.write('U3_gate(qprog, q_bits[{}], np.pi*{}, np.pi*{}, np.pi*{})\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
142 |
+
continue
|
143 |
+
|
144 |
+
f.write('result = qvm.directly_run(qprog)\n')
|
145 |
+
f.write('t_e = perf_counter()\n')
|
146 |
+
f.write('t_ep = process_time()\n')
|
147 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
148 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
149 |
+
f.write('print(t_e - t_s)\n')
|
150 |
+
f.write('print(t_ep - t_sp)\n')
|
151 |
+
# f.write('print(qvm.get_qstate())\n')
|
152 |
+
|
153 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
154 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
155 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
156 |
+
print(int(lc_res) == lc)
|
157 |
+
# print(lc)
|
158 |
+
|
159 |
+
task = 'hdyn'
|
160 |
+
sim_pack = 'qpanda'
|
161 |
+
c_cap = 'gpu'
|
162 |
+
prec = 'dp'
|
163 |
+
|
164 |
+
for N in range(6, 38, 2):
|
165 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
166 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
167 |
+
|
168 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, c_cap, prec)
|
169 |
+
|
170 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, c_cap, prec)
|
171 |
+
|
172 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
173 |
+
parse_qasm_to_package_gen(N, c_cap, input_file, output_file, save_file_path)
|
174 |
+
|
qasm_parser/qpp/qasm_qpp_parser.py
ADDED
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('#include <stdio.h>\n')
|
11 |
+
f.write('#include <math.h>\n')
|
12 |
+
f.write('#include <time.h>\n')
|
13 |
+
f.write('#include <stdlib.h>\n')
|
14 |
+
|
15 |
+
f.write('#include \"qpp.h\"\n')
|
16 |
+
|
17 |
+
f.write('using namespace qpp;\n')
|
18 |
+
|
19 |
+
f.write('#define BILLION 1E9\n')
|
20 |
+
|
21 |
+
f.write('\n')
|
22 |
+
|
23 |
+
f.write('cmat u2g(double p, double l){\n')
|
24 |
+
f.write('\tcmat u2{cmat::Zero(2, 2)};\n')
|
25 |
+
|
26 |
+
f.write('\tu2 << std::exp(-1_i * (p + l) / 2.0)/std::sqrt(2),\n')
|
27 |
+
f.write('\t-std::exp(-1_i * (p - l) / 2.0)/std::sqrt(2),\n')
|
28 |
+
f.write('\tstd::exp(1_i * (p - l) / 2.0)/std::sqrt(2),\n')
|
29 |
+
f.write('\tstd::exp(1_i * (p + l) / 2.0)/std::sqrt(2);\n')
|
30 |
+
f.write('\treturn u2;\n')
|
31 |
+
f.write('}\n')
|
32 |
+
|
33 |
+
f.write('cmat u3g(double t, double p, double l){\n')
|
34 |
+
f.write('\tcmat u3{cmat::Zero(2, 2)};\n')
|
35 |
+
|
36 |
+
f.write('\tu3 << std::exp(-1_i * (p + l) / 2.0) * std::cos(t / 2.0),\n')
|
37 |
+
f.write('\t-std::exp(-1_i * (p - l) / 2.0) * std::sin(t / 2.0),\n')
|
38 |
+
f.write('\tstd::exp(1_i * (p - l) / 2.0) * std::sin(t / 2.0),\n')
|
39 |
+
f.write('\tstd::exp(1_i * (p + l) / 2.0) * std::cos(t / 2.0);\n')
|
40 |
+
|
41 |
+
f.write('\treturn u3;\n')
|
42 |
+
f.write('}\n')
|
43 |
+
|
44 |
+
# f.write('int main(int argc, char* argv[]){\n')
|
45 |
+
f.write('int main(){\n')
|
46 |
+
|
47 |
+
f.write('\tusing namespace qpp;\n')
|
48 |
+
# f.write('\tQCircuit qc{{}};\n'.format(N))
|
49 |
+
f.write('\tQCircuit qc{' + str(N) + '};\n')
|
50 |
+
|
51 |
+
f.write('\tstruct timespec requestStart, requestEnd;\n')
|
52 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestStart);\n')
|
53 |
+
|
54 |
+
lc = 0
|
55 |
+
|
56 |
+
with open(input_filename, "r") as ifile:
|
57 |
+
lines = ifile.readlines()
|
58 |
+
|
59 |
+
k = 0
|
60 |
+
for line in lines:
|
61 |
+
|
62 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
63 |
+
|
64 |
+
if s is None:
|
65 |
+
continue
|
66 |
+
|
67 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits', 'cirq']:
|
68 |
+
lc = lc + 1
|
69 |
+
f.write('// {}\n'.format(s.group()))
|
70 |
+
continue
|
71 |
+
|
72 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 'sx' or s.group() == 'sxdg' or s.group() == 'h' or s.group() == 's':
|
73 |
+
lc = lc + 1
|
74 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
75 |
+
t_qbit = int(m_i[0].strip('[]'))
|
76 |
+
f.write('\tqc.gate(gt.{}, {});\n'.format(s.group().upper(), t_qbit))
|
77 |
+
continue
|
78 |
+
|
79 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
80 |
+
lc = lc + 1
|
81 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
82 |
+
# print(m_r)
|
83 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
84 |
+
sp_str = m_r[0].split('e-')
|
85 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
86 |
+
elif 'pi' in m_r[0]:
|
87 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
88 |
+
# print(m_r)
|
89 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
90 |
+
t_qbit = int(m_i[0].strip('[]'))
|
91 |
+
f.write('\tcmat U_{} = gt.{}(M_PI*{});\n'.format(k, s.group().upper(), float(m_r[0][0])))
|
92 |
+
f.write('\tqc.gate(U_{}, {}, \"{}\");\n'.format(k, t_qbit, s.group()))
|
93 |
+
k = k + 1
|
94 |
+
continue
|
95 |
+
|
96 |
+
elif s.group() == 'cx':
|
97 |
+
lc = lc + 1
|
98 |
+
match = re.findall(r'\[\d\d*\]', line)
|
99 |
+
c_qbit = int(match[0].strip('[]'))
|
100 |
+
t_qbit = int(match[1].strip('[]'))
|
101 |
+
f.write('\tqc.gate(gt.CNOT, {}, {});\n'.format(c_qbit, t_qbit))
|
102 |
+
continue
|
103 |
+
|
104 |
+
elif s.group() == 'u2':
|
105 |
+
lc = lc + 1
|
106 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
107 |
+
m_r = m_r[0].split(',')
|
108 |
+
for i, m in enumerate(m_r):
|
109 |
+
if 'pi' in m and 'e' in m:
|
110 |
+
sp_str = m_r[i].split('e-')
|
111 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
112 |
+
elif 'pi' in m:
|
113 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
114 |
+
else:
|
115 |
+
m_r[i] = '0'
|
116 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
117 |
+
t_qbit = int(m_i[0].strip('[]'))
|
118 |
+
|
119 |
+
f.write('\tcmat u2_' + str(k) + '{cmat::Zero(2, 2)};\n')
|
120 |
+
|
121 |
+
f.write('\tu2_{} << u2g(M_PI*{}, M_PI*{});\n'.format(k, float(m_r[0][0]), float(m_r[1][0])))
|
122 |
+
f.write('\tqc.gate(u2_{}, {}, \"u2\");\n'.format(k, t_qbit))
|
123 |
+
k = k + 1
|
124 |
+
|
125 |
+
continue
|
126 |
+
|
127 |
+
elif s.group() == 'u3':
|
128 |
+
lc = lc + 1
|
129 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
130 |
+
m_r = m_r[0].split(',')
|
131 |
+
for i, m in enumerate(m_r):
|
132 |
+
if 'pi' in m and 'e' in m:
|
133 |
+
sp_str = m_r[i].split('e-')
|
134 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
135 |
+
elif 'pi' in m:
|
136 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
137 |
+
else:
|
138 |
+
m_r[i] = '0'
|
139 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
140 |
+
t_qbit = int(m_i[0].strip('[]'))
|
141 |
+
|
142 |
+
f.write('\tcmat u3_' + str(k) + '{cmat::Zero(2, 2)};\n')
|
143 |
+
|
144 |
+
f.write('\tu3_{} << u3g(M_PI*{}, M_PI*{}, M_PI*{});\n'.format(k, float(m_r[0][0]), float(m_r[1][0]), float(m_r[2][0])))
|
145 |
+
f.write('\tqc.gate(u3_{}, {}, \"u3\");\n'.format(k, t_qbit))
|
146 |
+
k = k + 1
|
147 |
+
|
148 |
+
continue
|
149 |
+
|
150 |
+
f.write('\tQEngine engine{qc};\n')
|
151 |
+
f.write('\tengine.execute();\n')
|
152 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestEnd);\n')
|
153 |
+
|
154 |
+
f.write('\tdouble accum = (requestEnd.tv_sec - requestStart.tv_sec) + (requestEnd.tv_nsec - requestStart.tv_nsec)/BILLION;\n')
|
155 |
+
f.write('\tprintf( "%lf, ", accum );\n')
|
156 |
+
f.write('\treturn 0;\n')
|
157 |
+
f.write('}')
|
158 |
+
|
159 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
160 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
161 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
162 |
+
print(int(lc_res) == lc)
|
163 |
+
# print(lc)
|
164 |
+
|
165 |
+
task = 'qft'
|
166 |
+
sim_pack = 'qpp'
|
167 |
+
com_cap = 'mt'
|
168 |
+
prec = 'dp'
|
169 |
+
|
170 |
+
for N in range(6, 40, 2):
|
171 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
172 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
173 |
+
|
174 |
+
# save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
175 |
+
|
176 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
177 |
+
|
178 |
+
output_file = output_file_path + '{}_n{}.cpp'.format(task, N)
|
179 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
180 |
+
|
181 |
+
|
182 |
+
r"""
|
183 |
+
for N in range(12, 42, 2):
|
184 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
185 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
186 |
+
|
187 |
+
output_file_path = '/data/user/gangap_a/rqc/quest/QuEST/rqc/quest_sp_run_files/'
|
188 |
+
output_file = output_file_path + 'quest_rqc_n{}.c'.format(N)
|
189 |
+
parse_qasm_to_package_gen(N, 'float', input_file, output_file)
|
190 |
+
|
191 |
+
for N in range(41, 51):
|
192 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
193 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
194 |
+
|
195 |
+
output_file_path = '/data/user/gangap_a/rqc/quest/QuEST/rqc/quest_sp_run_files/'
|
196 |
+
output_file = output_file_path + 'quest_rqc_n{}.c'.format(N)
|
197 |
+
parse_qasm_to_package_gen(N, 'float', input_file, output_file)
|
198 |
+
|
199 |
+
"""
|
qasm_parser/qrack/qasm_qrack_opt_gpu_parser.py
ADDED
@@ -0,0 +1,179 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, _prec, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('#include <time.h>\n')
|
11 |
+
f.write('#define BILLION 1E9\n')
|
12 |
+
|
13 |
+
# f.write('#include \"qrack/qfactory.hpp\"\n')
|
14 |
+
f.write('#include \"/sim_lib/qrack/qrack/build_gpu_{}/include/qfactory.hpp\"\n'.format(_prec))
|
15 |
+
|
16 |
+
f.write('#include <algorithm>\n') # // std::random_shuffle
|
17 |
+
f.write('#include <cstddef>\n') # // size_t
|
18 |
+
f.write('#include <iostream>\n') # // std::cout
|
19 |
+
|
20 |
+
f.write('using namespace Qrack;\n')
|
21 |
+
|
22 |
+
f.write('\n')
|
23 |
+
f.write('int main(int argc, char* argv[]){\n')
|
24 |
+
|
25 |
+
f.write('\tstruct timespec requestStart, requestEnd;\n')
|
26 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestStart);\n')
|
27 |
+
f.write('\tQInterfacePtr qReg = CreateQuantumInterface(QINTERFACE_OPTIMAL, {}, 0);\n'.format(N))
|
28 |
+
# f.write('\tQInterfacePtr qReg = CreateQuantumInterface(QINTERFACE_CPU, {}, 0);\n'.format(N))
|
29 |
+
|
30 |
+
lc = 0
|
31 |
+
|
32 |
+
with open(input_filename, "r") as ifile:
|
33 |
+
lines = ifile.readlines()
|
34 |
+
|
35 |
+
for line in lines:
|
36 |
+
|
37 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
38 |
+
|
39 |
+
if s is None:
|
40 |
+
continue
|
41 |
+
|
42 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits', 'cirq']:
|
43 |
+
lc = lc + 1
|
44 |
+
f.write('\t// {}\n'.format(s.group()))
|
45 |
+
continue
|
46 |
+
|
47 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 'h' or s.group() == 's':
|
48 |
+
lc = lc + 1
|
49 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
50 |
+
t_qbit = int(m_i[0].strip('[]'))
|
51 |
+
# f.write('\tpauli{}(qubits, {});\n'.format(s.group().upper(), t_qbit))
|
52 |
+
f.write('\tqReg->{}({});\n'.format(s.group().upper(), t_qbit))
|
53 |
+
continue
|
54 |
+
|
55 |
+
elif s.group() == 'sx':
|
56 |
+
lc = lc + 1
|
57 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
58 |
+
t_qbit = int(m_i[0].strip('[]'))
|
59 |
+
# f.write('\thadamard(qubits, {});\n'.format(t_qbit))
|
60 |
+
f.write('\tqReg->SqrtX({});\n'.format(t_qbit))
|
61 |
+
continue
|
62 |
+
|
63 |
+
elif s.group() == 'sxdg':
|
64 |
+
lc = lc + 1
|
65 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
66 |
+
t_qbit = int(m_i[0].strip('[]'))
|
67 |
+
# f.write('\thadamard(qubits, {});\n'.format(t_qbit))
|
68 |
+
f.write('\tqReg->ISqrtX({});\n'.format(t_qbit))
|
69 |
+
continue
|
70 |
+
|
71 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
72 |
+
lc = lc + 1
|
73 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
74 |
+
# print(m_r)
|
75 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
76 |
+
sp_str = m_r[0].split('e-')
|
77 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
78 |
+
elif 'pi' in m_r[0]:
|
79 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
80 |
+
# print(m_r)
|
81 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
82 |
+
t_qbit = int(m_i[0].strip('[]'))
|
83 |
+
# f.write('\trotate{}(qubits, {}, M_PI*{});\n'.format(s.group()[1].upper(), t_qbit, float(m_r[0][0])))
|
84 |
+
f.write('\tqReg->R{}(M_PI*{}, {});\n'.format(s.group()[1].upper(), float(m_r[0][0]), t_qbit))
|
85 |
+
continue
|
86 |
+
|
87 |
+
elif s.group() == 'cx':
|
88 |
+
lc = lc + 1
|
89 |
+
match = re.findall(r'\[\d\d*\]', line)
|
90 |
+
c_qbit = int(match[0].strip('[]'))
|
91 |
+
t_qbit = int(match[1].strip('[]'))
|
92 |
+
# f.write('\tcontrolledNot(qubits, {}, {});\n'.format(c_qbit, t_qbit))
|
93 |
+
f.write('\tqReg->CNOT({}, {});\n'.format(c_qbit, t_qbit))
|
94 |
+
continue
|
95 |
+
|
96 |
+
elif s.group() == 'u2':
|
97 |
+
lc = lc + 1
|
98 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
99 |
+
m_r = m_r[0].split(',')
|
100 |
+
for i, m in enumerate(m_r):
|
101 |
+
if 'pi' in m and 'e' in m:
|
102 |
+
sp_str = m_r[i].split('e-')
|
103 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
104 |
+
elif 'pi' in m:
|
105 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
106 |
+
else:
|
107 |
+
m_r[i] = '0'
|
108 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
109 |
+
t_qbit = int(m_i[0].strip('[]'))
|
110 |
+
# f.write('\tu2(qubits, {}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
111 |
+
f.write('\tqReg->U2({}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
112 |
+
continue
|
113 |
+
|
114 |
+
elif s.group() == 'u3':
|
115 |
+
lc = lc + 1
|
116 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
117 |
+
m_r = m_r[0].split(',')
|
118 |
+
for i, m in enumerate(m_r):
|
119 |
+
if 'pi' in m and 'e' in m:
|
120 |
+
sp_str = m_r[i].split('e-')
|
121 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
122 |
+
elif 'pi' in m:
|
123 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
124 |
+
else:
|
125 |
+
m_r[i] = '0'
|
126 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
127 |
+
t_qbit = int(m_i[0].strip('[]'))
|
128 |
+
# f.write('\tu3(qubits, {}, M_PI*{}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
129 |
+
f.write('\tqReg->U({}, M_PI*{}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
130 |
+
continue
|
131 |
+
|
132 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestEnd);\n')
|
133 |
+
|
134 |
+
f.write('\tdouble accum = (requestEnd.tv_sec - requestStart.tv_sec) + (requestEnd.tv_nsec - requestStart.tv_nsec)/BILLION;\n')
|
135 |
+
f.write('\tprintf( "%lf, ", accum );\n')
|
136 |
+
f.write('\treturn 0;\n')
|
137 |
+
f.write('}')
|
138 |
+
|
139 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
140 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
141 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
142 |
+
print(int(lc_res) == lc)
|
143 |
+
# print(lc)
|
144 |
+
|
145 |
+
task = 'qft'
|
146 |
+
sim_pack = 'qrack_opt'
|
147 |
+
com_cap = 'gpu'
|
148 |
+
prec = 'sp'
|
149 |
+
|
150 |
+
for N in range(6, 40, 2):
|
151 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
152 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
153 |
+
|
154 |
+
# save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
155 |
+
|
156 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
157 |
+
|
158 |
+
output_file = output_file_path + '{}_n{}.cpp'.format(task, N)
|
159 |
+
parse_qasm_to_package_gen(N, prec, input_file, output_file)
|
160 |
+
|
161 |
+
|
162 |
+
r"""
|
163 |
+
for N in range(12, 42, 2):
|
164 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
165 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
166 |
+
|
167 |
+
output_file_path = '/data/user/gangap_a/rqc/quest/QuEST/rqc/quest_sp_run_files/'
|
168 |
+
output_file = output_file_path + 'quest_rqc_n{}.c'.format(N)
|
169 |
+
parse_qasm_to_package_gen(N, 'float', input_file, output_file)
|
170 |
+
|
171 |
+
for N in range(41, 51):
|
172 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
173 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
174 |
+
|
175 |
+
output_file_path = '/data/user/gangap_a/rqc/quest/QuEST/rqc/quest_sp_run_files/'
|
176 |
+
output_file = output_file_path + 'quest_rqc_n{}.c'.format(N)
|
177 |
+
parse_qasm_to_package_gen(N, 'float', input_file, output_file)
|
178 |
+
|
179 |
+
"""
|
qasm_parser/qrack/qasm_qrack_opt_parser.py
ADDED
@@ -0,0 +1,179 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, _prec, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('#include <time.h>\n')
|
11 |
+
f.write('#define BILLION 1E9\n')
|
12 |
+
|
13 |
+
# f.write('#include \"qrack/qfactory.hpp\"\n')
|
14 |
+
f.write('#include \"/sim_lib/qrack/qrack/build_st_{}/include/qfactory.hpp\"\n'.format(_prec))
|
15 |
+
|
16 |
+
f.write('#include <algorithm>\n') # // std::random_shuffle
|
17 |
+
f.write('#include <cstddef>\n') # // size_t
|
18 |
+
f.write('#include <iostream>\n') # // std::cout
|
19 |
+
|
20 |
+
f.write('using namespace Qrack;\n')
|
21 |
+
|
22 |
+
f.write('\n')
|
23 |
+
f.write('int main(int argc, char* argv[]){\n')
|
24 |
+
|
25 |
+
f.write('\tstruct timespec requestStart, requestEnd;\n')
|
26 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestStart);\n')
|
27 |
+
f.write('\tQInterfacePtr qReg = CreateQuantumInterface(QINTERFACE_OPTIMAL, {}, 0);\n'.format(N))
|
28 |
+
# f.write('\tQInterfacePtr qReg = CreateQuantumInterface(QINTERFACE_CPU, {}, 0);\n'.format(N))
|
29 |
+
|
30 |
+
lc = 0
|
31 |
+
|
32 |
+
with open(input_filename, "r") as ifile:
|
33 |
+
lines = ifile.readlines()
|
34 |
+
|
35 |
+
for line in lines:
|
36 |
+
|
37 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
38 |
+
|
39 |
+
if s is None:
|
40 |
+
continue
|
41 |
+
|
42 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits', 'cirq']:
|
43 |
+
lc = lc + 1
|
44 |
+
f.write('\t// {}\n'.format(s.group()))
|
45 |
+
continue
|
46 |
+
|
47 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 'h' or s.group() == 's':
|
48 |
+
lc = lc + 1
|
49 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
50 |
+
t_qbit = int(m_i[0].strip('[]'))
|
51 |
+
# f.write('\tpauli{}(qubits, {});\n'.format(s.group().upper(), t_qbit))
|
52 |
+
f.write('\tqReg->{}({});\n'.format(s.group().upper(), t_qbit))
|
53 |
+
continue
|
54 |
+
|
55 |
+
elif s.group() == 'sx':
|
56 |
+
lc = lc + 1
|
57 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
58 |
+
t_qbit = int(m_i[0].strip('[]'))
|
59 |
+
# f.write('\thadamard(qubits, {});\n'.format(t_qbit))
|
60 |
+
f.write('\tqReg->SqrtX({});\n'.format(t_qbit))
|
61 |
+
continue
|
62 |
+
|
63 |
+
elif s.group() == 'sxdg':
|
64 |
+
lc = lc + 1
|
65 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
66 |
+
t_qbit = int(m_i[0].strip('[]'))
|
67 |
+
# f.write('\thadamard(qubits, {});\n'.format(t_qbit))
|
68 |
+
f.write('\tqReg->ISqrtX({});\n'.format(t_qbit))
|
69 |
+
continue
|
70 |
+
|
71 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
72 |
+
lc = lc + 1
|
73 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
74 |
+
# print(m_r)
|
75 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
76 |
+
sp_str = m_r[0].split('e-')
|
77 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
78 |
+
elif 'pi' in m_r[0]:
|
79 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
80 |
+
# print(m_r)
|
81 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
82 |
+
t_qbit = int(m_i[0].strip('[]'))
|
83 |
+
# f.write('\trotate{}(qubits, {}, M_PI*{});\n'.format(s.group()[1].upper(), t_qbit, float(m_r[0][0])))
|
84 |
+
f.write('\tqReg->R{}(M_PI*{}, {});\n'.format(s.group()[1].upper(), float(m_r[0][0]), t_qbit))
|
85 |
+
continue
|
86 |
+
|
87 |
+
elif s.group() == 'cx':
|
88 |
+
lc = lc + 1
|
89 |
+
match = re.findall(r'\[\d\d*\]', line)
|
90 |
+
c_qbit = int(match[0].strip('[]'))
|
91 |
+
t_qbit = int(match[1].strip('[]'))
|
92 |
+
# f.write('\tcontrolledNot(qubits, {}, {});\n'.format(c_qbit, t_qbit))
|
93 |
+
f.write('\tqReg->CNOT({}, {});\n'.format(c_qbit, t_qbit))
|
94 |
+
continue
|
95 |
+
|
96 |
+
elif s.group() == 'u2':
|
97 |
+
lc = lc + 1
|
98 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
99 |
+
m_r = m_r[0].split(',')
|
100 |
+
for i, m in enumerate(m_r):
|
101 |
+
if 'pi' in m and 'e' in m:
|
102 |
+
sp_str = m_r[i].split('e-')
|
103 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
104 |
+
elif 'pi' in m:
|
105 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
106 |
+
else:
|
107 |
+
m_r[i] = '0'
|
108 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
109 |
+
t_qbit = int(m_i[0].strip('[]'))
|
110 |
+
# f.write('\tu2(qubits, {}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
111 |
+
f.write('\tqReg->U2({}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
112 |
+
continue
|
113 |
+
|
114 |
+
elif s.group() == 'u3':
|
115 |
+
lc = lc + 1
|
116 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
117 |
+
m_r = m_r[0].split(',')
|
118 |
+
for i, m in enumerate(m_r):
|
119 |
+
if 'pi' in m and 'e' in m:
|
120 |
+
sp_str = m_r[i].split('e-')
|
121 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
122 |
+
elif 'pi' in m:
|
123 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
124 |
+
else:
|
125 |
+
m_r[i] = '0'
|
126 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
127 |
+
t_qbit = int(m_i[0].strip('[]'))
|
128 |
+
# f.write('\tu3(qubits, {}, M_PI*{}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
129 |
+
f.write('\tqReg->U({}, M_PI*{}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
130 |
+
continue
|
131 |
+
|
132 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestEnd);\n')
|
133 |
+
|
134 |
+
f.write('\tdouble accum = (requestEnd.tv_sec - requestStart.tv_sec) + (requestEnd.tv_nsec - requestStart.tv_nsec)/BILLION;\n')
|
135 |
+
f.write('\tprintf( "%lf, ", accum );\n')
|
136 |
+
f.write('\treturn 0;\n')
|
137 |
+
f.write('}')
|
138 |
+
|
139 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
140 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
141 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
142 |
+
print(int(lc_res) == lc)
|
143 |
+
# print(lc)
|
144 |
+
|
145 |
+
task = 'qft'
|
146 |
+
sim_pack = 'qrack_opt'
|
147 |
+
com_cap = 'mt'
|
148 |
+
prec = 'sp'
|
149 |
+
|
150 |
+
for N in range(6, 40, 2):
|
151 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
152 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
153 |
+
|
154 |
+
# save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
155 |
+
|
156 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
157 |
+
|
158 |
+
output_file = output_file_path + '{}_n{}.cpp'.format(task, N)
|
159 |
+
parse_qasm_to_package_gen(N, prec, input_file, output_file)
|
160 |
+
|
161 |
+
|
162 |
+
r"""
|
163 |
+
for N in range(12, 42, 2):
|
164 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
165 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
166 |
+
|
167 |
+
output_file_path = '/data/user/gangap_a/rqc/quest/QuEST/rqc/quest_sp_run_files/'
|
168 |
+
output_file = output_file_path + 'quest_rqc_n{}.c'.format(N)
|
169 |
+
parse_qasm_to_package_gen(N, 'float', input_file, output_file)
|
170 |
+
|
171 |
+
for N in range(41, 51):
|
172 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
173 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
174 |
+
|
175 |
+
output_file_path = '/data/user/gangap_a/rqc/quest/QuEST/rqc/quest_sp_run_files/'
|
176 |
+
output_file = output_file_path + 'quest_rqc_n{}.c'.format(N)
|
177 |
+
parse_qasm_to_package_gen(N, 'float', input_file, output_file)
|
178 |
+
|
179 |
+
"""
|
qasm_parser/qrack/qasm_qrack_sch_gpu_parser.py
ADDED
@@ -0,0 +1,179 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, _prec, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('#include <time.h>\n')
|
11 |
+
f.write('#define BILLION 1E9\n')
|
12 |
+
|
13 |
+
# f.write('#include \"qrack/qfactory.hpp\"\n')
|
14 |
+
f.write('#include \"/sim_lib/qrack/qrack/build_gpu_{}/include/qfactory.hpp\"\n'.format(_prec))
|
15 |
+
|
16 |
+
f.write('#include <algorithm>\n') # // std::random_shuffle
|
17 |
+
f.write('#include <cstddef>\n') # // size_t
|
18 |
+
f.write('#include <iostream>\n') # // std::cout
|
19 |
+
|
20 |
+
f.write('using namespace Qrack;\n')
|
21 |
+
|
22 |
+
f.write('\n')
|
23 |
+
f.write('int main(int argc, char* argv[]){\n')
|
24 |
+
|
25 |
+
f.write('\tstruct timespec requestStart, requestEnd;\n')
|
26 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestStart);\n')
|
27 |
+
# f.write('\tQInterfacePtr qReg = CreateQuantumInterface(QINTERFACE_OPTIMAL, {}, 0);\n'.format(N))
|
28 |
+
f.write('\tQInterfacePtr qReg = CreateQuantumInterface(QINTERFACE_OPENCL, {}, 0);\n'.format(N))
|
29 |
+
|
30 |
+
lc = 0
|
31 |
+
|
32 |
+
with open(input_filename, "r") as ifile:
|
33 |
+
lines = ifile.readlines()
|
34 |
+
|
35 |
+
for line in lines:
|
36 |
+
|
37 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
38 |
+
|
39 |
+
if s is None:
|
40 |
+
continue
|
41 |
+
|
42 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits', 'cirq']:
|
43 |
+
lc = lc + 1
|
44 |
+
f.write('\t// {}\n'.format(s.group()))
|
45 |
+
continue
|
46 |
+
|
47 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 'h' or s.group() == 's':
|
48 |
+
lc = lc + 1
|
49 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
50 |
+
t_qbit = int(m_i[0].strip('[]'))
|
51 |
+
# f.write('\tpauli{}(qubits, {});\n'.format(s.group().upper(), t_qbit))
|
52 |
+
f.write('\tqReg->{}({});\n'.format(s.group().upper(), t_qbit))
|
53 |
+
continue
|
54 |
+
|
55 |
+
elif s.group() == 'sx':
|
56 |
+
lc = lc + 1
|
57 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
58 |
+
t_qbit = int(m_i[0].strip('[]'))
|
59 |
+
# f.write('\thadamard(qubits, {});\n'.format(t_qbit))
|
60 |
+
f.write('\tqReg->SqrtX({});\n'.format(t_qbit))
|
61 |
+
continue
|
62 |
+
|
63 |
+
elif s.group() == 'sxdg':
|
64 |
+
lc = lc + 1
|
65 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
66 |
+
t_qbit = int(m_i[0].strip('[]'))
|
67 |
+
# f.write('\thadamard(qubits, {});\n'.format(t_qbit))
|
68 |
+
f.write('\tqReg->ISqrtX({});\n'.format(t_qbit))
|
69 |
+
continue
|
70 |
+
|
71 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
72 |
+
lc = lc + 1
|
73 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
74 |
+
# print(m_r)
|
75 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
76 |
+
sp_str = m_r[0].split('e-')
|
77 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
78 |
+
elif 'pi' in m_r[0]:
|
79 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
80 |
+
# print(m_r)
|
81 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
82 |
+
t_qbit = int(m_i[0].strip('[]'))
|
83 |
+
# f.write('\trotate{}(qubits, {}, M_PI*{});\n'.format(s.group()[1].upper(), t_qbit, float(m_r[0][0])))
|
84 |
+
f.write('\tqReg->R{}(M_PI*{}, {});\n'.format(s.group()[1].upper(), float(m_r[0][0]), t_qbit))
|
85 |
+
continue
|
86 |
+
|
87 |
+
elif s.group() == 'cx':
|
88 |
+
lc = lc + 1
|
89 |
+
match = re.findall(r'\[\d\d*\]', line)
|
90 |
+
c_qbit = int(match[0].strip('[]'))
|
91 |
+
t_qbit = int(match[1].strip('[]'))
|
92 |
+
# f.write('\tcontrolledNot(qubits, {}, {});\n'.format(c_qbit, t_qbit))
|
93 |
+
f.write('\tqReg->CNOT({}, {});\n'.format(c_qbit, t_qbit))
|
94 |
+
continue
|
95 |
+
|
96 |
+
elif s.group() == 'u2':
|
97 |
+
lc = lc + 1
|
98 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
99 |
+
m_r = m_r[0].split(',')
|
100 |
+
for i, m in enumerate(m_r):
|
101 |
+
if 'pi' in m and 'e' in m:
|
102 |
+
sp_str = m_r[i].split('e-')
|
103 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
104 |
+
elif 'pi' in m:
|
105 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
106 |
+
else:
|
107 |
+
m_r[i] = '0'
|
108 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
109 |
+
t_qbit = int(m_i[0].strip('[]'))
|
110 |
+
# f.write('\tu2(qubits, {}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
111 |
+
f.write('\tqReg->U2({}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
112 |
+
continue
|
113 |
+
|
114 |
+
elif s.group() == 'u3':
|
115 |
+
lc = lc + 1
|
116 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
117 |
+
m_r = m_r[0].split(',')
|
118 |
+
for i, m in enumerate(m_r):
|
119 |
+
if 'pi' in m and 'e' in m:
|
120 |
+
sp_str = m_r[i].split('e-')
|
121 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
122 |
+
elif 'pi' in m:
|
123 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
124 |
+
else:
|
125 |
+
m_r[i] = '0'
|
126 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
127 |
+
t_qbit = int(m_i[0].strip('[]'))
|
128 |
+
# f.write('\tu3(qubits, {}, M_PI*{}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
129 |
+
f.write('\tqReg->U({}, M_PI*{}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
130 |
+
continue
|
131 |
+
|
132 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestEnd);\n')
|
133 |
+
|
134 |
+
f.write('\tdouble accum = (requestEnd.tv_sec - requestStart.tv_sec) + (requestEnd.tv_nsec - requestStart.tv_nsec)/BILLION;\n')
|
135 |
+
f.write('\tprintf( "%lf, ", accum );\n')
|
136 |
+
f.write('\treturn 0;\n')
|
137 |
+
f.write('}')
|
138 |
+
|
139 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
140 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
141 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
142 |
+
print(int(lc_res) == lc)
|
143 |
+
# print(lc)
|
144 |
+
|
145 |
+
task = 'hdyn'
|
146 |
+
sim_pack = 'qrack_sch'
|
147 |
+
com_cap = 'gpu'
|
148 |
+
prec = 'sp'
|
149 |
+
|
150 |
+
for N in range(6, 36, 2):
|
151 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
152 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
153 |
+
|
154 |
+
# save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
155 |
+
|
156 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
157 |
+
|
158 |
+
output_file = output_file_path + '{}_n{}.cpp'.format(task, N)
|
159 |
+
parse_qasm_to_package_gen(N, prec, input_file, output_file)
|
160 |
+
|
161 |
+
|
162 |
+
r"""
|
163 |
+
for N in range(12, 42, 2):
|
164 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
165 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
166 |
+
|
167 |
+
output_file_path = '/data/user/gangap_a/rqc/quest/QuEST/rqc/quest_sp_run_files/'
|
168 |
+
output_file = output_file_path + 'quest_rqc_n{}.c'.format(N)
|
169 |
+
parse_qasm_to_package_gen(N, 'float', input_file, output_file)
|
170 |
+
|
171 |
+
for N in range(41, 51):
|
172 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
173 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
174 |
+
|
175 |
+
output_file_path = '/data/user/gangap_a/rqc/quest/QuEST/rqc/quest_sp_run_files/'
|
176 |
+
output_file = output_file_path + 'quest_rqc_n{}.c'.format(N)
|
177 |
+
parse_qasm_to_package_gen(N, 'float', input_file, output_file)
|
178 |
+
|
179 |
+
"""
|
qasm_parser/qrack/qasm_qrack_sch_parser.py
ADDED
@@ -0,0 +1,179 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, _prec, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('#include <time.h>\n')
|
11 |
+
f.write('#define BILLION 1E9\n')
|
12 |
+
|
13 |
+
# f.write('#include \"qrack/qfactory.hpp\"\n')
|
14 |
+
f.write('#include \"/sim_lib/qrack/qrack/build_st_{}/include/qfactory.hpp\"\n'.format(_prec))
|
15 |
+
|
16 |
+
f.write('#include <algorithm>\n') # // std::random_shuffle
|
17 |
+
f.write('#include <cstddef>\n') # // size_t
|
18 |
+
f.write('#include <iostream>\n') # // std::cout
|
19 |
+
|
20 |
+
f.write('using namespace Qrack;\n')
|
21 |
+
|
22 |
+
f.write('\n')
|
23 |
+
f.write('int main(int argc, char* argv[]){\n')
|
24 |
+
|
25 |
+
f.write('\tstruct timespec requestStart, requestEnd;\n')
|
26 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestStart);\n')
|
27 |
+
# f.write('\tQInterfacePtr qReg = CreateQuantumInterface(QINTERFACE_OPTIMAL, {}, 0);\n'.format(N))
|
28 |
+
f.write('\tQInterfacePtr qReg = CreateQuantumInterface(QINTERFACE_CPU, {}, 0);\n'.format(N))
|
29 |
+
|
30 |
+
lc = 0
|
31 |
+
|
32 |
+
with open(input_filename, "r") as ifile:
|
33 |
+
lines = ifile.readlines()
|
34 |
+
|
35 |
+
for line in lines:
|
36 |
+
|
37 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
38 |
+
|
39 |
+
if s is None:
|
40 |
+
continue
|
41 |
+
|
42 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits', 'cirq']:
|
43 |
+
lc = lc + 1
|
44 |
+
f.write('\t// {}\n'.format(s.group()))
|
45 |
+
continue
|
46 |
+
|
47 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 'h' or s.group() == 's':
|
48 |
+
lc = lc + 1
|
49 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
50 |
+
t_qbit = int(m_i[0].strip('[]'))
|
51 |
+
# f.write('\tpauli{}(qubits, {});\n'.format(s.group().upper(), t_qbit))
|
52 |
+
f.write('\tqReg->{}({});\n'.format(s.group().upper(), t_qbit))
|
53 |
+
continue
|
54 |
+
|
55 |
+
elif s.group() == 'sx':
|
56 |
+
lc = lc + 1
|
57 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
58 |
+
t_qbit = int(m_i[0].strip('[]'))
|
59 |
+
# f.write('\thadamard(qubits, {});\n'.format(t_qbit))
|
60 |
+
f.write('\tqReg->SqrtX({});\n'.format(t_qbit))
|
61 |
+
continue
|
62 |
+
|
63 |
+
elif s.group() == 'sxdg':
|
64 |
+
lc = lc + 1
|
65 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
66 |
+
t_qbit = int(m_i[0].strip('[]'))
|
67 |
+
# f.write('\thadamard(qubits, {});\n'.format(t_qbit))
|
68 |
+
f.write('\tqReg->ISqrtX({});\n'.format(t_qbit))
|
69 |
+
continue
|
70 |
+
|
71 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
72 |
+
lc = lc + 1
|
73 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
74 |
+
# print(m_r)
|
75 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
76 |
+
sp_str = m_r[0].split('e-')
|
77 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
78 |
+
elif 'pi' in m_r[0]:
|
79 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
80 |
+
# print(m_r)
|
81 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
82 |
+
t_qbit = int(m_i[0].strip('[]'))
|
83 |
+
# f.write('\trotate{}(qubits, {}, M_PI*{});\n'.format(s.group()[1].upper(), t_qbit, float(m_r[0][0])))
|
84 |
+
f.write('\tqReg->R{}(M_PI*{}, {});\n'.format(s.group()[1].upper(), float(m_r[0][0]), t_qbit))
|
85 |
+
continue
|
86 |
+
|
87 |
+
elif s.group() == 'cx':
|
88 |
+
lc = lc + 1
|
89 |
+
match = re.findall(r'\[\d\d*\]', line)
|
90 |
+
c_qbit = int(match[0].strip('[]'))
|
91 |
+
t_qbit = int(match[1].strip('[]'))
|
92 |
+
# f.write('\tcontrolledNot(qubits, {}, {});\n'.format(c_qbit, t_qbit))
|
93 |
+
f.write('\tqReg->CNOT({}, {});\n'.format(c_qbit, t_qbit))
|
94 |
+
continue
|
95 |
+
|
96 |
+
elif s.group() == 'u2':
|
97 |
+
lc = lc + 1
|
98 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
99 |
+
m_r = m_r[0].split(',')
|
100 |
+
for i, m in enumerate(m_r):
|
101 |
+
if 'pi' in m and 'e' in m:
|
102 |
+
sp_str = m_r[i].split('e-')
|
103 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
104 |
+
elif 'pi' in m:
|
105 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
106 |
+
else:
|
107 |
+
m_r[i] = '0'
|
108 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
109 |
+
t_qbit = int(m_i[0].strip('[]'))
|
110 |
+
# f.write('\tu2(qubits, {}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
111 |
+
f.write('\tqReg->U2({}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
112 |
+
continue
|
113 |
+
|
114 |
+
elif s.group() == 'u3':
|
115 |
+
lc = lc + 1
|
116 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
117 |
+
m_r = m_r[0].split(',')
|
118 |
+
for i, m in enumerate(m_r):
|
119 |
+
if 'pi' in m and 'e' in m:
|
120 |
+
sp_str = m_r[i].split('e-')
|
121 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
122 |
+
elif 'pi' in m:
|
123 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
124 |
+
else:
|
125 |
+
m_r[i] = '0'
|
126 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
127 |
+
t_qbit = int(m_i[0].strip('[]'))
|
128 |
+
# f.write('\tu3(qubits, {}, M_PI*{}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
129 |
+
f.write('\tqReg->U({}, M_PI*{}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
130 |
+
continue
|
131 |
+
|
132 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestEnd);\n')
|
133 |
+
|
134 |
+
f.write('\tdouble accum = (requestEnd.tv_sec - requestStart.tv_sec) + (requestEnd.tv_nsec - requestStart.tv_nsec)/BILLION;\n')
|
135 |
+
f.write('\tprintf( "%lf, ", accum );\n')
|
136 |
+
f.write('\treturn 0;\n')
|
137 |
+
f.write('}')
|
138 |
+
|
139 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
140 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
141 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
142 |
+
print(int(lc_res) == lc)
|
143 |
+
# print(lc)
|
144 |
+
|
145 |
+
task = 'rqc'
|
146 |
+
sim_pack = 'qrack_sch'
|
147 |
+
com_cap = 'mt'
|
148 |
+
prec = 'dp'
|
149 |
+
|
150 |
+
for N in range(12, 38, 2):
|
151 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
152 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
153 |
+
|
154 |
+
# save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
155 |
+
|
156 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
157 |
+
|
158 |
+
output_file = output_file_path + '{}_n{}.cpp'.format(task, N)
|
159 |
+
parse_qasm_to_package_gen(N, prec, input_file, output_file)
|
160 |
+
|
161 |
+
|
162 |
+
r"""
|
163 |
+
for N in range(12, 42, 2):
|
164 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
165 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
166 |
+
|
167 |
+
output_file_path = '/data/user/gangap_a/rqc/quest/QuEST/rqc/quest_sp_run_files/'
|
168 |
+
output_file = output_file_path + 'quest_rqc_n{}.c'.format(N)
|
169 |
+
parse_qasm_to_package_gen(N, 'float', input_file, output_file)
|
170 |
+
|
171 |
+
for N in range(41, 51):
|
172 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
173 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
174 |
+
|
175 |
+
output_file_path = '/data/user/gangap_a/rqc/quest/QuEST/rqc/quest_sp_run_files/'
|
176 |
+
output_file = output_file_path + 'quest_rqc_n{}.c'.format(N)
|
177 |
+
parse_qasm_to_package_gen(N, 'float', input_file, output_file)
|
178 |
+
|
179 |
+
"""
|
qasm_parser/qsimcirq/qasm_qsimcirq_a100_parser.py
ADDED
@@ -0,0 +1,211 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
f.write('os.chdir(\'/data/user/gangap_a/rqc/qsimcirq_a100/qsim\')\n')
|
12 |
+
f.write('cwd = os.getcwd()\n')
|
13 |
+
f.write('import sys\n')
|
14 |
+
f.write('sys.path.insert(0, \'\')\n')
|
15 |
+
|
16 |
+
f.write('from time import process_time, perf_counter\n')
|
17 |
+
f.write('import numpy as np\n\n')
|
18 |
+
f.write('import cirq\n')
|
19 |
+
f.write('import qsimcirq\n')
|
20 |
+
|
21 |
+
f.write('class U2(cirq.Gate):\n')
|
22 |
+
f.write(' def __init__(self, phi, lam):\n')
|
23 |
+
f.write(' super(U2, self)\n')
|
24 |
+
f.write(' self._p = phi\n')
|
25 |
+
f.write(' self._l = lam\n\n')
|
26 |
+
|
27 |
+
f.write(' def _num_qubits_(self):\n')
|
28 |
+
f.write(' return 1\n\n')
|
29 |
+
|
30 |
+
f.write(' def _unitary_(self):\n')
|
31 |
+
f.write(' mat = ((np.cos(0.5*(self._p + self._l)) - 1j*np.sin(0.5*(self._p + self._l)))/np.sqrt(2))*np.array([[1, -np.cos(self._l)-1j*np.sin(self._l)], [np.cos(self._p) + 1j*np.sin(self._p), np.cos(self._l + self._p) + 1j*np.sin(self._l+self._p)]])\n')
|
32 |
+
f.write(' return mat\n\n')
|
33 |
+
|
34 |
+
f.write('class U3(cirq.Gate):\n')
|
35 |
+
f.write(' def __init__(self, theta, phi, lam):\n')
|
36 |
+
f.write(' super(U3, self)\n')
|
37 |
+
f.write(' self._t = theta\n')
|
38 |
+
f.write(' self._p = phi\n')
|
39 |
+
f.write(' self._l = lam\n\n')
|
40 |
+
|
41 |
+
f.write(' def _num_qubits_(self):\n')
|
42 |
+
f.write(' return 1\n\n')
|
43 |
+
|
44 |
+
f.write(' def _unitary_(self):\n')
|
45 |
+
f.write(' mat = (np.cos(0.5*(self._p + self._l)) - 1j*np.sin(0.5*(self._p + self._l)))*np.array([[np.cos(self._t/2.), -np.sin(self._t/2.)*(np.cos(self._l) + 1j*np.sin(self._l))], [np.sin(self._t/2.)*(np.cos(self._p) + 1j*np.sin(self._p)), np.cos(self._t/2.)*(np.cos(self._l + self._p) + 1j*np.sin(self._l + self._p))]])\n')
|
46 |
+
f.write(' return mat\n\n')
|
47 |
+
|
48 |
+
f.write('t_sp = process_time()\n')
|
49 |
+
f.write('t_s = perf_counter()\n')
|
50 |
+
|
51 |
+
f.write('q = cirq.LineQubit.range({})\n'.format(N))
|
52 |
+
|
53 |
+
f.write('cir = cirq.Circuit()\n')
|
54 |
+
|
55 |
+
with open(input_filename, "r") as ifile:
|
56 |
+
lines = ifile.readlines()
|
57 |
+
|
58 |
+
lc = 0
|
59 |
+
cirq_c = 0
|
60 |
+
for line in lines:
|
61 |
+
|
62 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
63 |
+
|
64 |
+
if s is None:
|
65 |
+
continue
|
66 |
+
|
67 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
68 |
+
lc = lc + 1
|
69 |
+
f.write('# {}\n'.format(s.group()))
|
70 |
+
continue
|
71 |
+
|
72 |
+
elif s.group() == 'cirq':
|
73 |
+
lc = lc + 1
|
74 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
75 |
+
cirq_c = cirq_c + 1
|
76 |
+
continue
|
77 |
+
|
78 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'h':
|
79 |
+
lc = lc + 1
|
80 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
81 |
+
t_qbit = int(m_i[0].strip('[]'))
|
82 |
+
f.write('cir.append(cirq.{}(q[{}]))\n'.format(s.group().upper(), t_qbit))
|
83 |
+
continue
|
84 |
+
|
85 |
+
elif s.group() == 'sx':
|
86 |
+
lc = lc + 1
|
87 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
88 |
+
t_qbit = int(m_i[0].strip('[]'))
|
89 |
+
f.write('cir.append(cirq.XPowGate(exponent=0.5).on(q[{}]))\n'.format(t_qbit))
|
90 |
+
|
91 |
+
elif s.group() == 'sxdg':
|
92 |
+
lc = lc + 1
|
93 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
94 |
+
t_qbit = int(m_i[0].strip('[]'))
|
95 |
+
f.write('cir.append(cirq.XPowGate(exponent=-0.5).on(q[{}]))\n'.format(t_qbit))
|
96 |
+
|
97 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
98 |
+
lc = lc + 1
|
99 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
100 |
+
# print(m_r)
|
101 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
102 |
+
sp_str = m_r[0].split('e-')
|
103 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
104 |
+
elif 'pi' in m_r[0]:
|
105 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
106 |
+
# print(m_r)
|
107 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
108 |
+
t_qbit = int(m_i[0].strip('[]'))
|
109 |
+
f.write('cir.append(cirq.{}(np.pi*{}).on(q[{}]))\n'.format(s.group(), float(m_r[0][0]), t_qbit))
|
110 |
+
continue
|
111 |
+
|
112 |
+
elif s.group() == 'cx':
|
113 |
+
lc = lc + 1
|
114 |
+
match = re.findall(r'\[\d\d*\]', line)
|
115 |
+
c_qbit = int(match[0].strip('[]'))
|
116 |
+
t_qbit = int(match[1].strip('[]'))
|
117 |
+
# f.write('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit))
|
118 |
+
f.write('cir.append(cirq.CNOT(q[{}], q[{}]))\n'.format(c_qbit, t_qbit))
|
119 |
+
continue
|
120 |
+
|
121 |
+
elif s.group() == 'u2':
|
122 |
+
lc = lc + 1
|
123 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
124 |
+
m_r = m_r[0].split(',')
|
125 |
+
for i, m in enumerate(m_r):
|
126 |
+
if 'pi' in m and 'e' in m:
|
127 |
+
sp_str = m_r[i].split('e-')
|
128 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
129 |
+
elif 'pi' in m:
|
130 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
131 |
+
else:
|
132 |
+
m_r[i] = '0'
|
133 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
134 |
+
t_qbit = int(m_i[0].strip('[]'))
|
135 |
+
f.write('cir.append(U2(np.pi*{}, np.pi*{}).on(q[{}]))\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
136 |
+
continue
|
137 |
+
|
138 |
+
elif s.group() == 'u3':
|
139 |
+
lc = lc + 1
|
140 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
141 |
+
m_r = m_r[0].split(',')
|
142 |
+
for i, m in enumerate(m_r):
|
143 |
+
if 'pi' in m and 'e' in m:
|
144 |
+
sp_str = m_r[i].split('e-')
|
145 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
146 |
+
elif 'pi' in m:
|
147 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
148 |
+
else:
|
149 |
+
m_r[i] = '0'
|
150 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
151 |
+
t_qbit = int(m_i[0].strip('[]'))
|
152 |
+
f.write('cir.append(U3(np.pi*{}, np.pi*{}, np.pi*{}).on(q[{}]))\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
153 |
+
continue
|
154 |
+
|
155 |
+
|
156 |
+
f.write('gpu_options = qsimcirq.QSimOptions(use_gpu=True)\n')
|
157 |
+
f.write('qsim_simulator = qsimcirq.QSimSimulator(qsim_options=gpu_options)\n')
|
158 |
+
f.write('result = qsim_simulator.simulate(cir)\n')
|
159 |
+
|
160 |
+
f.write('t_e = perf_counter()\n')
|
161 |
+
f.write('t_ep = process_time()\n')
|
162 |
+
f.write('print(t_e - t_s)\n')
|
163 |
+
f.write('print(t_ep - t_sp)\n')
|
164 |
+
# f.write('print(result.final_state_vector)\n')
|
165 |
+
|
166 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
167 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
168 |
+
|
169 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
170 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
171 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
172 |
+
print(int(lc_res) == lc)
|
173 |
+
# print(lc)
|
174 |
+
|
175 |
+
# parse_qasm_to_package_gen(12, "../qasm_test/qasm_rqc_test.qasm", "test_cirq.py")
|
176 |
+
# parse_qasm_to_package_gen(12, 1, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_qsimcirq.py")
|
177 |
+
|
178 |
+
task = 'rqc'
|
179 |
+
sim_pack = 'qsimcirq'
|
180 |
+
com_cap = 'gpu'
|
181 |
+
prec = 'sp'
|
182 |
+
|
183 |
+
for N in range(12, 40, 2):
|
184 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
185 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
186 |
+
|
187 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
188 |
+
|
189 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
190 |
+
|
191 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
192 |
+
parse_qasm_to_package_gen(N, input_file, output_file, save_file_path)
|
193 |
+
|
194 |
+
r"""
|
195 |
+
for N in range(12, 42, 2):
|
196 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
197 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
198 |
+
|
199 |
+
output_file_path = '/data/user/gangap_a/rqc/qsimcirq/qsimcirq_gpu_a100_run_files/'
|
200 |
+
output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N)
|
201 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
202 |
+
|
203 |
+
for N in range(41, 51):
|
204 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
205 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
206 |
+
|
207 |
+
output_file_path = '/data/user/gangap_a/rqc/qsimcirq/qsimcirq_gpu_a100_run_files/'
|
208 |
+
output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N)
|
209 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
210 |
+
|
211 |
+
"""
|
qasm_parser/qsimcirq/qasm_qsimcirq_gpu_parser.py
ADDED
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
f.write('os.chdir(\'/sim_lib/qsimcirq/qsim/\')\n')
|
12 |
+
f.write('cwd = os.getcwd()\n')
|
13 |
+
f.write('import sys\n')
|
14 |
+
f.write('sys.path.insert(0, \'\')\n')
|
15 |
+
|
16 |
+
f.write('from time import process_time, perf_counter\n')
|
17 |
+
f.write('import numpy as np\n\n')
|
18 |
+
f.write('import cirq\n')
|
19 |
+
f.write('import qsimcirq\n')
|
20 |
+
|
21 |
+
f.write('class U2(cirq.Gate):\n')
|
22 |
+
f.write(' def __init__(self, phi, lam):\n')
|
23 |
+
f.write(' super(U2, self)\n')
|
24 |
+
f.write(' self._p = phi\n')
|
25 |
+
f.write(' self._l = lam\n\n')
|
26 |
+
|
27 |
+
f.write(' def _num_qubits_(self):\n')
|
28 |
+
f.write(' return 1\n\n')
|
29 |
+
|
30 |
+
f.write(' def _unitary_(self):\n')
|
31 |
+
f.write(' mat = ((np.cos(0.5*(self._p + self._l)) - 1j*np.sin(0.5*(self._p + self._l)))/np.sqrt(2))*np.array([[1, -np.cos(self._l)-1j*np.sin(self._l)], [np.cos(self._p) + 1j*np.sin(self._p), np.cos(self._l + self._p) + 1j*np.sin(self._l+self._p)]])\n')
|
32 |
+
f.write(' return mat\n\n')
|
33 |
+
|
34 |
+
f.write('class U3(cirq.Gate):\n')
|
35 |
+
f.write(' def __init__(self, theta, phi, lam):\n')
|
36 |
+
f.write(' super(U3, self)\n')
|
37 |
+
f.write(' self._t = theta\n')
|
38 |
+
f.write(' self._p = phi\n')
|
39 |
+
f.write(' self._l = lam\n\n')
|
40 |
+
|
41 |
+
f.write(' def _num_qubits_(self):\n')
|
42 |
+
f.write(' return 1\n\n')
|
43 |
+
|
44 |
+
f.write(' def _unitary_(self):\n')
|
45 |
+
f.write(' mat = (np.cos(0.5*(self._p + self._l)) - 1j*np.sin(0.5*(self._p + self._l)))*np.array([[np.cos(self._t/2.), -np.sin(self._t/2.)*(np.cos(self._l) + 1j*np.sin(self._l))], [np.sin(self._t/2.)*(np.cos(self._p) + 1j*np.sin(self._p)), np.cos(self._t/2.)*(np.cos(self._l + self._p) + 1j*np.sin(self._l + self._p))]])\n')
|
46 |
+
f.write(' return mat\n\n')
|
47 |
+
|
48 |
+
f.write('t_sp = process_time()\n')
|
49 |
+
f.write('t_s = perf_counter()\n')
|
50 |
+
|
51 |
+
f.write('q = cirq.LineQubit.range({})\n'.format(N))
|
52 |
+
|
53 |
+
f.write('cir = cirq.Circuit()\n')
|
54 |
+
|
55 |
+
with open(input_filename, "r") as ifile:
|
56 |
+
lines = ifile.readlines()
|
57 |
+
|
58 |
+
lc = 0
|
59 |
+
cirq_c = 0
|
60 |
+
for line in lines:
|
61 |
+
|
62 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
63 |
+
|
64 |
+
if s is None:
|
65 |
+
continue
|
66 |
+
|
67 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
68 |
+
lc = lc + 1
|
69 |
+
f.write('# {}\n'.format(s.group()))
|
70 |
+
continue
|
71 |
+
|
72 |
+
elif s.group() == 'cirq':
|
73 |
+
lc = lc + 1
|
74 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
75 |
+
cirq_c = cirq_c + 1
|
76 |
+
continue
|
77 |
+
|
78 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's':
|
79 |
+
lc = lc + 1
|
80 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
81 |
+
t_qbit = int(m_i[0].strip('[]'))
|
82 |
+
f.write('cir.append(cirq.{}(q[{}]))\n'.format(s.group().upper(), t_qbit))
|
83 |
+
continue
|
84 |
+
|
85 |
+
elif s.group() == 'sx':
|
86 |
+
lc = lc + 1
|
87 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
88 |
+
t_qbit = int(m_i[0].strip('[]'))
|
89 |
+
f.write('cir.append(cirq.XPowGate(exponent=0.5).on(q[{}]))\n'.format(t_qbit))
|
90 |
+
|
91 |
+
elif s.group() == 'sxdg':
|
92 |
+
lc = lc + 1
|
93 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
94 |
+
t_qbit = int(m_i[0].strip('[]'))
|
95 |
+
f.write('cir.append(cirq.XPowGate(exponent=-0.5).on(q[{}]))\n'.format(t_qbit))
|
96 |
+
|
97 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
98 |
+
lc = lc + 1
|
99 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
100 |
+
# print(m_r)
|
101 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
102 |
+
sp_str = m_r[0].split('e-')
|
103 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
104 |
+
elif 'pi' in m_r[0]:
|
105 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
106 |
+
# print(m_r)
|
107 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
108 |
+
t_qbit = int(m_i[0].strip('[]'))
|
109 |
+
f.write('cir.append(cirq.{}(np.pi*{}).on(q[{}]))\n'.format(s.group(), float(m_r[0][0]), t_qbit))
|
110 |
+
continue
|
111 |
+
|
112 |
+
elif s.group() == 'cx':
|
113 |
+
lc = lc + 1
|
114 |
+
match = re.findall(r'\[\d\d*\]', line)
|
115 |
+
c_qbit = int(match[0].strip('[]'))
|
116 |
+
t_qbit = int(match[1].strip('[]'))
|
117 |
+
# f.write('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit))
|
118 |
+
f.write('cir.append(cirq.CNOT(q[{}], q[{}]))\n'.format(c_qbit, t_qbit))
|
119 |
+
continue
|
120 |
+
|
121 |
+
elif s.group() == 'u2':
|
122 |
+
lc = lc + 1
|
123 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
124 |
+
m_r = m_r[0].split(',')
|
125 |
+
for i, m in enumerate(m_r):
|
126 |
+
if 'pi' in m and 'e' in m:
|
127 |
+
sp_str = m_r[i].split('e-')
|
128 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
129 |
+
elif 'pi' in m:
|
130 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
131 |
+
else:
|
132 |
+
m_r[i] = '0'
|
133 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
134 |
+
t_qbit = int(m_i[0].strip('[]'))
|
135 |
+
f.write('cir.append(U2(np.pi*{}, np.pi*{}).on(q[{}]))\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
136 |
+
continue
|
137 |
+
|
138 |
+
elif s.group() == 'u3':
|
139 |
+
lc = lc + 1
|
140 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
141 |
+
m_r = m_r[0].split(',')
|
142 |
+
for i, m in enumerate(m_r):
|
143 |
+
if 'pi' in m and 'e' in m:
|
144 |
+
sp_str = m_r[i].split('e-')
|
145 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
146 |
+
elif 'pi' in m:
|
147 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
148 |
+
else:
|
149 |
+
m_r[i] = '0'
|
150 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
151 |
+
t_qbit = int(m_i[0].strip('[]'))
|
152 |
+
f.write('cir.append(U3(np.pi*{}, np.pi*{}, np.pi*{}).on(q[{}]))\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
153 |
+
continue
|
154 |
+
|
155 |
+
|
156 |
+
f.write('gpu_options = qsimcirq.QSimOptions(use_gpu=True)\n')
|
157 |
+
f.write('qsim_simulator = qsimcirq.QSimSimulator(qsim_options=gpu_options)\n')
|
158 |
+
f.write('result = qsim_simulator.simulate(cir)\n')
|
159 |
+
|
160 |
+
f.write('t_e = perf_counter()\n')
|
161 |
+
f.write('t_ep = process_time()\n')
|
162 |
+
f.write('print(t_e - t_s)\n')
|
163 |
+
f.write('print(t_ep - t_sp)\n')
|
164 |
+
# f.write('print(result.final_state_vector)\n')
|
165 |
+
|
166 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
167 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
168 |
+
|
169 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
170 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
171 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
172 |
+
print(int(lc_res) == lc)
|
173 |
+
# print(lc)
|
174 |
+
|
175 |
+
# parse_qasm_to_package_gen(12, "../qasm_test/qasm_rqc_test.qasm", "test_cirq.py")
|
176 |
+
# parse_qasm_to_package_gen(12, 1, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_qsimcirq.py")
|
177 |
+
|
178 |
+
task = 'hdyn'
|
179 |
+
sim_pack = 'qsimcirq'
|
180 |
+
com_cap = 'gpu'
|
181 |
+
prec = 'sp'
|
182 |
+
|
183 |
+
for N in range(6, 38, 2):
|
184 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
185 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
186 |
+
|
187 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
188 |
+
|
189 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
190 |
+
|
191 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
192 |
+
parse_qasm_to_package_gen(N, input_file, output_file, save_file_path)
|
193 |
+
|
194 |
+
r"""
|
195 |
+
for N in range(12, 42, 2):
|
196 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
197 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
198 |
+
|
199 |
+
output_file_path = '/data/user/gangap_a/rqc/qsimcirq/qsimcirq_gpu_run_files/'
|
200 |
+
output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N)
|
201 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
202 |
+
|
203 |
+
for N in range(41, 51):
|
204 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
205 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
206 |
+
|
207 |
+
output_file_path = '/data/user/gangap_a/rqc/qsimcirq/qsimcirq_gpu_run_files/'
|
208 |
+
output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N)
|
209 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
210 |
+
"""
|
qasm_parser/qsimcirq/qasm_qsimcirq_mt_parser.py
ADDED
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, cpu_threads, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
f.write('from time import process_time, perf_counter\n')
|
12 |
+
f.write('import numpy as np\n\n')
|
13 |
+
f.write('import cirq\n')
|
14 |
+
f.write('import qsimcirq\n')
|
15 |
+
|
16 |
+
f.write('class U2(cirq.Gate):\n')
|
17 |
+
f.write(' def __init__(self, phi, lam):\n')
|
18 |
+
f.write(' super(U2, self)\n')
|
19 |
+
f.write(' self._p = phi\n')
|
20 |
+
f.write(' self._l = lam\n\n')
|
21 |
+
|
22 |
+
f.write(' def _num_qubits_(self):\n')
|
23 |
+
f.write(' return 1\n\n')
|
24 |
+
|
25 |
+
f.write(' def _unitary_(self):\n')
|
26 |
+
f.write(' mat = ((np.cos(0.5*(self._p + self._l)) - 1j*np.sin(0.5*(self._p + self._l)))/np.sqrt(2))*np.array([[1, -np.cos(self._l)-1j*np.sin(self._l)], [np.cos(self._p) + 1j*np.sin(self._p), np.cos(self._l + self._p) + 1j*np.sin(self._l+self._p)]])\n')
|
27 |
+
f.write(' return mat\n\n')
|
28 |
+
|
29 |
+
f.write('class U3(cirq.Gate):\n')
|
30 |
+
f.write(' def __init__(self, theta, phi, lam):\n')
|
31 |
+
f.write(' super(U3, self)\n')
|
32 |
+
f.write(' self._t = theta\n')
|
33 |
+
f.write(' self._p = phi\n')
|
34 |
+
f.write(' self._l = lam\n\n')
|
35 |
+
|
36 |
+
f.write(' def _num_qubits_(self):\n')
|
37 |
+
f.write(' return 1\n\n')
|
38 |
+
|
39 |
+
f.write(' def _unitary_(self):\n')
|
40 |
+
f.write(' mat = (np.cos(0.5*(self._p + self._l)) - 1j*np.sin(0.5*(self._p + self._l)))*np.array([[np.cos(self._t/2.), -np.sin(self._t/2.)*(np.cos(self._l) + 1j*np.sin(self._l))], [np.sin(self._t/2.)*(np.cos(self._p) + 1j*np.sin(self._p)), np.cos(self._t/2.)*(np.cos(self._l + self._p) + 1j*np.sin(self._l + self._p))]])\n')
|
41 |
+
f.write(' return mat\n\n')
|
42 |
+
|
43 |
+
f.write('t_sp = process_time()\n')
|
44 |
+
f.write('t_s = perf_counter()\n')
|
45 |
+
|
46 |
+
f.write('q = cirq.LineQubit.range({})\n'.format(N))
|
47 |
+
|
48 |
+
f.write('cir = cirq.Circuit()\n')
|
49 |
+
|
50 |
+
with open(input_filename, "r") as ifile:
|
51 |
+
lines = ifile.readlines()
|
52 |
+
|
53 |
+
lc = 0
|
54 |
+
cirq_c = 0
|
55 |
+
for line in lines:
|
56 |
+
|
57 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
58 |
+
|
59 |
+
if s is None:
|
60 |
+
continue
|
61 |
+
|
62 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
63 |
+
lc = lc + 1
|
64 |
+
f.write('# {}\n'.format(s.group()))
|
65 |
+
continue
|
66 |
+
|
67 |
+
elif s.group() == 'cirq':
|
68 |
+
lc = lc + 1
|
69 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
70 |
+
cirq_c = cirq_c + 1
|
71 |
+
continue
|
72 |
+
|
73 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's':
|
74 |
+
lc = lc + 1
|
75 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
76 |
+
t_qbit = int(m_i[0].strip('[]'))
|
77 |
+
f.write('cir.append(cirq.{}(q[{}]))\n'.format(s.group().upper(), t_qbit))
|
78 |
+
continue
|
79 |
+
|
80 |
+
elif s.group() == 'sx':
|
81 |
+
lc = lc + 1
|
82 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
83 |
+
t_qbit = int(m_i[0].strip('[]'))
|
84 |
+
f.write('cir.append(cirq.XPowGate(exponent=0.5).on(q[{}]))\n'.format(t_qbit))
|
85 |
+
|
86 |
+
elif s.group() == 'sxdg':
|
87 |
+
lc = lc + 1
|
88 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
89 |
+
t_qbit = int(m_i[0].strip('[]'))
|
90 |
+
f.write('cir.append(cirq.XPowGate(exponent=-0.5).on(q[{}]))\n'.format(t_qbit))
|
91 |
+
|
92 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
93 |
+
lc = lc + 1
|
94 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
95 |
+
# print(m_r)
|
96 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
97 |
+
sp_str = m_r[0].split('e-')
|
98 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
99 |
+
elif 'pi' in m_r[0]:
|
100 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
101 |
+
# print(m_r)
|
102 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
103 |
+
t_qbit = int(m_i[0].strip('[]'))
|
104 |
+
f.write('cir.append(cirq.{}(np.pi*{}).on(q[{}]))\n'.format(s.group(), float(m_r[0][0]), t_qbit))
|
105 |
+
continue
|
106 |
+
|
107 |
+
elif s.group() == 'cx':
|
108 |
+
lc = lc + 1
|
109 |
+
match = re.findall(r'\[\d\d*\]', line)
|
110 |
+
c_qbit = int(match[0].strip('[]'))
|
111 |
+
t_qbit = int(match[1].strip('[]'))
|
112 |
+
# f.write('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit))
|
113 |
+
f.write('cir.append(cirq.CNOT(q[{}], q[{}]))\n'.format(c_qbit, t_qbit))
|
114 |
+
continue
|
115 |
+
|
116 |
+
elif s.group() == 'u2':
|
117 |
+
lc = lc + 1
|
118 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
119 |
+
m_r = m_r[0].split(',')
|
120 |
+
for i, m in enumerate(m_r):
|
121 |
+
if 'pi' in m and 'e' in m:
|
122 |
+
sp_str = m_r[i].split('e-')
|
123 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
124 |
+
elif 'pi' in m:
|
125 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
126 |
+
else:
|
127 |
+
m_r[i] = '0'
|
128 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
129 |
+
t_qbit = int(m_i[0].strip('[]'))
|
130 |
+
f.write('cir.append(U2(np.pi*{}, np.pi*{}).on(q[{}]))\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
131 |
+
continue
|
132 |
+
|
133 |
+
elif s.group() == 'u3':
|
134 |
+
lc = lc + 1
|
135 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
136 |
+
m_r = m_r[0].split(',')
|
137 |
+
for i, m in enumerate(m_r):
|
138 |
+
if 'pi' in m and 'e' in m:
|
139 |
+
sp_str = m_r[i].split('e-')
|
140 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
141 |
+
elif 'pi' in m:
|
142 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
143 |
+
else:
|
144 |
+
m_r[i] = '0'
|
145 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
146 |
+
t_qbit = int(m_i[0].strip('[]'))
|
147 |
+
f.write('cir.append(U3(np.pi*{}, np.pi*{}, np.pi*{}).on(q[{}]))\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
148 |
+
continue
|
149 |
+
|
150 |
+
f.write('qsim_options = qsimcirq.QSimOptions(cpu_threads={})\n'.format(cpu_threads))
|
151 |
+
f.write('qsim_simulator = qsimcirq.QSimSimulator(qsim_options)\n')
|
152 |
+
f.write('result = qsim_simulator.simulate(cir)\n')
|
153 |
+
|
154 |
+
f.write('t_e = perf_counter()\n')
|
155 |
+
f.write('t_ep = process_time()\n')
|
156 |
+
f.write('print(t_e - t_s)\n')
|
157 |
+
f.write('print(t_ep - t_sp)\n')
|
158 |
+
# f.write('print(result.final_state_vector)\n')
|
159 |
+
|
160 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
161 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
162 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
163 |
+
print(int(lc_res) == lc)
|
164 |
+
# print(lc)
|
165 |
+
|
166 |
+
# parse_qasm_to_package_gen(12, "../qasm_test/qasm_rqc_test.qasm", "test_cirq.py")
|
167 |
+
# parse_qasm_to_package_gen(12, 1, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_qsimcirq.py")
|
168 |
+
|
169 |
+
for N in range(12, 42, 2):
|
170 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
171 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
172 |
+
|
173 |
+
output_file_path = '/data/user/gangap_a/rqc/qsimcirq/qsimcirq_mt_run_files/'
|
174 |
+
output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N)
|
175 |
+
parse_qasm_to_package_gen(N, 84, input_file, output_file)
|
176 |
+
|
177 |
+
for N in range(41, 51):
|
178 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
179 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
180 |
+
|
181 |
+
output_file_path = '/data/user/gangap_a/rqc/qsimcirq/qsimcirq_mt_run_files/'
|
182 |
+
output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N)
|
183 |
+
parse_qasm_to_package_gen(N, 84, input_file, output_file)
|
184 |
+
|
qasm_parser/qsimcirq/qasm_qsimcirq_parser.py
ADDED
@@ -0,0 +1,213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, cpu_threads, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
f.write('from time import process_time, perf_counter\n')
|
12 |
+
f.write('import numpy as np\n\n')
|
13 |
+
f.write('import cirq\n')
|
14 |
+
f.write('import qsimcirq\n')
|
15 |
+
|
16 |
+
f.write('class U2(cirq.Gate):\n')
|
17 |
+
f.write(' def __init__(self, phi, lam):\n')
|
18 |
+
f.write(' super(U2, self)\n')
|
19 |
+
f.write(' self._p = phi\n')
|
20 |
+
f.write(' self._l = lam\n\n')
|
21 |
+
|
22 |
+
f.write(' def _num_qubits_(self):\n')
|
23 |
+
f.write(' return 1\n\n')
|
24 |
+
|
25 |
+
f.write(' def _unitary_(self):\n')
|
26 |
+
f.write(' mat = ((np.cos(0.5*(self._p + self._l)) - 1j*np.sin(0.5*(self._p + self._l)))/np.sqrt(2))*np.array([[1, -np.cos(self._l)-1j*np.sin(self._l)], [np.cos(self._p) + 1j*np.sin(self._p), np.cos(self._l + self._p) + 1j*np.sin(self._l+self._p)]])\n')
|
27 |
+
f.write(' return mat\n\n')
|
28 |
+
|
29 |
+
f.write('class U3(cirq.Gate):\n')
|
30 |
+
f.write(' def __init__(self, theta, phi, lam):\n')
|
31 |
+
f.write(' super(U3, self)\n')
|
32 |
+
f.write(' self._t = theta\n')
|
33 |
+
f.write(' self._p = phi\n')
|
34 |
+
f.write(' self._l = lam\n\n')
|
35 |
+
|
36 |
+
f.write(' def _num_qubits_(self):\n')
|
37 |
+
f.write(' return 1\n\n')
|
38 |
+
|
39 |
+
f.write(' def _unitary_(self):\n')
|
40 |
+
f.write(' mat = (np.cos(0.5*(self._p + self._l)) - 1j*np.sin(0.5*(self._p + self._l)))*np.array([[np.cos(self._t/2.), -np.sin(self._t/2.)*(np.cos(self._l) + 1j*np.sin(self._l))], [np.sin(self._t/2.)*(np.cos(self._p) + 1j*np.sin(self._p)), np.cos(self._t/2.)*(np.cos(self._l + self._p) + 1j*np.sin(self._l + self._p))]])\n')
|
41 |
+
f.write(' return mat\n\n')
|
42 |
+
|
43 |
+
f.write('t_sp = process_time()\n')
|
44 |
+
f.write('t_s = perf_counter()\n')
|
45 |
+
|
46 |
+
f.write('q = cirq.LineQubit.range({})\n'.format(N))
|
47 |
+
|
48 |
+
f.write('cir = cirq.Circuit()\n')
|
49 |
+
|
50 |
+
with open(input_filename, "r") as ifile:
|
51 |
+
lines = ifile.readlines()
|
52 |
+
|
53 |
+
lc = 0
|
54 |
+
cirq_c = 0
|
55 |
+
for line in lines:
|
56 |
+
|
57 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
58 |
+
|
59 |
+
if s is None:
|
60 |
+
continue
|
61 |
+
|
62 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
63 |
+
lc = lc + 1
|
64 |
+
f.write('# {}\n'.format(s.group()))
|
65 |
+
continue
|
66 |
+
|
67 |
+
elif s.group() == 'cirq':
|
68 |
+
lc = lc + 1
|
69 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
70 |
+
cirq_c = cirq_c + 1
|
71 |
+
continue
|
72 |
+
|
73 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'h':
|
74 |
+
lc = lc + 1
|
75 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
76 |
+
t_qbit = int(m_i[0].strip('[]'))
|
77 |
+
f.write('cir.append(cirq.{}(q[{}]))\n'.format(s.group().upper(), t_qbit))
|
78 |
+
continue
|
79 |
+
|
80 |
+
elif s.group() == 'sx':
|
81 |
+
lc = lc + 1
|
82 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
83 |
+
t_qbit = int(m_i[0].strip('[]'))
|
84 |
+
f.write('cir.append(cirq.XPowGate(exponent=0.5).on(q[{}]))\n'.format(t_qbit))
|
85 |
+
|
86 |
+
elif s.group() == 'sxdg':
|
87 |
+
lc = lc + 1
|
88 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
89 |
+
t_qbit = int(m_i[0].strip('[]'))
|
90 |
+
f.write('cir.append(cirq.XPowGate(exponent=-0.5).on(q[{}]))\n'.format(t_qbit))
|
91 |
+
|
92 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
93 |
+
lc = lc + 1
|
94 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
95 |
+
# print(m_r)
|
96 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
97 |
+
sp_str = m_r[0].split('e-')
|
98 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
99 |
+
elif 'pi' in m_r[0]:
|
100 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
101 |
+
# print(m_r)
|
102 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
103 |
+
t_qbit = int(m_i[0].strip('[]'))
|
104 |
+
f.write('cir.append(cirq.{}(np.pi*{}).on(q[{}]))\n'.format(s.group(), float(m_r[0][0]), t_qbit))
|
105 |
+
continue
|
106 |
+
|
107 |
+
elif s.group() == 'cx':
|
108 |
+
lc = lc + 1
|
109 |
+
match = re.findall(r'\[\d\d*\]', line)
|
110 |
+
c_qbit = int(match[0].strip('[]'))
|
111 |
+
t_qbit = int(match[1].strip('[]'))
|
112 |
+
# f.write('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit))
|
113 |
+
f.write('cir.append(cirq.CNOT(q[{}], q[{}]))\n'.format(c_qbit, t_qbit))
|
114 |
+
continue
|
115 |
+
|
116 |
+
elif s.group() == 'u2':
|
117 |
+
lc = lc + 1
|
118 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
119 |
+
m_r = m_r[0].split(',')
|
120 |
+
for i, m in enumerate(m_r):
|
121 |
+
if 'pi' in m and 'e' in m:
|
122 |
+
sp_str = m_r[i].split('e-')
|
123 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
124 |
+
elif 'pi' in m:
|
125 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
126 |
+
else:
|
127 |
+
m_r[i] = '0'
|
128 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
129 |
+
t_qbit = int(m_i[0].strip('[]'))
|
130 |
+
f.write('cir.append(U2(np.pi*{}, np.pi*{}).on(q[{}]))\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
131 |
+
continue
|
132 |
+
|
133 |
+
elif s.group() == 'u3':
|
134 |
+
lc = lc + 1
|
135 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
136 |
+
m_r = m_r[0].split(',')
|
137 |
+
for i, m in enumerate(m_r):
|
138 |
+
if 'pi' in m and 'e' in m:
|
139 |
+
sp_str = m_r[i].split('e-')
|
140 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
141 |
+
elif 'pi' in m:
|
142 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
143 |
+
else:
|
144 |
+
m_r[i] = '0'
|
145 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
146 |
+
t_qbit = int(m_i[0].strip('[]'))
|
147 |
+
f.write('cir.append(U3(np.pi*{}, np.pi*{}, np.pi*{}).on(q[{}]))\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
148 |
+
continue
|
149 |
+
|
150 |
+
f.write('qsim_options = qsimcirq.QSimOptions(cpu_threads={})\n'.format(cpu_threads))
|
151 |
+
f.write('qsim_simulator = qsimcirq.QSimSimulator(qsim_options)\n')
|
152 |
+
f.write('result = qsim_simulator.simulate(cir)\n')
|
153 |
+
|
154 |
+
f.write('t_e = perf_counter()\n')
|
155 |
+
f.write('t_ep = process_time()\n')
|
156 |
+
f.write('print(t_e - t_s)\n')
|
157 |
+
f.write('print(t_ep - t_sp)\n')
|
158 |
+
# f.write('print(result.final_state_vector)\n')
|
159 |
+
|
160 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
161 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
162 |
+
|
163 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
164 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
165 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
166 |
+
print(int(lc_res) == lc)
|
167 |
+
# print(lc)
|
168 |
+
|
169 |
+
# parse_qasm_to_package_gen(12, "../qasm_test/qasm_rqc_test.qasm", "test_cirq.py")
|
170 |
+
# parse_qasm_to_package_gen(12, 1, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_qsimcirq.py")
|
171 |
+
|
172 |
+
task = 'qft'
|
173 |
+
sim_pack = 'qsimcirq'
|
174 |
+
com_cap = 'mt'
|
175 |
+
prec = 'sp'
|
176 |
+
|
177 |
+
for N in range(6, 40, 2):
|
178 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
179 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
180 |
+
|
181 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
182 |
+
|
183 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
184 |
+
|
185 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
186 |
+
parse_qasm_to_package_gen(N, 84, input_file, output_file, save_file_path)
|
187 |
+
|
188 |
+
r"""
|
189 |
+
for N in range(12, 42, 2):
|
190 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
191 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
192 |
+
|
193 |
+
output_file_path = '/data/user/gangap_a/rqc/qsimcirq/qsimcirq_run_files/'
|
194 |
+
output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N)
|
195 |
+
parse_qasm_to_package_gen(N, 1, input_file, output_file)
|
196 |
+
|
197 |
+
for N in range(41, 51):
|
198 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
199 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
200 |
+
|
201 |
+
output_file_path = '/data/user/gangap_a/rqc/qsimcirq/qsimcirq_run_files/'
|
202 |
+
output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N)
|
203 |
+
parse_qasm_to_package_gen(N, 1, input_file, output_file)
|
204 |
+
|
205 |
+
|
206 |
+
for N in range(6, 38, 2):
|
207 |
+
input_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/data_files/'
|
208 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
209 |
+
|
210 |
+
output_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/qsimcirq/qsimcirq_run_files/'
|
211 |
+
output_file = output_file_path + 'h_dyn_n{}.py'.format(N)
|
212 |
+
parse_qasm_to_package_gen(N, 1, input_file, output_file)
|
213 |
+
"""
|
qasm_parser/qsimcirq_cont_ngpu/qasm_qsimcirq_mnvidia_parser.py
ADDED
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, n_gpus, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
# f.write('import os\n')
|
11 |
+
# f.write('os.chdir(\'/data/user/gangap_a/dynamics/qsimcirq_nvidia/qsim/\')\n')
|
12 |
+
# f.write('cwd = os.getcwd()\n')
|
13 |
+
# f.write('import sys\n')
|
14 |
+
# f.write('sys.path.insert(0, \'\')\n')
|
15 |
+
|
16 |
+
f.write('from time import process_time, perf_counter\n')
|
17 |
+
f.write('import numpy as np\n\n')
|
18 |
+
f.write('import cirq\n')
|
19 |
+
f.write('import qsimcirq\n')
|
20 |
+
|
21 |
+
f.write('class U2(cirq.Gate):\n')
|
22 |
+
f.write(' def __init__(self, phi, lam):\n')
|
23 |
+
f.write(' super(U2, self)\n')
|
24 |
+
f.write(' self._p = phi\n')
|
25 |
+
f.write(' self._l = lam\n\n')
|
26 |
+
|
27 |
+
f.write(' def _num_qubits_(self):\n')
|
28 |
+
f.write(' return 1\n\n')
|
29 |
+
|
30 |
+
f.write(' def _unitary_(self):\n')
|
31 |
+
f.write(' mat = ((np.cos(0.5*(self._p + self._l)) - 1j*np.sin(0.5*(self._p + self._l)))/np.sqrt(2))*np.array([[1, -np.cos(self._l)-1j*np.sin(self._l)], [np.cos(self._p) + 1j*np.sin(self._p), np.cos(self._l + self._p) + 1j*np.sin(self._l+self._p)]])\n')
|
32 |
+
f.write(' return mat\n\n')
|
33 |
+
|
34 |
+
f.write('class U3(cirq.Gate):\n')
|
35 |
+
f.write(' def __init__(self, theta, phi, lam):\n')
|
36 |
+
f.write(' super(U3, self)\n')
|
37 |
+
f.write(' self._t = theta\n')
|
38 |
+
f.write(' self._p = phi\n')
|
39 |
+
f.write(' self._l = lam\n\n')
|
40 |
+
|
41 |
+
f.write(' def _num_qubits_(self):\n')
|
42 |
+
f.write(' return 1\n\n')
|
43 |
+
|
44 |
+
f.write(' def _unitary_(self):\n')
|
45 |
+
f.write(' mat = (np.cos(0.5*(self._p + self._l)) - 1j*np.sin(0.5*(self._p + self._l)))*np.array([[np.cos(self._t/2.), -np.sin(self._t/2.)*(np.cos(self._l) + 1j*np.sin(self._l))], [np.sin(self._t/2.)*(np.cos(self._p) + 1j*np.sin(self._p)), np.cos(self._t/2.)*(np.cos(self._l + self._p) + 1j*np.sin(self._l + self._p))]])\n')
|
46 |
+
f.write(' return mat\n\n')
|
47 |
+
|
48 |
+
f.write('t_sp = process_time()\n')
|
49 |
+
f.write('t_s = perf_counter()\n')
|
50 |
+
|
51 |
+
f.write('q = cirq.LineQubit.range({})\n'.format(N))
|
52 |
+
|
53 |
+
f.write('cir = cirq.Circuit()\n')
|
54 |
+
|
55 |
+
with open(input_filename, "r") as ifile:
|
56 |
+
lines = ifile.readlines()
|
57 |
+
|
58 |
+
lc = 0
|
59 |
+
cirq_c = 0
|
60 |
+
for line in lines:
|
61 |
+
|
62 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
63 |
+
|
64 |
+
if s is None:
|
65 |
+
continue
|
66 |
+
|
67 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
68 |
+
lc = lc + 1
|
69 |
+
f.write('# {}\n'.format(s.group()))
|
70 |
+
continue
|
71 |
+
|
72 |
+
elif s.group() == 'cirq':
|
73 |
+
lc = lc + 1
|
74 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
75 |
+
cirq_c = cirq_c + 1
|
76 |
+
continue
|
77 |
+
|
78 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's':
|
79 |
+
lc = lc + 1
|
80 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
81 |
+
t_qbit = int(m_i[0].strip('[]'))
|
82 |
+
f.write('cir.append(cirq.{}(q[{}]))\n'.format(s.group().upper(), t_qbit))
|
83 |
+
continue
|
84 |
+
|
85 |
+
elif s.group() == 'sx':
|
86 |
+
lc = lc + 1
|
87 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
88 |
+
t_qbit = int(m_i[0].strip('[]'))
|
89 |
+
f.write('cir.append(cirq.XPowGate(exponent=0.5).on(q[{}]))\n'.format(t_qbit))
|
90 |
+
|
91 |
+
elif s.group() == 'sxdg':
|
92 |
+
lc = lc + 1
|
93 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
94 |
+
t_qbit = int(m_i[0].strip('[]'))
|
95 |
+
f.write('cir.append(cirq.XPowGate(exponent=-0.5).on(q[{}]))\n'.format(t_qbit))
|
96 |
+
|
97 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
98 |
+
lc = lc + 1
|
99 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
100 |
+
# print(m_r)
|
101 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
102 |
+
sp_str = m_r[0].split('e-')
|
103 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
104 |
+
elif 'pi' in m_r[0]:
|
105 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
106 |
+
# print(m_r)
|
107 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
108 |
+
t_qbit = int(m_i[0].strip('[]'))
|
109 |
+
f.write('cir.append(cirq.{}(np.pi*{}).on(q[{}]))\n'.format(s.group(), float(m_r[0][0]), t_qbit))
|
110 |
+
continue
|
111 |
+
|
112 |
+
elif s.group() == 'cx':
|
113 |
+
lc = lc + 1
|
114 |
+
match = re.findall(r'\[\d\d*\]', line)
|
115 |
+
c_qbit = int(match[0].strip('[]'))
|
116 |
+
t_qbit = int(match[1].strip('[]'))
|
117 |
+
# f.write('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit))
|
118 |
+
f.write('cir.append(cirq.CNOT(q[{}], q[{}]))\n'.format(c_qbit, t_qbit))
|
119 |
+
continue
|
120 |
+
|
121 |
+
elif s.group() == 'u2':
|
122 |
+
lc = lc + 1
|
123 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
124 |
+
m_r = m_r[0].split(',')
|
125 |
+
for i, m in enumerate(m_r):
|
126 |
+
if 'pi' in m and 'e' in m:
|
127 |
+
sp_str = m_r[i].split('e-')
|
128 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
129 |
+
elif 'pi' in m:
|
130 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
131 |
+
else:
|
132 |
+
m_r[i] = '0'
|
133 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
134 |
+
t_qbit = int(m_i[0].strip('[]'))
|
135 |
+
f.write('cir.append(U2(np.pi*{}, np.pi*{}).on(q[{}]))\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
136 |
+
continue
|
137 |
+
|
138 |
+
elif s.group() == 'u3':
|
139 |
+
lc = lc + 1
|
140 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
141 |
+
m_r = m_r[0].split(',')
|
142 |
+
for i, m in enumerate(m_r):
|
143 |
+
if 'pi' in m and 'e' in m:
|
144 |
+
sp_str = m_r[i].split('e-')
|
145 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
146 |
+
elif 'pi' in m:
|
147 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
148 |
+
else:
|
149 |
+
m_r[i] = '0'
|
150 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
151 |
+
t_qbit = int(m_i[0].strip('[]'))
|
152 |
+
f.write('cir.append(U3(np.pi*{}, np.pi*{}, np.pi*{}).on(q[{}]))\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
153 |
+
continue
|
154 |
+
|
155 |
+
|
156 |
+
|
157 |
+
f.write('gpu_options = qsimcirq.QSimOptions(gpu_mode={})\n'.format(n_gpus))
|
158 |
+
f.write('qsim_simulator = qsimcirq.QSimSimulator(qsim_options=gpu_options)\n')
|
159 |
+
f.write('result = qsim_simulator.simulate(cir)\n')
|
160 |
+
|
161 |
+
f.write('t_e = perf_counter()\n')
|
162 |
+
f.write('t_ep = process_time()\n')
|
163 |
+
f.write('print(t_e - t_s)\n')
|
164 |
+
f.write('print(t_ep - t_sp)\n')
|
165 |
+
# f.write('print(result.final_state_vector)\n')
|
166 |
+
|
167 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
168 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
169 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
170 |
+
print(int(lc_res) == lc)
|
171 |
+
# print(lc)
|
172 |
+
|
173 |
+
# parse_qasm_to_package_gen(12, "../qasm_test/qasm_rqc_test.qasm", "test_cirq.py")
|
174 |
+
# parse_qasm_to_package_gen(12, 1, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_qsimcirq.py")
|
175 |
+
|
176 |
+
for N in range(12, 42, 2):
|
177 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
178 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
179 |
+
|
180 |
+
output_file_path = '/data/user/gangap_a/rqc/qsimcirq_cont_na100/qsimcirq_n8_run_files/'
|
181 |
+
output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N)
|
182 |
+
parse_qasm_to_package_gen(N, 8, input_file, output_file)
|
183 |
+
|
184 |
+
for N in range(41, 51):
|
185 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
186 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
187 |
+
|
188 |
+
output_file_path = '/data/user/gangap_a/rqc/qsimcirq_cont_na100/qsimcirq_n8_run_files/'
|
189 |
+
output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N)
|
190 |
+
parse_qasm_to_package_gen(N, 8, input_file, output_file)
|
191 |
+
|
qasm_parser/quest/qasm_quest_parser.py
ADDED
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, precision, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('#include <stdio.h>\n')
|
11 |
+
f.write('#include <math.h>\n')
|
12 |
+
f.write('#include <time.h>\n')
|
13 |
+
f.write('#include <stdlib.h>\n')
|
14 |
+
|
15 |
+
f.write('#include "QuEST.h"\n')
|
16 |
+
|
17 |
+
if precision == 'single':
|
18 |
+
f.write('#define QuEST_PREC 1\n')
|
19 |
+
elif precision == 'double':
|
20 |
+
f.write('#define QuEST_PREC 2\n')
|
21 |
+
|
22 |
+
f.write('#define BILLION 1E9\n')
|
23 |
+
|
24 |
+
f.write('\n')
|
25 |
+
|
26 |
+
f.write('void u2(Qureg qubits, const int q, double p, double l) {\n')
|
27 |
+
f.write('ComplexMatrix2 u = {\n')
|
28 |
+
f.write('.real = {{cos(0.5*(p+l))/sqrt(2), -cos(0.5*(p-l))/sqrt(2)},\n')
|
29 |
+
f.write('\t{cos(0.5*(p-l))/sqrt(2), cos(0.5*(p+l))/sqrt(2)}},\n')
|
30 |
+
f.write('.imag = {{-sin(0.5*(p+l))/sqrt(2), sin(0.5*(p-l))/sqrt(2)},\n')
|
31 |
+
f.write('\t{sin(0.5*(p-l))/sqrt(2), sin(0.5*(p+l))/sqrt(2)}}};\n')
|
32 |
+
f.write('unitary(qubits, q, u);\n')
|
33 |
+
f.write('}\n\n')
|
34 |
+
|
35 |
+
f.write('void u3(Qureg qubits, const int q, double t, double p, double l) {\n')
|
36 |
+
f.write('ComplexMatrix2 u = {\n')
|
37 |
+
f.write('.real = {{cos(0.5*(p+l))*cos(0.5*t), -cos(0.5*(p-l))*sin(0.5*t)},\n')
|
38 |
+
f.write('\t{cos(0.5*(p-l))*sin(0.5*t), cos(0.5*(p+l))*cos(0.5*t)}},\n')
|
39 |
+
f.write('.imag = {{-sin(0.5*(p+l))*cos(0.5*t), sin(0.5*(p-l))*sin(0.5*t)},\n')
|
40 |
+
f.write('\t{sin(0.5*(p-l))*sin(0.5*t), sin(0.5*(p+l))*cos(0.5*t)}}};\n')
|
41 |
+
f.write('unitary(qubits, q, u);\n')
|
42 |
+
f.write('}\n\n')
|
43 |
+
|
44 |
+
f.write('void pauliSX(Qureg qubits, const int q) {\n')
|
45 |
+
f.write('ComplexMatrix2 u = {\n')
|
46 |
+
f.write('.real = {{0.5, 0.5},\n')
|
47 |
+
f.write('\t{0.5, 0.5}},\n')
|
48 |
+
f.write('.imag = {{0.5, -0.5},\n')
|
49 |
+
f.write('\t{-0.5, 0.5}}};\n')
|
50 |
+
f.write('unitary(qubits, q, u);\n')
|
51 |
+
f.write('}\n\n')
|
52 |
+
|
53 |
+
f.write('void pauliSXDG(Qureg qubits, const int q) {\n')
|
54 |
+
f.write('ComplexMatrix2 u = {\n')
|
55 |
+
f.write('.real = {{0.5, 0.5},\n')
|
56 |
+
f.write('\t{0.5, 0.5}},\n')
|
57 |
+
f.write('.imag = {{-0.5, 0.5},\n')
|
58 |
+
f.write('\t{0.5, -0.5}}};\n')
|
59 |
+
f.write('unitary(qubits, q, u);\n')
|
60 |
+
f.write('}\n\n')
|
61 |
+
|
62 |
+
f.write('int main(int argc, char* argv[]){\n')
|
63 |
+
|
64 |
+
f.write('\tQuESTEnv env = createQuESTEnv();\n')
|
65 |
+
f.write('\tQureg qubits = createQureg({}, env);\n'.format(N))
|
66 |
+
|
67 |
+
f.write('\tstruct timespec requestStart, requestEnd;\n')
|
68 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestStart);\n')
|
69 |
+
|
70 |
+
lc = 0
|
71 |
+
|
72 |
+
with open(input_filename, "r") as ifile:
|
73 |
+
lines = ifile.readlines()
|
74 |
+
|
75 |
+
for line in lines:
|
76 |
+
|
77 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
78 |
+
|
79 |
+
if s is None:
|
80 |
+
continue
|
81 |
+
|
82 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits', 'cirq']:
|
83 |
+
lc = lc + 1
|
84 |
+
f.write('// {}\n'.format(s.group()))
|
85 |
+
continue
|
86 |
+
|
87 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 'sx' or s.group() == 'sxdg':
|
88 |
+
lc = lc + 1
|
89 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
90 |
+
t_qbit = int(m_i[0].strip('[]'))
|
91 |
+
f.write('\tpauli{}(qubits, {});\n'.format(s.group().upper(), t_qbit))
|
92 |
+
continue
|
93 |
+
|
94 |
+
elif s.group() == 'h':
|
95 |
+
lc = lc + 1
|
96 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
97 |
+
t_qbit = int(m_i[0].strip('[]'))
|
98 |
+
f.write('\thadamard(qubits, {});\n'.format(t_qbit))
|
99 |
+
continue
|
100 |
+
|
101 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
102 |
+
lc = lc + 1
|
103 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
104 |
+
# print(m_r)
|
105 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
106 |
+
sp_str = m_r[0].split('e-')
|
107 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
108 |
+
elif 'pi' in m_r[0]:
|
109 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
110 |
+
# print(m_r)
|
111 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
112 |
+
t_qbit = int(m_i[0].strip('[]'))
|
113 |
+
f.write('\trotate{}(qubits, {}, M_PI*{});\n'.format(s.group()[1].upper(), t_qbit, float(m_r[0][0])))
|
114 |
+
continue
|
115 |
+
|
116 |
+
elif s.group() == "s":
|
117 |
+
lc = lc + 1
|
118 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
119 |
+
t_qbit = int(m_i[0].strip('[]'))
|
120 |
+
f.write('\tsGate(qubits, {});\n'.format(t_qbit))
|
121 |
+
continue
|
122 |
+
|
123 |
+
elif s.group() == 'cx':
|
124 |
+
lc = lc + 1
|
125 |
+
match = re.findall(r'\[\d\d*\]', line)
|
126 |
+
c_qbit = int(match[0].strip('[]'))
|
127 |
+
t_qbit = int(match[1].strip('[]'))
|
128 |
+
f.write('\tcontrolledNot(qubits, {}, {});\n'.format(c_qbit, t_qbit))
|
129 |
+
continue
|
130 |
+
|
131 |
+
elif s.group() == 'u2':
|
132 |
+
lc = lc + 1
|
133 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
134 |
+
m_r = m_r[0].split(',')
|
135 |
+
for i, m in enumerate(m_r):
|
136 |
+
if 'pi' in m and 'e' in m:
|
137 |
+
sp_str = m_r[i].split('e-')
|
138 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
139 |
+
elif 'pi' in m:
|
140 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
141 |
+
else:
|
142 |
+
m_r[i] = '0'
|
143 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
144 |
+
t_qbit = int(m_i[0].strip('[]'))
|
145 |
+
f.write('\tu2(qubits, {}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
146 |
+
continue
|
147 |
+
|
148 |
+
elif s.group() == 'u3':
|
149 |
+
lc = lc + 1
|
150 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
151 |
+
m_r = m_r[0].split(',')
|
152 |
+
for i, m in enumerate(m_r):
|
153 |
+
if 'pi' in m and 'e' in m:
|
154 |
+
sp_str = m_r[i].split('e-')
|
155 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
156 |
+
elif 'pi' in m:
|
157 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
158 |
+
else:
|
159 |
+
m_r[i] = '0'
|
160 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
161 |
+
t_qbit = int(m_i[0].strip('[]'))
|
162 |
+
f.write('\tu3(qubits, {}, M_PI*{}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
163 |
+
continue
|
164 |
+
|
165 |
+
f.write('\tdestroyQureg(qubits, env);\n')
|
166 |
+
f.write('\tdestroyQuESTEnv(env);\n')
|
167 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestEnd);\n')
|
168 |
+
|
169 |
+
f.write('\tdouble accum = (requestEnd.tv_sec - requestStart.tv_sec) + (requestEnd.tv_nsec - requestStart.tv_nsec)/BILLION;\n')
|
170 |
+
f.write('\tprintf( "%lf, ", accum );\n')
|
171 |
+
f.write('\treturn 0;\n')
|
172 |
+
f.write('}')
|
173 |
+
|
174 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
175 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
176 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
177 |
+
print(int(lc_res) == lc)
|
178 |
+
# print(lc)
|
179 |
+
|
180 |
+
task = 'rqc'
|
181 |
+
sim_pack = 'quest'
|
182 |
+
com_cap = 'mt'
|
183 |
+
prec = 'dp'
|
184 |
+
|
185 |
+
for N in range(12, 40, 2):
|
186 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
187 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
188 |
+
|
189 |
+
# save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
190 |
+
|
191 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
192 |
+
|
193 |
+
output_file = output_file_path + '{}_n{}.c'.format(task, N)
|
194 |
+
parse_qasm_to_package_gen(N, 'double', input_file, output_file)
|
195 |
+
|
196 |
+
|
197 |
+
r"""
|
198 |
+
for N in range(12, 42, 2):
|
199 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
200 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
201 |
+
|
202 |
+
output_file_path = '/data/user/gangap_a/rqc/quest/QuEST/rqc/quest_sp_run_files/'
|
203 |
+
output_file = output_file_path + 'quest_rqc_n{}.c'.format(N)
|
204 |
+
parse_qasm_to_package_gen(N, 'float', input_file, output_file)
|
205 |
+
|
206 |
+
for N in range(41, 51):
|
207 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
208 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
209 |
+
|
210 |
+
output_file_path = '/data/user/gangap_a/rqc/quest/QuEST/rqc/quest_sp_run_files/'
|
211 |
+
output_file = output_file_path + 'quest_rqc_n{}.c'.format(N)
|
212 |
+
parse_qasm_to_package_gen(N, 'float', input_file, output_file)
|
213 |
+
|
214 |
+
"""
|
qasm_parser/quest/qasm_quest_parser_sp.py
ADDED
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, precision, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('#include <stdio.h>\n')
|
11 |
+
f.write('#include <math.h>\n')
|
12 |
+
f.write('#include <time.h>\n')
|
13 |
+
f.write('#include <stdlib.h>\n')
|
14 |
+
|
15 |
+
f.write('#include "QuEST.h"\n')
|
16 |
+
|
17 |
+
if precision == 'single':
|
18 |
+
f.write('#define QuEST_PREC 1\n')
|
19 |
+
elif precision == 'double':
|
20 |
+
f.write('#define QuEST_PREC 2\n')
|
21 |
+
|
22 |
+
f.write('#define BILLION 1E9\n')
|
23 |
+
|
24 |
+
f.write('\n')
|
25 |
+
|
26 |
+
f.write('void u2(Qureg qubits, const int q, float p, float l) {\n')
|
27 |
+
f.write('ComplexMatrix2 u = {\n')
|
28 |
+
f.write('.real = {{cos(0.5*(p+l))/sqrt(2), -cos(0.5*(p-l))/sqrt(2)},\n')
|
29 |
+
f.write('\t{cos(0.5*(p-l))/sqrt(2), cos(0.5*(p+l))/sqrt(2)}},\n')
|
30 |
+
f.write('.imag = {{-sin(0.5*(p+l))/sqrt(2), sin(0.5*(p-l))/sqrt(2)},\n')
|
31 |
+
f.write('\t{sin(0.5*(p-l))/sqrt(2), sin(0.5*(p+l))/sqrt(2)}}};\n')
|
32 |
+
f.write('unitary(qubits, q, u);\n')
|
33 |
+
f.write('}\n\n')
|
34 |
+
|
35 |
+
f.write('void u3(Qureg qubits, const int q, float t, float p, float l) {\n')
|
36 |
+
f.write('ComplexMatrix2 u = {\n')
|
37 |
+
f.write('.real = {{cos(0.5*(p+l))*cos(0.5*t), -cos(0.5*(p-l))*sin(0.5*t)},\n')
|
38 |
+
f.write('\t{cos(0.5*(p-l))*sin(0.5*t), cos(0.5*(p+l))*cos(0.5*t)}},\n')
|
39 |
+
f.write('.imag = {{-sin(0.5*(p+l))*cos(0.5*t), sin(0.5*(p-l))*sin(0.5*t)},\n')
|
40 |
+
f.write('\t{sin(0.5*(p-l))*sin(0.5*t), sin(0.5*(p+l))*cos(0.5*t)}}};\n')
|
41 |
+
f.write('unitary(qubits, q, u);\n')
|
42 |
+
f.write('}\n\n')
|
43 |
+
|
44 |
+
f.write('void pauliSX(Qureg qubits, const int q) {\n')
|
45 |
+
f.write('ComplexMatrix2 u = {\n')
|
46 |
+
f.write('.real = {{0.5, 0.5},\n')
|
47 |
+
f.write('\t{0.5, 0.5}},\n')
|
48 |
+
f.write('.imag = {{0.5, -0.5},\n')
|
49 |
+
f.write('\t{-0.5, 0.5}}};\n')
|
50 |
+
f.write('unitary(qubits, q, u);\n')
|
51 |
+
f.write('}\n\n')
|
52 |
+
|
53 |
+
f.write('void pauliSXDG(Qureg qubits, const int q) {\n')
|
54 |
+
f.write('ComplexMatrix2 u = {\n')
|
55 |
+
f.write('.real = {{0.5, 0.5},\n')
|
56 |
+
f.write('\t{0.5, 0.5}},\n')
|
57 |
+
f.write('.imag = {{-0.5, 0.5},\n')
|
58 |
+
f.write('\t{0.5, -0.5}}};\n')
|
59 |
+
f.write('unitary(qubits, q, u);\n')
|
60 |
+
f.write('}\n\n')
|
61 |
+
|
62 |
+
f.write('int main(int argc, char* argv[]){\n')
|
63 |
+
|
64 |
+
f.write('\tQuESTEnv env = createQuESTEnv();\n')
|
65 |
+
f.write('\tQureg qubits = createQureg({}, env);\n'.format(N))
|
66 |
+
|
67 |
+
f.write('\tstruct timespec requestStart, requestEnd;\n')
|
68 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestStart);\n')
|
69 |
+
|
70 |
+
lc = 0
|
71 |
+
|
72 |
+
with open(input_filename, "r") as ifile:
|
73 |
+
lines = ifile.readlines()
|
74 |
+
|
75 |
+
for line in lines:
|
76 |
+
|
77 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
78 |
+
|
79 |
+
if s is None:
|
80 |
+
continue
|
81 |
+
|
82 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits', 'cirq']:
|
83 |
+
lc = lc + 1
|
84 |
+
f.write('// {}\n'.format(s.group()))
|
85 |
+
continue
|
86 |
+
|
87 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 'sx' or s.group() == 'sxdg':
|
88 |
+
lc = lc + 1
|
89 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
90 |
+
t_qbit = int(m_i[0].strip('[]'))
|
91 |
+
f.write('\tpauli{}(qubits, {});\n'.format(s.group().upper(), t_qbit))
|
92 |
+
continue
|
93 |
+
|
94 |
+
elif s.group() == 'h':
|
95 |
+
lc = lc + 1
|
96 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
97 |
+
t_qbit = int(m_i[0].strip('[]'))
|
98 |
+
f.write('\thadamard(qubits, {});\n'.format(t_qbit))
|
99 |
+
continue
|
100 |
+
|
101 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
102 |
+
lc = lc + 1
|
103 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
104 |
+
# print(m_r)
|
105 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
106 |
+
sp_str = m_r[0].split('e-')
|
107 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
108 |
+
elif 'pi' in m_r[0]:
|
109 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
110 |
+
# print(m_r)
|
111 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
112 |
+
t_qbit = int(m_i[0].strip('[]'))
|
113 |
+
f.write('\trotate{}(qubits, {}, M_PI*{});\n'.format(s.group()[1].upper(), t_qbit, float(m_r[0][0])))
|
114 |
+
continue
|
115 |
+
|
116 |
+
elif s.group() == "s":
|
117 |
+
lc = lc + 1
|
118 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
119 |
+
t_qbit = int(m_i[0].strip('[]'))
|
120 |
+
f.write('\tsGate(qubits, {});\n'.format(t_qbit))
|
121 |
+
continue
|
122 |
+
|
123 |
+
elif s.group() == 'cx':
|
124 |
+
lc = lc + 1
|
125 |
+
match = re.findall(r'\[\d\d*\]', line)
|
126 |
+
c_qbit = int(match[0].strip('[]'))
|
127 |
+
t_qbit = int(match[1].strip('[]'))
|
128 |
+
f.write('\tcontrolledNot(qubits, {}, {});\n'.format(c_qbit, t_qbit))
|
129 |
+
continue
|
130 |
+
|
131 |
+
elif s.group() == 'u2':
|
132 |
+
lc = lc + 1
|
133 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
134 |
+
m_r = m_r[0].split(',')
|
135 |
+
for i, m in enumerate(m_r):
|
136 |
+
if 'pi' in m and 'e' in m:
|
137 |
+
sp_str = m_r[i].split('e-')
|
138 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
139 |
+
elif 'pi' in m:
|
140 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
141 |
+
else:
|
142 |
+
m_r[i] = '0'
|
143 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
144 |
+
t_qbit = int(m_i[0].strip('[]'))
|
145 |
+
f.write('\tu2(qubits, {}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
146 |
+
continue
|
147 |
+
|
148 |
+
elif s.group() == 'u3':
|
149 |
+
lc = lc + 1
|
150 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
151 |
+
m_r = m_r[0].split(',')
|
152 |
+
for i, m in enumerate(m_r):
|
153 |
+
if 'pi' in m and 'e' in m:
|
154 |
+
sp_str = m_r[i].split('e-')
|
155 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
156 |
+
elif 'pi' in m:
|
157 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
158 |
+
else:
|
159 |
+
m_r[i] = '0'
|
160 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
161 |
+
t_qbit = int(m_i[0].strip('[]'))
|
162 |
+
f.write('\tu3(qubits, {}, M_PI*{}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
163 |
+
continue
|
164 |
+
|
165 |
+
f.write('\tdestroyQureg(qubits, env);\n')
|
166 |
+
f.write('\tdestroyQuESTEnv(env);\n')
|
167 |
+
f.write('\tclock_gettime(CLOCK_REALTIME, &requestEnd);\n')
|
168 |
+
|
169 |
+
f.write('\tdouble accum = (requestEnd.tv_sec - requestStart.tv_sec) + (requestEnd.tv_nsec - requestStart.tv_nsec)/BILLION;\n')
|
170 |
+
f.write('\tprintf( "%lf, ", accum );\n')
|
171 |
+
f.write('\treturn 0;\n')
|
172 |
+
f.write('}')
|
173 |
+
|
174 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
175 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
176 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
177 |
+
print(int(lc_res) == lc)
|
178 |
+
# print(lc)
|
179 |
+
|
180 |
+
task = 'rqc'
|
181 |
+
sim_pack = 'quest'
|
182 |
+
com_cap = 'mt'
|
183 |
+
prec = 'sp'
|
184 |
+
|
185 |
+
for N in range(12, 40, 2):
|
186 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
187 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
188 |
+
|
189 |
+
# save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
190 |
+
|
191 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
192 |
+
|
193 |
+
output_file = output_file_path + '{}_n{}.c'.format(task, N)
|
194 |
+
parse_qasm_to_package_gen(N, 'single', input_file, output_file)
|
195 |
+
|
196 |
+
|
197 |
+
r"""
|
198 |
+
for N in range(12, 42, 2):
|
199 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
200 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
201 |
+
|
202 |
+
output_file_path = '/data/user/gangap_a/rqc/quest/QuEST/rqc/quest_sp_run_files/'
|
203 |
+
output_file = output_file_path + 'quest_rqc_n{}.c'.format(N)
|
204 |
+
parse_qasm_to_package_gen(N, 'float', input_file, output_file)
|
205 |
+
|
206 |
+
for N in range(41, 51):
|
207 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
208 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
209 |
+
|
210 |
+
output_file_path = '/data/user/gangap_a/rqc/quest/QuEST/rqc/quest_sp_run_files/'
|
211 |
+
output_file = output_file_path + 'quest_rqc_n{}.c'.format(N)
|
212 |
+
parse_qasm_to_package_gen(N, 'float', input_file, output_file)
|
213 |
+
|
214 |
+
"""
|
qasm_parser/qulacs/qasm_qulacs_gpu_new_parser.py
ADDED
@@ -0,0 +1,200 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import numpy as np\n')
|
11 |
+
f.write('import qulacs as qul\n')
|
12 |
+
f.write('from qulacs.gate import DenseMatrix\n')
|
13 |
+
f.write('import os\n')
|
14 |
+
f.write('from time import process_time, perf_counter\n')
|
15 |
+
|
16 |
+
f.write('def Rx(q, t):\n')
|
17 |
+
f.write(' mat = [[np.cos(0.5*t), -1j*np.sin(0.5*t)], [-1j*np.sin(0.5*t), np.cos(0.5*t)]]\n')
|
18 |
+
f.write(' return DenseMatrix(q, mat)\n\n')
|
19 |
+
|
20 |
+
f.write('def Ry(q, t):\n')
|
21 |
+
f.write(' mat = [[np.cos(0.5*t), -np.sin(0.5*t)], [np.sin(0.5*t), np.cos(0.5*t)]]\n')
|
22 |
+
f.write(' return DenseMatrix(q, mat)\n\n')
|
23 |
+
|
24 |
+
f.write('def Rz(q, t):\n')
|
25 |
+
f.write(' mat = [[np.exp(-0.5j*t), 0.], [0., np.exp(0.5j*t)]]\n')
|
26 |
+
f.write(' return DenseMatrix(q, mat)\n\n')
|
27 |
+
|
28 |
+
f.write('def U2(q, p, l):\n')
|
29 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
30 |
+
f.write(' return DenseMatrix(q, mat)\n\n')
|
31 |
+
|
32 |
+
f.write('def U3(q, t, p, l):\n')
|
33 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
34 |
+
f.write(' return DenseMatrix(q, mat)\n\n')
|
35 |
+
|
36 |
+
f.write('t_sp = process_time()\n')
|
37 |
+
f.write('t_s = perf_counter()\n')
|
38 |
+
|
39 |
+
f.write('qc = qul.QuantumCircuit({})\n'.format(N))
|
40 |
+
|
41 |
+
with open(input_filename, "r") as ifile:
|
42 |
+
lines = ifile.readlines()
|
43 |
+
|
44 |
+
lc = 0
|
45 |
+
cirq_c = 0
|
46 |
+
for line in lines:
|
47 |
+
|
48 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
49 |
+
|
50 |
+
if s is None:
|
51 |
+
continue
|
52 |
+
|
53 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
54 |
+
lc = lc + 1
|
55 |
+
f.write('# {}\n'.format(s.group()))
|
56 |
+
continue
|
57 |
+
|
58 |
+
elif s.group() == 'cirq':
|
59 |
+
lc = lc + 1
|
60 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
61 |
+
cirq_c = cirq_c + 1
|
62 |
+
continue
|
63 |
+
|
64 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'h':
|
65 |
+
lc = lc + 1
|
66 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
67 |
+
t_qbit = int(m_i[0].strip('[]'))
|
68 |
+
f.write('qc.add_{}_gate({})\n'.format(s.group().upper(), t_qbit))
|
69 |
+
continue
|
70 |
+
|
71 |
+
elif s.group() == 'sx':
|
72 |
+
lc = lc + 1
|
73 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
74 |
+
t_qbit = int(m_i[0].strip('[]'))
|
75 |
+
f.write('qc.add_sqrtX_gate({})\n'.format(t_qbit))
|
76 |
+
|
77 |
+
elif s.group() == 'sxdg':
|
78 |
+
lc = lc + 1
|
79 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
80 |
+
t_qbit = int(m_i[0].strip('[]'))
|
81 |
+
f.write('qc.add_sqrtXdag_gate({})\n'.format(t_qbit))
|
82 |
+
continue
|
83 |
+
|
84 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
85 |
+
lc = lc + 1
|
86 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
87 |
+
# print(m_r)
|
88 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
89 |
+
sp_str = m_r[0].split('e-')
|
90 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
91 |
+
elif 'pi' in m_r[0]:
|
92 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
93 |
+
# print(m_r)
|
94 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
95 |
+
t_qbit = int(m_i[0].strip('[]'))
|
96 |
+
# f.write('qc.add_{}gate({}, np.pi*{})\n'.format(s.group().upper(), t_qbit, float(m_r[0][0])))
|
97 |
+
f.write('qc.add_gate(R{}({}, np.pi*{}))\n'.format(s.group()[1], t_qbit, float(m_r[0][0])))
|
98 |
+
continue
|
99 |
+
|
100 |
+
elif s.group() == 'cx':
|
101 |
+
lc = lc + 1
|
102 |
+
match = re.findall(r'\[\d\d*\]', line)
|
103 |
+
c_qbit = int(match[0].strip('[]'))
|
104 |
+
t_qbit = int(match[1].strip('[]'))
|
105 |
+
f.write('qc.add_CNOT_gate({}, {})\n'.format(c_qbit, t_qbit))
|
106 |
+
continue
|
107 |
+
|
108 |
+
elif s.group() == 'u2':
|
109 |
+
lc = lc + 1
|
110 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
111 |
+
m_r = m_r[0].split(',')
|
112 |
+
for i, m in enumerate(m_r):
|
113 |
+
if 'pi' in m and 'e' in m:
|
114 |
+
sp_str = m_r[i].split('e-')
|
115 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
116 |
+
elif 'pi' in m:
|
117 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
118 |
+
else:
|
119 |
+
m_r[i] = '0'
|
120 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
121 |
+
t_qbit = int(m_i[0].strip('[]'))
|
122 |
+
# f.write('qc.add_U2_gate({}, np.pi*{}, np.pi*{})\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
123 |
+
f.write('qc.add_gate(U2({}, np.pi*{}, np.pi*{}))\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
124 |
+
continue
|
125 |
+
|
126 |
+
elif s.group() == 'u3':
|
127 |
+
lc = lc + 1
|
128 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
129 |
+
m_r = m_r[0].split(',')
|
130 |
+
for i, m in enumerate(m_r):
|
131 |
+
if 'pi' in m and 'e' in m:
|
132 |
+
sp_str = m_r[i].split('e-')
|
133 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
134 |
+
elif 'pi' in m:
|
135 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
136 |
+
else:
|
137 |
+
m_r[i] = '0'
|
138 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
139 |
+
t_qbit = int(m_i[0].strip('[]'))
|
140 |
+
f.write('qc.add_gate(U3({}, np.pi*{}, np.pi*{}, np.pi*{}))\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
141 |
+
continue
|
142 |
+
|
143 |
+
# f.write('init_state = qul.QuantumState({})\n'.format(N))
|
144 |
+
f.write('init_state = qul.QuantumStateGpu({})\n'.format(N))
|
145 |
+
f.write('init_state.set_zero_state()\n')
|
146 |
+
f.write('qc.update_quantum_state(init_state)\n')
|
147 |
+
|
148 |
+
f.write('del init_state\n')
|
149 |
+
f.write('del qc\n')
|
150 |
+
|
151 |
+
f.write('t_e = perf_counter()\n')
|
152 |
+
f.write('t_ep = process_time()\n')
|
153 |
+
f.write('print(t_e - t_s)\n')
|
154 |
+
f.write('print(t_ep - t_sp)\n')
|
155 |
+
|
156 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
157 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
158 |
+
|
159 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
160 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
161 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
162 |
+
print(int(lc_res) == lc)
|
163 |
+
# print(lc)
|
164 |
+
|
165 |
+
# parse_qasm_to_package_gen(12, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "n12_qulacs.py")
|
166 |
+
|
167 |
+
task = 'qft'
|
168 |
+
sim_pack = 'qulacs'
|
169 |
+
com_cap = 'gpu'
|
170 |
+
prec = 'dp'
|
171 |
+
|
172 |
+
for N in range(6, 40, 2):
|
173 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
174 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
175 |
+
|
176 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
177 |
+
|
178 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
179 |
+
|
180 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
181 |
+
parse_qasm_to_package_gen(N, input_file, output_file, save_file_path)
|
182 |
+
|
183 |
+
r"""
|
184 |
+
for N in range(12, 42, 2):
|
185 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
186 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
187 |
+
|
188 |
+
output_file_path = '/data/user/gangap_a/rqc/qulacs/qulacs_gpu_run_files/'
|
189 |
+
output_file = output_file_path + 'qulacs_rqc_n{}.py'.format(N)
|
190 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
191 |
+
|
192 |
+
for N in range(41, 51):
|
193 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
194 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
195 |
+
|
196 |
+
output_file_path = '/data/user/gangap_a/rqc/qulacs/qulacs_gpu_run_files/'
|
197 |
+
output_file = output_file_path + 'qulacs_rqc_n{}.py'.format(N)
|
198 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
199 |
+
|
200 |
+
"""
|
qasm_parser/qulacs/qasm_qulacs_new_parser.py
ADDED
@@ -0,0 +1,200 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, input_filename, output_filename, save_data_path):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import numpy as np\n')
|
11 |
+
f.write('import qulacs as qul\n')
|
12 |
+
f.write('from qulacs.gate import DenseMatrix\n')
|
13 |
+
f.write('import os\n')
|
14 |
+
f.write('from time import process_time, perf_counter\n')
|
15 |
+
|
16 |
+
f.write('def Rx(q, t):\n')
|
17 |
+
f.write(' mat = [[np.cos(0.5*t), -1j*np.sin(0.5*t)], [-1j*np.sin(0.5*t), np.cos(0.5*t)]]\n')
|
18 |
+
f.write(' return DenseMatrix(q, mat)\n\n')
|
19 |
+
|
20 |
+
f.write('def Ry(q, t):\n')
|
21 |
+
f.write(' mat = [[np.cos(0.5*t), -np.sin(0.5*t)], [np.sin(0.5*t), np.cos(0.5*t)]]\n')
|
22 |
+
f.write(' return DenseMatrix(q, mat)\n\n')
|
23 |
+
|
24 |
+
f.write('def Rz(q, t):\n')
|
25 |
+
f.write(' mat = [[np.exp(-0.5j*t), 0.], [0., np.exp(0.5j*t)]]\n')
|
26 |
+
f.write(' return DenseMatrix(q, mat)\n\n')
|
27 |
+
|
28 |
+
f.write('def U2(q, p, l):\n')
|
29 |
+
f.write(' mat = ((np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))/np.sqrt(2))*np.array([[1, -np.cos(l)-1j*np.sin(l)], [np.cos(p) + 1j*np.sin(p), np.cos(l + p) + 1j*np.sin(l + p)]])\n')
|
30 |
+
f.write(' return DenseMatrix(q, mat)\n\n')
|
31 |
+
|
32 |
+
f.write('def U3(q, t, p, l):\n')
|
33 |
+
f.write(' mat = (np.cos(0.5*(p + l)) - 1j*np.sin(0.5*(p + l)))*np.array([[np.cos(t/2.), -np.sin(t/2.)*(np.cos(l) + 1j*np.sin(l))], [np.sin(t/2.)*(np.cos(p) + 1j*np.sin(p)), np.cos(t/2.)*(np.cos(l + p) + 1j*np.sin(l + p))]])\n')
|
34 |
+
f.write(' return DenseMatrix(q, mat)\n\n')
|
35 |
+
|
36 |
+
f.write('t_sp = process_time()\n')
|
37 |
+
f.write('t_s = perf_counter()\n')
|
38 |
+
|
39 |
+
f.write('qc = qul.QuantumCircuit({})\n'.format(N))
|
40 |
+
|
41 |
+
with open(input_filename, "r") as ifile:
|
42 |
+
lines = ifile.readlines()
|
43 |
+
|
44 |
+
lc = 0
|
45 |
+
cirq_c = 0
|
46 |
+
for line in lines:
|
47 |
+
|
48 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
49 |
+
|
50 |
+
if s is None:
|
51 |
+
continue
|
52 |
+
|
53 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
54 |
+
lc = lc + 1
|
55 |
+
f.write('# {}\n'.format(s.group()))
|
56 |
+
continue
|
57 |
+
|
58 |
+
elif s.group() == 'cirq':
|
59 |
+
lc = lc + 1
|
60 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
61 |
+
cirq_c = cirq_c + 1
|
62 |
+
continue
|
63 |
+
|
64 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'h':
|
65 |
+
lc = lc + 1
|
66 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
67 |
+
t_qbit = int(m_i[0].strip('[]'))
|
68 |
+
f.write('qc.add_{}_gate({})\n'.format(s.group().upper(), t_qbit))
|
69 |
+
continue
|
70 |
+
|
71 |
+
elif s.group() == 'sx':
|
72 |
+
lc = lc + 1
|
73 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
74 |
+
t_qbit = int(m_i[0].strip('[]'))
|
75 |
+
f.write('qc.add_sqrtX_gate({})\n'.format(t_qbit))
|
76 |
+
|
77 |
+
elif s.group() == 'sxdg':
|
78 |
+
lc = lc + 1
|
79 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
80 |
+
t_qbit = int(m_i[0].strip('[]'))
|
81 |
+
f.write('qc.add_sqrtXdag_gate({})\n'.format(t_qbit))
|
82 |
+
continue
|
83 |
+
|
84 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
85 |
+
lc = lc + 1
|
86 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
87 |
+
# print(m_r)
|
88 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
89 |
+
sp_str = m_r[0].split('e-')
|
90 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
91 |
+
elif 'pi' in m_r[0]:
|
92 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
93 |
+
# print(m_r)
|
94 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
95 |
+
t_qbit = int(m_i[0].strip('[]'))
|
96 |
+
# f.write('qc.add_{}gate({}, np.pi*{})\n'.format(s.group().upper(), t_qbit, float(m_r[0][0])))
|
97 |
+
f.write('qc.add_gate(R{}({}, np.pi*{}))\n'.format(s.group()[1], t_qbit, float(m_r[0][0])))
|
98 |
+
continue
|
99 |
+
|
100 |
+
elif s.group() == 'cx':
|
101 |
+
lc = lc + 1
|
102 |
+
match = re.findall(r'\[\d\d*\]', line)
|
103 |
+
c_qbit = int(match[0].strip('[]'))
|
104 |
+
t_qbit = int(match[1].strip('[]'))
|
105 |
+
f.write('qc.add_CNOT_gate({}, {})\n'.format(c_qbit, t_qbit))
|
106 |
+
continue
|
107 |
+
|
108 |
+
elif s.group() == 'u2':
|
109 |
+
lc = lc + 1
|
110 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
111 |
+
m_r = m_r[0].split(',')
|
112 |
+
for i, m in enumerate(m_r):
|
113 |
+
if 'pi' in m and 'e' in m:
|
114 |
+
sp_str = m_r[i].split('e-')
|
115 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
116 |
+
elif 'pi' in m:
|
117 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
118 |
+
else:
|
119 |
+
m_r[i] = '0'
|
120 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
121 |
+
t_qbit = int(m_i[0].strip('[]'))
|
122 |
+
# f.write('qc.add_U2_gate({}, np.pi*{}, np.pi*{})\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
123 |
+
f.write('qc.add_gate(U2({}, np.pi*{}, np.pi*{}))\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
|
124 |
+
continue
|
125 |
+
|
126 |
+
elif s.group() == 'u3':
|
127 |
+
lc = lc + 1
|
128 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
129 |
+
m_r = m_r[0].split(',')
|
130 |
+
for i, m in enumerate(m_r):
|
131 |
+
if 'pi' in m and 'e' in m:
|
132 |
+
sp_str = m_r[i].split('e-')
|
133 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
134 |
+
elif 'pi' in m:
|
135 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
136 |
+
else:
|
137 |
+
m_r[i] = '0'
|
138 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
139 |
+
t_qbit = int(m_i[0].strip('[]'))
|
140 |
+
f.write('qc.add_gate(U3({}, np.pi*{}, np.pi*{}, np.pi*{}))\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0])))
|
141 |
+
continue
|
142 |
+
|
143 |
+
f.write('init_state = qul.QuantumState({})\n'.format(N))
|
144 |
+
f.write('init_state.set_zero_state()\n')
|
145 |
+
f.write('qc.update_quantum_state(init_state)\n')
|
146 |
+
|
147 |
+
f.write('del init_state\n')
|
148 |
+
f.write('del qc\n')
|
149 |
+
|
150 |
+
f.write('t_e = perf_counter()\n')
|
151 |
+
f.write('t_ep = process_time()\n')
|
152 |
+
f.write('print(t_e - t_s)\n')
|
153 |
+
f.write('print(t_ep - t_sp)\n')
|
154 |
+
|
155 |
+
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
|
156 |
+
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
|
157 |
+
|
158 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
159 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
160 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
161 |
+
print(int(lc_res) == lc)
|
162 |
+
# print(lc)
|
163 |
+
|
164 |
+
# parse_qasm_to_package_gen(12, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "n12_qulacs.py")
|
165 |
+
|
166 |
+
task = 'rqc'
|
167 |
+
sim_pack = 'qulacs'
|
168 |
+
com_cap = 'mt'
|
169 |
+
prec = 'dp'
|
170 |
+
|
171 |
+
for N in range(12, 40, 2):
|
172 |
+
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
|
173 |
+
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
|
174 |
+
|
175 |
+
save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
|
176 |
+
|
177 |
+
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
|
178 |
+
|
179 |
+
output_file = output_file_path + '{}_n{}.py'.format(task, N)
|
180 |
+
parse_qasm_to_package_gen(N, input_file, output_file, save_file_path)
|
181 |
+
|
182 |
+
|
183 |
+
"""
|
184 |
+
for N in range(12, 42, 2):
|
185 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
186 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
187 |
+
|
188 |
+
output_file_path = '/data/user/gangap_a/rqc/qulacs/qulacs_run_files/'
|
189 |
+
output_file = output_file_path + 'qulacs_rqc_n{}.py'.format(N)
|
190 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
191 |
+
|
192 |
+
for N in range(41, 51):
|
193 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
194 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
195 |
+
|
196 |
+
output_file_path = '/data/user/gangap_a/rqc/qulacs/qulacs_run_files/'
|
197 |
+
output_file = output_file_path + 'qulacs_rqc_n{}.py'.format(N)
|
198 |
+
parse_qasm_to_package_gen(N, input_file, output_file)
|
199 |
+
|
200 |
+
"""
|
qasm_parser/svsim/qasm_svsim_gpu_a100_parser.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, n_cores, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
f.write('import sys\n')
|
12 |
+
f.write('sys.path.insert(0, \'/data/user/gangap_a/rqc/svsim/SV-Sim/svsim/build_a100\')\n')
|
13 |
+
f.write('import numpy as np\n')
|
14 |
+
f.write('from time import process_time, perf_counter\n')
|
15 |
+
f.write('import libsvsim as svsim\n')
|
16 |
+
|
17 |
+
f.write('t_sp = process_time()\n')
|
18 |
+
f.write('t_s = perf_counter()\n')
|
19 |
+
|
20 |
+
f.write('cir = svsim.Simulation({}, {})\n'.format(N, n_cores))
|
21 |
+
|
22 |
+
with open(input_filename, "r") as ifile:
|
23 |
+
lines = ifile.readlines()
|
24 |
+
|
25 |
+
lc = 0
|
26 |
+
cirq_c = 0
|
27 |
+
for line in lines:
|
28 |
+
|
29 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|h|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
30 |
+
|
31 |
+
if s is None:
|
32 |
+
continue
|
33 |
+
|
34 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
35 |
+
lc = lc + 1
|
36 |
+
f.write('# {}\n'.format(s.group()))
|
37 |
+
continue
|
38 |
+
|
39 |
+
elif s.group() == 'cirq':
|
40 |
+
lc = lc + 1
|
41 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
42 |
+
cirq_c = cirq_c + 1
|
43 |
+
continue
|
44 |
+
|
45 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'h':
|
46 |
+
lc = lc + 1
|
47 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
48 |
+
t_qbit = int(m_i[0].strip('[]'))
|
49 |
+
f.write('cir.append(cir.{}({}))\n'.format(s.group().upper(), t_qbit))
|
50 |
+
continue
|
51 |
+
|
52 |
+
elif s.group() == 'sx':
|
53 |
+
lc = lc + 1
|
54 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
55 |
+
t_qbit = int(m_i[0].strip('[]'))
|
56 |
+
f.write('cir.append(cir.SRN({}))\n'.format(t_qbit))
|
57 |
+
|
58 |
+
elif s.group() == 'sxdg':
|
59 |
+
lc = lc + 1
|
60 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
61 |
+
t_qbit = int(m_i[0].strip('[]'))
|
62 |
+
f.write('cir.append(cir.RX(-0.5*np.pi, {}))\n'.format(t_qbit))
|
63 |
+
continue
|
64 |
+
|
65 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
66 |
+
lc = lc + 1
|
67 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
68 |
+
# print(m_r)
|
69 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
70 |
+
sp_str = m_r[0].split('e-')
|
71 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
72 |
+
elif 'pi' in m_r[0]:
|
73 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
74 |
+
# print(m_r)
|
75 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
76 |
+
t_qbit = int(m_i[0].strip('[]'))
|
77 |
+
f.write('cir.append(cir.{}(np.pi*{}, {}))\n'.format(s.group().upper(), float(m_r[0][0]), t_qbit))
|
78 |
+
continue
|
79 |
+
|
80 |
+
elif s.group() == 'cx':
|
81 |
+
lc = lc + 1
|
82 |
+
match = re.findall(r'\[\d\d*\]', line)
|
83 |
+
c_qbit = int(match[0].strip('[]'))
|
84 |
+
t_qbit = int(match[1].strip('[]'))
|
85 |
+
f.write('cir.append(cir.CX({}, {}))\n'.format(c_qbit, t_qbit))
|
86 |
+
continue
|
87 |
+
|
88 |
+
elif s.group() == 'u2':
|
89 |
+
lc = lc + 1
|
90 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
91 |
+
m_r = m_r[0].split(',')
|
92 |
+
for i, m in enumerate(m_r):
|
93 |
+
if 'pi' in m and 'e' in m:
|
94 |
+
sp_str = m_r[i].split('e-')
|
95 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
96 |
+
elif 'pi' in m:
|
97 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
98 |
+
else:
|
99 |
+
m_r[i] = '0'
|
100 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
101 |
+
t_qbit = int(m_i[0].strip('[]'))
|
102 |
+
f.write('cir.append(cir.U2(np.pi*{}, np.pi*{}, {}))\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
103 |
+
continue
|
104 |
+
|
105 |
+
elif s.group() == 'u3':
|
106 |
+
lc = lc + 1
|
107 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
108 |
+
m_r = m_r[0].split(',')
|
109 |
+
for i, m in enumerate(m_r):
|
110 |
+
if 'pi' in m and 'e' in m:
|
111 |
+
sp_str = m_r[i].split('e-')
|
112 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
113 |
+
elif 'pi' in m:
|
114 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
115 |
+
else:
|
116 |
+
m_r[i] = '0'
|
117 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
118 |
+
t_qbit = int(m_i[0].strip('[]'))
|
119 |
+
f.write('cir.append(cir.U3(np.pi*{}, np.pi*{}, np.pi*{}, {}))\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
120 |
+
continue
|
121 |
+
|
122 |
+
f.write('cir.upload()\n')
|
123 |
+
f.write('cir.run()\n')
|
124 |
+
|
125 |
+
f.write('t_e = perf_counter()\n')
|
126 |
+
f.write('t_ep = process_time()\n')
|
127 |
+
f.write('print(t_e - t_s)\n')
|
128 |
+
f.write('print(t_ep - t_sp)\n')
|
129 |
+
|
130 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
131 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
132 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
133 |
+
print(int(lc_res) == lc)
|
134 |
+
# print(lc)
|
135 |
+
|
136 |
+
|
137 |
+
for N in range(12, 42, 2):
|
138 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
139 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
140 |
+
|
141 |
+
output_file_path = '/data/user/gangap_a/rqc/svsim/svsim_gpu_a100_run_files/'
|
142 |
+
output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N)
|
143 |
+
parse_qasm_to_package_gen(N, 0, input_file, output_file)
|
144 |
+
|
145 |
+
for N in range(41, 51):
|
146 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
147 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
148 |
+
|
149 |
+
output_file_path = '/data/user/gangap_a/rqc/svsim/svsim_gpu_a100_run_files/'
|
150 |
+
output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N)
|
151 |
+
parse_qasm_to_package_gen(N, 0, input_file, output_file)
|
152 |
+
|
qasm_parser/svsim/qasm_svsim_gpu_ma100_parser.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, n_cores, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
f.write('import sys\n')
|
12 |
+
f.write('sys.path.insert(0, \'/data/user/gangap_a/rqc/svsim/SV-Sim/svsim/build_ma100\')\n')
|
13 |
+
f.write('import numpy as np\n')
|
14 |
+
f.write('from time import process_time, perf_counter\n')
|
15 |
+
f.write('import libsvsim as svsim\n')
|
16 |
+
|
17 |
+
f.write('t_sp = process_time()\n')
|
18 |
+
f.write('t_s = perf_counter()\n')
|
19 |
+
|
20 |
+
f.write('cir = svsim.Simulation({}, {})\n'.format(N, n_cores))
|
21 |
+
|
22 |
+
with open(input_filename, "r") as ifile:
|
23 |
+
lines = ifile.readlines()
|
24 |
+
|
25 |
+
lc = 0
|
26 |
+
cirq_c = 0
|
27 |
+
for line in lines:
|
28 |
+
|
29 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
30 |
+
|
31 |
+
if s is None:
|
32 |
+
continue
|
33 |
+
|
34 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
35 |
+
lc = lc + 1
|
36 |
+
f.write('# {}\n'.format(s.group()))
|
37 |
+
continue
|
38 |
+
|
39 |
+
elif s.group() == 'cirq':
|
40 |
+
lc = lc + 1
|
41 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
42 |
+
cirq_c = cirq_c + 1
|
43 |
+
continue
|
44 |
+
|
45 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's':
|
46 |
+
lc = lc + 1
|
47 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
48 |
+
t_qbit = int(m_i[0].strip('[]'))
|
49 |
+
f.write('cir.append(cir.{}({}))\n'.format(s.group().upper(), t_qbit))
|
50 |
+
continue
|
51 |
+
|
52 |
+
elif s.group() == 'sx':
|
53 |
+
lc = lc + 1
|
54 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
55 |
+
t_qbit = int(m_i[0].strip('[]'))
|
56 |
+
f.write('cir.append(cir.SRN({}))\n'.format(t_qbit))
|
57 |
+
|
58 |
+
elif s.group() == 'sxdg':
|
59 |
+
lc = lc + 1
|
60 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
61 |
+
t_qbit = int(m_i[0].strip('[]'))
|
62 |
+
f.write('cir.append(cir.RX(-0.5*np.pi, {}))\n'.format(t_qbit))
|
63 |
+
continue
|
64 |
+
|
65 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
66 |
+
lc = lc + 1
|
67 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
68 |
+
# print(m_r)
|
69 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
70 |
+
sp_str = m_r[0].split('e-')
|
71 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
72 |
+
elif 'pi' in m_r[0]:
|
73 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
74 |
+
# print(m_r)
|
75 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
76 |
+
t_qbit = int(m_i[0].strip('[]'))
|
77 |
+
f.write('cir.append(cir.{}(np.pi*{}, {}))\n'.format(s.group().upper(), float(m_r[0][0]), t_qbit))
|
78 |
+
continue
|
79 |
+
|
80 |
+
elif s.group() == 'cx':
|
81 |
+
lc = lc + 1
|
82 |
+
match = re.findall(r'\[\d\d*\]', line)
|
83 |
+
c_qbit = int(match[0].strip('[]'))
|
84 |
+
t_qbit = int(match[1].strip('[]'))
|
85 |
+
f.write('cir.append(cir.CX({}, {}))\n'.format(c_qbit, t_qbit))
|
86 |
+
continue
|
87 |
+
|
88 |
+
elif s.group() == 'u2':
|
89 |
+
lc = lc + 1
|
90 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
91 |
+
m_r = m_r[0].split(',')
|
92 |
+
for i, m in enumerate(m_r):
|
93 |
+
if 'pi' in m and 'e' in m:
|
94 |
+
sp_str = m_r[i].split('e-')
|
95 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
96 |
+
elif 'pi' in m:
|
97 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
98 |
+
else:
|
99 |
+
m_r[i] = '0'
|
100 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
101 |
+
t_qbit = int(m_i[0].strip('[]'))
|
102 |
+
f.write('cir.append(cir.U2(np.pi*{}, np.pi*{}, {}))\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
103 |
+
continue
|
104 |
+
|
105 |
+
elif s.group() == 'u3':
|
106 |
+
lc = lc + 1
|
107 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
108 |
+
m_r = m_r[0].split(',')
|
109 |
+
for i, m in enumerate(m_r):
|
110 |
+
if 'pi' in m and 'e' in m:
|
111 |
+
sp_str = m_r[i].split('e-')
|
112 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
113 |
+
elif 'pi' in m:
|
114 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
115 |
+
else:
|
116 |
+
m_r[i] = '0'
|
117 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
118 |
+
t_qbit = int(m_i[0].strip('[]'))
|
119 |
+
f.write('cir.append(cir.U3(np.pi*{}, np.pi*{}, np.pi*{}, {}))\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
120 |
+
continue
|
121 |
+
|
122 |
+
f.write('cir.upload()\n')
|
123 |
+
f.write('cir.run()\n')
|
124 |
+
|
125 |
+
f.write('t_e = perf_counter()\n')
|
126 |
+
f.write('t_ep = process_time()\n')
|
127 |
+
f.write('print(t_e - t_s)\n')
|
128 |
+
f.write('print(t_ep - t_sp)\n')
|
129 |
+
|
130 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
131 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
132 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
133 |
+
print(int(lc_res) == lc)
|
134 |
+
# print(lc)
|
135 |
+
|
136 |
+
|
137 |
+
for N in range(12, 42, 2):
|
138 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
139 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
140 |
+
|
141 |
+
output_file_path = '/data/user/gangap_a/rqc/svsim_na100/svsim_n8_run_files/'
|
142 |
+
output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N)
|
143 |
+
parse_qasm_to_package_gen(N, 8, input_file, output_file)
|
144 |
+
|
145 |
+
for N in range(41, 51):
|
146 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
147 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
148 |
+
|
149 |
+
output_file_path = '/data/user/gangap_a/rqc/svsim_na100/svsim_n8_run_files/'
|
150 |
+
output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N)
|
151 |
+
parse_qasm_to_package_gen(N, 8, input_file, output_file)
|
152 |
+
|
qasm_parser/svsim/qasm_svsim_gpu_parser.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, n_cores, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
f.write('import sys\n')
|
12 |
+
f.write('sys.path.insert(0, \'/data/user/gangap_a/dynamics/sv_sim/SV-Sim/svsim/build_gpu\')\n')
|
13 |
+
f.write('import numpy as np\n')
|
14 |
+
f.write('from time import process_time, perf_counter\n')
|
15 |
+
f.write('import libsvsim as svsim\n')
|
16 |
+
|
17 |
+
f.write('t_sp = process_time()\n')
|
18 |
+
f.write('t_s = perf_counter()\n')
|
19 |
+
|
20 |
+
f.write('cir = svsim.Simulation({}, {})\n'.format(N, n_cores))
|
21 |
+
|
22 |
+
with open(input_filename, "r") as ifile:
|
23 |
+
lines = ifile.readlines()
|
24 |
+
|
25 |
+
lc = 0
|
26 |
+
cirq_c = 0
|
27 |
+
for line in lines:
|
28 |
+
|
29 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
30 |
+
|
31 |
+
if s is None:
|
32 |
+
continue
|
33 |
+
|
34 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
35 |
+
lc = lc + 1
|
36 |
+
f.write('# {}\n'.format(s.group()))
|
37 |
+
continue
|
38 |
+
|
39 |
+
elif s.group() == 'cirq':
|
40 |
+
lc = lc + 1
|
41 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
42 |
+
cirq_c = cirq_c + 1
|
43 |
+
continue
|
44 |
+
|
45 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's':
|
46 |
+
lc = lc + 1
|
47 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
48 |
+
t_qbit = int(m_i[0].strip('[]'))
|
49 |
+
f.write('cir.append(cir.{}({}))\n'.format(s.group().upper(), t_qbit))
|
50 |
+
continue
|
51 |
+
|
52 |
+
elif s.group() == 'sx':
|
53 |
+
lc = lc + 1
|
54 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
55 |
+
t_qbit = int(m_i[0].strip('[]'))
|
56 |
+
f.write('cir.append(cir.SRN({}))\n'.format(t_qbit))
|
57 |
+
|
58 |
+
elif s.group() == 'sxdg':
|
59 |
+
lc = lc + 1
|
60 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
61 |
+
t_qbit = int(m_i[0].strip('[]'))
|
62 |
+
f.write('cir.append(cir.RX(-0.5*np.pi, {}))\n'.format(t_qbit))
|
63 |
+
continue
|
64 |
+
|
65 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
66 |
+
lc = lc + 1
|
67 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
68 |
+
# print(m_r)
|
69 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
70 |
+
sp_str = m_r[0].split('e-')
|
71 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
72 |
+
elif 'pi' in m_r[0]:
|
73 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
74 |
+
# print(m_r)
|
75 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
76 |
+
t_qbit = int(m_i[0].strip('[]'))
|
77 |
+
f.write('cir.append(cir.{}(np.pi*{}, {}))\n'.format(s.group().upper(), float(m_r[0][0]), t_qbit))
|
78 |
+
continue
|
79 |
+
|
80 |
+
elif s.group() == 'cx':
|
81 |
+
lc = lc + 1
|
82 |
+
match = re.findall(r'\[\d\d*\]', line)
|
83 |
+
c_qbit = int(match[0].strip('[]'))
|
84 |
+
t_qbit = int(match[1].strip('[]'))
|
85 |
+
f.write('cir.append(cir.CX({}, {}))\n'.format(c_qbit, t_qbit))
|
86 |
+
continue
|
87 |
+
|
88 |
+
elif s.group() == 'u2':
|
89 |
+
lc = lc + 1
|
90 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
91 |
+
m_r = m_r[0].split(',')
|
92 |
+
for i, m in enumerate(m_r):
|
93 |
+
if 'pi' in m and 'e' in m:
|
94 |
+
sp_str = m_r[i].split('e-')
|
95 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
96 |
+
elif 'pi' in m:
|
97 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
98 |
+
else:
|
99 |
+
m_r[i] = '0'
|
100 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
101 |
+
t_qbit = int(m_i[0].strip('[]'))
|
102 |
+
f.write('cir.append(cir.U2(np.pi*{}, np.pi*{}, {}))\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
103 |
+
continue
|
104 |
+
|
105 |
+
elif s.group() == 'u3':
|
106 |
+
lc = lc + 1
|
107 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
108 |
+
m_r = m_r[0].split(',')
|
109 |
+
for i, m in enumerate(m_r):
|
110 |
+
if 'pi' in m and 'e' in m:
|
111 |
+
sp_str = m_r[i].split('e-')
|
112 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
113 |
+
elif 'pi' in m:
|
114 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
115 |
+
else:
|
116 |
+
m_r[i] = '0'
|
117 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
118 |
+
t_qbit = int(m_i[0].strip('[]'))
|
119 |
+
f.write('cir.append(cir.U3(np.pi*{}, np.pi*{}, np.pi*{}, {}))\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
120 |
+
continue
|
121 |
+
|
122 |
+
f.write('cir.upload()\n')
|
123 |
+
f.write('cir.run()\n')
|
124 |
+
|
125 |
+
f.write('t_e = perf_counter()\n')
|
126 |
+
f.write('t_ep = process_time()\n')
|
127 |
+
f.write('print(t_e - t_s)\n')
|
128 |
+
f.write('print(t_ep - t_sp)\n')
|
129 |
+
|
130 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
131 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
132 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
133 |
+
print(int(lc_res) == lc)
|
134 |
+
# print(lc)
|
135 |
+
|
136 |
+
|
137 |
+
for N in range(12, 42, 2):
|
138 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
139 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
140 |
+
|
141 |
+
output_file_path = '/data/user/gangap_a/rqc/svsim/svsim_gpu_run_files/'
|
142 |
+
output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N)
|
143 |
+
parse_qasm_to_package_gen(N, 0, input_file, output_file)
|
144 |
+
|
145 |
+
for N in range(41, 51):
|
146 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
147 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
148 |
+
|
149 |
+
output_file_path = '/data/user/gangap_a/rqc/svsim/svsim_gpu_run_files/'
|
150 |
+
output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N)
|
151 |
+
parse_qasm_to_package_gen(N, 0, input_file, output_file)
|
152 |
+
|
qasm_parser/svsim/qasm_svsim_mt_parser.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
|
2 |
+
|
3 |
+
import re
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def parse_qasm_to_package_gen(N, n_cores, input_filename, output_filename):
|
7 |
+
|
8 |
+
with open(output_filename, 'w') as f:
|
9 |
+
|
10 |
+
f.write('import os\n')
|
11 |
+
f.write('import sys\n')
|
12 |
+
f.write('sys.path.insert(0, \'/data/user/gangap_a/dynamics/sv_sim/SV-Sim/svsim/build_omp\')\n')
|
13 |
+
f.write('import numpy as np\n')
|
14 |
+
f.write('from time import process_time, perf_counter\n')
|
15 |
+
f.write('import libsvsim as svsim\n')
|
16 |
+
|
17 |
+
f.write('t_sp = process_time()\n')
|
18 |
+
f.write('t_s = perf_counter()\n')
|
19 |
+
|
20 |
+
f.write('cir = svsim.Simulation({}, {})\n'.format(N, n_cores))
|
21 |
+
|
22 |
+
with open(input_filename, "r") as ifile:
|
23 |
+
lines = ifile.readlines()
|
24 |
+
|
25 |
+
lc = 0
|
26 |
+
cirq_c = 0
|
27 |
+
for line in lines:
|
28 |
+
|
29 |
+
s = re.search(r"Generated|QASM|include|qreg|Qubits|cirq|x|y|z|rx|ry|rz|cx|u2|u3|\bsx\b|\bsxdg\b|s", line)
|
30 |
+
|
31 |
+
if s is None:
|
32 |
+
continue
|
33 |
+
|
34 |
+
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits']:#, 'cirq']:
|
35 |
+
lc = lc + 1
|
36 |
+
f.write('# {}\n'.format(s.group()))
|
37 |
+
continue
|
38 |
+
|
39 |
+
elif s.group() == 'cirq':
|
40 |
+
lc = lc + 1
|
41 |
+
f.write('# {} {}\n'.format(s.group(), cirq_c))
|
42 |
+
cirq_c = cirq_c + 1
|
43 |
+
continue
|
44 |
+
|
45 |
+
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's':
|
46 |
+
lc = lc + 1
|
47 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
48 |
+
t_qbit = int(m_i[0].strip('[]'))
|
49 |
+
f.write('cir.append(cir.{}({}))\n'.format(s.group().upper(), t_qbit))
|
50 |
+
continue
|
51 |
+
|
52 |
+
elif s.group() == 'sx':
|
53 |
+
lc = lc + 1
|
54 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
55 |
+
t_qbit = int(m_i[0].strip('[]'))
|
56 |
+
f.write('cir.append(cir.SRN({}))\n'.format(t_qbit))
|
57 |
+
|
58 |
+
elif s.group() == 'sxdg':
|
59 |
+
lc = lc + 1
|
60 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
61 |
+
t_qbit = int(m_i[0].strip('[]'))
|
62 |
+
f.write('cir.append(cir.RX(-0.5*np.pi, {}))\n'.format(t_qbit))
|
63 |
+
continue
|
64 |
+
|
65 |
+
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
|
66 |
+
lc = lc + 1
|
67 |
+
m_r = re.findall(r'\((.*?)\)', line)
|
68 |
+
# print(m_r)
|
69 |
+
if 'pi' in m_r[0] and 'e' in m_r[0]:
|
70 |
+
sp_str = m_r[0].split('e-')
|
71 |
+
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
72 |
+
elif 'pi' in m_r[0]:
|
73 |
+
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
|
74 |
+
# print(m_r)
|
75 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
76 |
+
t_qbit = int(m_i[0].strip('[]'))
|
77 |
+
f.write('cir.append(cir.{}(np.pi*{}, {}))\n'.format(s.group().upper(), float(m_r[0][0]), t_qbit))
|
78 |
+
continue
|
79 |
+
|
80 |
+
elif s.group() == 'cx':
|
81 |
+
lc = lc + 1
|
82 |
+
match = re.findall(r'\[\d\d*\]', line)
|
83 |
+
c_qbit = int(match[0].strip('[]'))
|
84 |
+
t_qbit = int(match[1].strip('[]'))
|
85 |
+
f.write('cir.append(cir.CX({}, {}))\n'.format(c_qbit, t_qbit))
|
86 |
+
continue
|
87 |
+
|
88 |
+
elif s.group() == 'u2':
|
89 |
+
lc = lc + 1
|
90 |
+
m_r = re.findall(r'\((.*?,.*?)\)', line)
|
91 |
+
m_r = m_r[0].split(',')
|
92 |
+
for i, m in enumerate(m_r):
|
93 |
+
if 'pi' in m and 'e' in m:
|
94 |
+
sp_str = m_r[i].split('e-')
|
95 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
96 |
+
elif 'pi' in m:
|
97 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
98 |
+
else:
|
99 |
+
m_r[i] = '0'
|
100 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
101 |
+
t_qbit = int(m_i[0].strip('[]'))
|
102 |
+
f.write('cir.append(cir.U2(np.pi*{}, np.pi*{}, {}))\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
|
103 |
+
continue
|
104 |
+
|
105 |
+
elif s.group() == 'u3':
|
106 |
+
lc = lc + 1
|
107 |
+
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
|
108 |
+
m_r = m_r[0].split(',')
|
109 |
+
for i, m in enumerate(m_r):
|
110 |
+
if 'pi' in m and 'e' in m:
|
111 |
+
sp_str = m_r[i].split('e-')
|
112 |
+
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
|
113 |
+
elif 'pi' in m:
|
114 |
+
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
|
115 |
+
else:
|
116 |
+
m_r[i] = '0'
|
117 |
+
m_i = re.findall(r'\[\d\d*\]', line)
|
118 |
+
t_qbit = int(m_i[0].strip('[]'))
|
119 |
+
f.write('cir.append(cir.U3(np.pi*{}, np.pi*{}, np.pi*{}, {}))\n'.format(float(m_r[0][0]), float(m_r[1][0]),float(m_r[2][0]), t_qbit))
|
120 |
+
continue
|
121 |
+
|
122 |
+
f.write('cir.upload()\n')
|
123 |
+
f.write('cir.run()\n')
|
124 |
+
|
125 |
+
f.write('t_e = perf_counter()\n')
|
126 |
+
f.write('t_ep = process_time()\n')
|
127 |
+
f.write('print(t_e - t_s)\n')
|
128 |
+
f.write('print(t_ep - t_sp)\n')
|
129 |
+
|
130 |
+
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
131 |
+
_cmd = "grep . " + input_filename + "| wc -l"
|
132 |
+
lc_res = subprocess.check_output(_cmd, shell=True)
|
133 |
+
print(int(lc_res) == lc)
|
134 |
+
# print(lc)
|
135 |
+
|
136 |
+
|
137 |
+
for N in range(12, 42, 2):
|
138 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
139 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
140 |
+
|
141 |
+
output_file_path = '/data/user/gangap_a/rqc/svsim/svsim_mt_run_files/'
|
142 |
+
output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N)
|
143 |
+
parse_qasm_to_package_gen(N, 84, input_file, output_file)
|
144 |
+
|
145 |
+
for N in range(41, 51):
|
146 |
+
input_file_path = '/data/user/gangap_a/rqc/data_files/'
|
147 |
+
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
|
148 |
+
|
149 |
+
output_file_path = '/data/user/gangap_a/rqc/svsim/svsim_mt_run_files/'
|
150 |
+
output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N)
|
151 |
+
parse_qasm_to_package_gen(N, 84, input_file, output_file)
|
152 |
+
|