qs-benchmarks / qasm_parser /qsimcirq_cont_ngpu /qasm_qsimcirq_mnvidia_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, n_gpus, input_filename, output_filename):
with open(output_filename, 'w') as f:
# f.write('import os\n')
# f.write('os.chdir(\'/data/user/gangap_a/dynamics/qsimcirq_nvidia/qsim/\')\n')
# f.write('cwd = os.getcwd()\n')
# f.write('import sys\n')
# f.write('sys.path.insert(0, \'\')\n')
f.write('from time import process_time, perf_counter\n')
f.write('import numpy as np\n\n')
f.write('import cirq\n')
f.write('import qsimcirq\n')
f.write('class U2(cirq.Gate):\n')
f.write(' def __init__(self, phi, lam):\n')
f.write(' super(U2, self)\n')
f.write(' self._p = phi\n')
f.write(' self._l = lam\n\n')
f.write(' def _num_qubits_(self):\n')
f.write(' return 1\n\n')
f.write(' def _unitary_(self):\n')
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')
f.write(' return mat\n\n')
f.write('class U3(cirq.Gate):\n')
f.write(' def __init__(self, theta, phi, lam):\n')
f.write(' super(U3, self)\n')
f.write(' self._t = theta\n')
f.write(' self._p = phi\n')
f.write(' self._l = lam\n\n')
f.write(' def _num_qubits_(self):\n')
f.write(' return 1\n\n')
f.write(' def _unitary_(self):\n')
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')
f.write(' return mat\n\n')
f.write('t_sp = process_time()\n')
f.write('t_s = perf_counter()\n')
f.write('q = cirq.LineQubit.range({})\n'.format(N))
f.write('cir = cirq.Circuit()\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|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':
lc = lc + 1
m_i = re.findall(r'\[\d\d*\]', line)
t_qbit = int(m_i[0].strip('[]'))
f.write('cir.append(cirq.{}(q[{}]))\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('cir.append(cirq.XPowGate(exponent=0.5).on(q[{}]))\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('cir.append(cirq.XPowGate(exponent=-0.5).on(q[{}]))\n'.format(t_qbit))
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('cir.append(cirq.{}(np.pi*{}).on(q[{}]))\n'.format(s.group(), 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('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit))
f.write('cir.append(cirq.CNOT(q[{}], q[{}]))\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('cir.append(U2(np.pi*{}, np.pi*{}).on(q[{}]))\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('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))
continue
f.write('gpu_options = qsimcirq.QSimOptions(gpu_mode={})\n'.format(n_gpus))
f.write('qsim_simulator = qsimcirq.QSimSimulator(qsim_options=gpu_options)\n')
f.write('result = qsim_simulator.simulate(cir)\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('print(result.final_state_vector)\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, "../qasm_test/qasm_rqc_test.qasm", "test_cirq.py")
# parse_qasm_to_package_gen(12, 1, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_qsimcirq.py")
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/qsimcirq_cont_na100/qsimcirq_n8_run_files/'
output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N)
parse_qasm_to_package_gen(N, 8, 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/qsimcirq_cont_na100/qsimcirq_n8_run_files/'
output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N)
parse_qasm_to_package_gen(N, 8, input_file, output_file)