using System;
using System.Numerics;
using System.Globalization;
// Rationnels réduits ; la valeur par défaut représente zéro.
public readonly struct Q : IEquatable<Q>
{
public BigInteger N { get; }
private BigInteger Den { get; }
public BigInteger D => Den.IsZero ? BigInteger.One : Den;
public Q(BigInteger n, BigInteger d)
{
if (d.IsZero) throw new DivideByZeroException();
if (d.Sign < 0) { n = -n; d = -d; }
var g = BigInteger.GreatestCommonDivisor(BigInteger.Abs(n), d);
N = n / g; Den = d / g;
}
public static implicit operator Q(int n) => new Q(n, 1);
public static Q operator +(Q a, Q b) => new Q(a.N * b.D + b.N * a.D, a.D * b.D);
public static Q operator -(Q a, Q b) => new Q(a.N * b.D - b.N * a.D, a.D * b.D);
public static Q operator -(Q a) => new Q(-a.N, a.D);
public static Q operator *(Q a, Q b) => new Q(a.N * b.N, a.D * b.D);
public static Q operator /(Q a, Q b) => new Q(a.N * b.D, a.D * b.N);
public static bool operator ==(Q a, Q b) => a.N == b.N && a.D == b.D;
public static bool operator !=(Q a, Q b) => !(a == b);
public bool Equals(Q other) => this == other;
public override bool Equals(object obj) => obj is Q other && Equals(other);
public override int GetHashCode() => HashCode.Combine(N, D);
public override string ToString() => D.IsOne ? N.ToString(CultureInfo.InvariantCulture) : N.ToString(CultureInfo.InvariantCulture) + "/" + D.ToString(CultureInfo.InvariantCulture);
public decimal Decimal()
{
// La division entière précède la conversion pour éviter de convertir
// séparément un numérateur et un dénominateur arbitrairement grands.
BigInteger reste;
BigInteger entier = BigInteger.DivRem(BigInteger.Abs(N), D, out reste);
BigInteger echelle = BigInteger.Pow(10, 28);
BigInteger fraction = reste * echelle / D;
decimal valeur = (decimal)entier + (decimal)fraction / 10000000000000000000000000000m;
return N.Sign < 0 ? -valeur : valeur;
}
}
// Complexes à parties rationnelles exactes.
public readonly struct CQ : IEquatable<CQ>
{
public Q Re { get; }
public Q Im { get; }
public CQ(Q re, Q im) { Re = re; Im = im; }
public static implicit operator CQ(int n) => new CQ(n, 0);
public static implicit operator CQ(Q q) => new CQ(q, 0);
public static CQ operator +(CQ a, CQ b) => new CQ(a.Re + b.Re, a.Im + b.Im);
public static CQ operator -(CQ a, CQ b) => new CQ(a.Re - b.Re, a.Im - b.Im);
public static CQ operator *(CQ a, CQ b) => new CQ(a.Re * b.Re - a.Im * b.Im, a.Re * b.Im + a.Im * b.Re);
public CQ Conj() => new CQ(Re, -Im);
public static bool operator ==(CQ a, CQ b) => a.Re == b.Re && a.Im == b.Im;
public static bool operator !=(CQ a, CQ b) => !(a == b);
public bool Equals(CQ other) => this == other;
public override bool Equals(object obj) => obj is CQ other && Equals(other);
public override int GetHashCode() => HashCode.Combine(Re, Im);
}
// Corps Q(sqrt(2)), utilisé pour les projecteurs spectraux exacts.
public readonly struct Q2 : IEquatable<Q2>
{
public Q A { get; }
public Q B { get; }
public Q2(Q a, Q b) { A = a; B = b; }
public static implicit operator Q2(int n) => new Q2(n, 0);
public static implicit operator Q2(Q n) => new Q2(n, 0);
public static Q2 operator +(Q2 x, Q2 y) => new Q2(x.A + y.A, x.B + y.B);
public static Q2 operator -(Q2 x, Q2 y) => new Q2(x.A - y.A, x.B - y.B);
public static Q2 operator *(Q2 x, Q2 y) => new Q2(x.A * y.A + 2 * x.B * y.B, x.A * y.B + x.B * y.A);
public static Q2 operator /(Q2 x, Q2 y)
{
Q den = y.A * y.A - 2 * y.B * y.B;
return new Q2((x.A * y.A - 2 * x.B * y.B) / den, (x.B * y.A - x.A * y.B) / den);
}
public static bool operator ==(Q2 x, Q2 y) => x.A == y.A && x.B == y.B;
public static bool operator !=(Q2 x, Q2 y) => !(x == y);
public bool Equals(Q2 other) => this == other;
public override bool Equals(object obj) => obj is Q2 other && Equals(other);
public override int GetHashCode() => HashCode.Combine(A, B);
}
public static class Alg
{
public static Q R(long n, long d = 1) => new Q(n, d);
public static void Assert(bool condition, string message)
{
if (!condition) throw new InvalidOperationException(message);
}
public static CQ[,] Id(int n)
{
var m = new CQ[n, n];
for (int i = 0; i < n; i++) m[i, i] = 1;
return m;
}
public static CQ[,] Scale(CQ[,] a, Q q)
{
int n = a.GetLength(0), m = a.GetLength(1);
var r = new CQ[n, m];
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) r[i, j] = a[i, j] * (CQ)q;
return r;
}
public static CQ[,] Add(CQ[,] a, CQ[,] b)
{
int n = a.GetLength(0), m = a.GetLength(1);
var r = new CQ[n, m];
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) r[i, j] = a[i, j] + b[i, j];
return r;
}
public static CQ[,] Mul(CQ[,] a, CQ[,] b)
{
int n = a.GetLength(0), k = a.GetLength(1), m = b.GetLength(1);
if (k != b.GetLength(0)) throw new ArgumentException("Dimensions incompatibles");
var r = new CQ[n, m];
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) for (int t = 0; t < k; t++) r[i, j] = r[i, j] + a[i, t] * b[t, j];
return r;
}
public static CQ Trace(CQ[,] a)
{
CQ s = 0;
for (int i = 0; i < a.GetLength(0); i++) s = s + a[i, i];
return s;
}
public static CQ[,] Outer(CQ[] v)
{
var r = new CQ[v.Length, v.Length];
for (int i = 0; i < v.Length; i++) for (int j = 0; j < v.Length; j++) r[i, j] = v[i] * v[j].Conj();
return r;
}
public static CQ[] Ket(params CQ[][] states)
{
CQ[] v = { 1 };
foreach (var state in states)
{
var next = new CQ[v.Length * state.Length];
for (int i = 0; i < v.Length; i++) for (int j = 0; j < state.Length; j++) next[i * state.Length + j] = v[i] * state[j];
v = next;
}
return v;
}
public static CQ[,] TpB(CQ[,] a, int d1, int d2)
{
var r = new CQ[d1 * d2, d1 * d2];
for (int i = 0; i < d1; i++) for (int j = 0; j < d2; j++)
for (int k = 0; k < d1; k++) for (int l = 0; l < d2; l++) r[i * d2 + j, k * d2 + l] = a[i * d2 + l, k * d2 + j];
return r;
}
public static bool Equal(CQ[,] a, CQ[,] b)
{
if (a.GetLength(0) != b.GetLength(0) || a.GetLength(1) != b.GetLength(1)) return false;
for (int i = 0; i < a.GetLength(0); i++) for (int j = 0; j < a.GetLength(1); j++) if (a[i, j] != b[i, j]) return false;
return true;
}
}
// Le singulet de deux qubits, acteur central de tout le carnet
var e0 = new CQ[] { 1, 0 };
var e1 = new CQ[] { 0, 1 };
var singuletNonNormalise = new CQ[] { 0, 1, -1, 0 };
var S = Alg.Scale(Alg.Outer(singuletNonNormalise), Alg.R(1, 2));
Alg.Assert(Alg.Trace(S) == (CQ)1 && Alg.Equal(Alg.Mul(S, S), S), "Le singulet doit être un projecteur de trace un");
Q carreAmplitude = Alg.R(1, 2);
Alg.Assert(carreAmplitude == Alg.R(1, 2), "Carré de l'amplitude du singulet");
var amplitudeAffichee = Math.Sqrt((double)carreAmplitude.Decimal());
Console.WriteLine("|psi-> = [ 0. " + amplitudeAffichee.ToString("F8", CultureInfo.InvariantCulture) + " -" + amplitudeAffichee.ToString("F8", CultureInfo.InvariantCulture) + " 0. ]");