Completion Solvers¶
qsp_proc.completion
¶
Completion step solvers: Fejér-Wilson, BerSün, optimization, rootfinding.
list_methods()
¶
Return supported completion method names.
Source code in src\qsp_proc\completion\__init__.py
78 79 80 | |
complete(poly, method='bersun', **kwargs)
¶
Compute complementary completion Q for input Laurent polynomial P.
Dispatches to the selected completion backend while enforcing the precondition
||P||_∞ <= 1 on U(1) via dense unit-circle sampling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poly
|
LaurentPoly
|
Input Laurent polynomial |
required |
method
|
str
|
Completion backend name. See :func: |
'bersun'
|
**kwargs
|
Any
|
Forwarded directly to the selected solver. |
{}
|
Returns:
| Type | Description |
|---|---|
LaurentPoly
|
Complementary Laurent polynomial |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
RuntimeError
|
If the method is not implemented for Laurent inputs or the solver fails. |
Source code in src\qsp_proc\completion\__init__.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
qsp_proc.completion.bersun
¶
BerSün FFT completion (complementary Laurent Q from P).
See Berntson & Sunderhauf arXiv:2406.04246 and Skelton arXiv:2501.05977 §5.1.4.
laurent_at_roots_of_unity_ifft(poly, num_roots=None, *, N=1)
¶
P(ω^k) for ω = exp(2πi/n), with n from num_roots or N * support_width.
Zero-padded IFFT on causal coeffs, times ω^(k * min_degree) for the Laurent shift.
Source code in src\qsp_proc\completion\bersun.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
downscale_p_for_bersun(poly, epsilon)
¶
Scale P by (1 − ε/4) as in Algorithm 2 when ‖P‖∞,𝕋 = 1 (arXiv:2406.04246).
Source code in src\qsp_proc\completion\bersun.py
68 69 70 71 72 73 74 75 | |
one_minus_modulus_squared_laurent(poly)
¶
Laurent polynomial S with S(z) = 1 - |P(z)|² on |z|=1 (via P(z)conj(P(1/z))).
Implemented as 1 minus conv(p, conj(p)[::-1]) on the causal coefficient vector p.
Source code in src\qsp_proc\completion\bersun.py
78 79 80 81 82 83 84 85 86 87 88 | |
fourier_coeffs_log_one_minus_modulus_squared(poly, num_samples=None, *, N=1, epsilon=0.0)
¶
FFT coefficients of log(1-|P|²) on the n-th roots: fft(g)/n (numpy order).
Uses the same IFFT root evaluation as Algorithm 1 Step 1 (laurent_at_roots_of_unity_ifft),
not direct poly(z) evaluation.
Source code in src\qsp_proc\completion\bersun.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
apply_causal_projection_fourier(fourier_coeffs)
¶
Causal / Hardy projector Π on fft(g)/n: halve DC, zero negative wrapped bins.
Source code in src\qsp_proc\completion\bersun.py
121 122 123 124 125 126 127 128 129 130 131 | |
complementary_q_on_roots_from_projected_log(projected_log_fourier)
¶
Q(ω^k) ≈ exp(n * ifft(Π ĝ)) with ĝ = fft(g)/n from the log-defect step.
Source code in src\qsp_proc\completion\bersun.py
134 135 136 137 138 139 140 | |
laurent_q_from_unit_root_samples(q_on_roots, *, trim_tol=0.0, target_degree=None)
¶
Causal Laurent Q from samples Q(ω^k): coefficients are fft(samples)/n.
Optional target_degree caps the recovered degree (Algorithm 1, Step 6).
Source code in src\qsp_proc\completion\bersun.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
bersun_complementary_laurent(poly, num_samples=None, *, N=1, epsilon=0.0, trim_tol=0.0, target_degree=None)
¶
Run BerSün Algorithm 1: complementary causal Q from Laurent P.
By default, truncates recovered coefficients to degree max_degree - min_degree
(coefficient span), matching Step 6 for the usual P support.
Source code in src\qsp_proc\completion\bersun.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
qsp_proc.completion.wilson
¶
Fejér-Wilson completion method (Section 4).
fejer_problem_from_poly(p)
¶
Construct the Fejér-factorization input Laurent polynomial.
Builds
:math:F(z) = 1 - P(z)P(1/z)^*, which satisfies
:math:F(e^{i\theta}) = 1 - |P(e^{i\theta})|^2 on :math:U(1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
LaurentPoly
|
Input Laurent polynomial :math: |
required |
Returns:
| Type | Description |
|---|---|
LaurentPoly
|
Self-adjoint Laurent polynomial :math: |
Source code in src\qsp_proc\completion\wilson.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
wilson_iterate(f_coeffs, gamma_current)
¶
One Wilson (Newton on :math:U(1)) step for Fejér spectral factorization.
At roots :math:z_k, uses
:math:\gamma \leftarrow \tfrac12(\gamma + F/\overline{\gamma}), then recovers
causal coefficients via fft(samples)/L truncated to length n+1.
This is the FFT-based equivalent of the Toeplitz linear-system update in
Skelton (2025), Eq. 42, and runs in :math:O(n \log n) per iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f_coeffs
|
ndarray
|
Laurent coefficients of :math: |
required |
gamma_current
|
ndarray
|
Causal coefficients of the current :math: |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Updated causal coefficients (length |
Source code in src\qsp_proc\completion\wilson.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
wilson_completion(poly, max_iter=50, tol=1e-13, initial_guess=None)
¶
Run Wilson iteration to compute a causal complementary factor :math:\gamma.
Finds causal :math:\gamma with :math:F(z)=1-|P(z)|^2 \approx |\gamma(z)|^2 on
:math:U(1) (equivalently the Fejér Laurent factorization).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poly
|
LaurentPoly
|
Input Laurent polynomial :math: |
required |
max_iter
|
int
|
Maximum Wilson iterations. |
50
|
tol
|
float
|
Stop when max coefficient change falls below this. |
1e-13
|
initial_guess
|
ndarray | None
|
Optional length- |
None
|
Returns:
| Type | Description |
|---|---|
LaurentPoly
|
Completed causal Laurent polynomial :math: |
Raises:
| Type | Description |
|---|---|
ValueError
|
Invalid arguments or unusable default initial guess. |
RuntimeError
|
No convergence or post-validation failure. |
Source code in src\qsp_proc\completion\wilson.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
qsp_proc.completion.optimization
¶
PyTorch-based optimization for completion (Section 5.2).
build_qsp_unitary(phi, x)
¶
Build batched QSP unitaries from phase sequence and sample points.
For each sample :math:x_j, constructs
:math:U_\\Phi(x_j) = R(\\phi_0)\\prod_{k=1}^{d}[W(x_j)R(\\phi_k)], where
:math:W(x)=\\begin{bmatrix}x & i\\sqrt{1-x^2}\\\\ i\\sqrt{1-x^2} & x\\end{bmatrix},
:math:R(\\phi)=\\mathrm{diag}(e^{i\\phi}, e^{-i\\phi}).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phi
|
Tensor
|
Phase vector of length |
required |
x
|
Tensor
|
Real sample points of length |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Batched 2x2 unitaries with shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src\qsp_proc\completion\optimization.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
symmetric_qsp_loss(phi, target_fn, chebyshev_nodes, parity)
¶
Compute the symmetric-QSP least-squares objective (Skelton 2025, Eq. 51-52).
Reconstructs the full symmetric phase sequence :math:\\Phi from reduced phases
:math:\\hat\\Phi, evaluates :math:U_\\Phi(x_j) on collocation nodes, and returns
mean-squared error between :math:\\Re\\,U_{00}(x_j) and target values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phi
|
Tensor
|
Reduced phase vector :math: |
required |
target_fn
|
Callable[..., object]
|
Target function |
required |
chebyshev_nodes
|
Tensor
|
Collocation nodes (1D). |
required |
parity
|
int
|
Degree parity indicator (even/odd) used for symmetry reconstruction. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar MSE loss tensor. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If inputs have invalid shapes or unsupported parity. |
Source code in src\qsp_proc\completion\optimization.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
optimize_symmetric_qsp(target_coeffs, parity, max_iter=1000, tol=1e-12, lr=0.1, phase_padding=False)
¶
Optimize symmetric QSP phases with PyTorch LBFGS.
Uses a reduced symmetric phase vector :math:\\hat\\phi, initializes
:math:\\hat\\phi=(\\pi/4,0,\\dots,0), and optimizes with LBFGS
(strong Wolfe line search). Returns the full symmetric phase vector
:math:\\Phi as a NumPy array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_coeffs
|
ndarray
|
Target polynomial coefficients in Chebyshev basis. |
required |
parity
|
int
|
Target polynomial parity indicator. |
required |
max_iter
|
int
|
Maximum number of optimizer iterations per attempt. |
1000
|
tol
|
float
|
Convergence tolerance on loss. |
1e-12
|
lr
|
float
|
LBFGS learning rate. |
0.1
|
phase_padding
|
bool
|
If True, do one restart with padded phases (prepend/append
:math: |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Optimized full symmetric phase array :math: |
Source code in src\qsp_proc\completion\optimization.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |
gqsp_completion_loss(q_coeffs, p_coeffs)
¶
G-QSP completion loss from Skelton 2025 Eq. (54).
Squared Frobenius / :math:\ell_2 norm of
:math:p \star \mathrm{reverse}(p^*) + q \star \mathrm{reverse}(q^*) - \delta,
with :math:\delta the Kronecker delta at lag zero.
Source code in src\qsp_proc\completion\optimization.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
optimize_gqsp_completion(p_coeffs, max_iter=2000, tol=1e-10, *, num_restarts=8, q_init=None)
¶
Optimize complementary Q coefficients for G-QSP completion.
Tries several deterministic random starts (seeds 0 … num_restarts-1): a single
draw can trap LBFGS in a poor basin, while another seed often reaches very small
Skelton loss. If q_init is given, the first run starts from those coefficients
instead of a random draw (useful for high-degree P where random restarts stall).
Note
In line with Skelton 2025, this optimization method commonly plateaus near ~1e-8 loss precision, typically below FFT/root-based methods.
Source code in src\qsp_proc\completion\optimization.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | |
qsp_proc.completion.rootfinding
¶
Haah-style root-finding completion (Skelton 2025 Sec. 5.1.1; Haah, Quantum 3:190, 2019).
Factors the palindromic polynomial h(z)=z^n(1-|P|^2) on the circle (Fejér data). The
u=x^2 Chebyshev / Appendix-A sqrt(1-x^2) * W(u) description in the guide matches this
outer factorization after x = cos(θ) when the defect is even in θ; the code uses the
standard complex z-domain root split (Wilson/Fejér compatible).
partition_reciprocal_roots(roots, tol=1e-10)
¶
Group roots into reciprocal pairs (r, s) with r * s ≈ 1.
Each root appears in exactly one pair. A root with r^2 ≈ 1 (typically ±1) is paired
with itself. Matching is greedy: for each unused root, take the best unused partner among
later indices; otherwise accept a self-pair if r^2 ≈ 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
roots
|
Sequence[complex] | ndarray
|
Root values (e.g. from |
required |
tol
|
float
|
Tolerance for |
1e-10
|
Returns:
| Type | Description |
|---|---|
list[tuple[complex, complex]]
|
List of pairs |
Raises:
| Type | Description |
|---|---|
ValueError
|
If some root has no reciprocal partner under the tolerance. |
Source code in src\qsp_proc\completion\rootfinding.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
haah_completion(poly, *, root_tol=1e-10, num_validate=4096, validate_tol=1e-05)
¶
Complete P with causal Q via Haah root finding (Skelton Sec. 5.1.1).
Builds h(z)=z^n F(z) with F(z)=1-P(z)\overline{P(1/z)} (degree 2n,
n=len(P.coeffs)-1). Roots of h pair as (r,1/r); the outer factor
\gamma(z)=K\prod_{s\in S}(z-s) takes |s|>1 when possible (unit circle: break by
\operatorname{Im}s). Scale K so |P|^2+|Q|^2\approx 1 on U(1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poly
|
LaurentPoly
|
Laurent |
required |
root_tol
|
float
|
Reciprocal pairing tolerance for :func: |
1e-10
|
num_validate
|
int
|
Number of circle samples for scaling and validation. |
4096
|
validate_tol
|
float
|
Max |
1e-05
|
Returns:
| Type | Description |
|---|---|
LaurentPoly
|
Causal complementary |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If the Fejér DC term is negative or pairing does not cover all roots. |
RuntimeError
|
If scaling or validation fails numerically. |
Source code in src\qsp_proc\completion\rootfinding.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |