@@ -108,6 +108,186 @@ def isVip(self,x: str):
108108 def isDeny (self ,x : str ):
109109 return self .compare (x ,self .den )
110110
111+ def codef (codel : str ,text : str ): #code function
112+ if codel == "qiskit" :
113+ t = qiskit (text )
114+ elif codel == "wolfram" :
115+ t = wolfram (text )
116+ elif codel == "python" :
117+ t = python (text )
118+ return t
119+
120+ def qiskit (text : str ):
121+ T = ""
122+ T += "from qiskit import QuantumCircuit, execute, Aer\n "
123+ T += "from qiskit.visualization import plot_histogram,display\n "
124+ s = 0
125+ command = ""
126+ param = ""
127+ Q = 0
128+ gate = ""
129+ gatecount = 0
130+ qdef = 0
131+ for i in text :
132+ if i == "," :
133+ pass
134+ elif s == 1 : #settings mode on
135+ if i != " " :
136+ command += i
137+ else :
138+ s = 2
139+ elif s == 2 :
140+ if i != " " and i != "\n " :
141+ param += i
142+ else :
143+ T += qsettings (command ,param )
144+ command = ""
145+ param = ""
146+ s = 0
147+ elif i == "$" :
148+ s = 1
149+ elif qdef == 1 :
150+ if i == "q" :
151+ Q += 1
152+ elif i == "\n " :
153+ qdef = 2
154+ T += "circuit=QuantumCircuit(" + str (Q )+ "," + str (Q )+ ")\n "
155+ elif qdef == 2 :
156+ if i != "\n " and i != " " :
157+ gate += i
158+ elif i == " " :
159+ gatecount += 0.5
160+ T += quantum (gate ,gatecount )
161+ gate = ""
162+ else :
163+ T += quantum (gate ,gatecount )
164+ gate = ""
165+ gatecount = 0
166+ elif i == "q" :
167+ qdef = 1
168+ Q += 1
169+ return T
170+
171+ def wolfram (text : str ):
172+ T = ""
173+ T += "from wolframclient.evaluation import WolframLanguageSession\n "
174+ T += "from wolframclient.language import wl, wlexpr\n "
175+ T += "session = WolframLanguageSession()\n "
176+ s = 0
177+ command = ""
178+ param = ""
179+ t = ""
180+ for i in text :
181+ if i == "," :
182+ pass
183+ elif s == 1 : #settings mode on
184+ if i != " " :
185+ command += i
186+ else :
187+ s = 2
188+ elif s == 2 :
189+ if i != " " and i != "\n " :
190+ param += i
191+ else :
192+ T += wsettings (command ,param )
193+ command = ""
194+ param = ""
195+ s = 0
196+ elif i == "$" :
197+ s = 1
198+ elif i == "\n " and t != "" :
199+ T += "session.evaluate(wlexpr(\' " + t + "\' ))\n "
200+ t = ""
201+ elif i != "\n " :
202+ t += i
203+ return T
204+
205+ def python (text : str ):
206+ T = ""
207+ T += "from pyforchange.pfcf.utils import *\n "
208+ T += "from pyforchange.pfcf.read import *\n "
209+ s = 0
210+ command = ""
211+ param = ""
212+ for i in text :
213+ if i == "," :
214+ pass
215+ elif s == 1 : #settings mode on
216+ if i != " " :
217+ command += i
218+ else :
219+ s = 2
220+ elif s == 2 :
221+ if i != " " and i != "\n " :
222+ param += i
223+ else :
224+ T += psettings (command ,param )
225+ command = ""
226+ param = ""
227+ s = 0
228+ elif i == "$" :
229+ s = 1
230+ else :
231+ T += i
232+ return T
233+
234+ def psettings (command : str ,param ):
235+ t = ""
236+ if command == "pfcf" :
237+ t = "executepfcf(\" " + param + "\" )"
238+ elif command == "python" :
239+ t = "execute(\" " + param + "\" )"
240+ return t
241+
242+ def wsettings (command : str ,param ):
243+ t = ""
244+ if command == "inject" :
245+ t = param + "\n "
246+ return t
247+
248+ def qsettings (command : str ,param ):
249+ t = ""
250+ if command == "host" :
251+ t0 = "s=1024\n backend=Aer.get_backend('" + param + "')\n "
252+ t1 = "job=execute(circuit, backend, shots=s)\n "
253+ t2 = "result=job.result()\n "
254+ t3 = "counts=result.get_counts(circuit)\n "
255+ t = t0 + t1 + t2 + t3
256+ elif command == "shots" :
257+ t = "s=" + param
258+ elif command == "hist" :
259+ t1 = "graph=plot_histogram(counts)\n "
260+ t2 = "display(graph)\n "
261+ t = t1 + t2
262+ elif command == "draw" :
263+ t = "circuit.draw('mpl')\n "
264+ elif command == "inject" :
265+ t = param + "\n "
266+ return t
267+
268+ def quantum (gate : str ,n ):
269+ t = ""
270+ N = floor (n )
271+ n = str (N )
272+ T = "circuit."
273+ if len (gate )== 1 :
274+ t = T + gate .lower ()+ "(" + n + ")\n "
275+ elif gate == ".---X" :
276+ t = T + "cx(" + n + "," + str (N + 1 )+ ")\n "
277+ elif gate == "X---." :
278+ t = T + "cx(" + str (N + 1 )+ "," + n + ")\n "
279+ else :
280+ number = ""
281+ b = 0
282+ for i in gate :
283+ if i != "c" :
284+ number += i
285+ else :
286+ b = 1
287+ if b == 1 :
288+ t = T + "measure(" + n + "," + number + ")\n "
289+ return t
290+
111291def pfcfread (name : str ,printYesOrNo : int = 1 ,returnText : int = 0 ):
112292 lines = getLines (name + ".pfcf" )
113293 T = ""
@@ -138,7 +318,7 @@ def pfcfread(name: str,printYesOrNo: int =1,returnText: int =0):
138318 codem += 1
139319 elif codem == 4 :
140320 try :
141- codef (codel ,code )
321+ T += codef (codel ,code )
142322 codem == 0
143323 codel = ""
144324 code = ""
@@ -191,6 +371,7 @@ def export():
191371
192372#Tools Menu
193373filemenu .add_command (label = "Export" , command = export )
374+
194375'''
195376This code is based on the code from
196377https://docs.hektorprofe.net/python/interfaces-graficas-con-tkinter/editor-de-texto
0 commit comments