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
omega-mv / research / atemporal / at0 / model / at0_state.c
| 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 | /* Quantum state representation: a vector in C^N (x) C^2, clock factor first. |
| 18 | * Every accessor checks its indices; nothing here is global. */ |
| 19 | #include "at0_model.h" |
| 20 | #include <math.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | static int index_ok(const at0_state *s, int j, int sys) |
| 24 | { |
| 25 | return s && s->clock_dim >= AT0_CLOCK_DIM_MIN && s->clock_dim <= AT0_CLOCK_DIM_MAX && |
| 26 | j >= 0 && j < s->clock_dim && sys >= 0 && sys < AT0_SYSTEM_DIM; |
| 27 | } |
| 28 | |
| 29 | at0_status at0_state_init(at0_state *s, int clock_dim) |
| 30 | { |
| 31 | if (!s || clock_dim < AT0_CLOCK_DIM_MIN || clock_dim > AT0_CLOCK_DIM_MAX) return AT0_ERR_ARGUMENT; |
| 32 | memset(s, 0, sizeof *s); |
| 33 | s->clock_dim = clock_dim; |
| 34 | return AT0_OK; |
| 35 | } |
| 36 | |
| 37 | at0_status at0_state_get(const at0_state *s, int j, int sys, double complex *out) |
| 38 | { |
| 39 | if (!out || !index_ok(s, j, sys)) return AT0_ERR_ARGUMENT; |
| 40 | *out = s->amp[j * AT0_SYSTEM_DIM + sys]; |
| 41 | return AT0_OK; |
| 42 | } |
| 43 | |
| 44 | at0_status at0_state_set(at0_state *s, int j, int sys, double complex val) |
| 45 | { |
| 46 | if (!index_ok(s, j, sys)) return AT0_ERR_ARGUMENT; |
| 47 | s->amp[j * AT0_SYSTEM_DIM + sys] = val; |
| 48 | return AT0_OK; |
| 49 | } |
| 50 | |
| 51 | at0_status at0_state_norm2(const at0_state *s, double *out) |
| 52 | { |
| 53 | if (!s || !out || s->clock_dim < AT0_CLOCK_DIM_MIN || s->clock_dim > AT0_CLOCK_DIM_MAX) return AT0_ERR_ARGUMENT; |
| 54 | double acc = 0.0; |
| 55 | int n = s->clock_dim * AT0_SYSTEM_DIM; |
| 56 | for (int i = 0; i < n; i++) { |
| 57 | double re = creal(s->amp[i]), im = cimag(s->amp[i]); |
| 58 | acc += re * re + im * im; |
| 59 | } |
| 60 | if (!isfinite(acc)) return AT0_ERR_NONFINITE; |
| 61 | *out = acc; |
| 62 | return AT0_OK; |
| 63 | } |
| 64 | |
| 65 | at0_status at0_state_inner(const at0_state *a, const at0_state *b, double complex *out) |
| 66 | { |
| 67 | if (!a || !b || !out || a->clock_dim != b->clock_dim) return AT0_ERR_ARGUMENT; |
| 68 | if (a->clock_dim < AT0_CLOCK_DIM_MIN || a->clock_dim > AT0_CLOCK_DIM_MAX) return AT0_ERR_ARGUMENT; |
| 69 | double complex acc = 0.0; |
| 70 | int n = a->clock_dim * AT0_SYSTEM_DIM; |
| 71 | for (int i = 0; i < n; i++) acc += conj(a->amp[i]) * b->amp[i]; |
| 72 | if (!isfinite(creal(acc)) || !isfinite(cimag(acc))) return AT0_ERR_NONFINITE; |
| 73 | *out = acc; |
| 74 | return AT0_OK; |
| 75 | } |
| 76 | |
| 77 | at0_status at0_state_scale(at0_state *s, double complex z) |
| 78 | { |
| 79 | if (!s || s->clock_dim < AT0_CLOCK_DIM_MIN || s->clock_dim > AT0_CLOCK_DIM_MAX) return AT0_ERR_ARGUMENT; |
| 80 | int n = s->clock_dim * AT0_SYSTEM_DIM; |
| 81 | for (int i = 0; i < n; i++) s->amp[i] *= z; |
| 82 | return at0_state_check_finite(s); |
| 83 | } |
| 84 | |
| 85 | at0_status at0_state_check_finite(const at0_state *s) |
| 86 | { |
| 87 | if (!s || s->clock_dim < AT0_CLOCK_DIM_MIN || s->clock_dim > AT0_CLOCK_DIM_MAX) return AT0_ERR_ARGUMENT; |
| 88 | int n = s->clock_dim * AT0_SYSTEM_DIM; |
| 89 | for (int i = 0; i < n; i++) |
| 90 | if (!isfinite(creal(s->amp[i])) || !isfinite(cimag(s->amp[i]))) return AT0_ERR_NONFINITE; |
| 91 | return AT0_OK; |
| 92 | } |