Skip to content

Commit 067cc59

Browse files
author
ah744
committed
Afree() Implemetation Complete
1 parent 801b6f9 commit 067cc59

File tree

4 files changed

+96
-60
lines changed

4 files changed

+96
-60
lines changed

llvm/lib/Transforms/Scaffold/GenQASM.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,6 @@ void GenQASM::genQASM(Function* F)
933933
else if(fToPrint.find("Tdag") != string::npos) fToPrint = "Tdag";
934934
else if(fToPrint.find("X.") != string::npos) fToPrint = "X";
935935
else if(fToPrint.find("Z.") != string::npos) fToPrint = "Z";
936-
else if(fToPrint.find("afree") != string::npos) continue;
937936

938937
std::replace(fToPrint.begin(), fToPrint.end(), '.', '_');
939938
std::replace(fToPrint.begin(), fToPrint.end(), '-', '_');

scaffold.sh

+36-19
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,64 @@
22

33
ROOT=$(dirname $0)
44

5-
# Get CTQG path
6-
export CTQG_PATH=${ROOT}/ctqg/CTQG
7-
if [ $(echo $PATH | grep ${CTQG_PATH} | wc -l) -eq 0 ]; then
8-
export PATH=$PATH:$CTQG_PATH
5+
# Get RKQC path
6+
export RKQC_PATH=${ROOT}/rkqc
7+
if [ $(echo $PATH | grep ${RKQC_PATH} | wc -l) -eq 0 ]; then
8+
export PATH=$PATH:$RKQC_PATH
99
fi
1010

1111
function show_help {
12-
echo "Usage: $0 [-h] [-rqfRTFcpd] [-l #] [-P #] <filename>.scaffold"
12+
echo "Usage: $0 [-hv] [-rqfRFcpds] [-L #] <filename>.scaffold"
1313
echo " -r Generate resource estimate (default)"
1414
echo " -q Generate QASM"
1515
echo " -f Generate flattened QASM"
1616
echo " -R Disable rotation decomposition"
1717
echo " -T Disable Toffoli decomposition"
18-
echo " -l Levels of recursion to run (default=1)"
19-
echo " -P Set precision of rotation decomposition in decimal digits (default=10)"
18+
echo " -l Levels of recursion to run (default=1)"
2019
echo " -F Force running all steps"
2120
echo " -c Clean all files (no other actions)"
2221
echo " -p Purge all intermediate files (preserves specified output,"
2322
echo " but requires recompilation for any new output)"
2423
echo " -d Dry-run; show all commands to be run, but do not execute"
24+
echo " -s Generate QX Simulator input file"
25+
echo " -v Show current Scaffold version information"
26+
}
27+
28+
function show_version {
29+
echo "Scaffold - Release 2.0 (July 10, 2016) Beta"
2530
}
2631

2732
# Parse opts
2833
OPTIND=1 # Reset in case getopts has been used previously in the shell.
29-
ctqg=0
34+
rkqc=0
3035
clean=0
3136
dryrun=""
3237
force=0
3338
purge=0
3439
res=0
3540
rot=1
3641
toff=1
42+
flat=0
43+
qc=0
3744
targets=""
38-
precision=4
39-
while getopts "h?cdfFpqrRT:l:P:" opt; do
45+
while getopts "h?vcdfsFpqrRTl:" opt; do
4046
case "$opt" in
4147
h|\?)
4248
show_help
4349
exit 0
4450
;;
51+
v)
52+
show_version
53+
exit 0
54+
;;
4555
c) clean=1
4656
;;
4757
d) dryrun="--dry-run"
48-
;;
58+
;;
4959
F) force=1
5060
;;
51-
f) targets="${targets} flat"
52-
;;
61+
f) flat=1
62+
;;
5363
p) purge=1
5464
;;
5565
q) targets="${targets} qasm"
@@ -60,14 +70,23 @@ while getopts "h?cdfFpqrRT:l:P:" opt; do
6070
;;
6171
T) toff=0
6272
;;
73+
s) qc=1
74+
;;
6375
l) targets="${targets} SQCT_LEVELS=${OPTARG}"
6476
;;
65-
P) precision=${OPTARG}
6677
esac
6778
done
6879
shift $((OPTIND-1))
6980
[ "$1" = "--" ] && shift
7081

82+
if [ ${flat} -eq 1 ]; then
83+
targets="${targets} flat"
84+
fi
85+
86+
if [ ${qc} -eq 1 ]; then
87+
targets="${targets} qc"
88+
fi
89+
7190
# Put resources at the end so it is easy to read
7291
if [ ${res} -eq 1 ]; then
7392
targets="${targets} resources"
@@ -101,17 +120,15 @@ dir="$(dirname ${filename})/"
101120
file=$(basename ${filename} .scaffold)
102121
cfile="${file}.*"
103122

104-
if [ $(egrep '^ctqg.*{\s*' ${filename} | wc -l) -gt 0 ]; then
105-
ctqg=1
123+
if [ $(egrep '^rkqc.*{\s*' ${filename} | wc -l) -gt 0 ]; then
124+
rkqc=1
106125
toff=1
107-
#dir=""
108126
fi
109127

110128
if [ ${clean} -eq 1 ]; then
111129
make -f $ROOT/scaffold/Scaffold.makefile ${dryrun} ROOT=$ROOT DIRNAME=${dir} FILENAME=${filename} FILE=${file} CFILE=${cfile} clean
112130
exit
113131
fi
114-
make -f $ROOT/scaffold/Scaffold.makefile ${dryrun} ROOT=$ROOT DIRNAME=${dir} FILENAME=${filename} FILE=${file} CFILE=${cfile} TOFF=${toff} CTQG=${ctqg} ROTATIONS=${rot} PRECISION=${precision} ${targets}
132+
make -f $ROOT/scaffold/Scaffold.makefile ${dryrun} ROOT=$ROOT DIRNAME=${dir} FILENAME=${filename} FILE=${file} CFILE=${cfile} TOFF=${toff} RKQC=${rkqc} ROTATIONS=${rot} ${targets}
115133

116134
exit 0
117-

scaffold/Scaffold.makefile

+50-39
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,17 @@ PERL=/usr/bin/perl
33
PYTHON=/usr/bin/python
44

55
ROOT=".."
6-
DIRNAME=""
76
FILENAME=""
87
FILE=""
98
CFILE=""
10-
TOFF=0
11-
CTQG=0
12-
ROTATIONS=0
9+
DIRNAME=""
1310

1411
BUILD=$(ROOT)/build/Release+Asserts
1512

1613
SQCTPATH=$(ROOT)/Rotations/sqct/rotZ
1714
GRIDSYNTHPATH=$(ROOT)/Rotations/gridsynth/gridsynth
1815
ROTATIONPATH=$(GRIDSYNTHPATH) # select rotation decomposition tool
19-
PRECISION=""
16+
SCRIPTSPATH=$(ROOT)/scripts/ # select rotation decomposition tool
2017

2118
CC=$(BUILD)/bin/clang
2219
OPT=$(BUILD)/bin/opt
@@ -32,11 +29,14 @@ SCAFFOLD_LIB=$(ROOT)/build/Release+Asserts/lib/Scaffold.dylib
3229
endif
3330

3431

32+
RKQC=0
33+
ROTATIONS=0
34+
TOFF=0
35+
3536
################################
3637
# Resource Count Estimation
3738
################################
3839
resources: $(FILE).resources
39-
@cat $(FILE).resources
4040

4141
################################
4242
# Flat QASM generation
@@ -48,30 +48,24 @@ flat: $(FILE).qasmf
4848
################################
4949
qasm: $(FILE).qasmh
5050

51-
.PHONY: res_count qasm flat
51+
################################
52+
# QX Simulator generation
53+
################################
54+
qc: $(FILE).qc
55+
56+
.PHONY: resources qasm flat qc
5257

5358
################################
5459
# Intermediate targets
5560
################################
56-
# Pre-process CTQG
61+
# Compile Scaffold to LLVM bytecode
62+
5763
$(FILE)_merged.scaffold: $(FILENAME)
58-
@if [ $(CTQG) -eq 1 ]; then \
59-
echo "[Scaffold.makefile] Extracting CTQG from Scaffold ..."; \
60-
$(PERL) $(ROOT)/ctqg/trans/pre_process.pl $(FILENAME); \
61-
echo "[Scaffold.makefile] Compiling CTQG ..."; \
62-
$(ROOT)/ctqg/CTQG/ctqg $(CFILE).ctqg; \
63-
echo "[Scaffold.makefile] Merging CTQG output back into Scaffold ..."; \
64-
$(PERL) $(ROOT)/ctqg/trans/trans.pl $(CFILE).qasm > trans.qasm; \
65-
mv trans.qasm $(CFILE).qasm; \
66-
$(PERL) $(ROOT)/ctqg/trans/merge.pl $(CFILE).qasm; \
67-
else \
68-
cp $(FILENAME) $(FILE)_merged.scaffold; \
69-
fi
64+
@cp $(FILENAME) $(FILE)_merged.scaffold
7065

71-
# Compile Scaffold to LLVM bytecode
7266
$(FILE).ll: $(FILE)_merged.scaffold
73-
@echo "[Scaffold.makefile] Compiling $(FILE)_merged.scaffold ..."
74-
@$(CC) $(FILE)_merged.scaffold $(CC_FLAGS) -o $(FILE).ll
67+
@echo "[Scaffold.makefile] Compiling $(FILE).scaffold ..."
68+
@$(CC) $(FILE)_merged.scaffold $(CC_FLAGS) -o $(FILE).ll
7569

7670
$(FILE)1.ll: $(FILE).ll
7771
@echo "[Scaffold.makefile] Transforming cbits ..."
@@ -117,40 +111,53 @@ $(FILE)7.ll: $(FILE)6.ll
117111
cp $(FILE)6.ll $(FILE)7.ll; \
118112
elif [ $(ROTATIONS) -eq 1 ]; then \
119113
echo "[Scaffold.makefile] Decomposing Rotations ..."; \
120-
if [ ! -e /tmp/epsilon-net.0.bin ]; then echo "Generating decomposition databases"; fi; \
114+
if [ ! -e /tmp/epsilon-net.0.bin ]; then echo "Generating decomposition databases; this may take up to an hour"; fi; \
121115
export ROTATIONPATH=$(ROTATIONPATH); \
122-
export PRECISION=$(PRECISION); \
123116
$(OPT) -S -load $(SCAFFOLD_LIB) -Rotations $(FILE)6.ll -o $(FILE)7.ll > /dev/null; \
124117
else \
125118
cp $(FILE)6.ll $(FILE)7.ll; \
126119
fi
127120

128121
# Remove any code that is useless after optimizations
129-
$(FILE)10.ll: $(FILE)7.ll
122+
$(FILE)8.ll: $(FILE)7.ll
130123
@echo "[Scaffold.makefile] Internalizing and Removing Unused Functions ..."
131-
@$(OPT) -S $(FILE)7.ll -internalize -globaldce -deadargelim -o $(FILE)10.ll > /dev/null
124+
@$(OPT) -S $(FILE)7.ll -internalize -globaldce -deadargelim -o $(FILE)8.ll > /dev/null
125+
126+
# Compile RKQC
127+
$(FILE)9.ll: $(FILE)8.ll
128+
@if [ $(RKQC) -eq 1 ]; then \
129+
echo "[Scaffold.makefile] Compiling RKQC Functions ..."; \
130+
$(OPT) -S -load $(SCAFFOLD_LIB) -GenRKQC $(FILE)8.ll -o $(FILE)9.ll > /dev/null 2> $(FILE).errs; \
131+
else \
132+
mv $(FILE)8.ll $(FILE)9.ll; \
133+
fi
132134

133135
# Perform Toffoli decomposition if TOFF is 1
134-
$(FILE)11.ll: $(FILE)10.ll
136+
$(FILE)11.ll: $(FILE)9.ll
135137
@if [ $(TOFF) -eq 1 ]; then \
136138
echo "[Scaffold.makefile] Toffoli Decomposition ..."; \
137-
$(OPT) -S -load $(SCAFFOLD_LIB) -ToffoliReplace $(FILE)10.ll -o $(FILE)11.ll > /dev/null; \
139+
$(OPT) -S -load $(SCAFFOLD_LIB) -ToffoliReplace $(FILE)9.ll -o $(FILE)11.ll > /dev/null; \
138140
else \
139-
cp $(FILE)10.ll $(FILE)11.ll; \
141+
cp $(FILE)9.ll $(FILE)11.ll; \
140142
fi
141143

144+
# Insert reverse functions if REVERSE is 1
145+
$(FILE)12.ll: $(FILE)11.ll
146+
@echo "[Scaffold.makefile] Inserting Reverse Functions..."
147+
@$(OPT) -S -load $(SCAFFOLD_LIB) -FunctionReverse $(FILE)11.ll -o $(FILE)12.ll > /dev/null
148+
142149
# Generate resource counts from final LLVM output
143-
$(FILE).resources: $(FILE)11.ll
150+
$(FILE).resources: $(FILE)12.ll
144151
@echo "[Scaffold.makefile] Generating resource count ..."
145-
@$(OPT) -load $(SCAFFOLD_LIB) -ResourceCount $(FILE)11.ll 2> $(FILE).resources > /dev/null
152+
@$(OPT) -load $(SCAFFOLD_LIB) -ResourceCount $(FILE)12.ll 2> $(FILE).resources > /dev/null
146153
@echo "[Scaffold.makefile] Resources written to $(FILE).resources ..."
147154

148-
# Generate hierarchical QASM
149-
$(FILE).qasmh: $(FILE)11.ll
155+
$(FILE).qasmh: $(FILE)12.ll
150156
@echo "[Scaffold.makefile] Generating hierarchical QASM ..."
151-
@$(OPT) -load $(SCAFFOLD_LIB) -gen-qasm $(FILE)11.ll 2> $(FILE).qasmh > /dev/null
157+
@$(OPT) -load $(SCAFFOLD_LIB) -gen-qasm $(FILE)12.ll 2> $(FILE).qasmh > /dev/null
152158
@echo "[Scaffold.makefile] Hierarchical QASM written to $(FILE).qasmh ..."
153159

160+
154161
# Translate hierarchical QASM back to C++ for flattening
155162
$(FILE)_qasm.scaffold: $(FILE).qasmh
156163
@echo "[Scaffold.makefile] Generating flattened QASM ..."
@@ -164,15 +171,19 @@ $(FILE)_qasm: $(FILE)_qasm.scaffold
164171
$(FILE).qasmf: $(FILE)_qasm
165172
@./$(FILE)_qasm > $(FILE).tmp
166173
@cat fdecl.out $(FILE).tmp > $(FILE).qasmf
167-
@echo "[Scaffold.makefile] Flat QASM written to $(FILE).qasmf ..."
174+
@echo "[Scaffold.makefile] Flat QASM written to $(FILE).qasmf ..."
175+
176+
$(FILE).qc: $(FILE).qasmf
177+
@echo "[Scaffold.makefile] Transforming flat QASM to QX Simulator input ..."
178+
@$(SHELL) $(ROOT)/scripts/qasmf2qc.sh $(FILE).qasmf
179+
@echo "[Scaffold.makefile] QX Simulator input written to $(FILE).qc ..."
168180

169181
# purge cleans temp files
170182
purge:
171-
@rm -f $(FILE)_merged.scaffold $(FILE)_noctqg.scaffold $(FILE).ll $(FILE)1.ll $(FILE)1a.ll $(FILE)1b.ll $(FILE)2.ll $(FILE)3.ll $(FILE)4.ll $(FILE)5.ll $(FILE)5a.ll $(FILE)6.ll $(FILE)6tmp.ll $(FILE)7.ll $(FILE)8.ll $(FILE)9.ll $(FILE)10.ll $(FILE)11.ll $(FILE)tmp.ll $(FILE)_qasm $(FILE)_qasm.scaffold fdecl.out $(CFILE).ctqg $(CFILE).c $(CFILE).signals $(FILE).tmp sim_$(CFILE) $(FILE).*.qasm
183+
@rm -f $(FILE)_merged.scaffold $(FILE)_no.scaffold $(FILE).ll $(FILE)1.ll $(FILE)1a.ll $(FILE)1b.ll $(FILE)2.ll $(FILE)3.ll $(FILE)4.ll $(FILE)5.ll $(FILE)5a.ll $(FILE)6.ll $(FILE)6tmp.ll $(FILE)7.ll $(FILE)8.ll $(FILE)9.ll $(FILE)10.ll $(FILE)11.ll $(FILE)12.ll $(FILE)tmp.ll $(FILE)_qasm $(FILE)_qasm.scaffold fdecl.out $(CFILE).ctqg $(CFILE).c $(CFILE).signals $(FILE).tmp sim_$(CFILE) $(FILE).*.qasm
172184

173185
# clean removes all completed files
174186
clean: purge
175-
@rm -f $(FILE).resources $(FILE).qasmh $(FILE).qasmf
187+
@rm -f $(FILE).resources $(FILE).qasmh $(FILE).qasmf $(FILE).qc
176188

177189
.PHONY: clean purge
178-

scaffold/flatten-qasm.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def process_qasm(fname):
1212
qgates_4 = ['PrepX','PrepZ']
1313
qgates_5 = ['MeasX','MeasZ']
1414
qgates_6 = ['Rz']
15+
qgates_7 = ['afree']
1516

1617

1718
gateNames = {
@@ -30,7 +31,8 @@ def process_qasm(fname):
3031
'Rz':'Rz',
3132
'CNOT':'CNOT', #'CX',
3233
'Toffoli':'Tof',
33-
'Fredkin':'Fredkin'
34+
'Fredkin':'Fredkin',
35+
'afree':'afree'
3436
}
3537

3638
pattern_qbit_decl = re.compile(r"\s*\bqbit\b\s+(?P<qbit_var>\w+)\s*\[\s*(?P<array_size>\d+)\s*\]\s*;")
@@ -97,6 +99,13 @@ def process_qasm(fname):
9799
instFnName = 'qg_'+q
98100
fstr = 'void '+instFnName+'(char* a, double b){ printf("' +gateNames[q] +' %s,%f\\n",a,b); }\n'
99101
fout.write(fstr)
102+
103+
for q in qgates_7:
104+
instFnName = q
105+
fstr = 'void '+instFnName+'(char** a, int b ){ for(int i = 0; i < b; i++){ printf("' +gateNames[q] +' %s\\n",(*a)); a++; }}\n'
106+
107+
fout.write(fstr)
108+
100109

101110
fout.write('\n')
102111

0 commit comments

Comments
 (0)