query-complexity 0.1.1__tar.gz → 0.1.2__tar.gz
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.
- {query_complexity-0.1.1/src/query_complexity.egg-info → query_complexity-0.1.2}/PKG-INFO +15 -7
- {query_complexity-0.1.1 → query_complexity-0.1.2}/README.md +14 -6
- {query_complexity-0.1.1 → query_complexity-0.1.2}/pyproject.toml +1 -1
- {query_complexity-0.1.1 → query_complexity-0.1.2}/src/query_complexity/__init__.py +1 -1
- {query_complexity-0.1.1 → query_complexity-0.1.2}/src/query_complexity/core.py +260 -122
- {query_complexity-0.1.1 → query_complexity-0.1.2/src/query_complexity.egg-info}/PKG-INFO +15 -7
- {query_complexity-0.1.1 → query_complexity-0.1.2}/tests/test_smoke.py +4 -4
- {query_complexity-0.1.1 → query_complexity-0.1.2}/LICENSE +0 -0
- {query_complexity-0.1.1 → query_complexity-0.1.2}/setup.cfg +0 -0
- {query_complexity-0.1.1 → query_complexity-0.1.2}/src/query_complexity.egg-info/SOURCES.txt +0 -0
- {query_complexity-0.1.1 → query_complexity-0.1.2}/src/query_complexity.egg-info/dependency_links.txt +0 -0
- {query_complexity-0.1.1 → query_complexity-0.1.2}/src/query_complexity.egg-info/requires.txt +0 -0
- {query_complexity-0.1.1 → query_complexity-0.1.2}/src/query_complexity.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: query-complexity
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: Adversary-bound SDP and space-efficient transducer tools for Boolean query complexity
|
|
5
5
|
Author: Ke Li
|
|
6
6
|
License: MIT
|
|
@@ -129,7 +129,8 @@ for r, data in blocks.items():
|
|
|
129
129
|
## Main API
|
|
130
130
|
|
|
131
131
|
- `solve_adversary_sdp(...)`: full one-vector adversary SDP for arbitrary Boolean domains.
|
|
132
|
-
- `
|
|
132
|
+
- `solve_symmetric_value(...)`: value-only rescaled primal Schrijver/Terwilliger SDP for symmetric functions.
|
|
133
|
+
- `solve_symmetric_orbit_sdp(...)`: compact dual/orbit SDP for symmetric functions, used when you want witnesses/transducers.
|
|
133
134
|
- `solve_symmetric_adversary_sdp(...)`: symmetric solver plus expansion to the Boolean input basis.
|
|
134
135
|
- `build_adversary_transducer(...)`: construct the input-independent transducer unitary.
|
|
135
136
|
- `adversary_query(...)`: build the input-dependent phase oracle on the witness space.
|
|
@@ -157,16 +158,23 @@ print(res.objective_value) # phase-normalized value T
|
|
|
157
158
|
print(res.usual_adversary_value) # usual adversary value 2T
|
|
158
159
|
```
|
|
159
160
|
|
|
160
|
-
For a symmetric function using Schrijver/Terwilliger
|
|
161
|
+
For a symmetric function using the rescaled primal Schrijver/Terwilliger SDP:
|
|
161
162
|
|
|
162
163
|
```python
|
|
163
|
-
res = qc.
|
|
164
|
+
res = qc.solve_symmetric_value(
|
|
164
165
|
10,
|
|
165
166
|
qc.THRESHOLD_layers(10, 4),
|
|
166
167
|
solver="MOSEK",
|
|
167
168
|
)
|
|
168
|
-
print(res
|
|
169
|
-
print(res
|
|
169
|
+
print(res["value"]) # usual adversary value Adv^pm(f)
|
|
170
|
+
print(res["objective_convention"]) # full symmetric primal objective
|
|
170
171
|
```
|
|
171
172
|
|
|
172
|
-
|
|
173
|
+
`solve_symmetric_value` uses the primal variables from the original rescaled solver:
|
|
174
|
+
|
|
175
|
+
```text
|
|
176
|
+
alpha_k = binom(n, k) beta_k
|
|
177
|
+
eta_{a,b,t} = N_{a,b,t} gamma_{a,b,t}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
and imposes PSD through Schrijver/Terwilliger blocks. It skips witness extraction and trace minimization.
|
|
@@ -95,7 +95,8 @@ for r, data in blocks.items():
|
|
|
95
95
|
## Main API
|
|
96
96
|
|
|
97
97
|
- `solve_adversary_sdp(...)`: full one-vector adversary SDP for arbitrary Boolean domains.
|
|
98
|
-
- `
|
|
98
|
+
- `solve_symmetric_value(...)`: value-only rescaled primal Schrijver/Terwilliger SDP for symmetric functions.
|
|
99
|
+
- `solve_symmetric_orbit_sdp(...)`: compact dual/orbit SDP for symmetric functions, used when you want witnesses/transducers.
|
|
99
100
|
- `solve_symmetric_adversary_sdp(...)`: symmetric solver plus expansion to the Boolean input basis.
|
|
100
101
|
- `build_adversary_transducer(...)`: construct the input-independent transducer unitary.
|
|
101
102
|
- `adversary_query(...)`: build the input-dependent phase oracle on the witness space.
|
|
@@ -123,16 +124,23 @@ print(res.objective_value) # phase-normalized value T
|
|
|
123
124
|
print(res.usual_adversary_value) # usual adversary value 2T
|
|
124
125
|
```
|
|
125
126
|
|
|
126
|
-
For a symmetric function using Schrijver/Terwilliger
|
|
127
|
+
For a symmetric function using the rescaled primal Schrijver/Terwilliger SDP:
|
|
127
128
|
|
|
128
129
|
```python
|
|
129
|
-
res = qc.
|
|
130
|
+
res = qc.solve_symmetric_value(
|
|
130
131
|
10,
|
|
131
132
|
qc.THRESHOLD_layers(10, 4),
|
|
132
133
|
solver="MOSEK",
|
|
133
134
|
)
|
|
134
|
-
print(res
|
|
135
|
-
print(res
|
|
135
|
+
print(res["value"]) # usual adversary value Adv^pm(f)
|
|
136
|
+
print(res["objective_convention"]) # full symmetric primal objective
|
|
136
137
|
```
|
|
137
138
|
|
|
138
|
-
|
|
139
|
+
`solve_symmetric_value` uses the primal variables from the original rescaled solver:
|
|
140
|
+
|
|
141
|
+
```text
|
|
142
|
+
alpha_k = binom(n, k) beta_k
|
|
143
|
+
eta_{a,b,t} = N_{a,b,t} gamma_{a,b,t}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
and imposes PSD through Schrijver/Terwilliger blocks. It skips witness extraction and trace minimization.
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "query-complexity"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.2"
|
|
8
8
|
description = "Adversary-bound SDP and space-efficient transducer tools for Boolean query complexity"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
@@ -577,156 +577,294 @@ def orbit_keys(N: int, allowed_layers: Iterable[int]) -> list[OrbitKey]:
|
|
|
577
577
|
return keys
|
|
578
578
|
|
|
579
579
|
|
|
580
|
-
def
|
|
580
|
+
def full_layer_size(n: int, k: int) -> int:
|
|
581
|
+
"""Number of Boolean inputs of length n with Hamming weight k."""
|
|
582
|
+
return binom_safe(int(n), int(k))
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
def num_pairs_in_orbit(n: int, a: int, b: int, t: int) -> int:
|
|
586
|
+
"""
|
|
587
|
+
Number of ordered pairs (x,y) with |x|=a, |y|=b, and |x\cap y|=t.
|
|
588
|
+
"""
|
|
589
|
+
n = int(n)
|
|
590
|
+
a = int(a)
|
|
591
|
+
b = int(b)
|
|
592
|
+
t = int(t)
|
|
593
|
+
if not (0 <= a <= n and 0 <= b <= n):
|
|
594
|
+
return 0
|
|
595
|
+
if not (max(0, a + b - n) <= t <= min(a, b)):
|
|
596
|
+
return 0
|
|
597
|
+
return binom_safe(n, a) * binom_safe(a, t) * binom_safe(n - a, b - t)
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
def gamma_objective_multiplicity(n: int, a: int, b: int, t: int) -> int:
|
|
601
|
+
"""Multiplicity of the stored symmetric gamma variable with a <= b."""
|
|
602
|
+
if a == b:
|
|
603
|
+
return num_pairs_in_orbit(n, a, b, t)
|
|
604
|
+
return num_pairs_in_orbit(n, a, b, t) + num_pairs_in_orbit(n, b, a, t)
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
def _gamma_key(a: int, b: int, t: int) -> tuple[int, int, int]:
|
|
608
|
+
aa, bb = min(int(a), int(b)), max(int(a), int(b))
|
|
609
|
+
return (aa, bb, int(t))
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
def _active_primal_key(key: tuple[int, int, int, int, int], layer_values: Mapping[int, int]) -> bool:
|
|
613
|
+
"""Whether an H-orbit coefficient can appear in M_1 = diag(beta)-Gamma o Delta_1."""
|
|
614
|
+
x1, y1, a, b, t = key
|
|
615
|
+
if x1 == y1 and a == b == t:
|
|
616
|
+
return True
|
|
617
|
+
if x1 != y1 and layer_values[a] != layer_values[b]:
|
|
618
|
+
return True
|
|
619
|
+
return False
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
def _split_components_by_active_pattern(block: Mapping[str, Any], layer_values: Mapping[int, int]) -> list[list[int]]:
|
|
623
|
+
size = int(block["size"])
|
|
624
|
+
adj = [set([i]) for i in range(size)]
|
|
625
|
+
for row, col, key, coeff in block["terms"]:
|
|
626
|
+
if coeff != 0.0 and _active_primal_key(key, layer_values):
|
|
627
|
+
adj[row].add(col)
|
|
628
|
+
adj[col].add(row)
|
|
629
|
+
|
|
630
|
+
seen = [False] * size
|
|
631
|
+
components: list[list[int]] = []
|
|
632
|
+
for v in range(size):
|
|
633
|
+
if seen[v]:
|
|
634
|
+
continue
|
|
635
|
+
stack = [v]
|
|
636
|
+
seen[v] = True
|
|
637
|
+
comp: list[int] = []
|
|
638
|
+
while stack:
|
|
639
|
+
u = stack.pop()
|
|
640
|
+
comp.append(u)
|
|
641
|
+
for w in adj[u]:
|
|
642
|
+
if not seen[w]:
|
|
643
|
+
seen[w] = True
|
|
644
|
+
stack.append(w)
|
|
645
|
+
components.append(sorted(comp))
|
|
646
|
+
return components
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
def _primal_terwilliger_blocks(n: int, allowed_layers: Iterable[int]) -> list[dict[str, Any]]:
|
|
650
|
+
"""
|
|
651
|
+
Schrijver/Terwilliger blocks for the primal matrix
|
|
652
|
+
M_1 = diag(beta) - Gamma o Delta_1.
|
|
653
|
+
"""
|
|
654
|
+
allowed_set = set(int(s) for s in allowed_layers)
|
|
655
|
+
m = int(n) - 1
|
|
656
|
+
blocks: list[dict[str, Any]] = []
|
|
657
|
+
for r in range(m // 2 + 1):
|
|
658
|
+
labels = [
|
|
659
|
+
(eps, i)
|
|
660
|
+
for eps in (0, 1)
|
|
661
|
+
for i in range(r, m - r + 1)
|
|
662
|
+
if eps + i in allowed_set
|
|
663
|
+
]
|
|
664
|
+
if not labels:
|
|
665
|
+
continue
|
|
666
|
+
terms: list[tuple[int, int, tuple[int, int, int, int, int], float]] = []
|
|
667
|
+
for row, (x1, i) in enumerate(labels):
|
|
668
|
+
for col, (y1, j) in enumerate(labels):
|
|
669
|
+
for tau in valid_intersections(i, j, m):
|
|
670
|
+
coeff = terwilliger_coeff(m, i, j, r, tau)
|
|
671
|
+
if coeff == 0.0:
|
|
672
|
+
continue
|
|
673
|
+
a = x1 + i
|
|
674
|
+
b = y1 + j
|
|
675
|
+
t = x1 * y1 + tau
|
|
676
|
+
terms.append((row, col, (x1, y1, a, b, t), float(coeff)))
|
|
677
|
+
blocks.append({"r": r, "indices": labels, "size": len(labels), "terms": terms})
|
|
678
|
+
return blocks
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
def _coefficient_scale_for_primal_term(n: int, kind: str, data: Any) -> float:
|
|
682
|
+
"""Denominator converting rescaled primal variables back to beta/gamma."""
|
|
683
|
+
if kind == "beta":
|
|
684
|
+
return float(full_layer_size(n, int(data)))
|
|
685
|
+
if kind == "gamma":
|
|
686
|
+
mult = gamma_objective_multiplicity(n, *data)
|
|
687
|
+
if mult <= 0:
|
|
688
|
+
raise ValueError(f"zero gamma multiplicity for {data}")
|
|
689
|
+
return float(mult)
|
|
690
|
+
raise ValueError(f"unknown term kind {kind!r}")
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
def _primal_block_row_scales(
|
|
694
|
+
n: int,
|
|
695
|
+
m_block: int,
|
|
696
|
+
terms: list[tuple[int, int, float, str, Any]],
|
|
697
|
+
enabled: bool,
|
|
698
|
+
) -> list[float]:
|
|
699
|
+
"""Heuristic diagonal congruence scaling DBD for numerical conditioning."""
|
|
700
|
+
if not enabled:
|
|
701
|
+
return [1.0] * m_block
|
|
702
|
+
row_mass = [0.0] * m_block
|
|
703
|
+
for row, col, coeff, kind, data in terms:
|
|
704
|
+
denom = _coefficient_scale_for_primal_term(n, kind, data)
|
|
705
|
+
c = abs(float(coeff)) / denom
|
|
706
|
+
row_mass[row] += c
|
|
707
|
+
if col != row:
|
|
708
|
+
row_mass[col] += c
|
|
709
|
+
scales = []
|
|
710
|
+
for mass in row_mass:
|
|
711
|
+
scales.append(1.0 if mass <= 0 else 1.0 / sqrt(max(mass, 1e-300)))
|
|
712
|
+
return scales
|
|
713
|
+
|
|
714
|
+
|
|
715
|
+
def solve_symmetric_value(
|
|
581
716
|
n: int,
|
|
582
717
|
layer_values: Mapping[int, Any],
|
|
583
718
|
*,
|
|
584
719
|
solver: str | None = None,
|
|
585
720
|
solver_kwargs: Mapping[str, Any] | None = None,
|
|
586
721
|
verbose: bool = False,
|
|
587
|
-
|
|
722
|
+
split_blocks: bool = True,
|
|
723
|
+
diagonal_blocks_as_inequalities: bool = True,
|
|
724
|
+
balance_psd_blocks: bool = True,
|
|
725
|
+
) -> dict[str, Any]:
|
|
588
726
|
"""
|
|
589
|
-
Solve
|
|
727
|
+
Solve the rescaled primal adversary SDP for a symmetric Boolean function.
|
|
590
728
|
|
|
591
|
-
This is the
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
trace minimization fails.
|
|
729
|
+
This is the Schrijver/Terwilliger block-diagonalized primal SDP. It returns
|
|
730
|
+
only the value and primal variables; it does not run trace minimization,
|
|
731
|
+
does not construct one-vector witnesses, and does not build a transducer.
|
|
595
732
|
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
733
|
+
Variables are rescaled exactly as in the old rescaled implementation:
|
|
734
|
+
|
|
735
|
+
alpha_k = C(n,k) beta_k
|
|
736
|
+
eta_{a,b,t} = N_{a,b,t} gamma_{a,b,t}
|
|
737
|
+
|
|
738
|
+
where N_{a,b,t} is the ordered-pair multiplicity in the full objective.
|
|
739
|
+
Therefore the objective is simply sum eta, and the returned ``value`` is
|
|
740
|
+
the usual adversary bound Adv^pm(f), not the phase-normalized half-value.
|
|
599
741
|
"""
|
|
600
742
|
require_cvxpy()
|
|
601
743
|
n = int(n)
|
|
602
|
-
if n
|
|
603
|
-
raise ValueError("n must be
|
|
604
|
-
|
|
744
|
+
if n < 0:
|
|
745
|
+
raise ValueError("n must be non-negative")
|
|
746
|
+
|
|
605
747
|
lv = normalize_layer_values(layer_values)
|
|
748
|
+
if not lv:
|
|
749
|
+
raise ValueError("layer_values must be non-empty")
|
|
606
750
|
allowed = tuple(sorted(lv.keys()))
|
|
607
|
-
allowed_set = set(allowed)
|
|
608
|
-
if len({lv[s] for s in allowed}) < 2:
|
|
609
|
-
raise ValueError("function/promise must contain both outputs")
|
|
610
751
|
if min(allowed) < 0 or max(allowed) > n:
|
|
611
752
|
raise ValueError("layer outside 0..n")
|
|
753
|
+
zero_layers = [s for s in allowed if lv[s] == 0]
|
|
754
|
+
one_layers = [s for s in allowed if lv[s] == 1]
|
|
755
|
+
if not zero_layers or not one_layers:
|
|
756
|
+
raise ValueError("function/promise must contain both outputs")
|
|
612
757
|
|
|
613
|
-
|
|
614
|
-
key_set = set(keys)
|
|
615
|
-
var_map = {
|
|
616
|
-
key: cp.Variable(name=f"x_{key[0]}_{key[1]}_{key[2]}_{key[3]}_{key[4]}")
|
|
617
|
-
for key in keys
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
def xexpr(key: OrbitKey):
|
|
621
|
-
if key not in var_map:
|
|
622
|
-
raise KeyError(f"invalid orbit key {key}")
|
|
623
|
-
return var_map[key]
|
|
758
|
+
alpha = {s: cp.Variable(nonneg=True, name=f"alpha_{s}") for s in allowed}
|
|
624
759
|
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
760
|
+
gamma_keys: set[tuple[int, int, int]] = set()
|
|
761
|
+
for a in allowed:
|
|
762
|
+
for b in allowed:
|
|
763
|
+
if lv[a] == lv[b]:
|
|
764
|
+
continue
|
|
765
|
+
aa, bb = min(a, b), max(a, b)
|
|
766
|
+
for t in range(max(0, aa + bb - n), min(aa, bb) + 1):
|
|
767
|
+
gamma_keys.add((aa, bb, t))
|
|
768
|
+
gamma_keys_sorted = sorted(gamma_keys)
|
|
769
|
+
eta = {key: cp.Variable(name=f"eta_{key[0]}_{key[1]}_{key[2]}") for key in gamma_keys_sorted}
|
|
770
|
+
|
|
771
|
+
constraints: list[Any] = [
|
|
772
|
+
cp.sum([alpha[s] for s in zero_layers]) == 0.5,
|
|
773
|
+
cp.sum([alpha[s] for s in one_layers]) == 0.5,
|
|
774
|
+
]
|
|
775
|
+
|
|
776
|
+
raw_blocks = _primal_terwilliger_blocks(n, allowed)
|
|
777
|
+
final_block_sizes: list[int] = []
|
|
778
|
+
psd_cone_sizes: list[int] = []
|
|
779
|
+
scalar_inequality_count = 0
|
|
780
|
+
|
|
781
|
+
for block in raw_blocks:
|
|
782
|
+
components = _split_components_by_active_pattern(block, lv) if split_blocks else [list(range(int(block["size"])))]
|
|
783
|
+
for comp in components:
|
|
784
|
+
local = {old: new for new, old in enumerate(comp)}
|
|
785
|
+
active_terms: list[tuple[int, int, float, str, Any]] = []
|
|
786
|
+
|
|
787
|
+
for row, col, key, coeff in block["terms"]:
|
|
788
|
+
if row not in local or col not in local:
|
|
789
|
+
continue
|
|
790
|
+
x1, y1, a, b, t = key
|
|
791
|
+
if x1 == y1 and a == b == t:
|
|
792
|
+
active_terms.append((local[row], local[col], float(coeff), "beta", int(a)))
|
|
793
|
+
elif x1 != y1 and lv[a] != lv[b]:
|
|
794
|
+
active_terms.append((local[row], local[col], -float(coeff), "gamma", _gamma_key(a, b, t)))
|
|
634
795
|
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
if alpha != beta:
|
|
638
|
-
expr += xexpr((alpha, beta, a, b, t))
|
|
639
|
-
if a - t > 0:
|
|
640
|
-
expr += (a - t) * xexpr((1, 0, alpha + a - 1, beta + b, alpha * beta + t))
|
|
641
|
-
if b - t > 0:
|
|
642
|
-
expr += (b - t) * xexpr((0, 1, alpha + a, beta + b - 1, alpha * beta + t))
|
|
643
|
-
return expr
|
|
796
|
+
if not active_terms:
|
|
797
|
+
continue
|
|
644
798
|
|
|
645
|
-
|
|
799
|
+
m_block = len(comp)
|
|
800
|
+
row_scales = _primal_block_row_scales(n, m_block, active_terms, balance_psd_blocks)
|
|
801
|
+
final_block_sizes.append(m_block)
|
|
802
|
+
|
|
803
|
+
if diagonal_blocks_as_inequalities and all(row == col for row, col, *_ in active_terms):
|
|
804
|
+
diag_entries = [0 for _ in range(m_block)]
|
|
805
|
+
for row, _col, coeff, kind, data in active_terms:
|
|
806
|
+
var = alpha[int(data)] if kind == "beta" else eta[data]
|
|
807
|
+
denom = _coefficient_scale_for_primal_term(n, kind, data)
|
|
808
|
+
c = float(coeff) / denom
|
|
809
|
+
c *= row_scales[row] * row_scales[row]
|
|
810
|
+
diag_entries[row] = diag_entries[row] + c * var
|
|
811
|
+
for expr in diag_entries:
|
|
812
|
+
if isinstance(expr, Number) and abs(float(expr)) == 0.0:
|
|
813
|
+
continue
|
|
814
|
+
constraints.append(expr >= 0)
|
|
815
|
+
scalar_inequality_count += 1
|
|
816
|
+
continue
|
|
646
817
|
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
818
|
+
entries = [[0 for _ in range(m_block)] for __ in range(m_block)]
|
|
819
|
+
for row, col, coeff, kind, data in active_terms:
|
|
820
|
+
var = alpha[int(data)] if kind == "beta" else eta[data]
|
|
821
|
+
denom = _coefficient_scale_for_primal_term(n, kind, data)
|
|
822
|
+
c = float(coeff) / denom
|
|
823
|
+
c *= row_scales[row] * row_scales[col]
|
|
824
|
+
entries[row][col] = entries[row][col] + c * var
|
|
653
825
|
|
|
654
|
-
# Schrijver/Terwilliger PSD blocks.
|
|
655
|
-
for r in range(N // 2 + 1):
|
|
656
|
-
labels = [
|
|
657
|
-
(alpha, a)
|
|
658
|
-
for alpha in (0, 1)
|
|
659
|
-
for a in range(r, N - r + 1)
|
|
660
|
-
if alpha + a in allowed_set
|
|
661
|
-
]
|
|
662
|
-
if not labels:
|
|
663
|
-
continue
|
|
664
|
-
entries = [[0 for _ in labels] for __ in labels]
|
|
665
|
-
has_term = False
|
|
666
|
-
for row, (alpha, a) in enumerate(labels):
|
|
667
|
-
for col, (beta, b) in enumerate(labels):
|
|
668
|
-
e = 0
|
|
669
|
-
for t in valid_intersections(a, b, N):
|
|
670
|
-
key = (alpha, beta, a, b, t)
|
|
671
|
-
if key not in key_set:
|
|
672
|
-
continue
|
|
673
|
-
coeff = terwilliger_coeff(N, a, b, r, t)
|
|
674
|
-
if coeff != 0.0:
|
|
675
|
-
e = e + coeff * xexpr(key)
|
|
676
|
-
has_term = True
|
|
677
|
-
entries[row][col] = e
|
|
678
|
-
if has_term:
|
|
679
826
|
B = scalar_bmat(entries)
|
|
680
827
|
B = 0.5 * (B + B.T)
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
if alpha + a in allowed_set:
|
|
688
|
-
constraints.append(cost_expr(alpha, a) <= T)
|
|
689
|
-
|
|
690
|
-
for alpha in (0, 1):
|
|
691
|
-
for beta in (0, 1):
|
|
692
|
-
for a in range(N + 1):
|
|
693
|
-
if alpha + a not in allowed_set:
|
|
694
|
-
continue
|
|
695
|
-
for b in range(N + 1):
|
|
696
|
-
if beta + b not in allowed_set:
|
|
697
|
-
continue
|
|
698
|
-
for t in valid_intersections(a, b, N):
|
|
699
|
-
rhs = 1.0 if lv[alpha + a] != lv[beta + b] else 0.0
|
|
700
|
-
constraints.append(equality_lhs(alpha, beta, a, b, t) == rhs)
|
|
828
|
+
if m_block == 1:
|
|
829
|
+
constraints.append(B[0, 0] >= 0)
|
|
830
|
+
scalar_inequality_count += 1
|
|
831
|
+
else:
|
|
832
|
+
constraints.append(B >> 0)
|
|
833
|
+
psd_cone_sizes.append(m_block)
|
|
701
834
|
|
|
702
|
-
|
|
835
|
+
objective = cp.sum([eta[key] for key in gamma_keys_sorted])
|
|
836
|
+
problem = cp.Problem(cp.Maximize(objective), constraints)
|
|
703
837
|
value = float(solve_problem(problem, solver, solver_kwargs, verbose))
|
|
704
838
|
if problem.status not in {cp.OPTIMAL, cp.OPTIMAL_INACCURATE}:
|
|
705
|
-
raise RuntimeError(f"symmetric
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
839
|
+
raise RuntimeError(f"symmetric primal value SDP failed with status {problem.status}")
|
|
840
|
+
|
|
841
|
+
alpha_values = {k: None if v.value is None else float(v.value) for k, v in alpha.items()}
|
|
842
|
+
eta_values = {key: None if v.value is None else float(v.value) for key, v in eta.items()}
|
|
843
|
+
beta_values = {k: None if alpha_values[k] is None else alpha_values[k] / float(full_layer_size(n, k)) for k in alpha}
|
|
844
|
+
gamma_values = {
|
|
845
|
+
key: None if eta_values[key] is None else eta_values[key] / float(gamma_objective_multiplicity(n, *key))
|
|
846
|
+
for key in eta
|
|
847
|
+
}
|
|
713
848
|
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
849
|
+
return {
|
|
850
|
+
"value": value,
|
|
851
|
+
"usual_adversary_value": value,
|
|
852
|
+
"objective_convention": "usual_adversary_full_symmetric_sum",
|
|
853
|
+
"status": str(problem.status),
|
|
854
|
+
"alpha": alpha_values,
|
|
855
|
+
"eta": eta_values,
|
|
856
|
+
"beta": beta_values,
|
|
857
|
+
"gamma": gamma_values,
|
|
858
|
+
"allowed_layers": allowed,
|
|
859
|
+
"block_sizes": final_block_sizes,
|
|
860
|
+
"psd_cone_sizes": psd_cone_sizes,
|
|
861
|
+
"scalar_inequality_count": scalar_inequality_count,
|
|
862
|
+
"num_blocks": len(final_block_sizes),
|
|
863
|
+
"num_psd_cones": len(psd_cone_sizes),
|
|
864
|
+
"num_alpha_variables": len(alpha),
|
|
865
|
+
"num_eta_variables": len(eta),
|
|
866
|
+
"solver_stats": problem.solver_stats,
|
|
867
|
+
}
|
|
730
868
|
|
|
731
869
|
|
|
732
870
|
def solve_symmetric_orbit_sdp(
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: query-complexity
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: Adversary-bound SDP and space-efficient transducer tools for Boolean query complexity
|
|
5
5
|
Author: Ke Li
|
|
6
6
|
License: MIT
|
|
@@ -129,7 +129,8 @@ for r, data in blocks.items():
|
|
|
129
129
|
## Main API
|
|
130
130
|
|
|
131
131
|
- `solve_adversary_sdp(...)`: full one-vector adversary SDP for arbitrary Boolean domains.
|
|
132
|
-
- `
|
|
132
|
+
- `solve_symmetric_value(...)`: value-only rescaled primal Schrijver/Terwilliger SDP for symmetric functions.
|
|
133
|
+
- `solve_symmetric_orbit_sdp(...)`: compact dual/orbit SDP for symmetric functions, used when you want witnesses/transducers.
|
|
133
134
|
- `solve_symmetric_adversary_sdp(...)`: symmetric solver plus expansion to the Boolean input basis.
|
|
134
135
|
- `build_adversary_transducer(...)`: construct the input-independent transducer unitary.
|
|
135
136
|
- `adversary_query(...)`: build the input-dependent phase oracle on the witness space.
|
|
@@ -157,16 +158,23 @@ print(res.objective_value) # phase-normalized value T
|
|
|
157
158
|
print(res.usual_adversary_value) # usual adversary value 2T
|
|
158
159
|
```
|
|
159
160
|
|
|
160
|
-
For a symmetric function using Schrijver/Terwilliger
|
|
161
|
+
For a symmetric function using the rescaled primal Schrijver/Terwilliger SDP:
|
|
161
162
|
|
|
162
163
|
```python
|
|
163
|
-
res = qc.
|
|
164
|
+
res = qc.solve_symmetric_value(
|
|
164
165
|
10,
|
|
165
166
|
qc.THRESHOLD_layers(10, 4),
|
|
166
167
|
solver="MOSEK",
|
|
167
168
|
)
|
|
168
|
-
print(res
|
|
169
|
-
print(res
|
|
169
|
+
print(res["value"]) # usual adversary value Adv^pm(f)
|
|
170
|
+
print(res["objective_convention"]) # full symmetric primal objective
|
|
170
171
|
```
|
|
171
172
|
|
|
172
|
-
|
|
173
|
+
`solve_symmetric_value` uses the primal variables from the original rescaled solver:
|
|
174
|
+
|
|
175
|
+
```text
|
|
176
|
+
alpha_k = binom(n, k) beta_k
|
|
177
|
+
eta_{a,b,t} = N_{a,b,t} gamma_{a,b,t}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
and imposes PSD through Schrijver/Terwilliger blocks. It skips witness extraction and trace minimization.
|
|
@@ -13,7 +13,7 @@ def test_new_api_names():
|
|
|
13
13
|
assert not hasattr(qc, "solve_phase_sdp")
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
def
|
|
17
|
-
assert hasattr(qc, "
|
|
18
|
-
assert hasattr(qc, "solve_symmetric_orbit_value_sdp")
|
|
19
|
-
assert hasattr(qc, "solve_symmetric_adversary_value_sdp")
|
|
16
|
+
def test_symmetric_value_api_name():
|
|
17
|
+
assert hasattr(qc, "solve_symmetric_value")
|
|
18
|
+
assert not hasattr(qc, "solve_symmetric_orbit_value_sdp")
|
|
19
|
+
assert not hasattr(qc, "solve_symmetric_adversary_value_sdp")
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{query_complexity-0.1.1 → query_complexity-0.1.2}/src/query_complexity.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
{query_complexity-0.1.1 → query_complexity-0.1.2}/src/query_complexity.egg-info/requires.txt
RENAMED
|
File without changes
|
{query_complexity-0.1.1 → query_complexity-0.1.2}/src/query_complexity.egg-info/top_level.txt
RENAMED
|
File without changes
|