diff --git a/qasm_parser/braket/qasm_braket_parser.py b/qasm_parser/braket/qasm_braket_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..ba75f593585aca26d21e2d384d99b10408c20c27 --- /dev/null +++ b/qasm_parser/braket/qasm_braket_parser.py @@ -0,0 +1,186 @@ +# 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, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import numpy as np\n') + f.write('from braket.circuits import Circuit\n') + f.write('from braket.devices import LocalSimulator\n') + f.write('from braket.circuits.gates import Unitary\n') + f.write('import os\n') + f.write('from time import process_time, perf_counter\n') + + f.write('def U2(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('def U3(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('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('qc = 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|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('qc.{}({})\n'.format(s.group(), 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('qc.v({})\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('qc.vi({})\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('qc.{}({}, np.pi*{})\n'.format(s.group(), 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('qc.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('qc.unitary([{}], U2(np.pi*{}, np.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('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]))) + continue + + + f.write('qc.state_vector()\n') + f.write('device = LocalSimulator()\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 = 'hdyn' +sim_pack = 'braket' +com_cap = 'st' +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, 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) + +""" diff --git a/qasm_parser/cirq/qasm_cirq_parser.py b/qasm_parser/cirq/qasm_cirq_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..9dd58f0f5c5270c2aec5d8425a0cef97f9a62a76 --- /dev/null +++ b/qasm_parser/cirq/qasm_cirq_parser.py @@ -0,0 +1,205 @@ +# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py + +import re +import subprocess +import os + +def parse_qasm_to_package_gen(N, precision, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\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('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|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('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 + + if precision == 'single': + f.write('simulator = cirq.Simulator()\n') + elif precision == 'double': + f.write('simulator = cirq.Simulator(dtype=np.complex128)\n') + f.write('result = simulator.simulate(cir)\n') + f.write('t_e = perf_counter()\n') + f.write('t_ep = process_time()\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)) + 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, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_cirq.py") + +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/cirq/cirq_run_files/' + output_file = output_file_path + 'cirq_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/cirq/cirq_run_files/' + output_file = output_file_path + 'cirq_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, input_file, output_file) +""" + +task = 'qft' +sim_pack = 'cirq' +com_cap = 'st' +prec = 'sp' + +for N in range(6, 40, 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, 'single', input_file, output_file, save_file_path) + diff --git a/qasm_parser/cudaq/qasm_cudaq_parser.py b/qasm_parser/cudaq/qasm_cudaq_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..7232c4269281a10862cb452b704d9343e9f88017 --- /dev/null +++ b/qasm_parser/cudaq/qasm_cudaq_parser.py @@ -0,0 +1,177 @@ +# 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, comp_cap, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('import sys\n') + f.write('import numpy as np\n') + f.write('from time import process_time, perf_counter\n') + f.write('import cudaq\n') + if comp_cap == 'st' or comp_cap == 'mt': + f.write('cudaq.set_qpu(\'qpp\')\n') + elif comp_cap == 'gpu': + f.write('cudaq.set_qpu(\'cuquantum\')\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('cir = cudaq.make_kernel()\n'.format(N)) + f.write('q = cir.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('cir.{}(q[{}])\n'.format(s.group(), 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.rx(0.5*np.pi, 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.rx(-0.5*np.pi, q[{}])\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('cir.{}(np.pi*{}, 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.cx(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.U2(np.pi*{}, np.pi*{}, 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.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)) + continue + + f.write('state = cudaq.get_state(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('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) + + +task = 'rqc' +sim_pack = 'cudaq' +com_cap = 'st' +prec = 'sp' + +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{}.py'.format(task, N) + parse_qasm_to_package_gen(N, com_cap, input_file, output_file, save_file_path) + + +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/svsim/svsim_run_files/' + output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, 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/svsim/svsim_run_files/' + output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, input_file, output_file) + +""" diff --git a/qasm_parser/cuquantum_qiskit/qasm_qiskit_parser_gpu_2pt0_new.py b/qasm_parser/cuquantum_qiskit/qasm_qiskit_parser_gpu_2pt0_new.py new file mode 100644 index 0000000000000000000000000000000000000000..d9432ec6afe000d818d8edce2f7d68f7196f5941 --- /dev/null +++ b/qasm_parser/cuquantum_qiskit/qasm_qiskit_parser_gpu_2pt0_new.py @@ -0,0 +1,163 @@ +# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py + +import re +import subprocess +import os + +def parse_qasm_to_package_gen(N, f_precision, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('from qiskit import QuantumRegister, QuantumCircuit, transpile, ClassicalRegister\n') + f.write('from qiskit.quantum_info.operators import Operator\n') + f.write('from qiskit.providers.aer import QasmSimulator\n') + f.write('from qiskit import Aer\n') + f.write('from time import process_time, perf_counter\n') + f.write('import numpy as np\n\n') + + f.write('def U2(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 Operator(mat)\n\n') + + f.write('def U3(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 Operator(mat)\n\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + # f.write('backend = Aer.get_backend(\'statevector_simulator\')\n')#, max_parallel_threads={}, statevector_parallel_threshold={}, precision=\'{}\')\n'.format(mpt, spt, f_precision)) + f.write('backend = Aer.get_backend(\'aer_simulator_statevector\')\n') + f.write('opt = backend.options\n') + f.write('opt.device=\'GPU\'\n') + f.write('opt.precision=\'{}\'\n'.format(f_precision)) + + f.write('cir = QuantumCircuit({})\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() == 'sx' or s.group() == 'sxdg' or s.group() == 'h': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('cir.{}({})\n'.format(s.group(), 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('cir.{}(np.pi*{}, {})\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.cx({}, {})\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.u(np.pi/2., np.pi*{}, np.pi*{}, {})\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit)) + f.write('cir.append(U2(np.pi*{}, np.pi*{}), [{}])\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.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)) + 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)) + continue + + f.write('job = backend.run(transpile(cir, backend))\n') + f.write('result = job.result()\n') + # f.write('outputstate = result.get_statevector(cir)\n') + f.write('print(f\'backend: {result.backend_name}\')\n') + + f.write('t_e = perf_counter()\n') + f.write('t_ep = process_time()\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)) + f.write('print(t_e - t_s)\n') + f.write('print(t_ep - t_sp)\n') + # f.write('print(outputstate)') + + # 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, 1, 42, "../qasm_test/qasm_rqc_test.qasm", "test_qiskit.py") +# 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") + + +task = 'hdyn' +sim_pack = 'cuquantum_qiskit' +com_cap = 'gpu1' +prec = 'sp' + +for N in range(6, 40, 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, "single", input_file, output_file, save_file_path) diff --git a/qasm_parser/cuquantum_qiskit/qasm_qiskit_parser_ngpu_2pt0_new.py b/qasm_parser/cuquantum_qiskit/qasm_qiskit_parser_ngpu_2pt0_new.py new file mode 100644 index 0000000000000000000000000000000000000000..f1bc0a65666887b1b1f29b7477991aa7b64dde9c --- /dev/null +++ b/qasm_parser/cuquantum_qiskit/qasm_qiskit_parser_ngpu_2pt0_new.py @@ -0,0 +1,163 @@ +# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py + +import re +import subprocess +import os + +def parse_qasm_to_package_gen(N, f_precision, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('from qiskit import QuantumRegister, QuantumCircuit, transpile, ClassicalRegister\n') + f.write('from qiskit.quantum_info.operators import Operator\n') + f.write('from qiskit.providers.aer import QasmSimulator\n') + f.write('from qiskit import Aer\n') + f.write('from time import process_time, perf_counter\n') + f.write('import numpy as np\n\n') + + f.write('def U2(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 Operator(mat)\n\n') + + f.write('def U3(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 Operator(mat)\n\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + # f.write('backend = Aer.get_backend(\'statevector_simulator\')\n')#, max_parallel_threads={}, statevector_parallel_threshold={}, precision=\'{}\')\n'.format(mpt, spt, f_precision)) + f.write('backend = Aer.get_backend(\'aer_simulator_statevector\')\n') + f.write('opt = backend.options\n') + f.write('opt.device=\'GPU\'\n') + f.write('opt.precision=\'{}\'\n'.format(f_precision)) + + f.write('cir = QuantumCircuit({})\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() == 'sx' or s.group() == 'sxdg' or s.group() == 'h': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('cir.{}({})\n'.format(s.group(), 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('cir.{}(np.pi*{}, {})\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.cx({}, {})\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.u(np.pi/2., np.pi*{}, np.pi*{}, {})\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit)) + f.write('cir.append(U2(np.pi*{}, np.pi*{}), [{}])\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.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)) + 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)) + continue + + f.write('job = backend.run(transpile(cir, backend))\n') + f.write('result = job.result()\n') + # f.write('outputstate = result.get_statevector(cir)\n') + f.write('print(f\'backend: {result.backend_name}\')\n') + + f.write('t_e = perf_counter()\n') + f.write('t_ep = process_time()\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)) + f.write('print(t_e - t_s)\n') + # f.write('print(t_ep - t_sp)\n') + # f.write('print(outputstate)') + + # 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, 1, 42, "../qasm_test/qasm_rqc_test.qasm", "test_qiskit.py") +# 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") + + +task = 'qft' +sim_pack = 'cuquantum_qiskit' +com_cap = 'gpu8' +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, "double", input_file, output_file, save_file_path) diff --git a/qasm_parser/cuquantum_qsimcirq/qasm_qsimcirq_gpu_parser.py b/qasm_parser/cuquantum_qsimcirq/qasm_qsimcirq_gpu_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..b07e92d7d4684329c54880a4cfc438a624bbb242 --- /dev/null +++ b/qasm_parser/cuquantum_qsimcirq/qasm_qsimcirq_gpu_parser.py @@ -0,0 +1,210 @@ +# 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, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + # f.write('os.chdir(\'/sim_lib/qsimcirq/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|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('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=1)\n') + 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') + + 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, "../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") + +task = 'hdyn' +sim_pack = 'cuquantum_qsimcirq' +com_cap = 'gpu1' +prec = 'sp' + +for N in range(6, 40, 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, input_file, output_file, save_file_path) + +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/qsimcirq/qsimcirq_gpu_run_files/' + output_file = output_file_path + 'qsimcirq_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/qsimcirq/qsimcirq_gpu_run_files/' + output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, input_file, output_file) +""" diff --git a/qasm_parser/cuquantum_qsimcirq/qasm_qsimcirq_ngpu_parser.py b/qasm_parser/cuquantum_qsimcirq/qasm_qsimcirq_ngpu_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..774b21923f067f154eb5620615c9b8589d11a688 --- /dev/null +++ b/qasm_parser/cuquantum_qsimcirq/qasm_qsimcirq_ngpu_parser.py @@ -0,0 +1,210 @@ +# 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, input_filename, output_filename, save_data_path, cc): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + # f.write('os.chdir(\'/sim_lib/qsimcirq/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|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('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(int(cc[-1]))) + 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') + + 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, "../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") + +task = 'qft' +sim_pack = 'cuquantum_qsimcirq' +com_cap = 'gpu8' +prec = 'sp' + +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, input_file, output_file, save_file_path, com_cap) + +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/qsimcirq/qsimcirq_gpu_run_files/' + output_file = output_file_path + 'qsimcirq_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/qsimcirq/qsimcirq_gpu_run_files/' + output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, input_file, output_file) +""" diff --git a/qasm_parser/hiq/qasm_hiq_parser.py b/qasm_parser/hiq/qasm_hiq_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..c09b4f2f3f48332ddff3462ccd02c6160b90fed5 --- /dev/null +++ b/qasm_parser/hiq/qasm_hiq_parser.py @@ -0,0 +1,188 @@ +# 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, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('from time import process_time, perf_counter\n') + f.write('from projectq.backends import Simulator\n') + f.write('from projectq import MainEngine\n') + f.write('from projectq.meta import Compute, Control, Loop, Uncompute\n') + f.write('from projectq.ops import All, H, Measure, X, Z, Rx, Rz, Ry, CNOT, SGate, SqrtXGate, QubitOperator, MatrixGate, get_inverse\n') + f.write('import numpy as np\n\n') + + f.write('from hiq.projectq.cengines import GreedyScheduler, HiQMainEngine\n') + f.write('from hiq.projectq.backends import SimulatorMPI\n') + f.write('import projectq.setups.decompositions\n') + + f.write('def U2(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 MatrixGate(mat)\n\n') + + f.write('def U3(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 MatrixGate(mat)\n\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('backend = SimulatorMPI(num_local_qubits={})\n'.format(N)) + f.write('eng = HiQMainEngine(backend, [GreedyScheduler()])\n') + f.write('qureg = eng.allocate_qureg({})\n'.format(N)) + + with open(input_filename, "r") as ifile: + lines = ifile.readlines() + + lc = 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() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 'h': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('{} | qureg[{}]\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('SqrtXGate() | qureg[{}]\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('get_inverse(SqrtXGate()) | qureg[{}]\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('R{}(np.pi*{}) | qureg[{}]\n'.format(s.group()[1], float(m_r[0][0]), t_qbit)) + continue + + elif s.group() == "s": + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('SGate() | qureg[{}]\n'.format(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 | (qureg[{}], qureg[{}])\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('U2(np.pi*{}, np.pi*{}) | qureg[{}]\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('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)) + continue + + f.write('eng.flush()\n') + f.write('All(Measure) | qureg\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, "qasm_ex.qasm", "n12_projectq.py") + +task = 'qft' +sim_pack = 'hiq' +com_cap = 'mt' +prec = 'dp' + +for N in range(6, 40, 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, input_file, output_file, save_file_path) + +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/hiq_sim/hiq_run_files/' + output_file = output_file_path + 'hiq_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/hiq_sim/hiq_run_files/' + output_file = output_file_path + 'hiq_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, input_file, output_file) + +""" diff --git a/qasm_parser/hybridq/qasm_hybridq_gpu_parser.py b/qasm_parser/hybridq/qasm_hybridq_gpu_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..81f1616f4cb618ed5770f86c2af332fc70baa9f7 --- /dev/null +++ b/qasm_parser/hybridq/qasm_hybridq_gpu_parser.py @@ -0,0 +1,197 @@ +# 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, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('from time import process_time, perf_counter\n') + f.write('from hybridq.gate import Gate\n') + f.write('from hybridq.circuit import Circuit\n') + f.write('from hybridq.gate import MatrixGate\n') + f.write('from hybridq.circuit.simulation import simulate\n') + f.write('import numpy as np\n') + + f.write('def SqrtXdg(q0):\n') + f.write(' mat = [[(1-1j)/2., (1+1j)/2.], [(1+1j)/2., (1-1j)/2.]]\n') + f.write(' return MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n') + + f.write('def S(q0):\n') + f.write(' mat = [[1, 0], [0, 1j]]\n') + f.write(' return MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n') + + f.write('def U2(p, l, q0):\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 MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n') + + f.write('def U3(t, p, l, q0):\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 MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('init_state = \'0\'*{}\n'.format(N)) + + f.write('cir_arr = []\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() == 'h': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('cir_arr.append(Gate(\'{}\', qubits=[{}]))\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_arr.append(Gate(\'SQRT_X\', qubits=[{}]))\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_arr.append(Gate(\'{}\', qubits=[{}], params=[np.pi*{}]))\n'.format(s.group().upper(), t_qbit, float(m_r[0][0]))) + 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('cir_arr.append(SqrtXdg({}))\n'.format(t_qbit)) + continue + + elif s.group() == "s": + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('cir_arr.append(S({}))\n'.format(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_arr.append(Gate(\'CX\', qubits=[{}, {}]))\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_arr.append(U2(np.pi*{}, np.pi*{}, {}))\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_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)) + continue + + # f.write('psi = simulate(cir_arr, initial_state=init_state, simplify=False, optimize=\'evolution-einsum\', backend=\'jax\', max_largest_intermediate=2**40)\n') + f.write('psi = simulate(cir_arr, initial_state=init_state, optimize=\'evolution-einsum\', backend=\'jax\', max_largest_intermediate=2**40)\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) + +task = 'rqc' +sim_pack = 'hybridq' +com_cap = 'gpu' +prec = 'sp' + +for N in range(12, 36, 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, input_file, output_file, save_file_path) + +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/hybridq/hybridq_gpu_run_files/' + output_file = output_file_path + 'hybridq_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/hybridq/hybridq_gpu_run_files/' + output_file = output_file_path + 'hybridq_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, input_file, output_file) + +""" diff --git a/qasm_parser/hybridq/qasm_hybridq_mt_parser.py b/qasm_parser/hybridq/qasm_hybridq_mt_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..281d72983c3ebb6fa724a27260c9170eeb486d55 --- /dev/null +++ b/qasm_parser/hybridq/qasm_hybridq_mt_parser.py @@ -0,0 +1,175 @@ +# 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, input_filename, output_filename): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('from time import process_time, perf_counter\n') + f.write('from hybridq.gate import Gate\n') + f.write('from hybridq.circuit import Circuit\n') + f.write('from hybridq.gate import MatrixGate\n') + f.write('from hybridq.circuit.simulation import simulate\n') + f.write('import numpy as np\n') + + f.write('def SqrtXdg(q0):\n') + f.write(' mat = [[(1-1j)/2., (1+1j)/2.], [(1+1j)/2., (1-1j)/2.]]\n') + f.write(' return MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n') + + f.write('def S(q0):\n') + f.write(' mat = [[1, 0], [0, 1j]]\n') + f.write(' return MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n') + + f.write('def U2(p, l, q0):\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 MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n') + + f.write('def U3(t, p, l, q0):\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 MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('init_state = \'0\'*{}\n'.format(N)) + + f.write('cir_arr = []\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': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('cir_arr.append(Gate(\'{}\', qubits=[{}]))\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_arr.append(Gate(\'SQRT_X\', qubits=[{}]))\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_arr.append(Gate(\'{}\', qubits=[{}], params=[np.pi*{}]))\n'.format(s.group().upper(), t_qbit, float(m_r[0][0]))) + 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('cir_arr.append(SqrtXdg({}))\n'.format(t_qbit)) + continue + + elif s.group() == "s": + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('cir_arr.append(S({}))\n'.format(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_arr.append(Gate(\'CX\', qubits=[{}, {}]))\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_arr.append(U2(np.pi*{}, np.pi*{}, {}))\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_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)) + continue + + f.write('psi = simulate(cir_arr, initial_state=init_state, simplify=False, optimize=\'evolution-hybridq\', parallel=84, max_largest_intermediate=2**40)\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') + + # 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/hybridq/hybridq_mt_run_files/' + output_file = output_file_path + 'hybridq_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/hybridq/hybridq_mt_run_files/' + output_file = output_file_path + 'hybridq_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, input_file, output_file) + diff --git a/qasm_parser/hybridq/qasm_hybridq_parser.py b/qasm_parser/hybridq/qasm_hybridq_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..8fd1b47fc33cf75ccd2ee940a5a998874f3ba871 --- /dev/null +++ b/qasm_parser/hybridq/qasm_hybridq_parser.py @@ -0,0 +1,199 @@ +# 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_threads, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('from time import process_time, perf_counter\n') + f.write('from hybridq.gate import Gate\n') + f.write('from hybridq.circuit import Circuit\n') + f.write('from hybridq.gate import MatrixGate\n') + f.write('from hybridq.circuit.simulation import simulate\n') + f.write('import numpy as np\n') + + f.write('def SqrtXdg(q0):\n') + f.write(' mat = [[(1-1j)/2., (1+1j)/2.], [(1+1j)/2., (1-1j)/2.]]\n') + f.write(' return MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n') + + f.write('def S(q0):\n') + f.write(' mat = [[1, 0], [0, 1j]]\n') + f.write(' return MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n') + + f.write('def U2(p, l, q0):\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 MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n') + + f.write('def U3(t, p, l, q0):\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 MatrixGate(mat, qubits=[q0], n_qubits=1)\n\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('init_state = \'0\'*{}\n'.format(N)) + + f.write('cir_arr = []\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() == 'h': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('cir_arr.append(Gate(\'{}\', qubits=[{}]))\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_arr.append(Gate(\'SQRT_X\', qubits=[{}]))\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_arr.append(Gate(\'{}\', qubits=[{}], params=[np.pi*{}]))\n'.format(s.group().upper(), t_qbit, float(m_r[0][0]))) + 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('cir_arr.append(SqrtXdg({}))\n'.format(t_qbit)) + continue + + elif s.group() == "s": + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('cir_arr.append(S({}))\n'.format(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_arr.append(Gate(\'CX\', qubits=[{}, {}]))\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_arr.append(U2(np.pi*{}, np.pi*{}, {}))\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_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)) + continue + + # f.write('psi = simulate(cir_arr, initial_state=init_state, simplify=False, optimize=\'evolution-hybridq\', parallel=n_threads, max_largest_intermediate=2**40)\n') + f.write('psi = simulate(cir_arr, initial_state=init_state, optimize=\'evolution-hybridq\', parallel={}, max_largest_intermediate=2**40)\n'.format(n_threads)) + 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) + + +task = 'rqc' +sim_pack = 'hybridq' +com_cap = 'st' +prec = 'sp' + +for N in range(12, 40, 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, 1, input_file, output_file, save_file_path) + + +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/hybridq/hybridq_run_files/' + output_file = output_file_path + 'hybridq_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/hybridq/hybridq_run_files/' + output_file = output_file_path + 'hybridq_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, input_file, output_file) + +""" diff --git a/qasm_parser/intel_qs/qasm_intel_mt_parser.py b/qasm_parser/intel_qs/qasm_intel_mt_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..7172e9005faeac63a6b929fa0bdedf750be7943f --- /dev/null +++ b/qasm_parser/intel_qs/qasm_intel_mt_parser.py @@ -0,0 +1,213 @@ +# 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 \n') + f.write('#include \n') + + f.write('#define BILLION 1E9\n') + + f.write('#include "../../include/qureg.hpp"') + + f.write('\n') + + f.write('template\n') + f.write('void ApplyU2(iqs::QubitRegister &state, int q0, double p, double l) {\n') + f.write('TM2x2 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\n') + f.write('void ApplyU3(iqs::QubitRegister &state, int q0, double t, double p, double l) {\n') + f.write('TM2x2 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\n') + f.write('void ApplyS(iqs::QubitRegister &state, int q0) {\n') + f.write('TM2x2 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\n') + f.write('void ApplySXDG(iqs::QubitRegister &state, int q0) {\n') + f.write('TM2x2 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 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) + +""" diff --git a/qasm_parser/intel_qs/qasm_intel_parser.py b/qasm_parser/intel_qs/qasm_intel_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..7dee8c9bcf39d42107254528cf7dde76f2eaa369 --- /dev/null +++ b/qasm_parser/intel_qs/qasm_intel_parser.py @@ -0,0 +1,252 @@ +# 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 \n') + f.write('#include \n') + + f.write('#define BILLION 1E9\n') + + f.write('#include "/sim_lib/intel_qs_cpp/intel-qs/include/qureg.hpp"') + + f.write('\n') + + f.write('template\n') + f.write('void ApplyU2(iqs::QubitRegister &state, int q0, double p, double l) {\n') + f.write('TM2x2 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\n') + f.write('void ApplyU3(iqs::QubitRegister &state, int q0, double t, double p, double l) {\n') + f.write('TM2x2 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\n') + f.write('void ApplyS(iqs::QubitRegister &state, int q0) {\n') + f.write('TM2x2 D;\n') + f.write('D(0, 0) = Type(1, 0);\n') + f.write('D(0, 1) = Type(0, 0);\n') + f.write('D(1, 0) = Type(0, 0);\n') + f.write('D(1, 1) = Type(0, 1);\n') + f.write('state.Apply1QubitGate(q0, D);\n') + f.write('}\n\n') + + f.write('template\n') + f.write('void ApplySXDG(iqs::QubitRegister &state, int q0) {\n') + f.write('TM2x2 D;\n') + f.write('D(0, 0) = Type(0.5, -0.5);\n') + f.write('D(0, 1) = Type(0.5, 0.5);\n') + f.write('D(1, 0) = Type(0.5, 0.5);\n') + f.write('D(1, 1) = Type(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') + # if com_cap == 'st': + # f.write('\tassert(num_threads==omp_get_num_threads());\n') + # elif com_cap == 'mt': + f.write('\tnum_threads=omp_get_num_threads();\n') + f.write('\t#endif\n\n') + + f.write('\tiqs::mpi::StateBarrier();\n') + + f.write('\tiqs::QubitRegister psi ({});\n'.format(N)) + f.write('\tstd::size_t index = 0;\n') + f.write('\tpsi.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|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 + + # 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('\tpsi.ApplyPauli{}({});\n'.format(s.group().upper(), t_qbit)) + continue + + elif s.group() == 'h': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('\tpsi.ApplyHadamard({});\n'.format(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('\tpsi.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{}(psi, {});\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('\tpsi.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('\tpsi.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(psi, {}, 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(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]))) + 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) + + +task = 'qft' +sim_pack = 'intel_qs_cpp' +com_cap = 'mt' +prec = 'dp' + +for N in range(6, 40, 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(sim_pack, task, com_cap, prec, N) + parse_qasm_to_package_gen(N, 84, input_file, output_file) + +r""" +for N in range(16, 18, 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/intel_run_files/' + output_file = output_file_path + 'intel_rqc_n{}.cpp'.format(N) + parse_qasm_to_package_gen(N, 1, 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/intel_qs_cpp/intel-qs/rqc/intel_run_files/' + output_file = output_file_path + 'intel_rqc_n{}.cpp'.format(N) + parse_qasm_to_package_gen(N, 1, input_file, output_file) + + + +for N in range(16, 18, 2): + input_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/data_files/' + input_file = input_file_path + 'qasm_N_{}.qasm'.format(N) + + output_file_path = '/data/user/gangap_a/rqc/intel_qs_cpp/intel-qs/rqc/intel_run_files/' + output_file = output_file_path + 'intel_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, input_file, output_file) +""" diff --git a/qasm_parser/intel_qs/qasm_intel_parser_mpi.py b/qasm_parser/intel_qs/qasm_intel_parser_mpi.py new file mode 100644 index 0000000000000000000000000000000000000000..47d18b81e097509d608e0105618c1b4909e9724d --- /dev/null +++ b/qasm_parser/intel_qs/qasm_intel_parser_mpi.py @@ -0,0 +1,245 @@ +# 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 \n') + f.write('#include \n') + + f.write('#define BILLION 1E9\n') + + f.write('#include "/data/user/gangap_a/sim_lib_mpi/intel-qs/include/qureg.hpp"') + + f.write('\n') + + f.write('template\n') + f.write('void ApplyU2(iqs::QubitRegister &state, int q0, double p, double l) {\n') + f.write('TM2x2 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\n') + f.write('void ApplyU3(iqs::QubitRegister &state, int q0, double t, double p, double l) {\n') + f.write('TM2x2 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\n') + f.write('void ApplyS(iqs::QubitRegister &state, int q0) {\n') + f.write('TM2x2 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\n') + f.write('void ApplySXDG(iqs::QubitRegister &state, int q0) {\n') + f.write('TM2x2 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') + # if com_cap == 'st': + # f.write('\tassert(num_threads==omp_get_num_threads());\n') + # elif com_cap == 'mt': + f.write('\tnum_threads=omp_get_num_threads();\n') + f.write('\t#endif\n\n') + + f.write('\tiqs::mpi::StateBarrier();\n') + + f.write('\tiqs::QubitRegister psi ({});\n'.format(N)) + f.write('\tstd::size_t index = 0;\n') + f.write('\tpsi.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('\tpsi.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('\tpsi.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{}(psi, {});\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('\tpsi.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('\tpsi.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(psi, {}, 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(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]))) + 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) + + +task = 'hdyn' +sim_pack = 'intel_qs_cpp' +com_cap = 'mpi' +prec = 'dp' + +for N in range(12, 36, 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(sim_pack, task, com_cap, prec, N) + parse_qasm_to_package_gen(N, 1, input_file, output_file) + +r""" +for N in range(16, 18, 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/intel_run_files/' + output_file = output_file_path + 'intel_rqc_n{}.cpp'.format(N) + parse_qasm_to_package_gen(N, 1, 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/intel_qs_cpp/intel-qs/rqc/intel_run_files/' + output_file = output_file_path + 'intel_rqc_n{}.cpp'.format(N) + parse_qasm_to_package_gen(N, 1, input_file, output_file) + + + +for N in range(16, 18, 2): + input_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/data_files/' + input_file = input_file_path + 'qasm_N_{}.qasm'.format(N) + + output_file_path = '/data/user/gangap_a/rqc/intel_qs_cpp/intel-qs/rqc/intel_run_files/' + output_file = output_file_path + 'intel_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, input_file, output_file) +""" diff --git a/qasm_parser/intel_qs/qasm_intel_parser_sp.py b/qasm_parser/intel_qs/qasm_intel_parser_sp.py new file mode 100644 index 0000000000000000000000000000000000000000..eeadd1aac55efcce70020c65f598d800b22f9784 --- /dev/null +++ b/qasm_parser/intel_qs/qasm_intel_parser_sp.py @@ -0,0 +1,253 @@ +# 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 \n') + f.write('#include \n') + + f.write('#define BILLION 1E9\n') + + f.write('#include "/sim_lib/intel_qs_cpp/intel-qs/include/qureg.hpp"') + + f.write('\n') + + f.write('template\n') + f.write('void ApplyU2(iqs::QubitRegister &state, int q0, float p, float l) {\n') + f.write('TM2x2 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\n') + f.write('void ApplyU3(iqs::QubitRegister &state, int q0, float t, float p, float l) {\n') + f.write('TM2x2 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\n') + f.write('void ApplyS(iqs::QubitRegister &state, int q0) {\n') + f.write('TM2x2 D;\n') + f.write('D(0, 0) = Type(1, 0);\n') + f.write('D(0, 1) = Type(0, 0);\n') + f.write('D(1, 0) = Type(0, 0);\n') + f.write('D(1, 1) = Type(0, 1);\n') + f.write('state.Apply1QubitGate(q0, D);\n') + f.write('}\n\n') + + f.write('template\n') + f.write('void ApplySXDG(iqs::QubitRegister &state, int q0) {\n') + f.write('TM2x2 D;\n') + f.write('D(0, 0) = Type(0.5, -0.5);\n') + f.write('D(0, 1) = Type(0.5, 0.5);\n') + f.write('D(1, 0) = Type(0.5, 0.5);\n') + f.write('D(1, 1) = Type(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') + # if com_cap == 'st': + # f.write('\tassert(num_threads==omp_get_num_threads());\n') + # elif com_cap == 'mt': + f.write('\tnum_threads=omp_get_num_threads();\n') + f.write('\t#endif\n\n') + + f.write('\tiqs::mpi::StateBarrier();\n') + + f.write('\tiqs::QubitRegister psi ({});\n'.format(N)) + f.write('\tstd::size_t index = 0;\n') + f.write('\tpsi.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|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 + + # 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('\tpsi.ApplyPauli{}({});\n'.format(s.group().upper(), t_qbit)) + continue + + elif s.group() == 'h': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('\tpsi.ApplyHadamard({});\n'.format(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('\tpsi.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{}(psi, {});\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('\tpsi.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('\tpsi.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(psi, {}, 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(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]))) + 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) + + +task = 'rqc' +sim_pack = 'intel_qs_cpp' +com_cap = 'mt' +prec = 'sp' + +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(sim_pack, task, com_cap, prec, N) + parse_qasm_to_package_gen(N, 84, input_file, output_file) + +r""" +for N in range(16, 18, 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/intel_run_files/' + output_file = output_file_path + 'intel_rqc_n{}.cpp'.format(N) + parse_qasm_to_package_gen(N, 1, 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/intel_qs_cpp/intel-qs/rqc/intel_run_files/' + output_file = output_file_path + 'intel_rqc_n{}.cpp'.format(N) + parse_qasm_to_package_gen(N, 1, input_file, output_file) + + + +for N in range(16, 18, 2): + input_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/data_files/' + input_file = input_file_path + 'qasm_N_{}.qasm'.format(N) + + output_file_path = '/data/user/gangap_a/rqc/intel_qs_cpp/intel-qs/rqc/intel_run_files/' + output_file = output_file_path + 'intel_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, input_file, output_file) +""" diff --git a/qasm_parser/myqlm/qasm_myqlm_cpp_parser.py b/qasm_parser/myqlm/qasm_myqlm_cpp_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..1aa35a8d24cfa21bcaafbda507417f445d666fe0 --- /dev/null +++ b/qasm_parser/myqlm/qasm_myqlm_cpp_parser.py @@ -0,0 +1,210 @@ +# 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) + +""" diff --git a/qasm_parser/myqlm/qasm_myqlm_py_parser.py b/qasm_parser/myqlm/qasm_myqlm_py_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..ff22527c693023504d2fb48e88a1ec0eed8f13a7 --- /dev/null +++ b/qasm_parser/myqlm/qasm_myqlm_py_parser.py @@ -0,0 +1,201 @@ +# 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, 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.pylinalg import PyLinalg\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') + f.write('qpu = PyLinalg()\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' +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, 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) + +""" diff --git a/qasm_parser/pennylane/qasm_pennylane_gpu_parser.py b/qasm_parser/pennylane/qasm_pennylane_gpu_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..890104b8b7b7cd0134959ba083888592f75be9b4 --- /dev/null +++ b/qasm_parser/pennylane/qasm_pennylane_gpu_parser.py @@ -0,0 +1,198 @@ +# 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, precision, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('from time import process_time, perf_counter\n') + f.write('import numpy as np\n\n') + f.write('import pennylane as qml\n') + f.write('from pennylane import numpy as np\n') + + f.write('def U2(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 mat\n\n') + + f.write('def U3(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 mat\n\n') + + f.write('dev = qml.device("lightning.gpu", shots=None, wires={}, c_dtype={})\n'.format(N, precision)) + f.write('@qml.qnode(dev)\n') + f.write('def run_rqc():\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('\t# {}\n'.format(s.group())) + continue + + elif s.group() == 'cirq': + lc = lc + 1 + f.write('\t# {} {}\n'.format(s.group(), cirq_c)) + cirq_c = cirq_c + 1 + continue + + 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('\tqml.Pauli{}(wires={})\n'.format(s.group().upper(), t_qbit)) + continue + + elif s.group() == 'h': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('\tqml.Hadamard(wires={})\n'.format(t_qbit)) + continue + + elif s.group() == 's': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('\tqml.S(wires={})\n'.format(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('\tqml.SX(wires={})\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('\tqml.SX(wires={}).inv()\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('\tqml.{}(np.pi*{}, wires={})\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('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit)) + f.write('\tqml.CNOT(wires=[{}, {}])\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('\tqml.QubitUnitary(U2(np.pi*{}, np.pi*{}), wires={})\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('\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)) + continue + + f.write('\treturn qml.state()\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + f.write('fs=run_rqc()\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)) + + # 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) + +task = 'qft' +sim_pack = 'pennylane_l' +com_cap = 'gpu' +prec = 'dp' + +for N in range(6, 40, 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, 'np.complex128', input_file, output_file, save_file_path) + + +# parse_qasm_to_package_gen(12, "../qasm_test/qasm_rqc_test.qasm", "test_cirq.py") +# parse_qasm_to_package_gen(12, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_pennylane.py") +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/pennylane/pennylane_gpu_sp_run_files/' + output_file = output_file_path + 'pennylane_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 'np.complex64', 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/pennylane/pennylane_gpu_sp_run_files/' + output_file = output_file_path + 'pennylane_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 'np.complex64', input_file, output_file) +""" diff --git a/qasm_parser/pennylane/qasm_pennylane_l_parser.py b/qasm_parser/pennylane/qasm_pennylane_l_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..47f4e9df8d5e6855f2d140fda457e6c105cd5425 --- /dev/null +++ b/qasm_parser/pennylane/qasm_pennylane_l_parser.py @@ -0,0 +1,185 @@ +# 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, precision, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('from time import process_time, perf_counter\n') + f.write('import numpy as np\n\n') + f.write('import pennylane as qml\n') + f.write('from pennylane import numpy as np\n') + + f.write('def U2(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 mat\n\n') + + f.write('def U3(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 mat\n\n') + + if precision == 'single': + f.write('dev = qml.device("lightning.qubit", shots=None, wires={}, c_dtype=np.complex64)\n'.format(N)) + elif precision == 'double': + f.write('dev = qml.device("lightning.qubit", shots=None, wires={}, c_dtype=np.complex128)\n'.format(N)) + + f.write('@qml.qnode(dev)\n') + f.write('def run_rqc():\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('\t# {}\n'.format(s.group())) + continue + + elif s.group() == 'cirq': + lc = lc + 1 + f.write('\t# {} {}\n'.format(s.group(), cirq_c)) + cirq_c = cirq_c + 1 + continue + + 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('\tqml.Pauli{}(wires={})\n'.format(s.group().upper(), t_qbit)) + continue + + elif s.group() == 'h': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('\tqml.Hadamard(wires={})\n'.format(t_qbit)) + continue + + + elif s.group() == 's': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('\tqml.S(wires={})\n'.format(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('\tqml.SX(wires={})\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('\tqml.SX(wires={}).inv()\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('\tqml.{}(np.pi*{}, wires={})\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('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit)) + f.write('\tqml.CNOT(wires=[{}, {}])\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('\tqml.QubitUnitary(U2(np.pi*{}, np.pi*{}), wires={})\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('\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)) + continue + + f.write('\treturn qml.state()\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + f.write('fs=run_rqc()\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)) + + # 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, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_pennylane_l.py") + +task ='qft' +sim_pack = 'pennylane_l' +com_cap = 'mt' +prec = 'dp' + +for N in range(6, 40, 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, 'double', input_file, output_file, save_file_path) diff --git a/qasm_parser/pennylane/qasm_pennylane_parser.py b/qasm_parser/pennylane/qasm_pennylane_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..439923b91d90ab7613ebc5060e731086551f54ab --- /dev/null +++ b/qasm_parser/pennylane/qasm_pennylane_parser.py @@ -0,0 +1,184 @@ +# 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, precision, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('from time import process_time, perf_counter\n') + f.write('import numpy as np\n\n') + f.write('import pennylane as qml\n') + f.write('from pennylane import numpy as np\n') + + f.write('def U2(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 mat\n\n') + + f.write('def U3(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 mat\n\n') + + if precision == 'single': + f.write('dev = qml.device("default.qubit", shots=None, wires={}, r_dtype=np.float32, c_dtype=np.complex64)\n'.format(N)) + elif precision == 'double': + f.write('dev = qml.device("default.qubit", shots=None, wires={}, r_dtype=np.float64, c_dtype=np.complex128)\n'.format(N)) + + f.write('@qml.qnode(dev)\n') + f.write('def run_rqc():\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('\t# {}\n'.format(s.group())) + continue + + elif s.group() == 'cirq': + lc = lc + 1 + f.write('\t# {} {}\n'.format(s.group(), cirq_c)) + cirq_c = cirq_c + 1 + continue + + 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('\tqml.Pauli{}(wires={})\n'.format(s.group().upper(), t_qbit)) + continue + + elif s.group() == 'h': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('\tqml.Hadamard(wires={})\n'.format(t_qbit)) + continue + + elif s.group() == 's': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('\tqml.S(wires={})\n'.format(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('\tqml.SX(wires={})\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('\tqml.SX(wires={}).inv()\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('\tqml.{}(np.pi*{}, wires={})\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('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit)) + f.write('\tqml.CNOT(wires=[{}, {}])\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('\tqml.QubitUnitary(U2(np.pi*{}, np.pi*{}), wires={})\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('\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)) + continue + + f.write('\treturn qml.state()\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + f.write('fs=run_rqc()\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)) + + # 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, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_pennylane.py") + +task = 'qft' +sim_pack = 'pennylane' +com_cap = 'st' +prec = 'sp' + +for N in range(6, 40, 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, 'single', input_file, output_file, save_file_path) diff --git a/qasm_parser/projectq/qasm_projectq_mt_parser.py b/qasm_parser/projectq/qasm_projectq_mt_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..b248eee141485890c60b3c86b5520be790536509 --- /dev/null +++ b/qasm_parser/projectq/qasm_projectq_mt_parser.py @@ -0,0 +1,163 @@ +# 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, input_filename, output_filename): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('from time import process_time, perf_counter\n') + f.write('from projectq.backends import Simulator\n') + f.write('from projectq import MainEngine\n') + f.write('from projectq.meta import Compute, Control, Loop, Uncompute\n') + f.write('from projectq.ops import All, H, Measure, X, Z, Rx, Rz, Ry, CNOT, SGate, SqrtXGate, QubitOperator, MatrixGate, get_inverse\n') + f.write('import numpy as np\n\n') + + f.write('def U2(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 MatrixGate(mat)\n\n') + + f.write('def U3(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 MatrixGate(mat)\n\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('eng = MainEngine(backend=Simulator(gate_fusion=True))\n') + f.write('qureg = eng.allocate_qureg({})\n'.format(N)) + + with open(input_filename, "r") as ifile: + lines = ifile.readlines() + + lc = 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() == '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('{} | qureg[{}]\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('SqrtXGate() | qureg[{}]\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('get_inverse(SqrtXGate()) | qureg[{}]\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('R{}(np.pi*{}) | qureg[{}]\n'.format(s.group()[1], float(m_r[0][0]), t_qbit)) + continue + + elif s.group() == "s": + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('SGate() | qureg[{}]\n'.format(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 | (qureg[{}], qureg[{}])\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('U2(np.pi*{}, np.pi*{}) | qureg[{}]\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('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)) + continue + + f.write('eng.flush()\n') + f.write('All(Measure) | qureg\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') + + # 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_ex.qasm", "n12_projectq.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/projectq/projectq_mt_run_files/' + output_file = output_file_path + 'projectq_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/projectq/projectq_mt_run_files/' + output_file = output_file_path + 'projectq_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, input_file, output_file) + diff --git a/qasm_parser/projectq/qasm_projectq_parser.py b/qasm_parser/projectq/qasm_projectq_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..d1cd9ca44e784f2344fd71cd39abef26757e5ca1 --- /dev/null +++ b/qasm_parser/projectq/qasm_projectq_parser.py @@ -0,0 +1,185 @@ +# 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, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('from time import process_time, perf_counter\n') + f.write('from projectq.backends import Simulator\n') + f.write('from projectq import MainEngine\n') + f.write('from projectq.meta import Compute, Control, Loop, Uncompute\n') + f.write('from projectq.ops import All, H, Measure, X, Z, Rx, Rz, Ry, CNOT, SGate, SqrtXGate, QubitOperator, MatrixGate, get_inverse\n') + f.write('import numpy as np\n\n') + + f.write('def U2(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 MatrixGate(mat)\n\n') + + f.write('def U3(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 MatrixGate(mat)\n\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + # f.write('eng = MainEngine(backend=Simulator(gate_fusion=True))\n') + f.write('eng = MainEngine(backend=Simulator())\n') + f.write('qureg = eng.allocate_qureg({})\n'.format(N)) + + with open(input_filename, "r") as ifile: + lines = ifile.readlines() + + lc = 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() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 'h': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('{} | qureg[{}]\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('SqrtXGate() | qureg[{}]\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('get_inverse(SqrtXGate()) | qureg[{}]\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('R{}(np.pi*{}) | qureg[{}]\n'.format(s.group()[1], float(m_r[0][0]), t_qbit)) + continue + + elif s.group() == "s": + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('SGate() | qureg[{}]\n'.format(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 | (qureg[{}], qureg[{}])\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('U2(np.pi*{}, np.pi*{}) | qureg[{}]\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('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)) + continue + + f.write('eng.flush()\n') + f.write('All(Measure) | qureg\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, "qasm_ex.qasm", "n12_projectq.py") + + +task = 'qft' +sim_pack = 'projectq' +com_cap = 'mt' +prec = 'dp' + +for N in range(6, 40, 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, input_file, output_file, save_file_path) + +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/projectq/projectq_run_files/' + output_file = output_file_path + 'projectq_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/projectq/projectq_run_files/' + output_file = output_file_path + 'projectq_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, input_file, output_file) + +""" diff --git a/qasm_parser/qcgpu/qasm_gcgpu_parser.py b/qasm_parser/qcgpu/qasm_gcgpu_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..15ff34cb18c0f8ff339b22f7b4f319826b601700 --- /dev/null +++ b/qasm_parser/qcgpu/qasm_gcgpu_parser.py @@ -0,0 +1,198 @@ +# 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, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + + f.write('from time import process_time\n') + f.write('import qcgpu\n') + f.write('import numpy as np\n') + f.write('import os\n') + f.write('from time import process_time, perf_counter\n') + + f.write('def SqrtX():\n') + f.write(' mat = [[(1+1j)/2., (1-1j)/2.], [(1-1j)/2., (1+1j)/2.]]\n') + f.write(' return qcgpu.Gate(mat)\n\n') + + f.write('def SqrtXdg():\n') + f.write(' mat = [[(1-1j)/2., (1+1j)/2.], [(1+1j)/2., (1-1j)/2.]]\n') + f.write(' return qcgpu.Gate(mat)\n\n') + + f.write('def Rx(t):\n') + 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') + f.write(' return qcgpu.Gate(mat)\n\n') + + f.write('def Ry(t):\n') + f.write(' mat = [[np.cos(0.5*t), -np.sin(0.5*t)], [np.sin(0.5*t), np.cos(0.5*t)]]\n') + f.write(' return qcgpu.Gate(mat)\n\n') + + f.write('def Rz(t):\n') + f.write(' mat = [[np.exp(-0.5j*t), 0.], [0., np.exp(0.5j*t)]]\n') + f.write(' return qcgpu.Gate(mat)\n\n') + + f.write('def U2(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 qcgpu.Gate(mat)\n\n') + + f.write('def U3(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 qcgpu.Gate(mat)\n\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('reg = qcgpu.State({})\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('reg.{}({})\n'.format(s.group(), 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('reg.apply_gate(SqrtX(), {})\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('reg.apply_gate(SqrtXdg(), {})\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('reg.apply_gate(R{}(np.pi*{}), {})\n'.format(s.group()[1], 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('reg.cx({}, {})\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('reg.apply_gate(U2(np.pi*{}, np.pi*{}), {})\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('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)) + continue + + 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 = 'qcgpu' +com_cap = 'gpu' +prec = 'sp' + +for N in range(6, 40, 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, input_file, output_file, save_file_path) + +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/qcgpu/qcgpu_run_files/' + output_file = output_file_path + 'qcgpu_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/qcgpu/qcgpu_run_files/' + output_file = output_file_path + 'qcgpu_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, input_file, output_file) +""" diff --git a/qasm_parser/qibo/qasm_qibo_parser.py b/qasm_parser/qibo/qasm_qibo_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..410d9005ba799bdb500160e48c9ce992471635da --- /dev/null +++ b/qasm_parser/qibo/qasm_qibo_parser.py @@ -0,0 +1,150 @@ +# 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, precision, cpu_threads, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import numpy as np\n') + f.write('import qibo as qb\n') + + if cpu_threads == 1: + f.write('qb.set_threads({})\n'.format(cpu_threads)) + f.write('qb.set_backend("numpy")\n') + + f.write('qb.set_precision(\'{}\')\n'.format(precision)) + + f.write('import os\n') + f.write('from time import process_time, perf_counter\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('cir = qb.models.Circuit({})\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() == 'sx' or s.group() == 'sxdg' or s.group() == 'h': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('cir.add(qb.gates.{}({}))\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('cir.add(qb.gates.{}({}, theta=np.pi*{}, trainable=False))\n'.format(s.group().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('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit)) + f.write('cir.add(qb.gates.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('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]))) + 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.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]))) + continue + + f.write('result = 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('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)) + + # 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_qibo.py") + +task = 'qft' +sim_pack = 'qibo' +com_cap = 'mt' +prec = 'sp' + +for N in range(6, 40, 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, 'single', 84, input_file, output_file, save_file_path) diff --git a/qasm_parser/qibo/qasm_qibojit_gpu_parser.py b/qasm_parser/qibo/qasm_qibojit_gpu_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..126ccea079099a7e25de013c236e7ba3a57d49ed --- /dev/null +++ b/qasm_parser/qibo/qasm_qibojit_gpu_parser.py @@ -0,0 +1,167 @@ +# 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, precision, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import numpy as np\n') + f.write('import qibo as qb\n') + + f.write('qb.set_backend("qibojit")\n') + # f.write('qb.set_threads({})\n'.format(cpu_threads)) + + f.write('qb.set_precision(\'{}\')\n'.format(precision)) + + f.write('import os\n') + f.write('from time import process_time, perf_counter\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('cir = qb.models.Circuit({})\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() == 'sx' or s.group() == 'sxdg' or s.group() == 'h': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('cir.add(qb.gates.{}({}))\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('cir.add(qb.gates.{}({}, theta=np.pi*{}, trainable=False))\n'.format(s.group().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('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit)) + f.write('cir.add(qb.gates.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('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]))) + 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.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]))) + continue + + f.write('result = 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') + + 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, "../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_qibojit.py") + +task = 'rqc' +sim_pack = 'qibojit' +com_cap = 'gpu' +prec = 'dp' + +for N in range(12, 40, 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, 'double', input_file, output_file, save_file_path) + +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/qibo/qibojit_gpu_sp_run_files/' + output_file = output_file_path + 'qibojit_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 'single', 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/qibo/qibojit_gpu_sp_run_files/' + output_file = output_file_path + 'qibojit_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 'single', input_file, output_file) + +""" diff --git a/qasm_parser/qibo/qasm_qibojit_parser.py b/qasm_parser/qibo/qasm_qibojit_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..8eb71f0da6e7733773d3ff5e715cc42ffb6f479d --- /dev/null +++ b/qasm_parser/qibo/qasm_qibojit_parser.py @@ -0,0 +1,151 @@ +# 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, precision, cpu_threads, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import numpy as np\n') + f.write('import qibo as qb\n') + + f.write('qb.set_backend("qibojit")\n') + + if cpu_threads == 1: + f.write('qb.set_threads({})\n'.format(cpu_threads)) + + f.write('qb.set_precision(\'{}\')\n'.format(precision)) + + f.write('import os\n') + f.write('from time import process_time, perf_counter\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('cir = qb.models.Circuit({})\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() == 'sx' or s.group() == 'sxdg' or s.group() == 'h': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('cir.add(qb.gates.{}({}))\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('cir.add(qb.gates.{}({}, theta=np.pi*{}, trainable=False))\n'.format(s.group().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('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit)) + f.write('cir.add(qb.gates.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('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]))) + 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.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]))) + continue + + f.write('result = 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('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)) + + # 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_qibojit.py") + +task = 'qft' +sim_pack = 'qibojit' +com_cap = 'mt' +prec = 'sp' + +for N in range(6, 40, 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, 'single', 84, input_file, output_file, save_file_path) diff --git a/qasm_parser/qibo_ngpu/qasm_qibojit_gpu_parser.py b/qasm_parser/qibo_ngpu/qasm_qibojit_gpu_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..88ea1cfec6f374ff81989ce7908fedbbe6683d6d --- /dev/null +++ b/qasm_parser/qibo_ngpu/qasm_qibojit_gpu_parser.py @@ -0,0 +1,183 @@ +# 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, ngpus, precision, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import numpy as np\n') + f.write('import qibo as qb\n') + + f.write('qb.set_backend("qibojit")\n') + # f.write('qb.set_threads({})\n'.format(cpu_threads)) + + f.write('qb.set_precision(\'{}\')\n'.format(precision)) + + f.write('import os\n') + f.write('from time import process_time, perf_counter\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + gs_key_arr = [] + + if ngpus==1: + f.write('cir = qb.models.Circuit({})\n'.format(N)) + else: + gs_key_arr = [] + + for gpu in range(ngpus): + gs_key_arr.append('/GPU:' + str(gpu)) + + gs_val_arr = [1 for _ in range(ngpus)] + + gpu_dict = dict(zip(gs_key_arr, gs_val_arr)) + + f.write('cir = qb.models.Circuit({}, {})\n'.format(N, gpu_dict)) + + 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() == 'sx' or s.group() == 'sxdg' or s.group() == 'h': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('cir.add(qb.gates.{}({}))\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('cir.add(qb.gates.{}({}, theta=np.pi*{}, trainable=False))\n'.format(s.group().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('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit)) + f.write('cir.add(qb.gates.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('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]))) + 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.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]))) + continue + + f.write('result = 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('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)) + + # 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_qibojit.py") + +task = 'qft' +sim_pack = 'qibojit' +com_cap = 'gpu8' +prec = 'dp' + +for N in range(6, 40, 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, 8, 'double', input_file, output_file, save_file_path) + + +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/qibojit_na100/qibojit_n1_run_files/' + output_file = output_file_path + 'qibojit_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, 'double', 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/qibojit_na100/qibojit_n1_run_files/' + output_file = output_file_path + 'qibojit_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, 'double', input_file, output_file) + +""" diff --git a/qasm_parser/qiskit/qasm_qiskit_parser_2pt0_new.py b/qasm_parser/qiskit/qasm_qiskit_parser_2pt0_new.py new file mode 100644 index 0000000000000000000000000000000000000000..689c06286ada13cf7bea7827dec7f815ecb6a83f --- /dev/null +++ b/qasm_parser/qiskit/qasm_qiskit_parser_2pt0_new.py @@ -0,0 +1,200 @@ +# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py + +import re +import subprocess +import os + +def parse_qasm_to_package_gen(N, mpt, spt, f_precision, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('from qiskit import QuantumRegister, QuantumCircuit, transpile, ClassicalRegister\n') + f.write('from qiskit.quantum_info.operators import Operator\n') + f.write('from qiskit.providers.aer import QasmSimulator\n') + f.write('from qiskit import Aer\n') + f.write('from time import process_time, perf_counter\n') + f.write('import numpy as np\n\n') + + f.write('from qiskit.quantum_info import Statevector, shannon_entropy\n') + + f.write('def U2(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 Operator(mat)\n\n') + + f.write('def U3(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 Operator(mat)\n\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('backend = Aer.get_backend(\'statevector_simulator\')#, max_parallel_threads={}, statevector_parallel_threshold={}, precision=\'{}\')\n'.format(mpt, spt, f_precision)) + f.write('opt = backend.options\n') + f.write('opt.max_parallel_threads={}\n'.format(mpt)) + f.write('opt.statevector_parallel_threshold={}\n'.format(spt)) + f.write('opt.precision=\'{}\'\n'.format(f_precision)) + + f.write('cir = QuantumCircuit({})\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() == 'sx' or s.group() == 'sxdg' or s.group() == 'h': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('cir.{}({})\n'.format(s.group(), 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('cir.{}(np.pi*{}, {})\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.cx({}, {})\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.u(np.pi/2., np.pi*{}, np.pi*{}, {})\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit)) + f.write('cir.append(U2(np.pi*{}, np.pi*{}), [{}])\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.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)) + 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)) + continue + + f.write('job = backend.run(transpile(cir, backend))\n') + f.write('result = job.result()\n') + f.write('outputstate = result.get_statevector(cir)\n') + + f.write('probs = Statevector(outputstate).probabilities()\n') + f.write('print(sum(probs))\n') + + f.write('ent = shannon_entropy(probs)\n') + f.write('print(ent)\n') + + # f.write('t_e = perf_counter()\n') + # f.write('t_ep = process_time()\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)) + # f.write('print(t_e - t_s)\n') + # f.write('print(t_ep - t_sp)\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, 1, 42, "../qasm_test/qasm_rqc_test.qasm", "test_qiskit.py") +# 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") + +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/qiskit/qiskit_sp_run_files/' + output_file = output_file_path + 'qiskit_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, 42, 'single', 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/qiskit/qiskit_sp_run_files/' + output_file = output_file_path + 'qiskit_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, 42, 'single', input_file, output_file) + +for N in range(6, 38, 2): + input_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/data_files/' + input_file = input_file_path + 'qasm_N_{}.qasm'.format(N) + + output_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/qiskit/qiskit_run_files/' + output_file = output_file_path + 'h_dyn_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, 42, 'double', input_file, output_file) +""" + +task = 'hdyn' +sim_pack = 'qiskit' +com_cap = 'st' +prec = 'sp' + +for N in range(6, 40, 2): + input_file_path = '/home/amit/Documents/PSI_m/project_files_final/{}_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) + + save_file_path = None + + output_file_path = '/home/amit/Documents/PSI_m/project_files_final/{}_ent/{}/'.format(task, sim_pack) + + output_file = output_file_path + '{}_n{}.py'.format(task, N) + parse_qasm_to_package_gen(N, 1, 40, "single", input_file, output_file, save_file_path) diff --git a/qasm_parser/qiskit/qasm_qiskit_parser_gpu_2pt0_new.py b/qasm_parser/qiskit/qasm_qiskit_parser_gpu_2pt0_new.py new file mode 100644 index 0000000000000000000000000000000000000000..9b3dcab92ef455d48e15f31b77da577a00a1f45b --- /dev/null +++ b/qasm_parser/qiskit/qasm_qiskit_parser_gpu_2pt0_new.py @@ -0,0 +1,186 @@ +# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py + +import re +import subprocess +import os + +def parse_qasm_to_package_gen(N, f_precision, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('from qiskit import QuantumRegister, QuantumCircuit, transpile, ClassicalRegister\n') + f.write('from qiskit.quantum_info.operators import Operator\n') + f.write('from qiskit.providers.aer import QasmSimulator\n') + f.write('from qiskit import Aer\n') + f.write('from time import process_time, perf_counter\n') + f.write('import numpy as np\n\n') + + f.write('def U2(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 Operator(mat)\n\n') + + f.write('def U3(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 Operator(mat)\n\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('backend = Aer.get_backend(\'statevector_simulator\')\n')#, max_parallel_threads={}, statevector_parallel_threshold={}, precision=\'{}\')\n'.format(mpt, spt, f_precision)) + f.write('opt = backend.options\n') + f.write('opt.device=\'GPU\'\n') + f.write('opt.precision=\'{}\'\n'.format(f_precision)) + + f.write('cir = QuantumCircuit({})\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() == 'sx' or s.group() == 'sxdg' or s.group() == 'h': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('cir.{}({})\n'.format(s.group(), 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('cir.{}(np.pi*{}, {})\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.cx({}, {})\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.u(np.pi/2., np.pi*{}, np.pi*{}, {})\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit)) + f.write('cir.append(U2(np.pi*{}, np.pi*{}), [{}])\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.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)) + 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)) + continue + + f.write('job = backend.run(transpile(cir, backend))\n') + f.write('result = job.result()\n') + f.write('outputstate = result.get_statevector(cir)\n') + + f.write('t_e = perf_counter()\n') + f.write('t_ep = process_time()\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)) + f.write('print(t_e - t_s)\n') + f.write('print(t_ep - t_sp)\n') + # f.write('print(outputstate)') + + # 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, 1, 42, "../qasm_test/qasm_rqc_test.qasm", "test_qiskit.py") +# 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") + +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/qiskit/qiskit_sp_run_files/' + output_file = output_file_path + 'qiskit_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, 42, 'single', 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/qiskit/qiskit_sp_run_files/' + output_file = output_file_path + 'qiskit_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, 42, 'single', input_file, output_file) + +for N in range(6, 38, 2): + input_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/data_files/' + input_file = input_file_path + 'qasm_N_{}.qasm'.format(N) + + output_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/qiskit/qiskit_run_files/' + output_file = output_file_path + 'h_dyn_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, 42, 'double', input_file, output_file) +""" + +task = 'qft' +sim_pack = 'qiskit' +com_cap = 'gpu' +prec = 'sp' + +for N in range(6, 40, 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, "single", input_file, output_file, save_file_path) diff --git a/qasm_parser/qiskit/qasm_qiskit_parser_mt_2pt0_new.py b/qasm_parser/qiskit/qasm_qiskit_parser_mt_2pt0_new.py new file mode 100644 index 0000000000000000000000000000000000000000..3c0fc14bbb68d7c812b846a93ece545b7ad442fd --- /dev/null +++ b/qasm_parser/qiskit/qasm_qiskit_parser_mt_2pt0_new.py @@ -0,0 +1,187 @@ +# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py + +import re +import subprocess +import os + +def parse_qasm_to_package_gen(N, mpt, spt, f_precision, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('from qiskit import QuantumRegister, QuantumCircuit, transpile, ClassicalRegister\n') + f.write('from qiskit.quantum_info.operators import Operator\n') + f.write('from qiskit.providers.aer import QasmSimulator\n') + f.write('from qiskit import Aer\n') + f.write('from time import process_time, perf_counter\n') + f.write('import numpy as np\n\n') + + f.write('def U2(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 Operator(mat)\n\n') + + f.write('def U3(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 Operator(mat)\n\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('backend = Aer.get_backend(\'statevector_simulator\')\n')#, max_parallel_threads={}, statevector_parallel_threshold={}, precision=\'{}\')\n'.format(mpt, spt, f_precision)) + f.write('opt = backend.options\n') + f.write('opt.max_parallel_threads={}\n'.format(mpt)) + f.write('opt.statevector_parallel_threshold={}\n'.format(spt)) + f.write('opt.precision=\'{}\'\n'.format(f_precision)) + + f.write('cir = QuantumCircuit({})\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|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() == 'sx' or s.group() == 'sxdg': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('cir.{}({})\n'.format(s.group(), 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('cir.{}(np.pi*{}, {})\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.cx({}, {})\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.u(np.pi/2., np.pi*{}, np.pi*{}, {})\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit)) + f.write('cir.append(U2(np.pi*{}, np.pi*{}), [{}])\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.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)) + 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)) + continue + + f.write('job = backend.run(transpile(cir, backend))\n') + f.write('result = job.result()\n') + f.write('outputstate = result.get_statevector(cir)\n') + + f.write('t_e = perf_counter()\n') + f.write('t_ep = process_time()\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)) + f.write('print(t_e - t_s)\n') + f.write('print(t_ep - t_sp)\n') + # f.write('print(outputstate)') + + # 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, 1, 42, "../qasm_test/qasm_rqc_test.qasm", "test_qiskit.py") +# 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") + +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/qiskit/qiskit_sp_run_files/' + output_file = output_file_path + 'qiskit_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, 42, 'single', 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/qiskit/qiskit_sp_run_files/' + output_file = output_file_path + 'qiskit_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, 42, 'single', input_file, output_file) + +for N in range(6, 38, 2): + input_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/data_files/' + input_file = input_file_path + 'qasm_N_{}.qasm'.format(N) + + output_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/qiskit/qiskit_run_files/' + output_file = output_file_path + 'h_dyn_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, 42, 'double', input_file, output_file) +""" + +task = 'hdyn' +sim_pack = 'qiskit' +com_cap = 'mt' +prec = 'sp' + +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, 84, 5, "single", input_file, output_file, save_file_path) diff --git a/qasm_parser/qiskit_cont_ngpu/qasm_qiskit_parser_nvidia_container_2pt0.py b/qasm_parser/qiskit_cont_ngpu/qasm_qiskit_parser_nvidia_container_2pt0.py new file mode 100644 index 0000000000000000000000000000000000000000..de0584047aaf2cfce193e985e1c1b9aa9c989d34 --- /dev/null +++ b/qasm_parser/qiskit_cont_ngpu/qasm_qiskit_parser_nvidia_container_2pt0.py @@ -0,0 +1,173 @@ +# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py + +import re +import subprocess +import math + +def parse_qasm_to_package_gen(N, ngpus, mpi_on, f_precision, input_filename, output_filename): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('import sys\n') + # f.write('sys.path.insert(0, \'/data/user/gangap_a/rqc/qiskit_nvidia/cuquantum/lib\')\n') + f.write('from qiskit import QuantumRegister, QuantumCircuit, transpile, ClassicalRegister\n') + f.write('from qiskit.quantum_info.operators import Operator\n') + # f.write('from cusvaer.backends import StatevectorSimulator\n') + f.write('from qiskit import Aer\n') + f.write('import math\n') + f.write('from time import process_time, perf_counter\n') + f.write('import numpy as np\n\n') + + f.write('def U2(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 Operator(mat)\n\n') + + f.write('def U3(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 Operator(mat)\n\n') + + + blocking_e = False + blocking_q = None + + if mpi_on == True: + blocking_e = True + blocking_q = N - int(math.log2(ngpus)) + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + # f.write('backend = Aer.get_backend(\'statevector_simulator\', max_parallel_threads={}, statevector_parallel_threshold={})\n'.format(mpt, spt)) + 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)) + # f.write('backend.set_options(device=\'GPU\')\n') + + # f.write('backend = StatevectorSimulator()\n') + # f.write('backend.set_options(precision=\'{}\')\n'.format(f_precision)) + # f.write('backend.set_options(cusvaer_global_index_bits=[0, 0])\n') + # f.write('backend.set_options(cusvaer_p2p_device_bits=0)\n') + + f.write('cir = QuantumCircuit({})\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|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() == 'sx' or s.group() == 'sxdg': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('cir.{}({})\n'.format(s.group(), 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('cir.{}(np.pi*{}, {})\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.cx({}, {})\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.u(np.pi/2., np.pi*{}, np.pi*{}, {})\n'.format(float(m_r[0][0]), float(m_r[1][0]), t_qbit)) + f.write('cir.append(U2(np.pi*{}, np.pi*{}), [{}])\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.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)) + 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)) + continue + + f.write('job = backend.run(transpile(cir, backend))\n') + f.write('result = job.result()\n') + f.write('outputstate = result.get_statevector(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(outputstate)') + + # 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, 1, 42, "../qasm_test/qasm_rqc_test.qasm", "test_qiskit.py") +# 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") + +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/qiskit_cont_ngpu/qiskit_gpu_n8_off_run_files/' + output_file = output_file_path + 'qiskit_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 8, False, 'double', 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/qiskit_cont_ngpu/qiskit_gpu_n8_off_run_files/' + output_file = output_file_path + 'qiskit_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 8, False, 'double', input_file, output_file) diff --git a/qasm_parser/qpanda/qasm_qpanda_parser.py b/qasm_parser/qpanda/qasm_qpanda_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..a507a8b977407c100cfbb202b2acd25c05ad4f5c --- /dev/null +++ b/qasm_parser/qpanda/qasm_qpanda_parser.py @@ -0,0 +1,174 @@ +# Credit to: https://github.com/qulacs/cirq-qulacs/blob/master/benchmark/benchmark_state_vector_qulacs.py + +import re +import subprocess +import os + +def parse_qasm_to_package_gen(N, com_cap, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('from time import process_time, perf_counter\n') + f.write('import numpy as np\n\n') + f.write('from pyqpanda import *\n') + + f.write('def SX_gate(qp, qb):\n') + f.write(' qp << U4(qb, np.pi/4., np.pi/2., -np.pi/2., -np.pi/2.)\n') + + f.write('def SXDG_gate(qp, qb):\n') + f.write(' qp << U4(qb, -np.pi/4., np.pi/2., np.pi/2., -np.pi/2.)\n') + + f.write('def U2_gate(qp, qb, p, l):\n') + f.write(' qp << U4(qb, 0., p, np.pi/2., l)\n') + + f.write('def U3_gate(qp, qb, t, p, l):\n') + f.write(' qp << U4(qb, 0., p, t, l)\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + if com_cap == 'st': + # f.write('init(QMachineType.CPU_SINGLE_THREAD)\n') + f.write('qvm = init_quantum_machine(QMachineType.CPU_SINGLE_THREAD)\n') + elif com_cap == 'mt': + f.write('qvm = CPUQVM()\n') + f.write('qvm.init_qvm()\n') + elif com_cap == 'gpu': + f.write('qvm = init_quantum_machine(QMachineType.GPU)\n') + + f.write('qvm.set_configure({}, {})\n'.format(N, N)) + f.write('q_bits = qvm.qAlloc_many({})\n'.format(N)) + f.write('qprog = QProg()\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('qprog << {}(q_bits[{}])\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('SX_gate(qprog, q_bits[{}])\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('SXDG_gate(qprog, q_bits[{}])\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('qprog << {}(q_bits[{}], np.pi*{})\n'.format(s.group().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('cir.append(cirq.X(q[{}]).controlled_by(q[{}]))\n'.format(t_qbit, c_qbit)) + f.write('qprog << CNOT(q_bits[{}], q_bits[{}])\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('U2_gate(qprog, q_bits[{}], np.pi*{}, np.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('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]))) + continue + + f.write('result = qvm.directly_run(qprog)\n') + f.write('t_e = perf_counter()\n') + f.write('t_ep = process_time()\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)) + f.write('print(t_e - t_s)\n') + f.write('print(t_ep - t_sp)\n') + # f.write('print(qvm.get_qstate())\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) + +task = 'hdyn' +sim_pack = 'qpanda' +c_cap = 'gpu' +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, c_cap, prec) + + output_file_path = '/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/'.format(task, sim_pack, c_cap, prec) + + output_file = output_file_path + '{}_n{}.py'.format(task, N) + parse_qasm_to_package_gen(N, c_cap, input_file, output_file, save_file_path) + diff --git a/qasm_parser/qpp/qasm_qpp_parser.py b/qasm_parser/qpp/qasm_qpp_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..3c80c6759fbf344a19d78408bac406b8f428e9dd --- /dev/null +++ b/qasm_parser/qpp/qasm_qpp_parser.py @@ -0,0 +1,199 @@ +# 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, input_filename, output_filename): + + with open(output_filename, 'w') as f: + + f.write('#include \n') + f.write('#include \n') + f.write('#include \n') + f.write('#include \n') + + f.write('#include \"qpp.h\"\n') + + f.write('using namespace qpp;\n') + + f.write('#define BILLION 1E9\n') + + f.write('\n') + + f.write('cmat u2g(double p, double l){\n') + f.write('\tcmat u2{cmat::Zero(2, 2)};\n') + + f.write('\tu2 << std::exp(-1_i * (p + l) / 2.0)/std::sqrt(2),\n') + f.write('\t-std::exp(-1_i * (p - l) / 2.0)/std::sqrt(2),\n') + f.write('\tstd::exp(1_i * (p - l) / 2.0)/std::sqrt(2),\n') + f.write('\tstd::exp(1_i * (p + l) / 2.0)/std::sqrt(2);\n') + f.write('\treturn u2;\n') + f.write('}\n') + + f.write('cmat u3g(double t, double p, double l){\n') + f.write('\tcmat u3{cmat::Zero(2, 2)};\n') + + f.write('\tu3 << std::exp(-1_i * (p + l) / 2.0) * std::cos(t / 2.0),\n') + f.write('\t-std::exp(-1_i * (p - l) / 2.0) * std::sin(t / 2.0),\n') + f.write('\tstd::exp(1_i * (p - l) / 2.0) * std::sin(t / 2.0),\n') + f.write('\tstd::exp(1_i * (p + l) / 2.0) * std::cos(t / 2.0);\n') + + f.write('\treturn u3;\n') + f.write('}\n') + + # f.write('int main(int argc, char* argv[]){\n') + f.write('int main(){\n') + + f.write('\tusing namespace qpp;\n') + # f.write('\tQCircuit qc{{}};\n'.format(N)) + f.write('\tQCircuit qc{' + str(N) + '};\n') + + f.write('\tstruct timespec requestStart, requestEnd;\n') + f.write('\tclock_gettime(CLOCK_REALTIME, &requestStart);\n') + + lc = 0 + + with open(input_filename, "r") as ifile: + lines = ifile.readlines() + + k = 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() == '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': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('\tqc.gate(gt.{}, {});\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('\tcmat U_{} = gt.{}(M_PI*{});\n'.format(k, s.group().upper(), float(m_r[0][0]))) + f.write('\tqc.gate(U_{}, {}, \"{}\");\n'.format(k, t_qbit, s.group())) + k = k + 1 + 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('\tqc.gate(gt.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('\tcmat u2_' + str(k) + '{cmat::Zero(2, 2)};\n') + + f.write('\tu2_{} << u2g(M_PI*{}, M_PI*{});\n'.format(k, float(m_r[0][0]), float(m_r[1][0]))) + f.write('\tqc.gate(u2_{}, {}, \"u2\");\n'.format(k, t_qbit)) + k = k + 1 + + 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('\tcmat u3_' + str(k) + '{cmat::Zero(2, 2)};\n') + + 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]))) + f.write('\tqc.gate(u3_{}, {}, \"u3\");\n'.format(k, t_qbit)) + k = k + 1 + + continue + + f.write('\tQEngine engine{qc};\n') + f.write('\tengine.execute();\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) + +task = 'qft' +sim_pack = 'qpp' +com_cap = 'mt' +prec = 'dp' + +for N in range(6, 40, 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, 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) + +""" diff --git a/qasm_parser/qrack/qasm_qrack_opt_gpu_parser.py b/qasm_parser/qrack/qasm_qrack_opt_gpu_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..96e9c42068e748cf296a42a58e7830cea116d749 --- /dev/null +++ b/qasm_parser/qrack/qasm_qrack_opt_gpu_parser.py @@ -0,0 +1,179 @@ +# 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 \n') + f.write('#define BILLION 1E9\n') + + # f.write('#include \"qrack/qfactory.hpp\"\n') + f.write('#include \"/sim_lib/qrack/qrack/build_gpu_{}/include/qfactory.hpp\"\n'.format(_prec)) + + f.write('#include \n') # // std::random_shuffle + f.write('#include \n') # // size_t + f.write('#include \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 = 'qft' +sim_pack = 'qrack_opt' +com_cap = 'gpu' +prec = 'sp' + +for N in range(6, 40, 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) + +""" diff --git a/qasm_parser/qrack/qasm_qrack_opt_parser.py b/qasm_parser/qrack/qasm_qrack_opt_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..4df841ac9c34aab6d5cbb00d7056c8719f357797 --- /dev/null +++ b/qasm_parser/qrack/qasm_qrack_opt_parser.py @@ -0,0 +1,179 @@ +# 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 \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 \n') # // std::random_shuffle + f.write('#include \n') # // size_t + f.write('#include \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 = 'qft' +sim_pack = 'qrack_opt' +com_cap = 'mt' +prec = 'sp' + +for N in range(6, 40, 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) + +""" diff --git a/qasm_parser/qrack/qasm_qrack_sch_gpu_parser.py b/qasm_parser/qrack/qasm_qrack_sch_gpu_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..9c89d29612dd40325b47132975123ec71cf75298 --- /dev/null +++ b/qasm_parser/qrack/qasm_qrack_sch_gpu_parser.py @@ -0,0 +1,179 @@ +# 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 \n') + f.write('#define BILLION 1E9\n') + + # f.write('#include \"qrack/qfactory.hpp\"\n') + f.write('#include \"/sim_lib/qrack/qrack/build_gpu_{}/include/qfactory.hpp\"\n'.format(_prec)) + + f.write('#include \n') # // std::random_shuffle + f.write('#include \n') # // size_t + f.write('#include \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_OPENCL, {}, 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 = 'hdyn' +sim_pack = 'qrack_sch' +com_cap = 'gpu' +prec = 'sp' + +for N in range(6, 36, 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) + +""" diff --git a/qasm_parser/qrack/qasm_qrack_sch_parser.py b/qasm_parser/qrack/qasm_qrack_sch_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..56a6c65177a43f7e75eb7e53965e4e1106e305b2 --- /dev/null +++ b/qasm_parser/qrack/qasm_qrack_sch_parser.py @@ -0,0 +1,179 @@ +# 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 \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 \n') # // std::random_shuffle + f.write('#include \n') # // size_t + f.write('#include \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) + +""" diff --git a/qasm_parser/qsimcirq/qasm_qsimcirq_a100_parser.py b/qasm_parser/qsimcirq/qasm_qsimcirq_a100_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..42a7489734875aa348c6fc25e32d09c91beff0f1 --- /dev/null +++ b/qasm_parser/qsimcirq/qasm_qsimcirq_a100_parser.py @@ -0,0 +1,211 @@ +# 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, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('os.chdir(\'/data/user/gangap_a/rqc/qsimcirq_a100/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|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('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(use_gpu=True)\n') + 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') + + 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, "../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") + +task = 'rqc' +sim_pack = 'qsimcirq' +com_cap = 'gpu' +prec = 'sp' + +for N in range(12, 40, 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, input_file, output_file, save_file_path) + +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/qsimcirq/qsimcirq_gpu_a100_run_files/' + output_file = output_file_path + 'qsimcirq_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/qsimcirq/qsimcirq_gpu_a100_run_files/' + output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, input_file, output_file) + +""" diff --git a/qasm_parser/qsimcirq/qasm_qsimcirq_gpu_parser.py b/qasm_parser/qsimcirq/qasm_qsimcirq_gpu_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..532a109f9f4ad25ce55d8c95aa31274eacc53e76 --- /dev/null +++ b/qasm_parser/qsimcirq/qasm_qsimcirq_gpu_parser.py @@ -0,0 +1,210 @@ +# 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, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('os.chdir(\'/sim_lib/qsimcirq/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(use_gpu=True)\n') + 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') + + 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, "../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") + +task = 'hdyn' +sim_pack = 'qsimcirq' +com_cap = 'gpu' +prec = 'sp' + +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, input_file, output_file, save_file_path) + +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/qsimcirq/qsimcirq_gpu_run_files/' + output_file = output_file_path + 'qsimcirq_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/qsimcirq/qsimcirq_gpu_run_files/' + output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, input_file, output_file) +""" diff --git a/qasm_parser/qsimcirq/qasm_qsimcirq_mt_parser.py b/qasm_parser/qsimcirq/qasm_qsimcirq_mt_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..aaf58deb55fd0d4e48d59bff2cc0e73b02b7c685 --- /dev/null +++ b/qasm_parser/qsimcirq/qasm_qsimcirq_mt_parser.py @@ -0,0 +1,184 @@ +# 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, cpu_threads, input_filename, output_filename): + + with open(output_filename, 'w') as f: + + f.write('import os\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('qsim_options = qsimcirq.QSimOptions(cpu_threads={})\n'.format(cpu_threads)) + f.write('qsim_simulator = qsimcirq.QSimSimulator(qsim_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/qsimcirq_mt_run_files/' + output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 84, 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/qsimcirq_mt_run_files/' + output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 84, input_file, output_file) + diff --git a/qasm_parser/qsimcirq/qasm_qsimcirq_parser.py b/qasm_parser/qsimcirq/qasm_qsimcirq_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..b0af4bf8e75feed858f4f81c7c8adda31bb4e46b --- /dev/null +++ b/qasm_parser/qsimcirq/qasm_qsimcirq_parser.py @@ -0,0 +1,213 @@ +# 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, cpu_threads, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\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|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('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('qsim_options = qsimcirq.QSimOptions(cpu_threads={})\n'.format(cpu_threads)) + f.write('qsim_simulator = qsimcirq.QSimSimulator(qsim_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') + + 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, "../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") + +task = 'qft' +sim_pack = 'qsimcirq' +com_cap = 'mt' +prec = 'sp' + +for N in range(6, 40, 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, 84, input_file, output_file, save_file_path) + +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/qsimcirq/qsimcirq_run_files/' + output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, 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/qsimcirq_run_files/' + output_file = output_file_path + 'qsimcirq_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, input_file, output_file) + + +for N in range(6, 38, 2): + input_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/data_files/' + input_file = input_file_path + 'qasm_N_{}.qasm'.format(N) + + output_file_path = '/data/user/gangap_a/heisenberg_dyn_qasm/qsimcirq/qsimcirq_run_files/' + output_file = output_file_path + 'h_dyn_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, input_file, output_file) +""" diff --git a/qasm_parser/qsimcirq_cont_ngpu/qasm_qsimcirq_mnvidia_parser.py b/qasm_parser/qsimcirq_cont_ngpu/qasm_qsimcirq_mnvidia_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..74ac5b9e7ae52628c9d316a81394d753d554f495 --- /dev/null +++ b/qasm_parser/qsimcirq_cont_ngpu/qasm_qsimcirq_mnvidia_parser.py @@ -0,0 +1,191 @@ +# 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) + diff --git a/qasm_parser/quest/qasm_quest_parser.py b/qasm_parser/quest/qasm_quest_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..c7e779349fb5f028008f318fc336b527e9a8f71c --- /dev/null +++ b/qasm_parser/quest/qasm_quest_parser.py @@ -0,0 +1,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, precision, input_filename, output_filename): + + with open(output_filename, 'w') as f: + + f.write('#include \n') + f.write('#include \n') + f.write('#include \n') + f.write('#include \n') + + f.write('#include "QuEST.h"\n') + + if precision == 'single': + f.write('#define QuEST_PREC 1\n') + elif precision == 'double': + f.write('#define QuEST_PREC 2\n') + + f.write('#define BILLION 1E9\n') + + f.write('\n') + + f.write('void u2(Qureg qubits, const int q, double p, double l) {\n') + f.write('ComplexMatrix2 u = {\n') + f.write('.real = {{cos(0.5*(p+l))/sqrt(2), -cos(0.5*(p-l))/sqrt(2)},\n') + f.write('\t{cos(0.5*(p-l))/sqrt(2), cos(0.5*(p+l))/sqrt(2)}},\n') + f.write('.imag = {{-sin(0.5*(p+l))/sqrt(2), sin(0.5*(p-l))/sqrt(2)},\n') + f.write('\t{sin(0.5*(p-l))/sqrt(2), sin(0.5*(p+l))/sqrt(2)}}};\n') + f.write('unitary(qubits, q, u);\n') + f.write('}\n\n') + + f.write('void u3(Qureg qubits, const int q, double t, double p, double l) {\n') + f.write('ComplexMatrix2 u = {\n') + f.write('.real = {{cos(0.5*(p+l))*cos(0.5*t), -cos(0.5*(p-l))*sin(0.5*t)},\n') + f.write('\t{cos(0.5*(p-l))*sin(0.5*t), cos(0.5*(p+l))*cos(0.5*t)}},\n') + f.write('.imag = {{-sin(0.5*(p+l))*cos(0.5*t), sin(0.5*(p-l))*sin(0.5*t)},\n') + f.write('\t{sin(0.5*(p-l))*sin(0.5*t), sin(0.5*(p+l))*cos(0.5*t)}}};\n') + f.write('unitary(qubits, q, u);\n') + f.write('}\n\n') + + f.write('void pauliSX(Qureg qubits, const int q) {\n') + f.write('ComplexMatrix2 u = {\n') + f.write('.real = {{0.5, 0.5},\n') + f.write('\t{0.5, 0.5}},\n') + f.write('.imag = {{0.5, -0.5},\n') + f.write('\t{-0.5, 0.5}}};\n') + f.write('unitary(qubits, q, u);\n') + f.write('}\n\n') + + f.write('void pauliSXDG(Qureg qubits, const int q) {\n') + f.write('ComplexMatrix2 u = {\n') + f.write('.real = {{0.5, 0.5},\n') + f.write('\t{0.5, 0.5}},\n') + f.write('.imag = {{-0.5, 0.5},\n') + f.write('\t{0.5, -0.5}}};\n') + f.write('unitary(qubits, q, u);\n') + f.write('}\n\n') + + f.write('int main(int argc, char* argv[]){\n') + + f.write('\tQuESTEnv env = createQuESTEnv();\n') + f.write('\tQureg qubits = createQureg({}, env);\n'.format(N)) + + f.write('\tstruct timespec requestStart, requestEnd;\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|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() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 'sx' or s.group() == 'sxdg': + 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)) + continue + + elif s.group() == 'h': + 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)) + 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]))) + continue + + elif s.group() == "s": + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('\tsGate(qubits, {});\n'.format(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)) + 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]))) + 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]))) + continue + + f.write('\tdestroyQureg(qubits, env);\n') + f.write('\tdestroyQuESTEnv(env);\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) + +task = 'rqc' +sim_pack = 'quest' +com_cap = 'mt' +prec = 'dp' + +for N in range(12, 40, 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{}.c'.format(task, N) + parse_qasm_to_package_gen(N, 'double', 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) + +""" diff --git a/qasm_parser/quest/qasm_quest_parser_sp.py b/qasm_parser/quest/qasm_quest_parser_sp.py new file mode 100644 index 0000000000000000000000000000000000000000..4c3395dbca3bcacc8140b5a1bbd2141d4ea9bf6d --- /dev/null +++ b/qasm_parser/quest/qasm_quest_parser_sp.py @@ -0,0 +1,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, precision, input_filename, output_filename): + + with open(output_filename, 'w') as f: + + f.write('#include \n') + f.write('#include \n') + f.write('#include \n') + f.write('#include \n') + + f.write('#include "QuEST.h"\n') + + if precision == 'single': + f.write('#define QuEST_PREC 1\n') + elif precision == 'double': + f.write('#define QuEST_PREC 2\n') + + f.write('#define BILLION 1E9\n') + + f.write('\n') + + f.write('void u2(Qureg qubits, const int q, float p, float l) {\n') + f.write('ComplexMatrix2 u = {\n') + f.write('.real = {{cos(0.5*(p+l))/sqrt(2), -cos(0.5*(p-l))/sqrt(2)},\n') + f.write('\t{cos(0.5*(p-l))/sqrt(2), cos(0.5*(p+l))/sqrt(2)}},\n') + f.write('.imag = {{-sin(0.5*(p+l))/sqrt(2), sin(0.5*(p-l))/sqrt(2)},\n') + f.write('\t{sin(0.5*(p-l))/sqrt(2), sin(0.5*(p+l))/sqrt(2)}}};\n') + f.write('unitary(qubits, q, u);\n') + f.write('}\n\n') + + f.write('void u3(Qureg qubits, const int q, float t, float p, float l) {\n') + f.write('ComplexMatrix2 u = {\n') + f.write('.real = {{cos(0.5*(p+l))*cos(0.5*t), -cos(0.5*(p-l))*sin(0.5*t)},\n') + f.write('\t{cos(0.5*(p-l))*sin(0.5*t), cos(0.5*(p+l))*cos(0.5*t)}},\n') + f.write('.imag = {{-sin(0.5*(p+l))*cos(0.5*t), sin(0.5*(p-l))*sin(0.5*t)},\n') + f.write('\t{sin(0.5*(p-l))*sin(0.5*t), sin(0.5*(p+l))*cos(0.5*t)}}};\n') + f.write('unitary(qubits, q, u);\n') + f.write('}\n\n') + + f.write('void pauliSX(Qureg qubits, const int q) {\n') + f.write('ComplexMatrix2 u = {\n') + f.write('.real = {{0.5, 0.5},\n') + f.write('\t{0.5, 0.5}},\n') + f.write('.imag = {{0.5, -0.5},\n') + f.write('\t{-0.5, 0.5}}};\n') + f.write('unitary(qubits, q, u);\n') + f.write('}\n\n') + + f.write('void pauliSXDG(Qureg qubits, const int q) {\n') + f.write('ComplexMatrix2 u = {\n') + f.write('.real = {{0.5, 0.5},\n') + f.write('\t{0.5, 0.5}},\n') + f.write('.imag = {{-0.5, 0.5},\n') + f.write('\t{0.5, -0.5}}};\n') + f.write('unitary(qubits, q, u);\n') + f.write('}\n\n') + + f.write('int main(int argc, char* argv[]){\n') + + f.write('\tQuESTEnv env = createQuESTEnv();\n') + f.write('\tQureg qubits = createQureg({}, env);\n'.format(N)) + + f.write('\tstruct timespec requestStart, requestEnd;\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|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() == 'x' or s.group() == 'y' or s.group() == 'z' or s.group() == 'sx' or s.group() == 'sxdg': + 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)) + continue + + elif s.group() == 'h': + 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)) + 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]))) + continue + + elif s.group() == "s": + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('\tsGate(qubits, {});\n'.format(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)) + 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]))) + 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]))) + continue + + f.write('\tdestroyQureg(qubits, env);\n') + f.write('\tdestroyQuESTEnv(env);\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) + +task = 'rqc' +sim_pack = 'quest' +com_cap = 'mt' +prec = 'sp' + +for N in range(12, 40, 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{}.c'.format(task, N) + parse_qasm_to_package_gen(N, 'single', 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) + +""" diff --git a/qasm_parser/qulacs/qasm_qulacs_gpu_new_parser.py b/qasm_parser/qulacs/qasm_qulacs_gpu_new_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..ae1d05f366ec045773ca89b33fc05f68079cff40 --- /dev/null +++ b/qasm_parser/qulacs/qasm_qulacs_gpu_new_parser.py @@ -0,0 +1,200 @@ +# 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, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import numpy as np\n') + f.write('import qulacs as qul\n') + f.write('from qulacs.gate import DenseMatrix\n') + f.write('import os\n') + f.write('from time import process_time, perf_counter\n') + + f.write('def Rx(q, t):\n') + 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') + f.write(' return DenseMatrix(q, mat)\n\n') + + f.write('def Ry(q, t):\n') + f.write(' mat = [[np.cos(0.5*t), -np.sin(0.5*t)], [np.sin(0.5*t), np.cos(0.5*t)]]\n') + f.write(' return DenseMatrix(q, mat)\n\n') + + f.write('def Rz(q, t):\n') + f.write(' mat = [[np.exp(-0.5j*t), 0.], [0., np.exp(0.5j*t)]]\n') + f.write(' return DenseMatrix(q, mat)\n\n') + + f.write('def U2(q, 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 DenseMatrix(q, mat)\n\n') + + f.write('def U3(q, 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 DenseMatrix(q, mat)\n\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('qc = qul.QuantumCircuit({})\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('qc.add_{}_gate({})\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('qc.add_sqrtX_gate({})\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('qc.add_sqrtXdag_gate({})\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('qc.add_gate(R{}({}, np.pi*{}))\n'.format(s.group()[1], 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('qc.add_CNOT_gate({}, {})\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('qc.add_U2_gate({}, np.pi*{}, np.pi*{})\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]))) + f.write('qc.add_gate(U2({}, np.pi*{}, np.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('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]))) + continue + + # f.write('init_state = qul.QuantumState({})\n'.format(N)) + f.write('init_state = qul.QuantumStateGpu({})\n'.format(N)) + f.write('init_state.set_zero_state()\n') + f.write('qc.update_quantum_state(init_state)\n') + + f.write('del init_state\n') + f.write('del qc\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 = 'qulacs' +com_cap = 'gpu' +prec = 'dp' + +for N in range(6, 40, 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, input_file, output_file, save_file_path) + +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/qulacs/qulacs_gpu_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_gpu_run_files/' + output_file = output_file_path + 'qulacs_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, input_file, output_file) + +""" diff --git a/qasm_parser/qulacs/qasm_qulacs_new_parser.py b/qasm_parser/qulacs/qasm_qulacs_new_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..64866288a206ef58f5d45eda5c1e661b40ec3cbc --- /dev/null +++ b/qasm_parser/qulacs/qasm_qulacs_new_parser.py @@ -0,0 +1,200 @@ +# 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, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import numpy as np\n') + f.write('import qulacs as qul\n') + f.write('from qulacs.gate import DenseMatrix\n') + f.write('import os\n') + f.write('from time import process_time, perf_counter\n') + + f.write('def Rx(q, t):\n') + 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') + f.write(' return DenseMatrix(q, mat)\n\n') + + f.write('def Ry(q, t):\n') + f.write(' mat = [[np.cos(0.5*t), -np.sin(0.5*t)], [np.sin(0.5*t), np.cos(0.5*t)]]\n') + f.write(' return DenseMatrix(q, mat)\n\n') + + f.write('def Rz(q, t):\n') + f.write(' mat = [[np.exp(-0.5j*t), 0.], [0., np.exp(0.5j*t)]]\n') + f.write(' return DenseMatrix(q, mat)\n\n') + + f.write('def U2(q, 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 DenseMatrix(q, mat)\n\n') + + f.write('def U3(q, 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 DenseMatrix(q, mat)\n\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('qc = qul.QuantumCircuit({})\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('qc.add_{}_gate({})\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('qc.add_sqrtX_gate({})\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('qc.add_sqrtXdag_gate({})\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('qc.add_gate(R{}({}, np.pi*{}))\n'.format(s.group()[1], 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('qc.add_CNOT_gate({}, {})\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('qc.add_U2_gate({}, np.pi*{}, np.pi*{})\n'.format(t_qbit, float(m_r[0][0]), float(m_r[1][0]))) + f.write('qc.add_gate(U2({}, np.pi*{}, np.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('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]))) + continue + + f.write('init_state = qul.QuantumState({})\n'.format(N)) + f.write('init_state.set_zero_state()\n') + f.write('qc.update_quantum_state(init_state)\n') + + f.write('del init_state\n') + f.write('del qc\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 = 'rqc' +sim_pack = 'qulacs' +com_cap = 'mt' +prec = 'dp' + +for N in range(12, 40, 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, 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) + +""" diff --git a/qasm_parser/svsim/qasm_svsim_gpu_a100_parser.py b/qasm_parser/svsim/qasm_svsim_gpu_a100_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..d4f2e428a5497849896cc468201a382334adc61b --- /dev/null +++ b/qasm_parser/svsim/qasm_svsim_gpu_a100_parser.py @@ -0,0 +1,152 @@ +# 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_cores, input_filename, output_filename): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('import sys\n') + f.write('sys.path.insert(0, \'/data/user/gangap_a/rqc/svsim/SV-Sim/svsim/build_a100\')\n') + f.write('import numpy as np\n') + f.write('from time import process_time, perf_counter\n') + f.write('import libsvsim as svsim\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('cir = svsim.Simulation({}, {})\n'.format(N, n_cores)) + + 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('cir.append(cir.{}({}))\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(cir.SRN({}))\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(cir.RX(-0.5*np.pi, {}))\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('cir.append(cir.{}(np.pi*{}, {}))\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('cir.append(cir.CX({}, {}))\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(cir.U2(np.pi*{}, np.pi*{}, {}))\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(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)) + continue + + f.write('cir.upload()\n') + f.write('cir.run()\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') + + # 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/svsim/svsim_gpu_a100_run_files/' + output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 0, 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/svsim/svsim_gpu_a100_run_files/' + output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 0, input_file, output_file) + diff --git a/qasm_parser/svsim/qasm_svsim_gpu_ma100_parser.py b/qasm_parser/svsim/qasm_svsim_gpu_ma100_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..132b67a59bda9708608b47286b4a9e4de2f6826a --- /dev/null +++ b/qasm_parser/svsim/qasm_svsim_gpu_ma100_parser.py @@ -0,0 +1,152 @@ +# 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_cores, input_filename, output_filename): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('import sys\n') + f.write('sys.path.insert(0, \'/data/user/gangap_a/rqc/svsim/SV-Sim/svsim/build_ma100\')\n') + f.write('import numpy as np\n') + f.write('from time import process_time, perf_counter\n') + f.write('import libsvsim as svsim\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('cir = svsim.Simulation({}, {})\n'.format(N, n_cores)) + + 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(cir.{}({}))\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(cir.SRN({}))\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(cir.RX(-0.5*np.pi, {}))\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('cir.append(cir.{}(np.pi*{}, {}))\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('cir.append(cir.CX({}, {}))\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(cir.U2(np.pi*{}, np.pi*{}, {}))\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(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)) + continue + + f.write('cir.upload()\n') + f.write('cir.run()\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') + + # 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/svsim_na100/svsim_n8_run_files/' + output_file = output_file_path + 'svsim_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/svsim_na100/svsim_n8_run_files/' + output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 8, input_file, output_file) + diff --git a/qasm_parser/svsim/qasm_svsim_gpu_parser.py b/qasm_parser/svsim/qasm_svsim_gpu_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..d8ca23fc1d004748c4b783061418cad74b36809f --- /dev/null +++ b/qasm_parser/svsim/qasm_svsim_gpu_parser.py @@ -0,0 +1,152 @@ +# 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_cores, input_filename, output_filename): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('import sys\n') + f.write('sys.path.insert(0, \'/data/user/gangap_a/dynamics/sv_sim/SV-Sim/svsim/build_gpu\')\n') + f.write('import numpy as np\n') + f.write('from time import process_time, perf_counter\n') + f.write('import libsvsim as svsim\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('cir = svsim.Simulation({}, {})\n'.format(N, n_cores)) + + 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(cir.{}({}))\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(cir.SRN({}))\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(cir.RX(-0.5*np.pi, {}))\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('cir.append(cir.{}(np.pi*{}, {}))\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('cir.append(cir.CX({}, {}))\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(cir.U2(np.pi*{}, np.pi*{}, {}))\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(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)) + continue + + f.write('cir.upload()\n') + f.write('cir.run()\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') + + # 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/svsim/svsim_gpu_run_files/' + output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 0, 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/svsim/svsim_gpu_run_files/' + output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 0, input_file, output_file) + diff --git a/qasm_parser/svsim/qasm_svsim_mt_parser.py b/qasm_parser/svsim/qasm_svsim_mt_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..a88246a6ec60489e45f15b1337d1d3a92f4c22e3 --- /dev/null +++ b/qasm_parser/svsim/qasm_svsim_mt_parser.py @@ -0,0 +1,152 @@ +# 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_cores, input_filename, output_filename): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('import sys\n') + f.write('sys.path.insert(0, \'/data/user/gangap_a/dynamics/sv_sim/SV-Sim/svsim/build_omp\')\n') + f.write('import numpy as np\n') + f.write('from time import process_time, perf_counter\n') + f.write('import libsvsim as svsim\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('cir = svsim.Simulation({}, {})\n'.format(N, n_cores)) + + 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(cir.{}({}))\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(cir.SRN({}))\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(cir.RX(-0.5*np.pi, {}))\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('cir.append(cir.{}(np.pi*{}, {}))\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('cir.append(cir.CX({}, {}))\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(cir.U2(np.pi*{}, np.pi*{}, {}))\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(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)) + continue + + f.write('cir.upload()\n') + f.write('cir.run()\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') + + # 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/svsim/svsim_mt_run_files/' + output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 84, 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/svsim/svsim_mt_run_files/' + output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 84, input_file, output_file) + diff --git a/qasm_parser/svsim/qasm_svsim_parser.py b/qasm_parser/svsim/qasm_svsim_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..960b9baba6a99db13728878bd0403bb6273051f8 --- /dev/null +++ b/qasm_parser/svsim/qasm_svsim_parser.py @@ -0,0 +1,176 @@ +# 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, comp_cap, n_cores, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('import os\n') + f.write('import sys\n') + + # f.write('sys.path.insert(0, \'/data/user/gangap_a/dynamics/sv_sim/SV-Sim/svsim/build_omp\')\n') + f.write('sys.path.insert(0, \'/sim_lib/sv_sim/SV-Sim/build_{}\')\n'.format(comp_cap)) + f.write('import numpy as np\n') + f.write('from time import process_time, perf_counter\n') + f.write('import libsvsim as svsim\n') + + f.write('t_sp = process_time()\n') + f.write('t_s = perf_counter()\n') + + f.write('cir = svsim.Simulation({}, {})\n'.format(N, n_cores)) + + 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('cir.append(cir.{}({}))\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(cir.SRN({}))\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(cir.RX(-0.5*np.pi, {}))\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('cir.append(cir.{}(np.pi*{}, {}))\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('cir.append(cir.CX({}, {}))\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(cir.U2(np.pi*{}, np.pi*{}, {}))\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(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)) + continue + + f.write('cir.upload()\n') + f.write('cir.run()\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) + + +task = 'rqc' +sim_pack = 'svsim' +com_cap = 'mt' +prec = 'dp' + +for N in range(12, 40, 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, 84, input_file, output_file, save_file_path) + + +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/svsim/svsim_run_files/' + output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, 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/svsim/svsim_run_files/' + output_file = output_file_path + 'svsim_rqc_n{}.py'.format(N) + parse_qasm_to_package_gen(N, 1, input_file, output_file) + +""" diff --git a/qasm_parser/yao/qasm_yao_gpu_parser.py b/qasm_parser/yao/qasm_yao_gpu_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..0683ab14a17d218dc3192009261c8866697f401c --- /dev/null +++ b/qasm_parser/yao/qasm_yao_gpu_parser.py @@ -0,0 +1,215 @@ +# 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, precision, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('using Distributed\n') + f.write('@everywhere empty!(DEPOT_PATH)\n') + f.write('@everywhere push!(DEPOT_PATH, \"/data/user/gangap_a/jlib_new/\")\n') + f.write('@everywhere using Yao, YaoBlocks, YaoAPI, CuYao\n') + f.write('@everywhere using Base.Threads\n') + f.write('@everywhere using NPZ\n') + + f.write('mutable struct U2{T<:Number} <: PrimitiveBlock{2}\n') + f.write(' phi::T\n') + f.write(' lam::T\n') + f.write('end\n') + + f.write('YaoBlocks.nqudits(fs::U2) = 1\n') + f.write('YaoBlocks.print_block(io::IO, block::U2) = print(io, \"U2(phi=$(block.phi), lam=$(block.lam))\")\n') + + f.write('function YaoAPI.mat(::Type{T}, fs::U2) where T\n') + f.write(' p, l = fs.phi, fs.lam\n') + f.write(' ((cos(0.5*(p + l)) - 1im*sin(0.5*(p + l)))/sqrt(2))*T[1 -cos(l)-1im*sin(l); cos(p) + 1im*sin(p) cos(l + p) + 1im*sin(l + p)]\n') + f.write('end\n') + + f.write('mutable struct U3{T<:Number} <: PrimitiveBlock{2}\n') + f.write(' theta::T\n') + f.write(' phi::T\n') + f.write(' lam::T\n') + f.write('end\n') + + f.write('YaoBlocks.nqudits(fs::U3) = 1\n') + f.write('YaoBlocks.print_block(io::IO, block::U3) = print(io, \"U3(theta=$(block.theta), phi=$(block.phi), lam=$(block.lam))\")\n') + + f.write('function YaoAPI.mat(::Type{T}, fs::U3) where T\n') + f.write(' t, p, l = fs.theta, fs.phi, fs.lam\n') + f.write(' (cos(0.5*(p + l)) - 1im*sin(0.5*(p + l)))*T[cos(t/2.) -sin(t/2.)*(cos(l) + 1im*sin(l)); sin(t/2.)*(cos(p) + 1im*sin(p)) cos(t/2.)*(cos(l + p) + 1im*sin(l + p))]\n') + f.write('end\n') + + f.write('@const_gate Sx = 0.5*[1. + 1im 1-1im; 1-1im 1+1im]\n') + + f.write('@const_gate Sxdg = 0.5*[1. - 1im 1+1im; 1+1im 1-1im]\n') + + f.write('@const_gate S = [1. 0; 0 1im]\n') + + f.write('function main(n)\n') + f.write('cir = chain(n)\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('push!(cir, put({}=>{}))\n'.format(t_qbit+1, s.group().upper())) + 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('push!(cir, put({}=>Sx))\n'.format(t_qbit+1)) + + elif s.group() == 'sxdg': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('push!(cir, put({}=>Sxdg))\n'.format(t_qbit+1)) + + 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('push!(cir, put({}=>R{}(float(pi*{}))))\n'.format(t_qbit+1, s.group()[1], 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('push!(cir, cnot({}, {}))\n'.format(c_qbit+1, t_qbit+1)) + 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('push!(cir, put({}=>U2(float(pi*{}), float(pi*{}))))\n'.format(t_qbit+1, 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('push!(cir, put({}=>U3(float(pi*{}), float(pi*{}), float(pi*{}))))\n'.format(t_qbit+1, float(m_r[0][0]), float(m_r[1][0]), float(m_r[2][0]))) + continue + + + if precision == 'single': + f.write('zs = cuzero_state(ComplexF32, n)\n') + elif precision == 'double': + f.write('zs = cuzero_state(ComplexF64, n)\n') + + + # f.write('zs = cuzero_state(n)\n') + f.write('apply!(zs, cir)\n') + f.write('return zs\n') + f.write('end\n') + + f.write('main({})\n'.format(N)) + f.write('_time = @elapsed final_state = main({})\n'.format(N)) + f.write('println(_time)\n') + + f.write('cd(\"{}\")\n'.format(save_data_path)) + f.write('npzwrite(\"time_n{}.npy\", [_time])\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, "../qasm_test/qasm_rqc_test.qasm", "test_yao.jl") +# parse_qasm_to_package_gen(12, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_cirq.py") + +task = 'qft' +sim_pack = 'yao' +com_cap = 'gpu' +prec = 'sp' + +for N in range(6, 40, 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{}.jl'.format(task, N) + parse_qasm_to_package_gen(N, 'single', input_file, output_file, save_file_path) + + +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/yao/yao_gpu_run_files/' + output_file = output_file_path + 'yao_rqc_n{}.jl'.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/yao/yao_gpu_run_files/' + output_file = output_file_path + 'yao_rqc_n{}.jl'.format(N) + parse_qasm_to_package_gen(N, input_file, output_file) + +""" diff --git a/qasm_parser/yao/qasm_yao_parser.py b/qasm_parser/yao/qasm_yao_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..46cb45809b79197b5c0d265f44f3d62304a833dd --- /dev/null +++ b/qasm_parser/yao/qasm_yao_parser.py @@ -0,0 +1,215 @@ +# 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, precision, input_filename, output_filename, save_data_path): + + with open(output_filename, 'w') as f: + + f.write('using Distributed\n') + f.write('@everywhere empty!(DEPOT_PATH)\n') + f.write('@everywhere push!(DEPOT_PATH, \"/julia/jlib/\")\n') + f.write('@everywhere using Yao, YaoBlocks, YaoAPI\n') + f.write('@everywhere using Base.Threads\n') + f.write('@everywhere using NPZ\n') + + f.write('mutable struct U2{T<:Number} <: PrimitiveBlock{2}\n') + f.write(' phi::T\n') + f.write(' lam::T\n') + f.write('end\n') + + f.write('YaoBlocks.nqudits(fs::U2) = 1\n') + f.write('YaoBlocks.print_block(io::IO, block::U2) = print(io, \"U2(phi=$(block.phi), lam=$(block.lam))\")\n') + + f.write('function YaoAPI.mat(::Type{T}, fs::U2) where T\n') + f.write(' p, l = fs.phi, fs.lam\n') + f.write(' ((cos(0.5*(p + l)) - 1im*sin(0.5*(p + l)))/sqrt(2))*T[1 -cos(l)-1im*sin(l); cos(p) + 1im*sin(p) cos(l + p) + 1im*sin(l + p)]\n') + f.write('end\n') + + f.write('mutable struct U3{T<:Number} <: PrimitiveBlock{2}\n') + f.write(' theta::T\n') + f.write(' phi::T\n') + f.write(' lam::T\n') + f.write('end\n') + + f.write('YaoBlocks.nqudits(fs::U3) = 1\n') + f.write('YaoBlocks.print_block(io::IO, block::U3) = print(io, \"U3(theta=$(block.theta), phi=$(block.phi), lam=$(block.lam))\")\n') + + f.write('function YaoAPI.mat(::Type{T}, fs::U3) where T\n') + f.write(' t, p, l = fs.theta, fs.phi, fs.lam\n') + f.write(' (cos(0.5*(p + l)) - 1im*sin(0.5*(p + l)))*T[cos(t/2.) -sin(t/2.)*(cos(l) + 1im*sin(l)); sin(t/2.)*(cos(p) + 1im*sin(p)) cos(t/2.)*(cos(l + p) + 1im*sin(l + p))]\n') + f.write('end\n') + + f.write('@const_gate Sx = 0.5*[1. + 1im 1-1im; 1-1im 1+1im]\n') + + f.write('@const_gate Sxdg = 0.5*[1. - 1im 1+1im; 1+1im 1-1im]\n') + + f.write('@const_gate S = [1. 0; 0 1im]\n') + + f.write('function main(n)\n') + f.write('cir = chain(n)\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('push!(cir, put({}=>{}))\n'.format(t_qbit+1, s.group().upper())) + 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('push!(cir, put({}=>Sx))\n'.format(t_qbit+1)) + + elif s.group() == 'sxdg': + lc = lc + 1 + m_i = re.findall(r'\[\d\d*\]', line) + t_qbit = int(m_i[0].strip('[]')) + f.write('push!(cir, put({}=>Sxdg))\n'.format(t_qbit+1)) + + 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('push!(cir, put({}=>R{}(float(pi*{}))))\n'.format(t_qbit+1, s.group()[1], 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('push!(cir, cnot({}, {}))\n'.format(c_qbit+1, t_qbit+1)) + 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('push!(cir, put({}=>U2(float(pi*{}), float(pi*{}))))\n'.format(t_qbit+1, 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('push!(cir, put({}=>U3(float(pi*{}), float(pi*{}), float(pi*{}))))\n'.format(t_qbit+1, float(m_r[0][0]), float(m_r[1][0]), float(m_r[2][0]))) + continue + + if precision == 'single': + f.write('zs = zero_state(ComplexF32, n)\n') + elif precision == 'double': + f.write('zs = zero_state(ComplexF64, n)\n') + + + f.write('apply!(zs, cir)\n') + f.write('return zs\n') + f.write('end\n') + + + f.write('main({})\n'.format(N)) + f.write('_time = @elapsed final_state = main({})\n'.format(N)) + f.write('println(_time)\n') + + f.write('cd(\"{}\")\n'.format(save_data_path)) + f.write('npzwrite(\"time_n{}.npy\", [_time])\n'.format(N)) + + f.write('\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_yao.jl") +# parse_qasm_to_package_gen(12, "/data/user/gangap_a/rqc/data_files/circuit_n12_m14_s0_e0_pEFGH.qasm", "test_cirq.py") + +task = 'rqc' +sim_pack = 'yao' +com_cap = 'mt' +prec = 'dp' + +for N in range(12, 40, 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{}.jl'.format(task, N) + parse_qasm_to_package_gen(N, 'double', input_file, output_file, save_file_path) + +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/yao/yao_run_files/' + output_file = output_file_path + 'yao_rqc_n{}.jl'.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/yao/yao_run_files/' + output_file = output_file_path + 'yao_rqc_n{}.jl'.format(N) + parse_qasm_to_package_gen(N, input_file, output_file) + +""" diff --git a/toolchain/create_bash_script.py b/toolchain/create_bash_script.py new file mode 100644 index 0000000000000000000000000000000000000000..92a650d19ca771eddf164be1da6be8a2df40a5f2 --- /dev/null +++ b/toolchain/create_bash_script.py @@ -0,0 +1,288 @@ +import sys +import os +import subprocess + +# sim_pack = 'qiskit' +# task = 'hdyn' +# com_cap = 'st' +# prec = 'dp' + +sim_pack = sys.argv[1] +task = sys.argv[2] +com_cap = sys.argv[3] +prec = sys.argv[4] + +N_arr = range(6, 36, 2) + +if sim_pack == 'hiq': + + for N in N_arr: + + with open('{}_{}_{}_{}.sh'.format(task, com_cap, prec, N), 'a') as job_file: + + job_file.write('#!/bin/bash -l\n') + job_file.write('. /app/etc/profile.d/conda.sh\n') + + job_file.write('conda activate sim_{}\n'.format(sim_pack)) + job_file.write('python3 /sim_lib/hiq/HiQsimulator/examples/run_files_{}_{}/{}_n{}.py\n'.format(com_cap, prec, task, N)) + + +elif sim_pack == 'intel_qs_cpp': + + if com_cap == 'st': + with open('{}_{}_{}.sh'.format(task, com_cap, prec), 'a') as job_file: + + job_file.write('#!/bin/bash -l\n') + job_file.write('export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/sim_lib/intel_qs_cpp/intel-qs/build/lib\n') + job_file.write('export LIBRARY_PATH=$LIBRARY_PATH:/sim_lib/intel_qs_cpp/intel-qs/build/lib\n') + + job_file.write('cd /home\n') + job_file.write('cmake .\n') + job_file.write('cmake --build ./\n') + job_file.write('make\n') + + for N in N_arr: + + with open('{}_{}_{}_{}.sh'.format(task, com_cap, prec, N), 'a') as job_file: + + job_file.write('#!/bin/bash -l\n') + + job_file.write('export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/sim_lib/intel_qs_cpp/intel-qs/build/lib\n') + job_file.write('export LIBRARY_PATH=$LIBRARY_PATH:/sim_lib/intel_qs_cpp/intel-qs/build/lib\n') + + job_file.write('cd /home/bin_st_{}\n'.format(prec)) + job_file.write('./{}_{}_st_{}_n{}\n'.format(sim_pack, task, prec, N)) + + + +elif sim_pack == 'quest': + + # with open('{}_{}_{}.sh'.format(task, com_cap, prec), 'a') as job_file: + + # job_file.write('#!/bin/bash -l\n') + # job_file.write('cd /home\n') + + # if com_cap == 'st': + # job_file.write('cmake .\n') + + # elif com_cap == 'mt': + # job_file.write('cmake -DMULTITHREADED=1 .\n') + + # elif com_cap == 'gpu': + # job_file.write('cmake -DGPUACCELERATED=1 -DGPU_COMPUTE_CAPABILITY=80 .\n') + + # job_file.write('cmake --build ./\n') + # job_file.write('make\n') + + + with open('{}_{}_{}.sh'.format(task, com_cap, prec), 'a') as job_file: + + for N in N_arr: + + # bdir = "/sim_lib/quest/QuEST/build_{}_{}_{}".format(task, com_cap, prec) + + job_file.write('#!/bin/bash -l\n') + + # fn_c = "/sim_lib/quest/QuEST/{}/run_files_{}_{}/{}_n{}.c".format(task, com_cap, prec, task, N) + fn_c = "/home/{}_n{}.c".format(task, N) + + fn = "{}_{}_{}_{}_n{}".format(sim_pack, task, com_cap, prec, N) + + if com_cap == 'st': + # job_file.write('export QuEST_LIB_PATH=/sim_lib/quest/QuEST/build_st/QuEST\n') + # job_file.write('export QuEST_LIB_EXACT=/sim_lib/quest/QuEST/build_st/QuEST/libQuEST.so\n') + job_file.write('cd /sim_lib/quest/QuEST/build_{}_{}\n'.format(com_cap, prec)) + + if prec == 'sp': + job_file.write('cmake .. -DUSER_SOURCE=\"{}\" -DOUTPUT_EXE=\"{}\" -DPRECISION=1\n'.format(fn_c, fn)) + elif prec == 'dp': + job_file.write('cmake .. -DUSER_SOURCE=\"{}\" -DOUTPUT_EXE=\"{}\"\n'.format(fn_c, fn)) + job_file.write('make\n') + + elif com_cap == 'mt': + # job_file.write('export QuEST_LIB_PATH=/sim_lib/quest/QuEST/build_mt/QuEST\n') + # job_file.write('export QuEST_LIB_EXACT=/sim_lib/quest/QuEST/build_mt/QuEST/libQuEST.so\n') + job_file.write('cd /sim_lib/quest/QuEST/build_{}_{}\n'.format(com_cap, prec)) + + if prec == 'sp': + job_file.write('cmake .. -DUSER_SOURCE=\"{}\" -DOUTPUT_EXE=\"{}\" -DMULTITHREADED=1 -DPRECISION=1\n'.format(fn_c, fn)) + elif prec == 'dp': + job_file.write('cmake .. -DUSER_SOURCE=\"{}\" -DOUTPUT_EXE=\"{}\" -DMULTITHREADED=1\n'.format(fn_c, fn)) + job_file.write('make\n') + + elif com_cap == 'gpu': + # job_file.write('export QuEST_LIB_PATH=/sim_lib/quest/QuEST/build_gpu/QuEST\n') + # job_file.write('export QuEST_LIB_EXACT=/sim_lib/quest/QuEST/build_gpu/QuEST/libQuEST.so\n') + job_file.write('cd /sim_lib/quest/QuEST/build_{}_{}\n'.format(com_cap, prec)) + job_file.write('cmake .. -DUSER_SOURCE=\"{}\" -DOUTPUT_EXE=\"{}\" -DGPUACCELERATED=1 -DGPU_COMPUTE_CAPABILITY=80\n'.format(fn_c, fn)) + job_file.write('make\n') + + for N in N_arr: + + with open('{}_{}_{}_{}.sh'.format(task, com_cap, prec, N), 'a') as job_file: + + # job_file.write('cd /home/bin_{}_{}\n'.format(com_cap, prec)) + job_file.write('cd /sim_lib/quest/QuEST/build_{}_{}\n'.format(com_cap, prec)) + job_file.write('./{}_{}_{}_{}_n{}\n'.format(sim_pack, task, com_cap, prec, N)) + +elif sim_pack == 'qrack_sch' or sim_pack == 'qrack_opt': + + with open('{}_{}_{}.sh'.format(task, com_cap, prec), 'a') as job_file: + + job_file.write('#!/bin/bash -l\n') + + job_file.write('cd /bin_{}_{}\n'.format(com_cap, prec)) + + job_file.write('export LIBRARY_PATH=$LIBRARY_PATH:/usr/lib/x86_64-linux-gnu\n') + + for N in N_arr: + + # job_file.write('#!/bin/bash -l\n') + + fn_c = "/home/{}_n{}.cpp".format(task, N) + + fn = "{}_{}_{}_{}_n{}".format(sim_pack, task, com_cap, prec, N) + + if com_cap == 'st': # or com_cap == 'mt': + if prec == 'sp': + job_file.write('g++ -std=c++11 -march=native -Wall -Werror {} -I/sim_lib/qrack/qrack/build_st_sp/include -L/sim_lib/qrack/qrack/build_st_sp -lqrack -lpthread -o {}\n'.format(fn_c, fn)) + elif prec == 'dp': + job_file.write('g++ -std=c++11 -march=native -Wall -Werror {} -I/sim_lib/qrack/qrack/build_st_dp/include -L/sim_lib/qrack/qrack/build_st_dp -lqrack -lpthread -o {}\n'.format(fn_c, fn)) + + elif com_cap == 'gpu': + # job_file.write('export LIBRARY_PATH=$LIBRARY_PATH:/usr/lib/x86_64-linux-gnu\n') + if prec == 'sp': + job_file.write('g++ -std=c++11 -march=native -Wall -Werror {} -I/sim_lib/qrack/qrack/build_gpu_sp/include -L/sim_lib/qrack/qrack/build_gpu_sp -lqrack -lOpenCL -lpthread -o {}\n'.format(fn_c, fn)) + elif prec == 'dp': + job_file.write('g++ -std=c++11 -march=native -Wall -Werror {} -I/sim_lib/qrack/qrack/build_gpu_dp/include -L/sim_lib/qrack/qrack/build_gpu_dp -lqrack -lOpenCL -lpthread -o {}\n'.format(fn_c, fn)) + + for N in N_arr: + + with open('{}_{}_{}_{}.sh'.format(task, com_cap, prec, N), 'a') as job_file: + + if com_cap == 'st' or com_cap == 'mt': + job_file.write('cd /bin_st_{}\n'.format(prec)) + job_file.write('./{}_{}_st_{}_n{}\n'.format(sim_pack, task, prec, N)) + + elif com_cap == 'gpu': + job_file.write('cd /bin_gpu_{}\n'.format(prec)) + job_file.write('./{}_{}_gpu_{}_n{}\n'.format(sim_pack, task, prec, N)) + + +elif sim_pack == 'qpp': + + # with open('{}_{}_{}.sh'.format(task, com_cap, prec), 'a') as job_file: + + # job_file.write('#!/bin/bash -l\n') + + # job_file.write('cd /bin_{}_{}\n'.format(com_cap, prec)) + + # for N in N_arr: + + # fn_c = "/home/{}_n{}.cpp".format(task, N) + + # fn = "{}_{}_{}_{}_n{}".format(sim_pack, task, com_cap, prec, N) + + # if com_cap == 'st': # or com_cap == 'mt': + # job_file.write('c++ -pedantic -std=c++17 -Wall -Wextra -Weffc++ -fopenmp -isystem /usr/local/include/eigen3 -I /sim_lib/qpp/qpp/include -I /sim_lib/qpp/qpp/qasmtools/include {} -o {}\n'.format(fn_c, fn)) + + for N in N_arr: + + fn_c = "/home/{}_n{}.cpp".format(task, N) + + fn = "{}_{}_{}_{}_n{}".format(sim_pack, task, com_cap, prec, N) + + with open('{}_{}_{}_{}.sh'.format(task, com_cap, prec, N), 'a') as job_file: + + if com_cap == 'st': + job_file.write('cd /bin_st_{}\n'.format(prec)) + job_file.write('c++ -pedantic -std=c++17 -Wall -Wextra -Weffc++ -fopenmp -isystem /usr/local/include/eigen3 -I /sim_lib/qpp/qpp/include -I /sim_lib/qpp/qpp/qasmtools/include {} -o {}\n'.format(fn_c, fn)) + job_file.write('./{}_{}_st_{}_n{}\n'.format(sim_pack, task, prec, N)) + + elif com_cap == 'mt': + job_file.write('cd /bin_st_{}\n'.format(prec)) + job_file.write('./{}_{}_st_{}_n{}\n'.format(sim_pack, task, prec, N)) + + +elif sim_pack == 'yao': + for N in N_arr: + with open('{}_{}_{}_{}.sh'.format(task, com_cap, prec, N), 'a') as job_file: + + if com_cap == 'st' or com_cap == 'mt': + job_file.write('/julia/julia-1.8.5/bin/julia /home/{}_n{}.jl\n'.format(task, N)) + + elif com_cap == 'gpu': + job_file.write('/data/user/gangap_a/julia-1.8.5/bin/julia /data/user/gangap_a/{}_singular/yao/run_files_{}_{}/{}_n{}.jl'.format(task, com_cap, prec, task, N)) + +elif sim_pack == 'pennylane_l' and com_cap == 'gpu': + for N in N_arr: + with open('{}_{}_{}_{}.sh'.format(task, com_cap, prec, N), 'a') as job_file: + job_file.write('python3 /data/user/gangap_a/{}_singular/pennylane_l/run_files_{}_{}/{}_n{}.py'.format(task, com_cap, prec, task, N)) + +elif sim_pack == 'qsimcirq' and com_cap == 'gpu': + for N in N_arr: + with open('{}_{}_{}_{}.sh'.format(task, com_cap, prec, N), 'a') as job_file: + job_file.write('python3 /data/user/gangap_a/{}_singular/qsimcirq/run_files_{}_{}/{}_n{}.py'.format(task, com_cap, prec, task, N)) + +elif sim_pack == 'qpanda' and com_cap == 'gpu': + for N in N_arr: + with open('{}_{}_{}_{}.sh'.format(task, com_cap, prec, N), 'a') as job_file: + job_file.write('python3 /data/user/gangap_a/{}_singular/qpanda/run_files_{}_{}/{}_n{}.py'.format(task, com_cap, prec, task, N)) + +elif sim_pack == 'cuquantum_qiskit' and com_cap == 'gpu': + for N in N_arr: + with open('{}_{}_{}_{}.sh'.format(task, com_cap, prec, N), 'a') as job_file: + job_file.write('python3 /home/{}_n{}.py\n'.format(task, N)) + +elif sim_pack == 'cuquantum_qiskit' and (com_cap == 'gpu1' or com_cap == 'gpu2' or com_cap == 'gpu4' or com_cap == 'gpu8'): + ngpus = int(com_cap[-1]) + for N in N_arr: + with open('{}_{}_{}_{}.sh'.format(task, com_cap, prec, N), 'a') as job_file: + job_file.write('mpirun -n {} python3 /home/{}_n{}.py\n'.format(ngpus, task, N)) + +elif sim_pack == 'cuquantum_qsimcirq' and (com_cap == 'gpu' or com_cap == "gpu1" or com_cap == "gpu2" or com_cap == "gpu4" or com_cap == "gpu8"): + for N in N_arr: + with open('{}_{}_{}_{}.sh'.format(task, com_cap, prec, N), 'a') as job_file: + job_file.write('python3 /home/{}_n{}.py\n'.format(task, N)) + +elif sim_pack == 'cuda_quantum':# and (com_cap == 'gpu' or com_cap == "gpu1" or com_cap == "gpu2" or com_cap == "gpu4" or com_cap == "gpu8"): + for N in N_arr: + with open('{}_{}_{}_{}.sh'.format(task, com_cap, prec, N), 'a') as job_file: + job_file.write('python3 /home/{}_n{}.py\n'.format(task, N)) + +elif sim_pack == 'qibojit' and (com_cap == "gpu1" or com_cap == "gpu2" or com_cap == "gpu4" or com_cap == "gpu8"): + for N in N_arr: + with open('{}_{}_{}_{}.sh'.format(task, com_cap, prec, N), 'a') as job_file: + job_file.write('#!/bin/bash -l\n') + job_file.write('. /app/etc/profile.d/conda.sh\n') + + job_file.write('conda activate sim_{}_gpu\n'.format(sim_pack)) + job_file.write('python3 /home/{}_n{}.py\n'.format(task, N)) + +# elif sim_pack == 'pennylane_l' and com_cap == 'gpu': +# for N in N_arr: +# with open('{}_{}_{}_{}.sh'.format(task, com_cap, prec, N), 'a') as job_file: +# job_file.write('#!/bin/bash -l\n') +# job_file.write('. /app/etc/profile.d/conda.sh\n') + +# job_file.write('export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/cuda_lib\n') +# job_file.write('export LIBRARY_PATH=$LIBRARY_PATH:/cuda_lib\n') + +# job_file.write('conda activate sim_{}_gpu\n'.format(sim_pack)) +# job_file.write('python3 /home/{}_n{}.py\n'.format(task, N)) + + +else: + for N in N_arr: + with open('{}_{}_{}_{}.sh'.format(task, com_cap, prec, N), 'a') as job_file: + job_file.write('#!/bin/bash -l\n') + job_file.write('. /app/etc/profile.d/conda.sh\n') + + if com_cap == 'st' or com_cap == 'mt': + job_file.write('conda activate sim_{}\n'.format(sim_pack)) + + elif com_cap == 'gpu': + # job_file.write('module load cuda/11.5.1\n') + job_file.write('conda activate sim_{}_gpu\n'.format(sim_pack)) + + job_file.write('python3 /home/{}_n{}.py\n'.format(task, N)) diff --git a/toolchain/create_dir_struct.py b/toolchain/create_dir_struct.py new file mode 100644 index 0000000000000000000000000000000000000000..9b087d9655fc4fd5b13238c4cbb2c72590b0ab44 --- /dev/null +++ b/toolchain/create_dir_struct.py @@ -0,0 +1,100 @@ +import os + +sim_pack = 'braket' +task = 'hdyn' +com_cap = 'st' +prec = 'dp' + +gen_path = '/data/user/gangap_a/{}_singular/gen_files/'.format(task) +sim_path = '/data/user/gangap_a/{}_singular/{}/'.format(task, sim_pack) + +print(sim_path) + +# os.chdir(sim_path) + +print(' ############################ ') + +print('### Create job scripts folder for package: {}, task: {}, compute cap: {}, precision: {}'.format(sim_pack, task, com_cap, prec)) +job_scripts_path = sim_path+'job_scripts_{}_{}'.format(com_cap, prec) +print(job_scripts_path) +os.mkdir(job_scripts_path) + +print('### Create job scripts') +os.chdir(job_scripts_path) +cjs = gen_path+'create_job_script.py {} {} {} {}'.format(sim_pack, task, com_cap, prec) +os.system('python3 {}'.format(cjs)) + +print(' ############################ ') + +print('### Create bash scripts folder for package: {}, task: {}, compute cap: {}, precision: {}'.format(sim_pack, task, com_cap, prec)) +bash_scripts_path = sim_path+'bash_scripts_{}_{}'.format(com_cap, prec) +os.mkdir(bash_scripts_path) + +print('### Create bash scripts') +os.chdir(bash_scripts_path) +cbs = gen_path+'create_bash_script.py {} {} {} {}'.format(sim_pack, task, com_cap, prec) +os.system('python3 {}'.format(cbs)) + +print(' ############################ ') + +print('### Create output folder') +os.mkdir(sim_path+'output_{}_{}'.format(com_cap, prec)) + +print('### Create error folder') +os.mkdir(sim_path+'error_{}_{}'.format(com_cap, prec)) + +print('### Create data folder') +os.mkdir(sim_path+'data_{}_{}'.format(com_cap, prec)) + +print(' ############################ ') + +print('### Create run files folder package: {}, task: {}, compute cap: {}, precision: {}'.format(sim_pack, task, com_cap, prec)) +run_files_path = sim_path+'run_files_{}_{}'.format(com_cap, prec) +os.mkdir(run_files_path) + +sim_parser = input("Enter the python file including the path for the parser: ") +os.system('python3 {}'.format(sim_parser)) + +print(' ############################ ') + +print('### Create bash scripts to run jobs') +os.chdir(sim_path) + +with open('run_jobs_{}_{}.sh'.format(com_cap, prec), 'w') as f: + + if sim_pack == 'quest': + # f.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/quest/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/quest/:/home\"\n'.format(task, com_cap, prec, task)) + os.mkdir('/data/user/gangap_a/{}_singular/quest/build_{}_{}'.format(task, com_cap, prec)) + # f.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/quest/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/hdyn_singular/quest/run_files_st_dp/:/sim_lib/quest/QuEST/hdyn/run_files_st_dp,/data/user/gangap_a/{}_singular/quest/build_{}_{}:/home\"\n'.format(task, com_cap, prec, task, com_cap, prec)) + f.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/quest/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/quest/run_files_{}_{}/:/home,/data/user/gangap_a/{}_singular/quest/build_{}_{}:/sim_lib/quest/QuEST/build_{}_{}\"\n'.format(task, com_cap, prec, task, com_cap, prec, task, com_cap, prec, com_cap, prec)) + if com_cap == 'st' or com_cap == 'mt': + f.write('singularity exec /data/user/gangap_a/singularity_images/qucos_sim.sif bash /brf/{}_{}_{}.sh\n'.format(task, com_cap, prec)) + elif com_cap == 'gpu': + f.write('singularity exec --nv /data/user/gangap_a/singularity_images/qucos_gpu_sim.sif bash /brf/{}_{}_{}.sh\n'.format(task, com_cap, prec)) + + elif sim_pack == 'qrack_sch' or sim_pack == 'qrack_opt': + os.mkdir('/data/user/gangap_a/{}_singular/{}/bin_{}_{}'.format(task, sim_pack, com_cap, prec)) + f.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/{}/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/:/home,/data/user/gangap_a/{}_singular/{}/bin_{}_{}:/bin_{}_{}\"\n'.format(task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec, com_cap, prec)) + if com_cap == 'st' or com_cap == 'mt': + f.write('singularity exec /data/user/gangap_a/singularity_images/qucos_sim.sif bash /brf/{}_{}_{}.sh\n'.format(task, com_cap, prec)) + elif com_cap == 'gpu': + f.write('singularity exec --nv /data/user/gangap_a/singularity_images/qucos_gpu_sim.sif bash /brf/{}_{}_{}.sh\n'.format(task, com_cap, prec)) + + elif sim_pack == 'qpp': + os.mkdir('/data/user/gangap_a/{}_singular/{}/bin_{}_{}'.format(task, sim_pack, com_cap, prec)) + f.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/{}/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/:/home,/data/user/gangap_a/{}_singular/{}/bin_{}_{}:/bin_{}_{}\"\n'.format(task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec, com_cap, prec)) + # if com_cap == 'st' or com_cap == 'mt': + # f.write('singularity exec /data/user/gangap_a/singularity_images/qucos_sim.sif bash /brf/{}_{}_{}.sh\n'.format(task, com_cap, prec)) + + + # f.write('cd /psi/home/gangap_a\n') + # f.write('rm -rf CMakeCache.txt CMakeFiles cmake_install.cmake Makefile\n') + f.write('path=\"/data/user/gangap_a/{}_singular/{}/job_scripts_{}_{}\"\n'.format(task, sim_pack, com_cap, prec)) + f.write('cd $path\n') + f.write('for job_script in *.sh; do\n') + f.write('echo $job_script;\n') + f.write('sbatch $job_script;\n') + # if sim_pack == 'quest': + # f.write('sleep 2.5\n') + f.write('done\n') + diff --git a/toolchain/create_dir_struct_intel.py b/toolchain/create_dir_struct_intel.py new file mode 100644 index 0000000000000000000000000000000000000000..c7f0dd0b4f7e17d3830fb430fae80f578fd11593 --- /dev/null +++ b/toolchain/create_dir_struct_intel.py @@ -0,0 +1,125 @@ +import os + +N_arr = range(6, 38, 2) +sim_pack = 'intel_qs_cpp_exp' +task = 'hdyn' +com_cap = 'st' +prec = 'dp' + +gen_path = '/data/user/gangap_a/{}_singular/gen_files/'.format(task) +sim_path = '/data/user/gangap_a/{}_singular/{}/'.format(task, sim_pack) + +print(sim_path) + +# os.chdir(sim_path) + +print(' ############################ ') + +print('### Create job scripts folder for package: {}, task: {}, compute cap: {}, precision: {}'.format(sim_pack, task, com_cap, prec)) +job_scripts_path = sim_path+'job_scripts_{}_{}'.format(com_cap, prec) +print(job_scripts_path) +os.mkdir(job_scripts_path) + +print('### Create job scripts') +os.chdir(job_scripts_path) +cjs = gen_path+'create_job_script.py {} {} {} {}'.format(sim_pack, task, com_cap, prec) +os.system('python3 {}'.format(cjs)) + +print(' ############################ ') + +print('### Create bash scripts folder for package: {}, task:{}, compute cap:{}, precision {}'.format(sim_pack, task, com_cap, prec)) +bash_scripts_path = sim_path+'bash_scripts_{}_{}'.format(com_cap, prec) +os.mkdir(bash_scripts_path) + +print('### Create bash scripts') +os.chdir(bash_scripts_path) +cbs = gen_path+'create_bash_script.py {} {} {} {}'.format(sim_pack, task, com_cap, prec) +os.system('python3 {}'.format(cbs)) + +print(' ############################ ') + +print('### Create output folder') +os.mkdir(sim_path+'output_{}_{}'.format(com_cap, prec)) + +print('### Create error folder') +os.mkdir(sim_path+'error_{}_{}'.format(com_cap, prec)) + +print('### Create data folder') +os.mkdir(sim_path+'data_{}_{}'.format(com_cap, prec)) + +print(' ############################ ') + +print('### Create run files folder package: {}, task:{}, compute cap:{}, precision {}'.format(sim_pack, task, com_cap, prec)) +run_files_path = sim_path+'run_files_{}_{}'.format(com_cap, prec) +os.mkdir(run_files_path) + +sim_parser = input("Enter the python file including the path for the parser: ") +os.system('python3 {}'.format(sim_parser)) + +print(' ############################ ') + +print('### Generate the CMakeLists.txt file and the bin_{}_{} folder'.format(com_cap, prec)) + +os.chdir(sim_path) + +os.mkdir('bin_{}_{}'.format(com_cap, prec)) + +with open('CMakeLists.txt', 'w') as cml: + cml.write('message(STATUS \"Compiling\")\n') + cml.write('file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/' + 'run_files_{}_{}/*.cpp)\n'.format(com_cap, prec)) + + cml.write('foreach( sourcefile ${APP_SOURCES} )\n') + + cml.write('file(RELATIVE_PATH filename ${CMAKE_SOURCE_DIR}/' + 'run_files_{}_{}'.format(com_cap, prec) + ' ${sourcefile})\n') + cml.write('string( REPLACE \".cpp\" \"\" file ${filename} )\n') + cml.write('add_executable( ${file} ${sourcefile} )\n') + cml.write('target_link_libraries( ${file} PRIVATE iqs)\n') + + cml.write('set_target_properties(\n') + cml.write('${file}\n') + cml.write('PROPERTIES\n') + cml.write('RUNTIME_OUTPUT_DIRECTORY "' + str("${CMAKE_SOURCE_DIR}") + '/bin_{}_{}\"\n'.format(com_cap, prec)) + cml.write(')\n') + + cml.write('endforeach( sourcefile ${APP_SOURCES} )\n') + + +print(' ############################ ') + +print('### Create bash scripts to run jobs') +os.chdir(sim_path) + +with open('run_jobs_{}_{}.sh'.format(com_cap, prec), 'w') as f: + + f.write('path=\"/data/user/gangap_a/{}_singular/{}/job_scripts_{}_{}\"\n'.format(task, sim_pack, com_cap, prec)) + f.write('cd $path\n') + + if sim_pack == 'intel_qs_cpp': + + if com_cap == 'st': + + f.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/intel_qs_cpp/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/intel_qs_cpp/:/home\"\n'.format(task, com_cap, prec, task)) + f.write('singularity exec /data/user/gangap_a/singularity_images/qucos_sim.sif bash /brf/{}_{}_{}.sh\n'.format(task, com_cap, prec)) + # f.write('bash /data/user/gangap_a/{}_singular/{}/bash_scripts_{}_{}/{}_{}_{}.sh\n'.format(task, sim_pack, com_cap, prec, task, com_cap, prec)) + # f.write('sleep 200\n') + + f.write('for job_script in *.sh; do\n') + f.write('echo $job_script;\n') + # f.write('if [[ $job_script == \"{}_{}_{}.sh\" ]];\n'.format(task, com_cap, prec)) + # f.write('then\n') + # f.write('sbatch $job_script;\n') + # f.write('sleep 5\n') + # f.write('else\n') + f.write('sbatch $job_script;\n') + # f.write('fi\n') + f.write('done\n') + + + else: + f.write('for job_script in *.sh; do\n') + f.write('echo $job_script;\n') + f.write('sbatch $job_script;\n') + if sim_pack == 'quest': + f.write('sleep 2\n') + f.write('done\n') + diff --git a/toolchain/create_dir_struct_quest.py b/toolchain/create_dir_struct_quest.py new file mode 100644 index 0000000000000000000000000000000000000000..408ebc3d92cac2d411ccae0e3c5903dbc8848e9f --- /dev/null +++ b/toolchain/create_dir_struct_quest.py @@ -0,0 +1,105 @@ +import os + +N_arr = range(6, 38, 2) +sim_pack = 'quest' +task = 'hdyn' +com_cap = 'mt' +prec = 'dp' + +gen_path = '/data/user/gangap_a/{}_singular/gen_files/'.format(task) +sim_path = '/data/user/gangap_a/{}_singular/{}/'.format(task, sim_pack) + +print(sim_path) + +# os.chdir(sim_path) + +print(' ############################ ') + +print('### Create job scripts folder for package: {}, task: {}, compute cap: {}, precision: {}'.format(sim_pack, task, com_cap, prec)) +job_scripts_path = sim_path+'job_scripts_{}_{}'.format(com_cap, prec) +print(job_scripts_path) +os.mkdir(job_scripts_path) + +print('### Create job scripts') +os.chdir(job_scripts_path) +cjs = gen_path+'create_job_script.py {} {} {} {}'.format(sim_pack, task, com_cap, prec) +os.system('python3 {}'.format(cjs)) + +print(' ############################ ') + +print('### Create bash scripts folder for package: {}, task:{}, compute cap:{}, precision {}'.format(sim_pack, task, com_cap, prec)) +bash_scripts_path = sim_path+'bash_scripts_{}_{}'.format(com_cap, prec) +os.mkdir(bash_scripts_path) + +print('### Create bash scripts') +os.chdir(bash_scripts_path) +cbs = gen_path+'create_bash_script.py {} {} {} {}'.format(sim_pack, task, com_cap, prec) +os.system('python3 {}'.format(cbs)) + +print(' ############################ ') + +print('### Create output folder') +os.mkdir(sim_path+'output_{}_{}'.format(com_cap, prec)) + +print('### Create error folder') +os.mkdir(sim_path+'error_{}_{}'.format(com_cap, prec)) + +print('### Create data folder') +os.mkdir(sim_path+'data_{}_{}'.format(com_cap, prec)) + +print(' ############################ ') + +print('### Create run files folder package: {}, task:{}, compute cap:{}, precision {}'.format(sim_pack, task, com_cap, prec)) +run_files_path = sim_path+'run_files_{}_{}'.format(com_cap, prec) +os.mkdir(run_files_path) + +sim_parser = input("Enter the python file including the path for the parser: ") +os.system('python3 {}'.format(sim_parser)) + +print(' ############################ ') + +print('### Generate the CMakeLists.txt file and the bin_{}_{} folder'.format(com_cap, prec)) + +os.chdir(sim_path) + +os.mkdir('bin_{}_{}'.format(com_cap, prec)) + +with open('CMakeLists.txt', 'w') as cml: + cml.write('message(STATUS \"Compiling\")\n') + cml.write('file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/' + 'run_files_{}_{}/*.cpp)\n'.format(com_cap, prec)) + + cml.write('foreach( sourcefile ${APP_SOURCES} )\n') + + cml.write('file(RELATIVE_PATH filename ${CMAKE_SOURCE_DIR}/' + 'run_files_{}_{}'.format(com_cap, prec) + ' ${sourcefile})\n') + cml.write('string( REPLACE \".cpp\" \"\" file ${filename} )\n') + cml.write('add_executable( ${file} ${sourcefile} )\n') + cml.write('target_link_libraries( ${file} PRIVATE iqs)\n') + + cml.write('set_target_properties(\n') + cml.write('${file}\n') + cml.write('PROPERTIES\n') + cml.write('RUNTIME_OUTPUT_DIRECTORY "' + str("${CMAKE_SOURCE_DIR}") + '/bin_{}_{}\"\n'.format(com_cap, prec)) + cml.write(')\n') + + cml.write('endforeach( sourcefile ${APP_SOURCES} )\n') + + +print(' ############################ ') + +print('### Create bash scripts to run jobs') +os.chdir(sim_path) + +with open('run_jobs_{}_{}.sh'.format(com_cap, prec), 'w') as f: + + f.write('path=\"/data/user/gangap_a/{}_singular/{}/job_scripts_{}_{}\"\n'.format(task, sim_pack, com_cap, prec)) + f.write('cd $path\n') + + + f.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/quest/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/quest/:/home\"\n'.format(task, com_cap, prec, task)) + f.write('singularity exec /data/user/gangap_a/singularity_images/qucos_sim.sif bash /brf/{}_{}_{}.sh\n'.format(task, com_cap, prec)) + + f.write('for job_script in *.sh; do\n') + f.write('echo $job_script;\n') + f.write('sbatch $job_script;\n') + f.write('done\n') + diff --git a/toolchain/create_job_script.py b/toolchain/create_job_script.py new file mode 100644 index 0000000000000000000000000000000000000000..9de2d684e809f165857fd67d9e3ff974b8b4ee32 --- /dev/null +++ b/toolchain/create_job_script.py @@ -0,0 +1,252 @@ +import numpy as np +import sys + +N_arr = np.arange(6, 36, 2) +# N_arr = np.append(N_arr, np.arange(41, 51, 1)) +# _partition = 'hourly' + +# sim_pack = 'qiskit' +# task = 'hdyn' +# com_cap = 'st' +# prec = 'dp' + +sim_pack = sys.argv[1] +task = sys.argv[2] +com_cap = sys.argv[3] +prec = sys.argv[4] + +# print("hello: ", sim_pack) +# print(task) + +for ind, N in enumerate(N_arr): + with open('{}_{}_{}_{}.sh'.format(task, com_cap, prec, N), 'a') as job_file: + job_file.write('#!/bin/bash -l\n') + job_file.write('#SBATCH --job-name={}_{}_{}_{}_{}\n'.format(sim_pack, task, com_cap, prec, N)) + + if com_cap == 'gpu': + job_file.write('#SBATCH --cluster=gmerlin6\n') + job_file.write('#SBATCH --hint=nomultithread\n') + job_file.write('#SBATCH --nodes=1\n') + job_file.write('#SBATCH --ntasks=1\n') + if sim_pack == 'qulacs': + job_file.write('#SBATCH --partition=gpu-short\n') + job_file.write('#SBATCH --gpus=geforce_rtx_2080_ti:1\n') + else: + job_file.write('#SBATCH --partition=gwendolen\n') + job_file.write('#SBATCH --account=gwendolen\n') + job_file.write('#SBATCH --gpus=A100:1\n') + if N < 30: + job_file.write('#SBATCH --mem=30G\n') + else: + if sim_pack == 'qulacs': + job_file.write('#SBATCH --mem=100G\n') + else: + job_file.write('#SBATCH --mem=900G\n') + + if N < 30: + job_file.write('#SBATCH --time=00:30:00\n') + else: + job_file.write('#SBATCH --time=01:00:00\n') + + elif com_cap == 'gpu1' or com_cap == 'gpu2' or com_cap == 'gpu4' or com_cap == 'gpu8': + job_file.write('#SBATCH --cluster=gmerlin6\n') + job_file.write('#SBATCH --hint=nomultithread\n') + job_file.write('#SBATCH --nodes=1\n') + ngpus = int(com_cap[-1]) + if sim_pack == 'cuquantum_qiskit': + job_file.write('#SBATCH --ntasks={}\n'.format(ngpus)) + job_file.write('#SBATCH --partition=gwendolen\n') + job_file.write('#SBATCH --account=gwendolen\n') + ngpus = int(com_cap[-1]) + job_file.write('#SBATCH --gpus=A100:{}\n'.format(ngpus)) + if N < 30: + job_file.write('#SBATCH --mem=30G\n') + else: + job_file.write('#SBATCH --mem=900G\n') + + if N < 30: + job_file.write('#SBATCH --time=00:30:00\n') + else: + job_file.write('#SBATCH --time=01:00:00\n') + + else: + + if N < 26: + job_file.write('#SBATCH --partition=hourly\n') + else: + job_file.write('#SBATCH --partition=general\n') + + if com_cap == 'st': + job_file.write('#SBATCH --hint=nomultithread\n') + job_file.write('#SBATCH --nodes=1\n') + job_file.write('#SBATCH --ntasks=1\n') + job_file.write('#SBATCH --cpus-per-task=1\n') + job_file.write('#SBATCH --ntasks-per-core=1\n') + + elif com_cap == 'mt': + job_file.write('#SBATCH --hint=multithread\n') + job_file.write('#SBATCH --nodes=1\n') + job_file.write('#SBATCH --ntasks=1\n') + job_file.write('#SBATCH --cpus-per-task=42\n') + job_file.write('#SBATCH --ntasks-per-core=2\n') + + if N < 26: + job_file.write('#SBATCH --mem=30G\n') + else: + job_file.write('#SBATCH --mem=300G\n') + if N < 26: + job_file.write('#SBATCH --time=01:00:00\n') + else: + job_file.write('#SBATCH --time=23:00:00\n') + + job_file.write('#SBATCH --mail-user=amit.gangapuram@psi.ch\n') + job_file.write('#SBATCH --mail-type=BEGIN,END,FAIL\n') + job_file.write('#SBATCH --output /data/user/gangap_a/{}_singular/{}/output_{}_{}/{}_{}_{}_{}.out\n'.format(task, sim_pack, com_cap, prec, task, com_cap, prec, N)) + job_file.write('#SBATCH --error /data/user/gangap_a/{}_singular/{}/error_{}_{}/{}_{}_{}_{}.err\n'.format(task, sim_pack, com_cap, prec, task, com_cap, prec, N)) + job_file.write('\n\n') + + # job_file.write('module load anaconda\n') + # job_file.write('module load gcc/12.1.0\n') + # job_file.write('conda activate sim_{}\n'.format(sim_pack)) + if sim_pack == 'projectq' or sim_pack == 'quest' or sim_pack == 'intel_qs_cpp' or sim_pack == 'svsim' or sim_pack == 'hybridq' or sim_pack == 'hiq' or sim_pack == 'qibo' or sim_pack == 'qibojit' or sim_pack == 'qrack_sch' or sim_pack == 'qrack_opt' or sim_pack == 'pennylane_l' or sim_pack == 'qpp' or sim_pack == 'qpanda' or sim_pack =='braket' or sim_pack == 'myqlm' or sim_pack == 'myqlm_cpp': + if com_cap == 'st': + job_file.write('OMP_NUM_THREADS=1\n') + job_file.write('export OMP_NUM_THREADS\n') + + elif com_cap == 'mt': + job_file.write('OMP_NUM_THREADS=84\n') + job_file.write('export OMP_NUM_THREADS\n') + + elif sim_pack == 'qulacs': + if com_cap == 'st': + job_file.write('OMP_NUM_THREADS=1\n') + job_file.write('export OMP_NUM_THREADS\n') + job_file.write('QULACS_NUM_THREADS=1\n') + job_file.write('export QULACS_NUM_THREADS\n') + + elif com_cap == 'mt': + job_file.write('OMP_NUM_THREADS=84\n') + job_file.write('export OMP_NUM_THREADS\n') + job_file.write('QULACS_NUM_THREADS=84\n') + job_file.write('export QULACS_NUM_THREADS\n') + + elif sim_pack == 'yao': + if com_cap == 'st': + job_file.write('OMP_NUM_THREADS=1\n') + job_file.write('export OMP_NUM_THREADS\n') + job_file.write('JULIA_NUM_THREADS=1\n') + job_file.write('export JULIA_NUM_THREADS\n') + + elif com_cap == 'mt': + job_file.write('OMP_NUM_THREADS=84\n') + job_file.write('export OMP_NUM_THREADS\n') + job_file.write('JULIA_NUM_THREADS=84\n') + job_file.write('export JULIA_NUM_THREADS\n') + + + job_file.write('export SRUN_CPUS_PER_TASK=$SLURM_CPUS_PER_TASK\n') + + if sim_pack == 'hiq': + job_file.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/{}/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/:/sim_lib/hiq/HiQsimulator/examples/run_files_{}_{},/data/user/gangap_a/{}_singular/{}/data_{}_{}\"\n'.format(task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec, com_cap, prec, task, sim_pack, com_cap, prec)) + job_file.write('singularity exec /data/user/gangap_a/singularity_images/hiq_qucos_sim.sif bash /brf/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, N)) + + elif sim_pack == 'intel_qs_cpp': + job_file.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/{}/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/intel_qs_cpp/:/home\"\n'.format(task, sim_pack, com_cap, prec, task)) + # if N==N_arr[0]: + # job_file.write('singularity exec /data/user/gangap_a/singularity_images/qucos_sim.sif bash /brf/{}_{}_{}.sh\n'.format(task, com_cap, prec)) + # job_file.write('sleep 100\n') + job_file.write('singularity exec /data/user/gangap_a/singularity_images/qucos_sim.sif bash /brf/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, N)) + # else: + # job_file.write('singularity exec /data/user/gangap_a/singularity_images/qucos_sim.sif bash /brf/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, N)) + + + elif sim_pack == 'quest': + # job_file.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/{}/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/:/sim_lib/quest/QuEST/{}/run_files_{}_{}\"\n'.format(task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec, task, com_cap, prec)) + job_file.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/{}/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/:/home,/data/user/gangap_a/{}_singular/quest/build_{}_{}:/sim_lib/quest/QuEST/build_{}_{}\"\n'.format(task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec, task, com_cap, prec, com_cap, prec)) + if com_cap == 'st' or com_cap == 'mt': + job_file.write('singularity exec /data/user/gangap_a/singularity_images/qucos_sim.sif bash /brf/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, N)) + elif com_cap == 'gpu': + job_file.write('singularity exec --nv /data/user/gangap_a/singularity_images/qucos_gpu_sim.sif bash /brf/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, N)) + + elif sim_pack == 'qrack_sch' or sim_pack == 'qrack_opt': + if com_cap == 'st' or com_cap == 'mt': + job_file.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/{}/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/:/home,/data/user/gangap_a/{}_singular/{}/bin_st_{}:/bin_st_{}\"\n'.format(task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec, task, sim_pack, prec, prec)) + job_file.write('singularity exec /data/user/gangap_a/singularity_images/qucos_sim.sif bash /brf/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, N)) + elif com_cap == 'gpu': + job_file.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/{}/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/:/home,/data/user/gangap_a/{}_singular/{}/bin_gpu_{}:/bin_gpu_{}\"\n'.format(task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec, task, sim_pack, prec, prec)) + job_file.write('singularity exec --nv /data/user/gangap_a/singularity_images/qucos_gpu_sim.sif bash /brf/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, N)) + + + elif sim_pack == 'qpp': + if com_cap == 'st' or com_cap == 'mt': + job_file.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/{}/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/:/home,/data/user/gangap_a/{}_singular/{}/bin_st_{}:/bin_st_{}\"\n'.format(task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec, task, sim_pack, prec, prec)) + job_file.write('singularity exec /data/user/gangap_a/singularity_images/qucos_sim.sif bash /brf/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, N)) + + # elif sim_pack == 'pennylane_l' and com_cap == 'gpu': + elif sim_pack == 'qulacs' and com_cap == 'gpu': + # job_file.write('module load cuda/11.5.1\n') + job_file.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/{}/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/:/home,/data/user/gangap_a/{}_singular/{}/data_{}_{}/"\n'.format(task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec)) + job_file.write('singularity exec --nv /data/user/gangap_a/singularity_images/qucos_gpu_sim.sif bash /brf/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, N)) + + elif sim_pack == 'yao' and com_cap == 'gpu': + job_file.write('module load cuda/11.5.1\n') + job_file.write('bash /data/user/gangap_a/{}_singular/yao/bash_scripts_{}_{}/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, task, com_cap, prec, N)) + + elif sim_pack == 'pennylane_l' and com_cap == 'gpu': + job_file.write('module load anaconda\n') + job_file.write('conda activate sim_pennylane_gpu\n') + job_file.write('module load cuda/11.5.1\n') + job_file.write('bash /data/user/gangap_a/{}_singular/pennylane_l/bash_scripts_{}_{}/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, task, com_cap, prec, N)) + + elif sim_pack == 'qsimcirq' and com_cap == 'gpu': + job_file.write('module load anaconda\n') + job_file.write('conda activate sim_qsimcirq_a100\n') + job_file.write('module load cuda/11.4.3\n') + job_file.write('bash /data/user/gangap_a/{}_singular/qsimcirq/bash_scripts_{}_{}/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, task, com_cap, prec, N)) + + elif sim_pack == 'qpanda' and com_cap == 'gpu': + job_file.write('module load anaconda\n') + job_file.write('conda activate sim_qpanda_gpu\n') + job_file.write('module load cuda/11.4.3\n') + job_file.write('bash /data/user/gangap_a/{}_singular/qpanda/bash_scripts_{}_{}/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, task, com_cap, prec, N)) + + elif sim_pack == 'cuquantum_qiskit' and (com_cap == 'gpu' or com_cap == 'gpu1' or com_cap == 'gpu2' or com_cap == 'gpu4' or com_cap == 'gpu8'): + job_file.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/{}/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/:/home,/data/user/gangap_a/{}_singular/{}/data_{}_{}/\"\n'.format(task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec)) + job_file.write('singularity exec --nv /data/user/gangap_a/singularity_images/sim_cuq_nvidia.simg bash /brf/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, N)) + + elif sim_pack == 'cuquantum_qsimcirq' and (com_cap == 'gpu' or com_cap == 'gpu1' or com_cap == 'gpu2' or com_cap =='gpu4' or com_cap == 'gpu8'): + job_file.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/{}/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/:/home,/data/user/gangap_a/{}_singular/{}/data_{}_{}/\"\n'.format(task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec)) + job_file.write('singularity exec --nv /data/user/gangap_a/singularity_images/sim_cuq_nvidia.simg bash /brf/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, N)) + + elif sim_pack == 'cuda_quantum': # or com_cap == 'gpu1' or com_cap == 'gpu2' or com_cap =='gpu4' or com_cap == 'gpu8'): + if com_cap == 'st': + job_file.write('OMP_NUM_THREADS=1\n') + job_file.write('export OMP_NUM_THREADS\n') + job_file.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/{}/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/:/home,/data/user/gangap_a/{}_singular/{}/data_{}_{}/\"\n'.format(task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec)) + job_file.write('singularity exec /data/user/gangap_a/singularity_images/sim_cudaq_nvidia.sif bash /brf/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, N)) + elif com_cap == 'mt': + job_file.write('OMP_NUM_THREADS=84\n') + job_file.write('export OMP_NUM_THREADS\n') + job_file.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/{}/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/:/home,/data/user/gangap_a/{}_singular/{}/data_{}_{}/\"\n'.format(task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec)) + job_file.write('singularity exec /data/user/gangap_a/singularity_images/sim_cudaq_nvidia.sif bash /brf/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, N)) + elif com_cap == 'gpu': + job_file.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/{}/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/:/home,/data/user/gangap_a/{}_singular/{}/data_{}_{}/\"\n'.format(task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec)) + job_file.write('singularity exec --nv /data/user/gangap_a/singularity_images/sim_cudaq_nvidia.sif bash /brf/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, N)) + + elif sim_pack == 'qibojit' and (com_cap == 'gpu1' or com_cap == 'gpu2' or com_cap =='gpu4' or com_cap == 'gpu8'): + job_file.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/{}/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/:/home,/data/user/gangap_a/{}_singular/{}/data_{}_{}/\"\n'.format(task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec)) + job_file.write('singularity exec --nv /data/user/gangap_a/singularity_images/qucos_gpu_sim.sif bash /brf/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, N)) + + else: + + # if com_cap == 'gpu': + # job_file.write('module load cuda/11.5.1\n') + job_file.write('export APPTAINER_BIND=\"/data/user/gangap_a/{}_singular/{}/bash_scripts_{}_{}/:/brf,/data/user/gangap_a/{}_singular/{}/run_files_{}_{}/:/home,/data/user/gangap_a/{}_singular/{}/data_{}_{}/\"\n'.format(task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec, task, sim_pack, com_cap, prec)) + + if com_cap == 'gpu': + job_file.write('singularity exec --nv /data/user/gangap_a/singularity_images/qucos_gpu_sim.sif bash /brf/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, N)) + else: + job_file.write('singularity exec /data/user/gangap_a/singularity_images/qucos_sim.sif bash /brf/{}_{}_{}_{}.sh\n'.format(task, com_cap, prec, N)) + + + diff --git a/toolchain/run_jobs_st_dp.sh b/toolchain/run_jobs_st_dp.sh new file mode 100644 index 0000000000000000000000000000000000000000..e8328fcf3fa4898d765981156743e180d2860552 --- /dev/null +++ b/toolchain/run_jobs_st_dp.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +task='hdyn' +sim_pack='qiskit' +com_cap='st' +prec='dp' + +path="/data/user/gangap_a/"${task}"_singular/"${sim_pack}"/job_scripts_"${com_cap}"_"${prec} + +cd $path + +for job_script in *.sh; do +echo $job_script; +sbatch $job_script; +done +