File size: 8,445 Bytes
951488e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# 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)