-
Notifications
You must be signed in to change notification settings - Fork 5
/
lighter_to_tfc.py
80 lines (72 loc) · 2.37 KB
/
lighter_to_tfc.py
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
# Run with Python 2
# Basic implementation
# Functionality to convert the output of an implementation file which is output from LIGHTER (in C-like syntax) to a TFC [http://webhome.cs.uvic.ca/~dmaslov/mach-read.html] file
import sys
file_name = sys.argv[1] #'test.c'
impl_file = open(file_name, 'r')
import os.path
tfc_file = open(os.path.splitext(file_name)[0]+'.tfc', 'w')
print tfc_file
varible = 'x'
opening_msg = 'This file is auto-generated'
print >> tfc_file, '# '+opening_msg
# copy the content
for f in impl_file.readlines():
f = f.strip()
print >> tfc_file, '# '+f
def get_numbers(x, varible=varible):
assert type(x) == type(' ')
assert x.find('F') != -1
x = x.replace('F', '').replace('[', '').replace(']', '')
assert len(x) >= 1
if len(x) == 1:
assert str.isdigit(x)
return varible+'_'+(x)
else:
y = x.split(',')
assert [str.isdigit(yy) for yy in y]
return ','.join([varible+'_'+xx for xx in y])
impl_file.seek(0)
initial_ordering = []
print_str = []
final_ordering = [] # the final permutation of bits is not supported in RCViwer+
for f in impl_file.readlines():
f = f.strip()
# ignore comments or blank lines
if f.startswith('//') or f.strip() == '':
pass
# initial and final bit permutations
elif f.find('(') == -1:
d = ((f.replace(' ', '').replace(';', '')).split('=')[1])[-2]
assert str.isdigit(d)
d = int(d)
assert d in range(0, 4)
if len(initial_ordering) <= 3:
initial_ordering.append(d)
else:
final_ordering.append(d)
else:
f = f.replace(' ', '').replace(';','').replace(')','')
exp = f.split('=')[1].split('(')
if exp[0] == 'RNOT1':
print_str.append('t1'+' '+ get_numbers(exp[1]))
elif exp[0] == 'CNOT1':
print_str.append('t2'+' '+ get_numbers(exp[1]))
elif exp[0] == 'CCNOT2':
print_str.append('t3'+' '+ get_numbers(exp[1]))
elif exp[0] == 'CCCNOT2':
print_str.append('t4'+' '+ get_numbers(exp[1]))
elif exp[0] == 'CCCNOT2':
print_str.append('t4'+' '+ get_numbers(exp[1]))
else:
raise NotImplementedError
print >> tfc_file, ''
print >> tfc_file, '.v'+' '+ ','.join([varible+'_'+str(i) for i in initial_ordering])
print >> tfc_file, '.i'+' '+ ','.join([varible+'_'+str(i) for i in initial_ordering])
print >> tfc_file, '.o'+' '+ ','.join([varible+'_'+str(i) for i in final_ordering])
print >> tfc_file, 'BEGIN'
for p in print_str:
print >> tfc_file, p+''
print >> tfc_file, 'END'
impl_file.close()
tfc_file.close()