File size: 4,599 Bytes
81a3ea5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""Rank-one reference and relative-phase geometry for QÆNTHRIX.

The model uses rank-one orthogonal projectors in complex coordinates.
These routines are finite numerical constructions, not topological proofs.
"""
import numpy as np
from eve_reserve import Event


def ray(vector):
    """Return the rank-one projector of a nonzero vector; phase is discarded."""
    u = np.asarray(vector, dtype=complex).reshape(-1)
    norm = np.linalg.norm(u)
    if norm == 0:
        raise ValueError("a ray requires a nonzero vector")
    u = u / norm
    return np.outer(u, u.conj())


def _unit(vector, n):
    w = np.asarray(vector, dtype=complex).reshape(n)
    if not np.isclose(np.vdot(w, w).real, 1., atol=1e-12, rtol=1e-12):
        raise ValueError("reference must have unit norm")
    return w


def global_factor(p, weight, background=0.):
    """Continuous n-row factor of lambda P + mu (I-P), lambda >= mu >= 0."""
    if not 0 <= background <= weight:
        raise ValueError("weights must satisfy lambda >= mu >= 0")
    p = np.asarray(p, dtype=complex)
    return np.sqrt(weight)*p + np.sqrt(background)*(np.eye(len(p))-p)


def anchor_section(p, reference, blind_tolerance=1e-12):
    """Canonical unit vector P w / ||P w|| on the recognized domain.

    The numerical blind_tolerance is explicit; the analytic domain is ||P w||>0.
    """
    p = np.asarray(p, dtype=complex)
    w = _unit(reference, len(p))
    v = p @ w
    margin = np.linalg.norm(v)
    if margin <= blind_tolerance:
        raise ValueError("reference is blind at the declared numerical tolerance")
    return v / margin


def anchor_factor(p, reference, weight, blind_tolerance=1e-12):
    if weight < 0:
        raise ValueError("weight must be nonnegative")
    return np.sqrt(weight)*anchor_section(p, reference, blind_tolerance).conj()[None, :]


def recognition_margin(p, reference):
    """Exact-model distance in operator norm to the reference's blind locus."""
    p = np.asarray(p, dtype=complex)
    return float(np.linalg.norm(p @ _unit(reference, len(p))))


def select_reference(p, references, blind_tolerance=1e-12):
    """Choose a largest-margin column of an n-by-q unit-reference matrix.

    Index changes are chart switches, not one continuous global scalar factor.
    """
    p = np.asarray(p, dtype=complex)
    refs = np.asarray(references, dtype=complex)
    if refs.ndim != 2 or refs.shape[0] != len(p) or refs.shape[1] == 0:
        raise ValueError("references must be a nonempty n-by-q matrix")
    for j in range(refs.shape[1]):
        _unit(refs[:, j], len(p))
    margins = np.linalg.norm(p @ refs, axis=0)
    index = int(np.argmax(margins))
    section = anchor_section(p, refs[:, index], blind_tolerance)
    return index, section, float(margins[index])


def pair_transport(p, q, blind_tolerance=1e-12):
    """Canonical partial isometry from ray Q to ray P when they overlap."""
    product = np.asarray(p, dtype=complex) @ np.asarray(q, dtype=complex)
    margin = np.linalg.norm(product, 'fro')
    if margin <= blind_tolerance:
        raise ValueError("orthogonal rays have no canonical phase comparison")
    return product / margin


def cycle_holonomy(projectors, blind_tolerance=1e-12):
    """Phase of T(P0<-P1) ... T(P_last<-P0), with this orientation."""
    if len(projectors) < 2:
        raise ValueError("a cycle requires at least two vertices")
    ps = [np.asarray(p, dtype=complex) for p in projectors]
    product = np.eye(len(ps[0]), dtype=complex)
    for i, p in enumerate(ps):
        product = product @ pair_transport(p, ps[(i+1) % len(ps)], blind_tolerance)
    return complex(np.trace(ps[0] @ product))


def relational_direction(p):
    """Column-major vec(P); a global unit vector in a different, n^2-state space."""
    return np.asarray(p, dtype=complex).reshape(-1, order='F')


def relational_factor(p, weight):
    """One-row factor of weight |vec(P)><vec(P)| on matrix-valued inputs."""
    if weight < 0:
        raise ValueError("weight must be nonnegative")
    return np.sqrt(weight)*relational_direction(p).conj()[None, :]


def relational_event(p, cosine, colour, transport=None):
    """Lift a matrix state through X -> U A(X) U*, using column-major vec.

    The direction is vec(P), and U conjugation acts as conjugate(U) tensor U.
    This is not an encoder of the original single-vector amplitude.
    """
    p = np.asarray(p, dtype=complex)
    n = len(p)
    u = np.eye(n, dtype=complex) if transport is None else np.asarray(transport, dtype=complex)
    return Event(relational_direction(p), cosine, colour, np.kron(u.conj(), u))