circuit-quantization 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,381 @@
1
+ // Library: Poly Solver via Groebner Cover
2
+ // Loads required libs and defines a reusable proc to compute branches.
3
+
4
+ LIB "grobcov.lib";
5
+ LIB "primdec.lib";
6
+
7
+
8
+ // Quick solvability test
9
+ proc check_solvability(ideal Id) {
10
+ ideal Grob = std(Id);
11
+ // vdim(G) is 0 ONLY if G = [1] (Inconsistent)
12
+ // It is -1 (infinite) or >0 (finite) otherwise.
13
+ if (vdim(Grob) == 0) {
14
+ return(0);
15
+ }
16
+ return(1);
17
+ }
18
+
19
+ // solve_cover(fixed_params_csv, potential_vars_csv, eqs_csv)
20
+ // - fixed_params_csv: comma-separated parameter names (no spaces), e.g. "a,b"
21
+ // - potential_vars_csv: comma-separated variable names (no spaces), e.g. "x,y"
22
+ // - eqs_csv: comma-separated polynomial equations, e.g. "a*x-b, x^2+y-a"
23
+ // - filter_large_coeff: if true, skips branches with large coefficients
24
+ // - debug: if true, outputs debug information to /tmp/sing_debug.log
25
+ // Returns a single string with markers suitable for parsing in Python.
26
+ proc solve_cover(string fixed_params, string pot_vars, string eqs_csv, int filter_large_coeff, int debug)
27
+ {
28
+
29
+ if (debug){
30
+ system("sh", " echo '-------------------------- Beginning solve_cover -------------------------- '>> /tmp/sing_debug.log");
31
+ system("sh", " echo 'Fixed params: " + fixed_params + "'>> /tmp/sing_debug.log");
32
+ system("sh", " echo 'Potential vars: " + pot_vars + "'>> /tmp/sing_debug.log");
33
+ system("sh", " echo 'Equations: " + eqs_csv + "'>> /tmp/sing_debug.log");
34
+ system("sh", " echo 'Filter large coeffs: " + string(filter_large_coeff) + "'>> /tmp/sing_debug.log");
35
+ system("sh", " echo 'Debug mode: " + string(debug) + "'>> /tmp/sing_debug.log");
36
+ }
37
+ list out_list;
38
+ int out_idx = 0;
39
+ out_idx++; out_list[out_idx] = "|||START|||";
40
+
41
+ // Build base ring r (parameters in coefficient field, variables as ring vars)
42
+ int n_fixed = countCleanChars(fixed_params);
43
+ int n_pot = countCleanChars(pot_vars);
44
+ string ring_cmd;
45
+ if (size(fixed_params) > 0)
46
+ {
47
+ ring_cmd = "ring r_base = 0, (" + pot_vars + "," + fixed_params + "), (dp(" + string(n_pot) + "),"+"dp(" + string(n_fixed) + "));";
48
+ }
49
+ else
50
+ {
51
+ ring_cmd = "ring r_base = 0, (" + pot_vars + "), dp;";
52
+ }
53
+
54
+ execute(ring_cmd);
55
+
56
+
57
+ // Define ideal from CSV (assumes eqs_csv uses '^' for powers)
58
+ execute("ideal i_base = " + eqs_csv + ";");
59
+
60
+
61
+ // Step 1: Factor the ideal
62
+ // For each factorization component, run minAssGTZ to get prime components
63
+ list fac_basis = facstd(i_base);
64
+ int idx_p, idx_k, idx_j, idx_m, idx_q;
65
+ int branch_id = 0;
66
+ for (idx_q=1; idx_q <= size(fac_basis); idx_q++)
67
+ {
68
+ if (debug){
69
+ system("sh", " echo 'factorized basis " + string(idx_q) + "'>> /tmp/sing_debug.log");
70
+ system("sh", " echo 'factorized basis " + string(fac_basis[idx_q]) + "'>> /tmp/sing_debug.log");
71
+ }
72
+ // Optionally filter out components with large coefficients
73
+ int to_consider = 1;
74
+ if (filter_large_coeff){
75
+ if (has_large_coeffs(fac_basis[idx_q], 10000)){
76
+ to_consider = 0;
77
+ if (debug){
78
+ system("sh", " echo 'skipped due to large coeff'>> /tmp/sing_debug.log");
79
+ }
80
+ }
81
+ }
82
+ if (to_consider){
83
+ list prime_comps = minAssGTZ(fac_basis[idx_q]);
84
+ // Step 2: For each prime component, find maximal independent sets and run grobcov
85
+ for (idx_p=1; idx_p<=size(prime_comps); idx_p++)
86
+ {
87
+ if (debug){
88
+ system("sh", " echo 'prime component " + string(idx_p) + "'>> /tmp/sing_debug.log");
89
+ system("sh", " echo 'prime component " + string(prime_comps[idx_p]) + "'>> /tmp/sing_debug.log");
90
+ }
91
+ ideal comp = prime_comps[idx_p];
92
+ comp = std(comp);
93
+ // Find maximal independent set that respects fixed parameters
94
+ intvec indep;
95
+ int max_score = -1;
96
+ if (n_fixed == 0){
97
+ // No fixed params: take the first indep set
98
+ indep = indepSet(comp);
99
+ }
100
+ else{
101
+ // Need to find an indep set that maximized number of fixed params
102
+ list all_MIS = indepSet(comp, 1);
103
+ int current_score;
104
+ int subset_check;
105
+ for (int idx_k=1; idx_k<=size(all_MIS); idx_k++) {
106
+ intvec current_mis = all_MIS[idx_k];
107
+ current_score = 0;
108
+ // Check if desired_indices are a SUBSET of current_mis
109
+ subset_check = 1;
110
+ for (idx_j=n_pot+1; idx_j<=n_pot+n_fixed; idx_j++) {
111
+ // If we want var X, but current_mis has 0 for X, fail.
112
+ if (current_mis[idx_j] == 0) {
113
+ subset_check = 0;
114
+ }
115
+ else{
116
+ current_score = current_score + 1;
117
+ }
118
+ }
119
+ if (current_score > max_score)
120
+ {
121
+ max_score = current_score;
122
+ indep = current_mis;
123
+ }
124
+ if (subset_check == 1) {
125
+ indep = current_mis;
126
+ break; // We found a set that includes all sure params.
127
+ }
128
+ }
129
+ kill all_MIS;
130
+ kill current_score;
131
+ kill subset_check;
132
+ }
133
+ // Build parameter and variable lists
134
+ // Independent vars become additional parameters for grobcov
135
+ // Dependent vars are what we solve for
136
+ string param_str = "";
137
+ string var_str = "";
138
+ for (idx_m=1; idx_m<=nvars(basering); idx_m++)
139
+ {
140
+ if (indep[idx_m] == 1)
141
+ {
142
+ if (size(param_str) > 0) { param_str = param_str + ","; }
143
+ param_str = param_str + string(var(idx_m));
144
+ }
145
+ else
146
+ {
147
+ if (size(var_str) > 0) { var_str = var_str + ","; }
148
+ var_str = var_str + string(var(idx_m));
149
+ }
150
+ }
151
+ if (param_str == "")
152
+ {
153
+ // No independent parameters discovered; single branch with comp basis
154
+ branch_id = branch_id + 1;
155
+ out_idx++; out_list[out_idx] = "|||BRANCH|||" + string(branch_id) + newline;
156
+ out_idx++; out_list[out_idx] = "|||PARAMS|||" + param_str + newline;
157
+ out_idx++; out_list[out_idx] = "|||VARS|||" + var_str + newline;
158
+ out_idx++; out_list[out_idx] = "|||CONSTRAINTS|||" + newline;
159
+ out_idx++; out_list[out_idx] = "|||NUM_CONSTRAINT_SOLUTIONS|||" + newline;
160
+ out_idx++; out_list[out_idx] = "|||NONNULL|||" + newline;
161
+ out_idx++; out_list[out_idx] = "|||BASIS|||" + newline;
162
+ // Count expected number of solutions
163
+ if (size(variables(comp)) > 0){
164
+ execute("ring r_temp = 0, (" + string(variables(comp)) + "), lp;");
165
+ ideal comp_temp = std(imap(r_base, comp));
166
+ for (idx_j=1; idx_j<=size(comp_temp); idx_j++) {out_idx++; out_list[out_idx] = string(comp_temp[idx_j]) + newline; }
167
+ out_idx++; out_list[out_idx] = "|||NUM_SOLUTIONS|||" + string(vdim(comp_temp)) + newline;
168
+ out_idx++; out_list[out_idx] = "|||COMPONENT|||" + newline;
169
+ ideal lpp = lead(comp_temp);
170
+ for (idx_j=1; idx_j<=size(lpp); idx_j++){out_idx++; out_list[out_idx] = string(lpp[idx_j]) + newline;};
171
+ setring r_base;
172
+ }
173
+ else{
174
+ for (idx_j=1; idx_j<=size(comp); idx_j++) {out_idx++; out_list[out_idx] = string(comp[idx_j]) + newline;}
175
+ out_idx++; out_list[out_idx] = "|||NUM_SOLUTIONS|||" + string(vdim(comp)) + newline;
176
+ ideal lpp = lead(comp);
177
+ for (idx_j=1; idx_j<=size(lpp); idx_j++){out_idx++; out_list[out_idx] = string(lpp[idx_j]) + newline;};
178
+
179
+ }
180
+ }
181
+ else
182
+ {
183
+ // Has parameters: run grobcov
184
+ // Combine fixed_params with discovered independent vars
185
+ string ring_cmd_dp;
186
+ string ring_cmd_lp;
187
+ string ring_cmd_pr;
188
+ ring_cmd_dp = "ring r_gc_dp = (0," + param_str + "), (" + var_str + "), dp;";
189
+ ring_cmd_lp = "ring r_gc_lp = (0," + param_str + "), (" + var_str + "), lp;";
190
+ if(size(var_str)>0){
191
+ ring_cmd_pr = "ring r_gc_pr = (0," + var_str + "), (" + param_str + "), lp;";
192
+ }
193
+ else{
194
+ ring_cmd_pr = "ring r_gc_pr = (0,), (" + param_str + "), lp;";
195
+ };
196
+ execute(ring_cmd_pr);
197
+ execute(ring_cmd_lp);
198
+ execute(ring_cmd_dp);
199
+ // Use dp for speedy grobcov, lp for fglm conversions
200
+ ideal comp_gc = imap(r_base, comp);
201
+ def Cov = grobcov(comp_gc, "ext", 1, "rep", 1);
202
+ for (idx_k=1; idx_k<=size(Cov); idx_k++)
203
+ {
204
+
205
+ branch_id = branch_id + 1;
206
+ def seg = Cov[idx_k];
207
+ out_idx++; string(typeof(seg[2]));
208
+ out_idx++; string(seg[2]);
209
+ // Cast basis to ideal if needed
210
+ def raw_basis = seg[2];
211
+
212
+ if (debug){
213
+ system("sh", " echo 'grobcov component " + string(idx_k) + "'>> /tmp/sing_debug.log");
214
+ system("sh", " echo 'raw basis " + string(raw_basis) + "'>> /tmp/sing_debug.log");
215
+ }
216
+ ideal basis;
217
+ if (typeof(raw_basis) == "ideal") {
218
+ basis = raw_basis;
219
+ } else {
220
+ // It is a list. We must manually build the ideal.
221
+ int rb_size = size(raw_basis);
222
+ if (rb_size > 0) {
223
+ basis = ideal(raw_basis[1]);
224
+ for (int z=2; z<=rb_size; z++) {
225
+ basis = basis + raw_basis[z];
226
+ }
227
+ } else {
228
+ basis = ideal(0);
229
+ }
230
+ }
231
+
232
+ if (debug){
233
+ system("sh", " echo 'generated ideal " + string(basis) + "'>> /tmp/sing_debug.log");
234
+ }
235
+ // Test whether we have a solution here
236
+ if (basis[1] != 1)
237
+ {
238
+ // seg[1] = lpp, seg[2] = actual basis, seg[3] = segment info
239
+ // Count expected number of solutions
240
+ //def seginfo = seg[3];
241
+ ideal lpp = seg[1];
242
+ def constraints_info = seg[3];
243
+ ideal E_null = constraints_info[1];
244
+ ideal N_nonnull = constraints_info[2];
245
+ out_idx++; out_list[out_idx] = "|||BRANCH|||" + string(branch_id) + newline;
246
+ out_idx++; out_list[out_idx] = "|||COMPONENT|||" + newline;
247
+ for (idx_j=1; idx_j<=size(lpp); idx_j++){out_idx++; out_list[out_idx] = string(lpp[idx_j]) + newline;};
248
+ out_idx++; out_list[out_idx] = "|||PARAMS|||" + param_str + newline;
249
+ out_idx++; out_list[out_idx] = "|||VARS|||" + var_str + newline;
250
+ out_idx++; out_list[out_idx] = "|||CONSTRAINTS|||" + newline;
251
+ for (idx_j=1; idx_j<=size(E_null); idx_j++)
252
+ {
253
+ if (E_null[idx_j] != 0) { out_idx++; out_list[out_idx] = string(E_null[idx_j]) + newline; }
254
+ }
255
+ // Count number of solutions to parameter constraints
256
+ setring r_gc_pr;
257
+ ideal E_null_pr = imap(r_gc_dp, E_null);
258
+ if (size(variables(E_null_pr)) > 0)
259
+ {
260
+ string ring_cmd_temp;
261
+ if(size(var_str)>0){
262
+ ring_cmd_temp = "ring r_temp = (0," + var_str + "), (" + string(variables(E_null_pr)) + "), lp;";
263
+ }
264
+ else{
265
+ ring_cmd_temp = "ring r_temp = (0,), (" + string(variables(E_null_pr)) + "), lp;";
266
+ };
267
+ execute(ring_cmd_temp);
268
+ ideal E_null_temp = std(imap(r_gc_pr, E_null_pr));
269
+ int num_param_sols = vdim(E_null_temp);
270
+ out_idx++; out_list[out_idx] = "|||CONSTRAINT_BASIS|||"+newline;
271
+ if (num_param_sols > 0)
272
+ {
273
+ for (idx_j=1; idx_j<=size(E_null_temp); idx_j++)
274
+ {
275
+ out_idx++; out_list[out_idx] = string(E_null_temp[idx_j]) + newline;
276
+ }
277
+ }
278
+ out_idx++; out_list[out_idx] = "|||NUM_CONSTRAINT_SOLUTIONS|||" + string(num_param_sols) + newline;
279
+ }
280
+ else
281
+ {
282
+ out_idx++; out_list[out_idx] = "|||NUM_CONSTRAINT_SOLUTIONS|||-1" + newline;
283
+ }
284
+ setring r_gc_dp;
285
+ out_idx++; out_list[out_idx] = "|||NONNULL|||" + newline;
286
+ for (idx_j=1; idx_j<=size(N_nonnull); idx_j++)
287
+ {
288
+ if (N_nonnull[idx_j] != 0) { out_idx++; out_list[out_idx] = string(N_nonnull[idx_j]) + newline; }
289
+ }
290
+ out_idx++; out_list[out_idx] = "|||BASIS|||" + newline;
291
+ if (size(variables(basis)) > 0) {
292
+ execute("ring r_temp = (0," + param_str + "), (" + string(variables(basis)) + "), lp;");
293
+ ideal basis_temp = std(imap(r_gc_dp, basis));
294
+ for (idx_j=1; idx_j<=size(basis_temp); idx_j++)
295
+ {
296
+ out_idx++; out_list[out_idx] = string(basis_temp[idx_j]) + newline;
297
+ }
298
+ int num_solutions = vdim(basis_temp);
299
+ out_idx++; out_list[out_idx] = "|||NUM_SOLUTIONS|||" + string(num_solutions) + newline;
300
+ setring r_gc_dp;
301
+ }
302
+ else{
303
+ for (idx_j=1; idx_j<=size(basis); idx_j++)
304
+ {
305
+ out_idx++; out_list[out_idx] = string(basis[idx_j]) + newline;
306
+ }
307
+ out_idx++; out_list[out_idx] = "|||NUM_SOLUTIONS|||" + string(vdim(basis)) + newline;
308
+ }
309
+ // Kill local variables inside the IF block
310
+ kill constraints_info, E_null, N_nonnull;
311
+ }
312
+ // Kill loop variables unconditionally at end of iteration
313
+ kill seg, raw_basis, basis;
314
+ }
315
+ kill Cov;
316
+ }
317
+ setring r_base;
318
+ kill comp;
319
+ }
320
+ }
321
+ kill to_consider;
322
+ }
323
+ out_idx++; out_list[out_idx] = "|||END|||";
324
+ return(out_list);
325
+ }
326
+
327
+ // Helper procedure to count non-comma, non-whitespace characters in a string
328
+ proc countCleanChars(string st)
329
+ {
330
+ int len = size(st);
331
+ int count = 0;
332
+ string charac;
333
+
334
+ for (int cc_i=1; cc_i<=len; cc_i++)
335
+ {
336
+ charac = st[cc_i];
337
+ // Check if charac is NOT a comma and NOT whitespace (space, tab, newline)
338
+ if (charac != "," && charac != " " && charac != " " && charac != "\n")
339
+ {
340
+ count++;
341
+ }
342
+ }
343
+ return(count);
344
+ }
345
+
346
+
347
+ // Procedure: has_large_coeffs
348
+ // Inputs:
349
+ // Id : The ideal (or list of polys) to check
350
+ // limit : The integer threshold
351
+ // Returns:
352
+ // 1 if a coefficient > limit or < -limit is found
353
+ // 0 otherwise
354
+ proc has_large_coeffs(ideal Id, int limit)
355
+ {
356
+ int i;
357
+ poly p;
358
+ number c;
359
+
360
+ // Loop through every polynomial in the ideal
361
+ for (i=1; i<=size(Id); i++)
362
+ {
363
+ p = Id[i];
364
+
365
+ // Loop through every monomial term in the polynomial
366
+ while (p != 0)
367
+ {
368
+ c = leadcoef(p); // Get coefficient of the leading term
369
+
370
+ // Check magnitude (handling positive and negative)
371
+ if (c > limit || c < -limit)
372
+ {
373
+ return(1);
374
+ }
375
+
376
+ // Strip the leading term to process the next one
377
+ p = p - lead(p);
378
+ }
379
+ }
380
+ return(0);
381
+ }