qs-benchmarks / qasm_parser /qrack /qasm_qrack_sch_parser.py
Amit
Translator and toolchain added.
951488e
# 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, _prec, input_filename, output_filename):
with open(output_filename, 'w') as f:
f.write('#include <time.h>\n')
f.write('#define BILLION 1E9\n')
# f.write('#include \"qrack/qfactory.hpp\"\n')
f.write('#include \"/sim_lib/qrack/qrack/build_st_{}/include/qfactory.hpp\"\n'.format(_prec))
f.write('#include <algorithm>\n') # // std::random_shuffle
f.write('#include <cstddef>\n') # // size_t
f.write('#include <iostream>\n') # // std::cout
f.write('using namespace Qrack;\n')
f.write('\n')
f.write('int main(int argc, char* argv[]){\n')
f.write('\tstruct timespec requestStart, requestEnd;\n')
f.write('\tclock_gettime(CLOCK_REALTIME, &requestStart);\n')
# f.write('\tQInterfacePtr qReg = CreateQuantumInterface(QINTERFACE_OPTIMAL, {}, 0);\n'.format(N))
f.write('\tQInterfacePtr qReg = CreateQuantumInterface(QINTERFACE_CPU, {}, 0);\n'.format(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|h|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('\t// {}\n'.format(s.group()))
continue
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 'h' or s.group() == 's':
lc = lc + 1
m_i = re.findall(r'\[\d\d*\]', line)
t_qbit = int(m_i[0].strip('[]'))
# f.write('\tpauli{}(qubits, {});\n'.format(s.group().upper(), t_qbit))
f.write('\tqReg->{}({});\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('\thadamard(qubits, {});\n'.format(t_qbit))
f.write('\tqReg->SqrtX({});\n'.format(t_qbit))
continue
elif s.group() == 'sxdg':
lc = lc + 1
m_i = re.findall(r'\[\d\d*\]', line)
t_qbit = int(m_i[0].strip('[]'))
# f.write('\thadamard(qubits, {});\n'.format(t_qbit))
f.write('\tqReg->ISqrtX({});\n'.format(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('\trotate{}(qubits, {}, M_PI*{});\n'.format(s.group()[1].upper(), t_qbit, float(m_r[0][0])))
f.write('\tqReg->R{}(M_PI*{}, {});\n'.format(s.group()[1].upper(), float(m_r[0][0]), t_qbit))
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('\tcontrolledNot(qubits, {}, {});\n'.format(c_qbit, t_qbit))
f.write('\tqReg->CNOT({}, {});\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('\tu2(qubits, {}, M_PI*{}, M_PI*{});\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0])))
f.write('\tqReg->U2({}, 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('\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])))
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])))
continue
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)
task = 'rqc'
sim_pack = 'qrack_sch'
com_cap = 'mt'
prec = 'dp'
for N in range(12, 38, 2):
input_file_path = '/data/user/gangap_a/{}_singular/qasm_files/'.format(task)
input_file = input_file_path + 'qasm_N_{}.qasm'.format(N)
# save_file_path = '/data/user/gangap_a/{}_singular/{}/data_{}_{}'.format(task, sim_pack, com_cap, prec)
output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, com_cap, prec)
output_file = output_file_path + '{}_n{}.cpp'.format(task, N)
parse_qasm_to_package_gen(N, prec, input_file, output_file)
r"""
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/quest/QuEST/rqc/quest_sp_run_files/'
output_file = output_file_path + 'quest_rqc_n{}.c'.format(N)
parse_qasm_to_package_gen(N, 'float', input_file, output_file)
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/quest/QuEST/rqc/quest_sp_run_files/'
output_file = output_file_path + 'quest_rqc_n{}.c'.format(N)
parse_qasm_to_package_gen(N, 'float', input_file, output_file)
"""