interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / research / atemporal / at0 / model / at0_povm.c

90 lines · 3658 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* Internal clock POVM: covariant discrete clock states and the normalization
18 * residual. The phase of each component is reduced exactly (rational, mod one
19 * turn) before cos/sin, so no accumulated angle drift and no lookup table. */
20#include "at0_model.h"
21#include <math.h>
22#include <string.h>
23
24#define AT0_TWO_PI 6.283185307179586476925286766559
25
26/* exp(-2 pi i E_j k tau) with the turn count reduced exactly in 128-bit integers */
27static at0_status clock_phase(const at0_case *c, int j, int k, double complex *out)
28{
29 if (j < 0 || j >= c->clock_dim || k < 0 || k >= c->label_count) return AT0_ERR_ARGUMENT;
30 at0_rat e = c->clock_energies[j], tau = c->povm_tau_turns;
31 at0_i128 n = (at0_i128)e.n * k * tau.n; /* |n| <= 2^20 * 2^8 * 2^20 = 2^48 */
32 at0_i128 d = (at0_i128)e.d * tau.d; /* d <= 2^40 */
33 /* x = n/d ; reduce to [-1/2, 1/2): x - round_half_up(x) */
34 at0_i128 twice = 2 * n + d;
35 at0_i128 q = twice / (2 * d);
36 if (twice < 0 && twice % (2 * d) != 0) q -= 1;
37 at0_i128 rn = n - q * d; /* |rn| <= d */
38 double x = (double)(long long)rn / (double)(long long)d; /* both exact in binary64 */
39 double theta = -AT0_TWO_PI * x;
40 double cs = cos(theta), sn = sin(theta);
41 if (!isfinite(cs) || !isfinite(sn)) return AT0_ERR_NONFINITE;
42 *out = cs + I * sn;
43 return AT0_OK;
44}
45
46at0_status at0_povm_clock_state(const at0_case *c, int k, at0_clock_vec *out)
47{
48 if (!c || !out) return AT0_ERR_ARGUMENT;
49 if (c->clock_dim < AT0_CLOCK_DIM_MIN || c->clock_dim > AT0_CLOCK_DIM_MAX) return AT0_ERR_ARGUMENT;
50 if (k < 0 || k >= c->label_count) return AT0_ERR_ARGUMENT;
51 memset(out, 0, sizeof *out);
52 out->clock_dim = c->clock_dim;
53 double scale = 1.0 / sqrt((double)c->clock_dim);
54 for (int j = 0; j < c->clock_dim; j++) {
55 double complex ph;
56 at0_status st = clock_phase(c, j, k, &ph);
57 if (st != AT0_OK) return st;
58 out->v[j] = scale * ph;
59 }
60 return AT0_OK;
61}
62
63at0_status at0_povm_residual(const at0_case *c, double *out)
64{
65 if (!c || !out) return AT0_ERR_ARGUMENT;
66 int n = c->clock_dim;
67 if (n < AT0_CLOCK_DIM_MIN || n > AT0_CLOCK_DIM_MAX) return AT0_ERR_ARGUMENT;
68 double w = at0_rat_to_double(c->povm_weight);
69 double complex s[AT0_CLOCK_DIM_MAX][AT0_CLOCK_DIM_MAX];
70 memset(s, 0, sizeof s);
71 for (int k = 0; k < c->label_count; k++) {
72 at0_clock_vec t;
73 at0_status st = at0_povm_clock_state(c, k, &t);
74 if (st != AT0_OK) return st;
75 for (int j = 0; j < n; j++)
76 for (int l = 0; l < n; l++)
77 s[j][l] += w * t.v[j] * conj(t.v[l]);
78 }
79 double acc = 0.0;
80 for (int j = 0; j < n; j++) {
81 for (int l = 0; l < n; l++) {
82 double complex d = s[j][l] - (j == l ? 1.0 : 0.0);
83 acc += creal(d) * creal(d) + cimag(d) * cimag(d);
84 }
85 }
86 double r = sqrt(acc);
87 if (!isfinite(r)) return AT0_ERR_NONFINITE;
88 *out = r;
89 return AT0_OK;
90}