query-complexity 0.1.0__tar.gz → 0.1.1__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.0/src/query_complexity.egg-info → query_complexity-0.1.1}/PKG-INFO +33 -1
- {query_complexity-0.1.0 → query_complexity-0.1.1}/README.md +32 -0
- {query_complexity-0.1.0 → query_complexity-0.1.1}/pyproject.toml +1 -1
- {query_complexity-0.1.0 → query_complexity-0.1.1}/src/query_complexity/__init__.py +1 -1
- {query_complexity-0.1.0 → query_complexity-0.1.1}/src/query_complexity/core.py +216 -0
- {query_complexity-0.1.0 → query_complexity-0.1.1/src/query_complexity.egg-info}/PKG-INFO +33 -1
- {query_complexity-0.1.0 → query_complexity-0.1.1}/tests/test_smoke.py +6 -0
- {query_complexity-0.1.0 → query_complexity-0.1.1}/LICENSE +0 -0
- {query_complexity-0.1.0 → query_complexity-0.1.1}/setup.cfg +0 -0
- {query_complexity-0.1.0 → query_complexity-0.1.1}/src/query_complexity.egg-info/SOURCES.txt +0 -0
- {query_complexity-0.1.0 → query_complexity-0.1.1}/src/query_complexity.egg-info/dependency_links.txt +0 -0
- {query_complexity-0.1.0 → query_complexity-0.1.1}/src/query_complexity.egg-info/requires.txt +0 -0
- {query_complexity-0.1.0 → query_complexity-0.1.1}/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.1
|
|
4
4
|
Summary: Adversary-bound SDP and space-efficient transducer tools for Boolean query complexity
|
|
5
5
|
Author: Ke Li
|
|
6
6
|
License: MIT
|
|
@@ -138,3 +138,35 @@ for r, data in blocks.items():
|
|
|
138
138
|
- `qiskit_adversary_circuit(...)`: optional dense-unitary Qiskit circuit export.
|
|
139
139
|
- `analytic_or_solution(...)`: closed-form adversary solution for OR.
|
|
140
140
|
- `threshold_eta_blocks(...)`: closed-form rank-one Schrijver blocks for thresholds.
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
## Value-only SDP solvers
|
|
144
|
+
|
|
145
|
+
If you only need the adversary bound and do not want the second trace-minimization pass, use the value-only APIs.
|
|
146
|
+
|
|
147
|
+
For a general Boolean function:
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
import query_complexity as qc
|
|
151
|
+
|
|
152
|
+
inputs = qc.all_binary_inputs(3)
|
|
153
|
+
outputs = [int(any(x)) for x in inputs]
|
|
154
|
+
|
|
155
|
+
res = qc.solve_adversary_value_sdp(inputs, outputs, solver="MOSEK")
|
|
156
|
+
print(res.objective_value) # phase-normalized value T
|
|
157
|
+
print(res.usual_adversary_value) # usual adversary value 2T
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
For a symmetric function using Schrijver/Terwilliger blocks:
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
res = qc.solve_symmetric_orbit_value_sdp(
|
|
164
|
+
10,
|
|
165
|
+
qc.THRESHOLD_layers(10, 4),
|
|
166
|
+
solver="MOSEK",
|
|
167
|
+
)
|
|
168
|
+
print(res.objective_value)
|
|
169
|
+
print(res.usual_adversary_value)
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
These functions skip witness extraction and trace minimization.
|
|
@@ -104,3 +104,35 @@ for r, data in blocks.items():
|
|
|
104
104
|
- `qiskit_adversary_circuit(...)`: optional dense-unitary Qiskit circuit export.
|
|
105
105
|
- `analytic_or_solution(...)`: closed-form adversary solution for OR.
|
|
106
106
|
- `threshold_eta_blocks(...)`: closed-form rank-one Schrijver blocks for thresholds.
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
## Value-only SDP solvers
|
|
110
|
+
|
|
111
|
+
If you only need the adversary bound and do not want the second trace-minimization pass, use the value-only APIs.
|
|
112
|
+
|
|
113
|
+
For a general Boolean function:
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
import query_complexity as qc
|
|
117
|
+
|
|
118
|
+
inputs = qc.all_binary_inputs(3)
|
|
119
|
+
outputs = [int(any(x)) for x in inputs]
|
|
120
|
+
|
|
121
|
+
res = qc.solve_adversary_value_sdp(inputs, outputs, solver="MOSEK")
|
|
122
|
+
print(res.objective_value) # phase-normalized value T
|
|
123
|
+
print(res.usual_adversary_value) # usual adversary value 2T
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
For a symmetric function using Schrijver/Terwilliger blocks:
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
res = qc.solve_symmetric_orbit_value_sdp(
|
|
130
|
+
10,
|
|
131
|
+
qc.THRESHOLD_layers(10, 4),
|
|
132
|
+
solver="MOSEK",
|
|
133
|
+
)
|
|
134
|
+
print(res.objective_value)
|
|
135
|
+
print(res.usual_adversary_value)
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
These functions skip 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.1"
|
|
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"
|
|
@@ -95,6 +95,15 @@ class OrbitSolution:
|
|
|
95
95
|
input_costs_by_layer: dict[int, float]
|
|
96
96
|
|
|
97
97
|
|
|
98
|
+
@dataclass(frozen=True)
|
|
99
|
+
class SdpValueResult:
|
|
100
|
+
"""Value-only SDP result, with no trace minimization or witness extraction."""
|
|
101
|
+
|
|
102
|
+
objective_value: float # phase-normalized adversary value T
|
|
103
|
+
usual_adversary_value: float # standard adversary value 2T
|
|
104
|
+
status: str
|
|
105
|
+
|
|
106
|
+
|
|
98
107
|
@dataclass(frozen=True)
|
|
99
108
|
class AdversaryTransducer:
|
|
100
109
|
"""Input-independent adversary transducer unitary."""
|
|
@@ -361,6 +370,61 @@ def normalize_instance(
|
|
|
361
370
|
return xs, ys
|
|
362
371
|
|
|
363
372
|
|
|
373
|
+
def solve_adversary_value_sdp(
|
|
374
|
+
inputs: Sequence[Sequence[Any]],
|
|
375
|
+
outputs: Sequence[Any] | None = None,
|
|
376
|
+
f: Callable[[tuple[int, ...]], Any] | None = None,
|
|
377
|
+
*,
|
|
378
|
+
solver: str | None = None,
|
|
379
|
+
solver_kwargs: Mapping[str, Any] | None = None,
|
|
380
|
+
verbose: bool = False,
|
|
381
|
+
) -> SdpValueResult:
|
|
382
|
+
"""
|
|
383
|
+
Solve only the minimax adversary SDP value for a general Boolean function.
|
|
384
|
+
|
|
385
|
+
This runs only the first pass of :func:`solve_adversary_sdp`. It does not
|
|
386
|
+
perform trace minimization and does not factor Gram matrices, so use it when
|
|
387
|
+
you only need the adversary bound or when the trace-minimization pass is
|
|
388
|
+
numerically fragile.
|
|
389
|
+
|
|
390
|
+
Returns:
|
|
391
|
+
SdpValueResult where ``objective_value`` is the phase-normalized value
|
|
392
|
+
T and ``usual_adversary_value`` is ``2*T``.
|
|
393
|
+
"""
|
|
394
|
+
require_cvxpy()
|
|
395
|
+
xs, ys = normalize_instance(inputs, outputs, f)
|
|
396
|
+
m = len(xs)
|
|
397
|
+
n = len(xs[0])
|
|
398
|
+
|
|
399
|
+
X = [cp.Variable((m, m), symmetric=True, name=f"X_{i}") for i in range(n)]
|
|
400
|
+
T = cp.Variable(name="T")
|
|
401
|
+
|
|
402
|
+
constraints: list[Any] = [T >= 0]
|
|
403
|
+
constraints.extend(Xi >> 0 for Xi in X)
|
|
404
|
+
|
|
405
|
+
for x_idx in range(m):
|
|
406
|
+
cost = 0.5 * cp.sum([X[i][x_idx, x_idx] for i in range(n)])
|
|
407
|
+
constraints.append(cost <= T)
|
|
408
|
+
|
|
409
|
+
for a, x in enumerate(xs):
|
|
410
|
+
for b, y in enumerate(xs):
|
|
411
|
+
terms = [X[i][a, b] for i in range(n) if x[i] != y[i]]
|
|
412
|
+
lhs = cp.sum(terms) if terms else 0
|
|
413
|
+
rhs = 1.0 if ys[a] != ys[b] else 0.0
|
|
414
|
+
constraints.append(lhs == rhs)
|
|
415
|
+
|
|
416
|
+
problem = cp.Problem(cp.Minimize(T), constraints)
|
|
417
|
+
value = float(solve_problem(problem, solver, solver_kwargs, verbose))
|
|
418
|
+
if problem.status not in {cp.OPTIMAL, cp.OPTIMAL_INACCURATE}:
|
|
419
|
+
raise RuntimeError(f"adversary value SDP failed with status {problem.status}")
|
|
420
|
+
|
|
421
|
+
return SdpValueResult(
|
|
422
|
+
objective_value=value,
|
|
423
|
+
usual_adversary_value=2.0 * value,
|
|
424
|
+
status=str(problem.status),
|
|
425
|
+
)
|
|
426
|
+
|
|
427
|
+
|
|
364
428
|
def solve_adversary_sdp(
|
|
365
429
|
inputs: Sequence[Sequence[Any]],
|
|
366
430
|
outputs: Sequence[Any] | None = None,
|
|
@@ -513,6 +577,158 @@ def orbit_keys(N: int, allowed_layers: Iterable[int]) -> list[OrbitKey]:
|
|
|
513
577
|
return keys
|
|
514
578
|
|
|
515
579
|
|
|
580
|
+
def solve_symmetric_orbit_value_sdp(
|
|
581
|
+
n: int,
|
|
582
|
+
layer_values: Mapping[int, Any],
|
|
583
|
+
*,
|
|
584
|
+
solver: str | None = None,
|
|
585
|
+
solver_kwargs: Mapping[str, Any] | None = None,
|
|
586
|
+
verbose: bool = False,
|
|
587
|
+
) -> SdpValueResult:
|
|
588
|
+
"""
|
|
589
|
+
Solve only the Schrijver/Terwilliger orbit-reduced adversary SDP value.
|
|
590
|
+
|
|
591
|
+
This is the symmetric-function value-only solver. It runs only the minimax
|
|
592
|
+
pass, imposes PSD through Schrijver blocks, and skips the second
|
|
593
|
+
trace-minimization pass. It is useful when you only need the bound or when
|
|
594
|
+
trace minimization fails.
|
|
595
|
+
|
|
596
|
+
Returns:
|
|
597
|
+
SdpValueResult where ``objective_value`` is the phase-normalized value
|
|
598
|
+
T and ``usual_adversary_value`` is ``2*T``.
|
|
599
|
+
"""
|
|
600
|
+
require_cvxpy()
|
|
601
|
+
n = int(n)
|
|
602
|
+
if n <= 0:
|
|
603
|
+
raise ValueError("n must be positive")
|
|
604
|
+
N = n - 1
|
|
605
|
+
lv = normalize_layer_values(layer_values)
|
|
606
|
+
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
|
+
if min(allowed) < 0 or max(allowed) > n:
|
|
611
|
+
raise ValueError("layer outside 0..n")
|
|
612
|
+
|
|
613
|
+
keys = orbit_keys(N, allowed)
|
|
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]
|
|
624
|
+
|
|
625
|
+
def cost_expr(alpha: int, a: int):
|
|
626
|
+
expr = xexpr((alpha, alpha, a, a, a))
|
|
627
|
+
if a > 0:
|
|
628
|
+
s = alpha + a - 1
|
|
629
|
+
expr += a * xexpr((1, 1, s, s, s))
|
|
630
|
+
if N - a > 0:
|
|
631
|
+
s = alpha + a
|
|
632
|
+
expr += (N - a) * xexpr((0, 0, s, s, s))
|
|
633
|
+
return 0.5 * expr
|
|
634
|
+
|
|
635
|
+
def equality_lhs(alpha: int, beta: int, a: int, b: int, t: int):
|
|
636
|
+
expr = 0
|
|
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
|
|
644
|
+
|
|
645
|
+
constraints: list[Any] = []
|
|
646
|
+
|
|
647
|
+
# Gram symmetry X[x,y] = X[y,x].
|
|
648
|
+
for key in keys:
|
|
649
|
+
alpha, beta, a, b, t = key
|
|
650
|
+
kt = (beta, alpha, b, a, t)
|
|
651
|
+
if kt in key_set and key <= kt:
|
|
652
|
+
constraints.append(xexpr(key) == xexpr(kt))
|
|
653
|
+
|
|
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
|
+
B = scalar_bmat(entries)
|
|
680
|
+
B = 0.5 * (B + B.T)
|
|
681
|
+
constraints.append(B[0, 0] >= 0 if len(labels) == 1 else B >> 0)
|
|
682
|
+
|
|
683
|
+
T = cp.Variable(name="T")
|
|
684
|
+
constraints.append(T >= 0)
|
|
685
|
+
for alpha in (0, 1):
|
|
686
|
+
for a in range(N + 1):
|
|
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)
|
|
701
|
+
|
|
702
|
+
problem = cp.Problem(cp.Minimize(T), constraints)
|
|
703
|
+
value = float(solve_problem(problem, solver, solver_kwargs, verbose))
|
|
704
|
+
if problem.status not in {cp.OPTIMAL, cp.OPTIMAL_INACCURATE}:
|
|
705
|
+
raise RuntimeError(f"symmetric orbit value SDP failed with status {problem.status}")
|
|
706
|
+
|
|
707
|
+
return SdpValueResult(
|
|
708
|
+
objective_value=value,
|
|
709
|
+
usual_adversary_value=2.0 * value,
|
|
710
|
+
status=str(problem.status),
|
|
711
|
+
)
|
|
712
|
+
|
|
713
|
+
|
|
714
|
+
def solve_symmetric_adversary_value_sdp(
|
|
715
|
+
n: int,
|
|
716
|
+
layer_values: Mapping[int, Any],
|
|
717
|
+
*,
|
|
718
|
+
solver: str | None = None,
|
|
719
|
+
solver_kwargs: Mapping[str, Any] | None = None,
|
|
720
|
+
verbose: bool = False,
|
|
721
|
+
) -> SdpValueResult:
|
|
722
|
+
"""Alias for :func:`solve_symmetric_orbit_value_sdp`."""
|
|
723
|
+
return solve_symmetric_orbit_value_sdp(
|
|
724
|
+
n,
|
|
725
|
+
layer_values,
|
|
726
|
+
solver=solver,
|
|
727
|
+
solver_kwargs=solver_kwargs,
|
|
728
|
+
verbose=verbose,
|
|
729
|
+
)
|
|
730
|
+
|
|
731
|
+
|
|
516
732
|
def solve_symmetric_orbit_sdp(
|
|
517
733
|
n: int,
|
|
518
734
|
layer_values: Mapping[int, Any],
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: query-complexity
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.1
|
|
4
4
|
Summary: Adversary-bound SDP and space-efficient transducer tools for Boolean query complexity
|
|
5
5
|
Author: Ke Li
|
|
6
6
|
License: MIT
|
|
@@ -138,3 +138,35 @@ for r, data in blocks.items():
|
|
|
138
138
|
- `qiskit_adversary_circuit(...)`: optional dense-unitary Qiskit circuit export.
|
|
139
139
|
- `analytic_or_solution(...)`: closed-form adversary solution for OR.
|
|
140
140
|
- `threshold_eta_blocks(...)`: closed-form rank-one Schrijver blocks for thresholds.
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
## Value-only SDP solvers
|
|
144
|
+
|
|
145
|
+
If you only need the adversary bound and do not want the second trace-minimization pass, use the value-only APIs.
|
|
146
|
+
|
|
147
|
+
For a general Boolean function:
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
import query_complexity as qc
|
|
151
|
+
|
|
152
|
+
inputs = qc.all_binary_inputs(3)
|
|
153
|
+
outputs = [int(any(x)) for x in inputs]
|
|
154
|
+
|
|
155
|
+
res = qc.solve_adversary_value_sdp(inputs, outputs, solver="MOSEK")
|
|
156
|
+
print(res.objective_value) # phase-normalized value T
|
|
157
|
+
print(res.usual_adversary_value) # usual adversary value 2T
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
For a symmetric function using Schrijver/Terwilliger blocks:
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
res = qc.solve_symmetric_orbit_value_sdp(
|
|
164
|
+
10,
|
|
165
|
+
qc.THRESHOLD_layers(10, 4),
|
|
166
|
+
solver="MOSEK",
|
|
167
|
+
)
|
|
168
|
+
print(res.objective_value)
|
|
169
|
+
print(res.usual_adversary_value)
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
These functions skip witness extraction and trace minimization.
|
|
@@ -11,3 +11,9 @@ def test_analytic_or_transducer_builds():
|
|
|
11
11
|
def test_new_api_names():
|
|
12
12
|
assert hasattr(qc, "solve_adversary_sdp")
|
|
13
13
|
assert not hasattr(qc, "solve_phase_sdp")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def test_value_only_api_names():
|
|
17
|
+
assert hasattr(qc, "solve_adversary_value_sdp")
|
|
18
|
+
assert hasattr(qc, "solve_symmetric_orbit_value_sdp")
|
|
19
|
+
assert hasattr(qc, "solve_symmetric_adversary_value_sdp")
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{query_complexity-0.1.0 → query_complexity-0.1.1}/src/query_complexity.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
{query_complexity-0.1.0 → query_complexity-0.1.1}/src/query_complexity.egg-info/requires.txt
RENAMED
|
File without changes
|
{query_complexity-0.1.0 → query_complexity-0.1.1}/src/query_complexity.egg-info/top_level.txt
RENAMED
|
File without changes
|