qs-benchmarks / qasm_parser /myqlm /qasm_myqlm_cpp_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, cc, pr, input_filename, output_filename, save_data_path):
with open(output_filename, 'w') as f:
f.write('import numpy as np\n')
f.write('from qat.lang.AQASM import Program, X, Y, Z, H, S, T, CNOT, RX, RY, RZ\n')
# f.write('from qat.qpus import get_default_qpu\n')
f.write('from qat.clinalg import CLinalg\n')
f.write('from qat.lang.AQASM import AbstractGate\n')
f.write('import os\n')
f.write('from time import process_time, perf_counter\n')
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')
f.write('SX.set_matrix_generator(lambda: np.array([[(1+1j)/2., (1-1j)/2.], [(1-1j)/2., (1+1j)/2.]]))\n')
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')
f.write('SXDG.set_matrix_generator(lambda: np.array([[(1-1j)/2., (1+1j)/2.], [(1+1j)/2., (1-1j)/2.]]))\n')
f.write('def U2_generator(p, l):\n')
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')
f.write(' return np.array(mat)\n\n')
f.write('U2 = AbstractGate("U2", [float, float], arity=1, matrix_generator=U2_generator)\n')
f.write('def U3_generator(t, p, l):\n')
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')
f.write(' return np.array(mat)\n\n')
f.write('U3 = AbstractGate("U3", [float, float, float], arity=1, matrix_generator=U3_generator)\n')
f.write('t_sp = process_time()\n')
f.write('t_s = perf_counter()\n')
f.write('prog = Program()\n')
f.write('qbits = prog.qalloc({})\n'.format(N))
with open(input_filename, "r") as ifile:
lines = ifile.readlines()
lc = 0
cirq_c = 0
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('# {}\n'.format(s.group()))
continue
elif s.group() == 'cirq':
lc = lc + 1
f.write('# {} {}\n'.format(s.group(), cirq_c))
cirq_c = cirq_c + 1
continue
elif s.group() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 's' or s.group() == 'h':
lc = lc + 1
m_i = re.findall(r'\[\d\d*\]', line)
t_qbit = int(m_i[0].strip('[]'))
f.write('{}(qbits[{}])\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('prog.apply(SX(), qbits[{}])\n'.format(t_qbit))
elif s.group() == 'sxdg':
lc = lc + 1
m_i = re.findall(r'\[\d\d*\]', line)
t_qbit = int(m_i[0].strip('[]'))
f.write('prog.apply(SXDG(), qbits[{}])\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('qc.add_{}gate({}, np.pi*{})\n'.format(s.group().upper(), t_qbit, float(m_r[0][0])))
f.write('{}(np.pi*{})(qbits[{}])\n'.format(s.group().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('CNOT(qbits[{}], qbits[{}])\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('prog.apply(U2(np.pi*{}, np.pi*{}), qbits[{}])\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit))
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('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))
continue
# f.write('qpu = get_default_qpu()\n')
if (com_cap == 'st' or com_cap == 'mt') and pr == 'sp':
f.write('qpu = CLinalg(use_nbthreads_heuristic=True, precision=1)\n')
elif (com_cap == 'st' or com_cap == 'mt') and pr == 'dp':
f.write('qpu = CLinalg(use_nbthreads_heuristic=True, precision=2)\n')
elif com_cap == 'gpu' and pr == 'sp':
f.write('qpu = CLinalg(use_gpu=True, precision=1)\n')
elif com_cap == 'gpu' and pr == 'dp':
f.write('qpu = CLinalg(use_gpu=True, precision=2)\n')
f.write('circuit = prog.to_circ()\n')
f.write('job = circuit.to_job()\n')
f.write('result = qpu.submit(job)\n')
# f.write('f_state = device.run(qc).result().values[0]\n')
f.write('t_e = perf_counter()\n')
f.write('t_ep = process_time()\n')
f.write('print(t_e - t_s)\n')
f.write('print(t_ep - t_sp)\n')
f.write('os.chdir(\'{}\')\n'.format(save_data_path))
f.write('np.save(\'time_n{}.npy\', [t_e - t_s, t_ep - t_sp])\n'.format(N))
# 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)
# parse_qasm_to_package_gen(12, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "n12_qulacs.py")
task = 'qft'
sim_pack = 'myqlm_cpp'
com_cap = 'mt'
prec = 'dp'
for N in range(6, 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{}.py'.format(task, N)
parse_qasm_to_package_gen(N, com_cap, prec, input_file, output_file, save_file_path)
"""
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/qulacs/qulacs_run_files/'
output_file = output_file_path + 'qulacs_rqc_n{}.py'.format(N)
parse_qasm_to_package_gen(N, 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/qulacs/qulacs_run_files/'
output_file = output_file_path + 'qulacs_rqc_n{}.py'.format(N)
parse_qasm_to_package_gen(N, input_file, output_file)
"""