33
44import mpmath
55
6- from .diophantine import NO_SOLUTION , diophantine_dyadic , set_random_seed
6+ from .config import GridsynthConfig
7+ from .diophantine import NO_SOLUTION , diophantine_dyadic
78from .loop_controller import LoopController
89from .mymath import solve_quadratic , sqrt
910from .quantum_gate import Rz
@@ -115,15 +116,18 @@ def get_synthesized_unitary(gates):
115116 return DOmegaUnitary .from_gates (gates ).to_complex_matrix
116117
117118
118- def gridsynth (
119- theta ,
120- epsilon ,
121- dps = None ,
122- loop_controller = None ,
123- verbose = False ,
124- measure_time = False ,
125- show_graph = False ,
126- ):
119+ def gridsynth (theta , epsilon , cfg = None , ** kwargs ):
120+ if cfg is None :
121+ cfg = GridsynthConfig (** kwargs )
122+ elif kwargs :
123+ warnings .warn (
124+ "When 'cfg' is provided, 'kwargs' are ignored." ,
125+ stacklevel = 2 ,
126+ )
127+
128+ if cfg .dps is None :
129+ cfg .dps = _dps_for_epsilon (epsilon )
130+
127131 if isinstance (theta , float ):
128132 warnings .warn (
129133 (
@@ -148,50 +152,48 @@ def gridsynth(
148152 stacklevel = 2 ,
149153 )
150154
151- if dps is None :
152- dps = _dps_for_epsilon (epsilon )
153- with mpmath .workdps (dps ):
155+ with mpmath .workdps (cfg .dps ):
154156 theta = mpmath .mpmathify (theta )
155157 epsilon = mpmath .mpmathify (epsilon )
156- if loop_controller is None :
157- loop_controller = LoopController ()
158158
159159 epsilon_region = EpsilonRegion (theta , epsilon )
160160 unit_disk = UnitDisk ()
161161 k = 0
162162
163- if measure_time :
163+ if cfg . measure_time :
164164 start = time .time ()
165165 transformed = to_upright_set_pair (
166- epsilon_region , unit_disk , verbose = verbose , show_graph = show_graph
166+ epsilon_region , unit_disk , verbose = cfg . verbose , show_graph = cfg . show_graph
167167 )
168- if measure_time :
168+ if cfg . measure_time :
169169 print (f"to_upright_set_pair: { time .time () - start } s" )
170- if verbose :
170+ if cfg . verbose :
171171 print ("------------------" )
172172
173173 time_of_solve_TDGP = 0
174174 time_of_diophantine_dyadic = 0
175175 while True :
176- if measure_time :
176+ if cfg . measure_time :
177177 start = time .time ()
178178 sol = solve_TDGP (
179179 epsilon_region ,
180180 unit_disk ,
181181 * transformed ,
182182 k ,
183- verbose = verbose ,
184- show_graph = show_graph ,
183+ verbose = cfg . verbose ,
184+ show_graph = cfg . show_graph ,
185185 )
186- if measure_time :
186+ if cfg . measure_time :
187187 time_of_solve_TDGP += time .time () - start
188188 start = time .time ()
189189
190190 for z in sol :
191191 if (z * z .conj ).residue == 0 :
192192 continue
193193 xi = 1 - DRootTwo .fromDOmega (z .conj * z )
194- w = diophantine_dyadic (xi , loop_controller = loop_controller )
194+ w = diophantine_dyadic (
195+ xi , seed = cfg .seed , loop_controller = cfg .loop_controller
196+ )
195197 if w != NO_SOLUTION :
196198 z = z .reduce_denomexp ()
197199 w = w .reduce_denomexp ()
@@ -203,78 +205,43 @@ def gridsynth(
203205 u_approx = DOmegaUnitary (z , w , 0 )
204206 else :
205207 u_approx = DOmegaUnitary (z , w .mul_by_omega (), 0 )
206- if measure_time :
208+ if cfg . measure_time :
207209 time_of_diophantine_dyadic += time .time () - start
208210 print (f"time of solve_TDGP: { time_of_solve_TDGP * 1000 } ms" )
209211 print (
210212 "time of diophantine_dyadic: "
211213 f"{ time_of_diophantine_dyadic * 1000 } ms"
212214 )
213- if verbose :
215+ if cfg . verbose :
214216 print (f"{ z = } , { w = } " )
215217 print ("------------------" )
216218 return u_approx
217- if measure_time :
219+ if cfg . measure_time :
218220 time_of_diophantine_dyadic += time .time () - start
219221 k += 1
220222
221223
222- def gridsynth_circuit (
223- theta ,
224- epsilon ,
225- wires = [0 ],
226- decompose_phase_gate = True ,
227- dps = None ,
228- loop_controller = None ,
229- verbose = False ,
230- measure_time = False ,
231- show_graph = False ,
232- ):
233- if isinstance (theta , float ):
224+ def gridsynth_circuit (theta , epsilon , wires = [0 ], cfg = None , ** kwargs ):
225+ if cfg is None :
226+ cfg = GridsynthConfig (** kwargs )
227+ elif kwargs :
234228 warnings .warn (
235- (
236- f"pygridsynth is synthesizing the angle { theta } . "
237- "Please verify that this is the intended value. "
238- "Using float may introduce precision errors; "
239- "consider using mpmath.mpf for exact precision."
240- ),
241- UserWarning ,
229+ "When 'cfg' is provided, 'kwargs' are ignored." ,
242230 stacklevel = 2 ,
243231 )
244232
245- if isinstance (epsilon , float ):
246- warnings .warn (
247- (
248- f"pygridsynth is using epsilon={ epsilon } as the tolerance. "
249- "Please verify that this is the intended value. "
250- "Using float may introduce precision errors; "
251- "consider using mpmath.mpf for exact precision."
252- ),
253- UserWarning ,
254- stacklevel = 2 ,
255- )
233+ if cfg .dps is None :
234+ cfg .dps = _dps_for_epsilon (epsilon )
256235
257- if dps is None :
258- dps = _dps_for_epsilon (epsilon )
259- with mpmath .workdps (dps ):
260- theta = mpmath .mpmathify (theta )
261- epsilon = mpmath .mpmathify (epsilon )
262- start_total = time .time () if measure_time else 0.0
263- u_approx = gridsynth (
264- theta = theta ,
265- epsilon = epsilon ,
266- dps = dps ,
267- loop_controller = loop_controller ,
268- verbose = verbose ,
269- measure_time = measure_time ,
270- show_graph = show_graph ,
271- )
236+ with mpmath .workdps (cfg .dps ):
237+ start_total = time .time () if cfg .measure_time else 0.0
238+ u_approx = gridsynth (theta = theta , epsilon = epsilon , cfg = cfg )
272239
273- start = time .time () if measure_time else 0.0
240+ start = time .time () if cfg . measure_time else 0.0
274241 circuit = decompose_domega_unitary (
275- u_approx , wires = wires , decompose_phase_gate = decompose_phase_gate
242+ u_approx , wires = wires , upto_phase = cfg . upto_phase
276243 )
277- if measure_time :
244+ if cfg . measure_time :
278245 print (
279246 f"time of decompose_domega_unitary: { (time .time () - start ) * 1000 } ms"
280247 )
@@ -283,65 +250,24 @@ def gridsynth_circuit(
283250 return circuit
284251
285252
286- def gridsynth_gates (
287- theta ,
288- epsilon ,
289- dps = None ,
290- dtimeout = None ,
291- ftimeout = None ,
292- dloop = 10 ,
293- floop = 10 ,
294- seed = 0 ,
295- verbose = False ,
296- measure_time = False ,
297- show_graph = False ,
298- decompose_phase_gate = True ,
299- ):
300- if isinstance (theta , float ):
301- warnings .warn (
302- (
303- f"pygridsynth is synthesizing the angle { theta } . "
304- "Please verify that this is the intended value. "
305- "Using float may introduce precision errors; "
306- "consider using mpmath.mpf for exact precision."
307- ),
308- UserWarning ,
309- stacklevel = 2 ,
310- )
311-
312- if isinstance (epsilon , float ):
253+ def gridsynth_gates (theta , epsilon , cfg = None , ** kwargs ):
254+ if cfg is None :
255+ cfg = GridsynthConfig (** kwargs )
256+ elif kwargs :
313257 warnings .warn (
314- (
315- f"pygridsynth is using epsilon={ epsilon } as the tolerance. "
316- "Please verify that this is the intended value. "
317- "Using float may introduce precision errors; "
318- "consider using mpmath.mpf for exact precision."
319- ),
320- UserWarning ,
258+ "When 'cfg' is provided, 'kwargs' are ignored." ,
321259 stacklevel = 2 ,
322260 )
323261
324- set_random_seed (seed )
325-
326- loop_controller = LoopController (
327- dloop = dloop , floop = floop , dtimeout = dtimeout , ftimeout = ftimeout
328- )
262+ if cfg .dps is None :
263+ cfg .dps = _dps_for_epsilon (epsilon )
329264
330- if dps is None :
331- dps = _dps_for_epsilon (epsilon )
332- with mpmath .workdps (dps ):
333- theta = mpmath .mpmathify (theta )
334- epsilon = mpmath .mpmathify (epsilon )
265+ with mpmath .workdps (cfg .dps ):
335266 circuit = gridsynth_circuit (
336267 theta = theta ,
337268 epsilon = epsilon ,
338269 wires = [0 ],
339- decompose_phase_gate = decompose_phase_gate ,
340- dps = dps ,
341- loop_controller = loop_controller ,
342- verbose = verbose ,
343- measure_time = measure_time ,
344- show_graph = show_graph ,
270+ cfg = cfg ,
345271 )
346272 return circuit .to_simple_str ()
347273
0 commit comments