Spaces:
Runtime error
Runtime error
File size: 9,699 Bytes
951488e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py
import re
import subprocess
def parse_qasm_to_package_gen(N, _nt, input_filename, output_filename):
with open(output_filename, 'w') as f:
f.write('#include <time.h>\n')
f.write('#include <complex.h>\n')
f.write('#define BILLION 1E9\n')
f.write('#include "../../include/qureg.hpp"')
f.write('\n')
f.write('template<typename Type>\n')
f.write('void ApplyU2(iqs::QubitRegister<Type> &state, int q0, double p, double l) {\n')
f.write('TM2x2<ComplexDP> D;\n')
f.write('D(0, 0) = Type(std::cos(0.5*(p+l))/sqrt(2), -std::sin(0.5*(p+l))/sqrt(2));\n')
f.write('D(0, 1) = Type(-std::cos(0.5*(p-l))/sqrt(2), std::sin(0.5*(p-l))/sqrt(2));\n')
f.write('D(1, 0) = Type(std::cos(0.5*(p-l))/sqrt(2), std::sin(0.5*(p-l))/sqrt(2));\n')
f.write('D(1, 1) = Type(std::cos(0.5*(p+l))/sqrt(2), std::sin(0.5*(p+l))/sqrt(2));\n')
f.write('state.Apply1QubitGate(q0, D);\n')
f.write('}\n\n')
f.write('template<typename Type>\n')
f.write('void ApplyU3(iqs::QubitRegister<Type> &state, int q0, double t, double p, double l) {\n')
f.write('TM2x2<ComplexDP> D;\n')
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')
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')
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')
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')
f.write('state.Apply1QubitGate(q0, D);\n')
f.write('}\n\n')
f.write('template<typename Type>\n')
f.write('void ApplyS(iqs::QubitRegister<Type> &state, int q0) {\n')
f.write('TM2x2<ComplexDP> D;\n')
f.write('D(0, 0) = (1, 0);\n')
f.write('D(0, 1) = (0, 0);\n')
f.write('D(1, 0) = (0, 0);\n')
f.write('D(1, 1) = (0, 1);\n')
f.write('state.Apply1QubitGate(q0, D);\n')
f.write('}\n\n')
f.write('template<typename Type>\n')
f.write('void ApplySXDG(iqs::QubitRegister<Type> &state, int q0) {\n')
f.write('TM2x2<ComplexDP> D;\n')
f.write('D(0, 0) = (0.5, -0.5);\n')
f.write('D(0, 1) = (0.5, 0.5);\n')
f.write('D(1, 0) = (0.5, 0.5);\n')
f.write('D(1, 1) = (0.5, -0.5);\n')
f.write('state.Apply1QubitGate(q0, D);\n')
f.write('}\n\n')
f.write('int main(int argc, char **argv){\n')
f.write('\t#ifndef INTELQS_HAS_MPI\n')
f.write('\t#endif\n')
f.write('\tiqs::mpi::Environment env(argc, argv);\n')
f.write('\tif (env.IsUsefulRank() == false) return 0;\n')
f.write('\tassert(env.GetNumStates()==1);\n')
f.write('\tint my_rank = env.GetStateRank();\n')
f.write('\tint num_ranks = env.GetStateSize();\n')
f.write('\tint num_threads = {};\n\n'.format(_nt))
f.write('\t#ifdef _OPENMP\n')
f.write('\t#pragma omp parallel\n')
f.write('\tnum_threads=omp_get_num_threads();\n')
f.write('\t#endif\n\n')
f.write('\tiqs::QubitRegister<ComplexDP> state ({});\n'.format(N))
f.write('\tstd::size_t index = 0;\n')
f.write('\tstate.Initialize("base", index);\n')
f.write('\tstruct timespec requestStart, requestEnd;\n')
f.write('\tiqs::mpi::StateBarrier();\n')
f.write('\tclock_gettime(CLOCK_REALTIME, &requestStart);\n')
lc = 0
with open(input_filename, "r") as ifile:
lines = ifile.readlines()
for line in lines:
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)
if s is None:
continue
elif s.group() in ['Generated', 'QASM', 'include', 'qreg', 'Qubits', 'cirq']:
lc = lc + 1
f.write('// {}\n'.format(s.group()))
continue
# this can be done better by using key value pairs
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z':
lc = lc + 1
m_i = re.findall(r'\[\d\d*\]', line)
t_qbit = int(m_i[0].strip('[]'))
f.write('\tstate.ApplyPauli{}({});\n'.format(s.group().upper(), t_qbit))
continue
elif s.group() == 'sx':
lc = lc + 1
m_i = re.findall(r'\[\d\d*\]', line)
t_qbit = int(m_i[0].strip('[]'))
f.write('\tstate.ApplyPauliSqrt{}({});\n'.format(s.group()[1].upper(), t_qbit))
continue
elif s.group() == "s" or s.group() == 'sxdg':
lc = lc + 1
m_i = re.findall(r'\[\d\d*\]', line)
t_qbit = int(m_i[0].strip('[]'))
f.write('\tApply{}(state, {});\n'.format(s.group().upper(), t_qbit))
continue
#################
elif s.group() == 'rx' or s.group() == 'ry' or s.group() == 'rz':
lc = lc + 1
m_r = re.findall(r'\((.*?)\)', line)
# print(m_r)
if 'pi' in m_r[0] and 'e' in m_r[0]:
sp_str = m_r[0].split('e-')
m_r[0] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
elif 'pi' in m_r[0]:
m_r[0] = re.findall(r'[-]?\d*\.\d\d*', m_r[0])
# print(m_r)
m_i = re.findall(r'\[\d\d*\]', line)
t_qbit = int(m_i[0].strip('[]'))
f.write('\tstate.ApplyRotation{}({}, M_PI*{});\n'.format(s.group()[1].upper(), t_qbit, float(m_r[0][0])))
continue
elif s.group() == 'cx':
lc = lc + 1
match = re.findall(r'\[\d\d*\]', line)
c_qbit = int(match[0].strip('[]'))
t_qbit = int(match[1].strip('[]'))
f.write('\tstate.ApplyCPauliX({}, {});\n'.format(c_qbit, t_qbit))
continue
elif s.group() == 'u2':
lc = lc + 1
m_r = re.findall(r'\((.*?,.*?)\)', line)
m_r = m_r[0].split(',')
for i, m in enumerate(m_r):
if 'pi' in m and 'e' in m:
sp_str = m_r[i].split('e-')
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
elif 'pi' in m:
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
else:
m_r[i] = '0'
m_i = re.findall(r'\[\d\d*\]', line)
t_qbit = int(m_i[0].strip('[]'))
f.write('\tApplyU2(state, {}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
continue
elif s.group() == 'u3':
lc = lc + 1
m_r = re.findall(r'\((.*?,.*?,.*?)\)', line)
m_r = m_r[0].split(',')
for i, m in enumerate(m_r):
if 'pi' in m and 'e' in m:
sp_str = m_r[i].split('e-')
m_r[i] = [re.findall(r'[-]?\d*\.\d\d*', sp_str[0])[0] + 'E-' + sp_str[1]]
elif 'pi' in m:
m_r[i] = re.findall(r'[-]?\d*\.\d\d*', m)
else:
m_r[i] = '0'
m_i = re.findall(r'\[\d\d*\]', line)
t_qbit = int(m_i[0].strip('[]'))
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])))
continue
f.write('\tiqs::mpi::StateBarrier();\n')
f.write('\tclock_gettime(CLOCK_REALTIME, &requestEnd);\n')
f.write('\tdouble accum = (requestEnd.tv_sec - requestStart.tv_sec) + (requestEnd.tv_nsec - requestStart.tv_nsec)/BILLION;\n')
f.write('\tprintf( "%lf, ", accum );\n')
f.write('\treturn 0;\n')
f.write('}')
# result = subprocess.run(["grep", ".", input_filename, "|", "wc", "-l"], text=True)#stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
_cmd = "grep . " + input_filename + "| wc -l"
lc_res = subprocess.check_output(_cmd, shell=True)
print(int(lc_res) == lc)
# print(lc)
for N in range(12, 42, 2):
input_file_path = '/data/user/gangap_a/rqc/data_files/'
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
output_file_path = '/data/user/gangap_a/rqc/intel_qs_cpp/intel-qs/rqc_mt/intel_mt_run_files/'
output_file = output_file_path + 'intel_rqc_n{}.cpp'.format(N)
parse_qasm_to_package_gen(N, 84, input_file, output_file)
r"""
for N in range(41, 51):
input_file_path = '/data/user/gangap_a/rqc/data_files/'
input_file = input_file_path + 'circuit_n{}_m14_s0_e0_pEFGH.qasm'.format(N)
output_file_path = '/data/user/gangap_a/rqc/intel_qs_cpp/intel-qs/rqc/intel_mt_run_files/'
output_file = output_file_path + 'intel_rqc_n{}.cpp'.format(N)
parse_qasm_to_package_gen(N, 84, input_file, output_file)
"""
|