#nullable enable
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Numerics;
// Tout le carnet tourne en arithmetique exacte : System.Numerics.BigInteger pour les
// entiers, un rationnel exact reduit (numerateur et denominateur entiers) la ou la
// source emploie fractions.Fraction, un complexe exact (partie reelle et partie
// imaginaire rationnelles) et l'anneau cyclotomique Z[omega] pour les lettres de Weyl
// en dimension 3. Aucune egalite flottante n'est testee : chaque controle leve sur
// ecart (InvalidOperationException).
Console.WriteLine(".NET " + Environment.Version + " ; arithmetique exacte BigInteger, aucune egalite flottante testee");
public static class VerifC02
{
public static void Egale(bool condition, string message)
{
if (!condition) throw new InvalidOperationException("ECART : " + message);
}
}
public readonly struct RationnelC02 : IEquatable<RationnelC02>
{
public BigInteger Numerateur { get; }
public BigInteger Denominateur { get; }
public RationnelC02(BigInteger numerateur) : this(numerateur, BigInteger.One) { }
public RationnelC02(BigInteger numerateur, BigInteger denominateur)
{
if (denominateur == BigInteger.Zero)
throw new DivideByZeroException("Denominateur nul : rationnel indefini.");
if (denominateur < BigInteger.Zero)
{
numerateur = -numerateur;
denominateur = -denominateur;
}
BigInteger g = BigInteger.GreatestCommonDivisor(BigInteger.Abs(numerateur), denominateur);
if (g == BigInteger.Zero) g = BigInteger.One;
Numerateur = numerateur / g;
Denominateur = denominateur / g;
}
public static readonly RationnelC02 Zero = new RationnelC02(BigInteger.Zero);
public static readonly RationnelC02 Un = new RationnelC02(BigInteger.One);
public static RationnelC02 operator +(RationnelC02 a, RationnelC02 b) =>
new RationnelC02(a.Numerateur * b.Denominateur + b.Numerateur * a.Denominateur,
a.Denominateur * b.Denominateur);
public static RationnelC02 operator -(RationnelC02 a, RationnelC02 b) =>
new RationnelC02(a.Numerateur * b.Denominateur - b.Numerateur * a.Denominateur,
a.Denominateur * b.Denominateur);
public static RationnelC02 operator *(RationnelC02 a, RationnelC02 b) =>
new RationnelC02(a.Numerateur * b.Numerateur, a.Denominateur * b.Denominateur);
public static RationnelC02 operator *(RationnelC02 a, BigInteger k) =>
new RationnelC02(k * a.Numerateur, a.Denominateur);
public static RationnelC02 operator *(BigInteger k, RationnelC02 a) => a * k;
public static RationnelC02 operator /(RationnelC02 a, RationnelC02 b) =>
new RationnelC02(a.Numerateur * b.Denominateur, a.Denominateur * b.Numerateur);
public static RationnelC02 operator -(RationnelC02 a) =>
new RationnelC02(-a.Numerateur, a.Denominateur);
public static bool operator ==(RationnelC02 a, RationnelC02 b) =>
a.Numerateur == b.Numerateur && a.Denominateur == b.Denominateur;
public static bool operator !=(RationnelC02 a, RationnelC02 b) => !(a == b);
public bool Equals(RationnelC02 autre) => this == autre;
public override bool Equals(object? obj) => obj is RationnelC02 r && Equals(r);
public override int GetHashCode() => HashCode.Combine(Numerateur, Denominateur);
// Ecriture decimale EXACTE quand elle existe (denominateur = 2^a 5^b), sinon la fraction.
// Le passage a la base 10 se fait en compensant 2^(k-a) et 5^(k-b) : jamais d'arrondi.
public string EnChaineDecimale(int decimales = 6)
{
BigInteger reste = Denominateur;
int a = 0, b = 0;
while (reste % 2 == 0) { reste /= 2; a++; }
while (reste % 5 == 0) { reste /= 5; b++; }
if (reste != BigInteger.One) return ToString();
int k = Math.Max(a, b);
if (k > decimales) return ToString();
BigInteger num = Numerateur * BigInteger.Pow(2, k - a) * BigInteger.Pow(5, k - b);
bool negatif = num.Sign < 0;
if (negatif) num = -num;
BigInteger echelle = BigInteger.Pow(10, k);
BigInteger partieEntiere = num / echelle;
BigInteger resteDecimal = num % echelle;
string s = partieEntiere.ToString(CultureInfo.InvariantCulture);
if (k > 0)
{
s += "." + resteDecimal.ToString(CultureInfo.InvariantCulture).PadLeft(k, '0');
if (decimales > k) s += new string('0', decimales - k);
}
else if (decimales > 0)
{
s += "." + new string('0', decimales);
}
return (negatif ? "-" : "") + s;
}
public override string ToString() => Denominateur == BigInteger.One
? Numerateur.ToString(CultureInfo.InvariantCulture)
: Numerateur.ToString(CultureInfo.InvariantCulture) + "/" + Denominateur.ToString(CultureInfo.InvariantCulture);
}
public readonly struct CplxC02 : IEquatable<CplxC02>
{
public RationnelC02 Re { get; }
public RationnelC02 Im { get; }
public CplxC02(RationnelC02 re) : this(re, RationnelC02.Zero) { }
public CplxC02(RationnelC02 re, RationnelC02 im) { Re = re; Im = im; }
public static readonly CplxC02 Zero = new CplxC02(RationnelC02.Zero, RationnelC02.Zero);
public static readonly CplxC02 Un = new CplxC02(RationnelC02.Un, RationnelC02.Zero);
public static CplxC02 operator +(CplxC02 a, CplxC02 b) => new(a.Re + b.Re, a.Im + b.Im);
public static CplxC02 operator -(CplxC02 a, CplxC02 b) => new(a.Re - b.Re, a.Im - b.Im);
public static CplxC02 operator *(CplxC02 a, CplxC02 b) =>
new(a.Re * b.Re - a.Im * b.Im, a.Re * b.Im + a.Im * b.Re);
public static CplxC02 operator *(CplxC02 a, RationnelC02 k) => new(a.Re * k, a.Im * k);
public static CplxC02 operator *(RationnelC02 k, CplxC02 a) => a * k;
public static CplxC02 operator /(CplxC02 a, RationnelC02 k) => new(a.Re / k, a.Im / k);
public static bool operator ==(CplxC02 a, CplxC02 b) => a.Re == b.Re && a.Im == b.Im;
public static bool operator !=(CplxC02 a, CplxC02 b) => !(a == b);
public bool Equals(CplxC02 autre) => this == autre;
public override bool Equals(object? obj) => obj is CplxC02 c && Equals(c);
public override int GetHashCode() => HashCode.Combine(Re, Im);
// |z|^2 exact : jamais une racine carree en double.
public RationnelC02 ModuleCarre => Re * Re + Im * Im;
public override string ToString()
{
if (Im.Numerateur.Sign == 0) return Re.ToString();
string signe = Im.Numerateur.Sign < 0 ? " - " : " + ";
RationnelC02 absolu = Im.Numerateur.Sign < 0 ? -Im : Im;
return Re.ToString() + signe + absolu.ToString() + "i";
}
}
public sealed class MatCplxC02
{
public int Lignes { get; }
public int Colonnes { get; }
private readonly CplxC02[][] _e;
public MatCplxC02(int lignes, int colonnes)
{
Lignes = lignes;
Colonnes = colonnes;
_e = new CplxC02[lignes][];
for (int i = 0; i < lignes; i++)
{
_e[i] = new CplxC02[colonnes];
for (int j = 0; j < colonnes; j++) _e[i][j] = CplxC02.Zero;
}
}
public CplxC02 this[int i, int j]
{
get => _e[i][j];
set => _e[i][j] = value;
}
public static MatCplxC02 Identite(int n)
{
var m = new MatCplxC02(n, n);
for (int i = 0; i < n; i++) m[i, i] = CplxC02.Un;
return m;
}
public static MatCplxC02 Diagonale(params BigInteger[] valeurs)
{
var m = new MatCplxC02(valeurs.Length, valeurs.Length);
for (int i = 0; i < valeurs.Length; i++) m[i, i] = new CplxC02(new RationnelC02(valeurs[i]));
return m;
}
public static MatCplxC02 Produit(MatCplxC02 a, MatCplxC02 b)
{
var m = new MatCplxC02(a.Lignes, b.Colonnes);
for (int i = 0; i < a.Lignes; i++)
for (int j = 0; j < b.Colonnes; j++)
{
CplxC02 somme = CplxC02.Zero;
for (int k = 0; k < a.Colonnes; k++) somme += a[i, k] * b[k, j];
m[i, j] = somme;
}
return m;
}
public static MatCplxC02 Difference(MatCplxC02 a, MatCplxC02 b)
{
var m = new MatCplxC02(a.Lignes, a.Colonnes);
for (int i = 0; i < a.Lignes; i++)
for (int j = 0; j < a.Colonnes; j++) m[i, j] = a[i, j] - b[i, j];
return m;
}
public static MatCplxC02 MultipliePar(MatCplxC02 a, CplxC02 k)
{
var m = new MatCplxC02(a.Lignes, a.Colonnes);
for (int i = 0; i < a.Lignes; i++)
for (int j = 0; j < a.Colonnes; j++) m[i, j] = a[i, j] * k;
return m;
}
// DIVISION VRAIE par un rationnel : a / k. Pour multiplier par 1/2, on divise par 2.
public static MatCplxC02 DivisePar(MatCplxC02 a, RationnelC02 k)
{
var m = new MatCplxC02(a.Lignes, a.Colonnes);
for (int i = 0; i < a.Lignes; i++)
for (int j = 0; j < a.Colonnes; j++) m[i, j] = a[i, j] / k;
return m;
}
public MatCplxC02 Adjointe()
{
var m = new MatCplxC02(Colonnes, Lignes);
for (int i = 0; i < Lignes; i++)
for (int j = 0; j < Colonnes; j++)
m[j, i] = new CplxC02(this[i, j].Re, -this[i, j].Im);
return m;
}
public CplxC02 Trace()
{
CplxC02 somme = CplxC02.Zero;
for (int i = 0; i < Math.Min(Lignes, Colonnes); i++) somme += this[i, i];
return somme;
}
public CplxC02 Determinant2x2()
{
if (Lignes != 2 || Colonnes != 2)
throw new InvalidOperationException("Determinant2x2 : matrice non carree 2x2.");
return this[0, 0] * this[1, 1] - this[0, 1] * this[1, 0];
}
public static bool Egale(MatCplxC02 a, MatCplxC02 b)
{
if (a.Lignes != b.Lignes || a.Colonnes != b.Colonnes) return false;
for (int i = 0; i < a.Lignes; i++)
for (int j = 0; j < a.Colonnes; j++)
if (a[i, j] != b[i, j]) return false;
return true;
}
public string DiagonaleEnChaine()
{
var morceaux = new List<string>();
for (int i = 0; i < Math.Min(Lignes, Colonnes); i++) morceaux.Add(this[i, i].ToString());
return "[" + string.Join(", ", morceaux) + "]";
}
public string EnChaine()
{
var lignes = new List<string>();
for (int i = 0; i < Lignes; i++)
{
var morceaux = new List<string>();
for (int j = 0; j < Colonnes; j++) morceaux.Add(this[i, j].ToString());
lignes.Add("[" + string.Join(", ", morceaux) + "]");
}
return string.Join("\n", lignes);
}
}
// Un ket de paire : coefficients (1/sqrt(D)) * P. L'etat reduit vaut (1/D) P P^dagger,
// donc tout reste rationnel exact : la racine carree ne sort jamais du calcul.
public sealed class EtatPaireC02
{
public int DenominateurCarre { get; }
public MatCplxC02 P { get; }
public EtatPaireC02(int denominateurCarre, MatCplxC02 p)
{
DenominateurCarre = denominateurCarre;
P = p;
}
public static EtatPaireC02 PaireMaximale(int d1, int d2)
{
int m = Math.Min(d1, d2);
var p = new MatCplxC02(d1, d2);
for (int i = 0; i < m; i++) p[i, i] = CplxC02.Un;
return new EtatPaireC02(m, p);
}
// L'etat reduit vaut (1/D) P P^dagger : on DIVISE donc par D (entier), pas par 1/D.
private RationnelC02 Diviseur => new RationnelC02(new BigInteger(DenominateurCarre));
public MatCplxC02 EtatReduitGauche() =>
MatCplxC02.DivisePar(MatCplxC02.Produit(P, P.Adjointe()), Diviseur);
public MatCplxC02 EtatReduitDroit() =>
MatCplxC02.DivisePar(MatCplxC02.Produit(P.Adjointe(), P), Diviseur);
public MatCplxC02 CoeurDePaire() => MatCplxC02.DivisePar(MatCplxC02.Produit(P, P), Diviseur);
}
public static class VecteursC02
{
// (U (x) V) applique a un ket de deux qudits : resultat[k,l] = somme_{i,j} U[k,i] V[l,j] psi[i,j].
public static CplxC02[] TenseurApplique(MatCplxC02 u, MatCplxC02 v, CplxC02[] psi, int dim)
{
var resultat = new CplxC02[dim * dim];
for (int k = 0; k < dim; k++)
for (int l = 0; l < dim; l++)
{
CplxC02 somme = CplxC02.Zero;
for (int i = 0; i < dim; i++)
for (int j = 0; j < dim; j++)
somme += u[k, i] * v[l, j] * psi[(i * dim) + j];
resultat[(k * dim) + l] = somme;
}
return resultat;
}
public static bool Egal(CplxC02[] a, CplxC02[] b)
{
if (a.Length != b.Length) return false;
for (int i = 0; i < a.Length; i++) if (a[i] != b[i]) return false;
return true;
}
public static string EnChaine(CplxC02[] v)
{
var morceaux = new List<string>();
foreach (var z in v) morceaux.Add(z.ToString());
return "[" + string.Join(", ", morceaux) + "]";
}
}
public static class UnitairesC02
{
// Unitaire exacte a coefficients rationnels : U = (1/h) [[p, i q],[i q, p]],
// avec (p, q, h) un triplet pythagoricien, donc p^2 + q^2 = h^2 exactement.
public static MatCplxC02 Exacte(BigInteger p, BigInteger q, BigInteger h)
{
VerifC02.Egale(p * p + q * q == h * h, $"triplet pythagoricien attendu : {p}^2 + {q}^2 = {h}^2");
var u = new MatCplxC02(2, 2);
u[0, 0] = new CplxC02(new RationnelC02(p, h));
u[0, 1] = new CplxC02(RationnelC02.Zero, new RationnelC02(q, h));
u[1, 0] = new CplxC02(RationnelC02.Zero, new RationnelC02(q, h));
u[1, 1] = new CplxC02(new RationnelC02(p, h));
return u;
}
public static bool EstUnitaire(MatCplxC02 u) =>
MatCplxC02.Egale(MatCplxC02.Produit(u, u.Adjointe()), MatCplxC02.Identite(2));
}
// Element de l'anneau Z[w] : a + b*w, avec w = e^{2 pi i / 3} et w^2 = -1 - w.
public readonly struct CycloC02 : IEquatable<CycloC02>
{
public RationnelC02 A { get; }
public RationnelC02 B { get; }
public CycloC02(RationnelC02 a) : this(a, RationnelC02.Zero) { }
public CycloC02(RationnelC02 a, RationnelC02 b) { A = a; B = b; }
public static readonly CycloC02 Zero = new CycloC02(RationnelC02.Zero, RationnelC02.Zero);
public static readonly CycloC02 Un = new CycloC02(RationnelC02.Un, RationnelC02.Zero);
public static readonly CycloC02 Omega = new CycloC02(RationnelC02.Zero, RationnelC02.Un);
public static readonly CycloC02 OmegaCarre = new CycloC02(-RationnelC02.Un, -RationnelC02.Un);
// (a + b w)(c + d w) = (ac - bd) + (ad + bc - bd) w
public static CycloC02 operator *(CycloC02 x, CycloC02 y) =>
new(x.A * y.A - x.B * y.B,
x.A * y.B + x.B * y.A - x.B * y.B);
public static CycloC02 operator +(CycloC02 x, CycloC02 y) => new(x.A + y.A, x.B + y.B);
public static CycloC02 operator -(CycloC02 x, CycloC02 y) => new(x.A - y.A, x.B - y.B);
public static bool operator ==(CycloC02 x, CycloC02 y) => x.A == y.A && x.B == y.B;
public static bool operator !=(CycloC02 x, CycloC02 y) => !(x == y);
public bool Equals(CycloC02 autre) => this == autre;
public override bool Equals(object? obj) => obj is CycloC02 c && Equals(c);
public override int GetHashCode() => HashCode.Combine(A, B);
public bool EstNul => A == RationnelC02.Zero && B == RationnelC02.Zero;
public override string ToString() => B.Numerateur.Sign == 0
? A.ToString()
: A.ToString() + " + " + B.ToString() + "w";
}
public sealed class MatCycloC02
{
public int Lignes { get; }
public int Colonnes { get; }
private readonly CycloC02[][] _e;
public MatCycloC02(int lignes, int colonnes)
{
Lignes = lignes;
Colonnes = colonnes;
_e = new CycloC02[lignes][];
for (int i = 0; i < lignes; i++)
{
_e[i] = new CycloC02[colonnes];
for (int j = 0; j < colonnes; j++) _e[i][j] = CycloC02.Zero;
}
}
public CycloC02 this[int i, int j]
{
get => _e[i][j];
set => _e[i][j] = value;
}
public static MatCycloC02 Produit(MatCycloC02 a, MatCycloC02 b)
{
var m = new MatCycloC02(a.Lignes, b.Colonnes);
for (int i = 0; i < a.Lignes; i++)
for (int j = 0; j < b.Colonnes; j++)
{
CycloC02 somme = CycloC02.Zero;
for (int k = 0; k < a.Colonnes; k++) somme += a[i, k] * b[k, j];
m[i, j] = somme;
}
return m;
}
public static MatCycloC02 MultipliePar(MatCycloC02 a, CycloC02 k)
{
var m = new MatCycloC02(a.Lignes, a.Colonnes);
for (int i = 0; i < a.Lignes; i++)
for (int j = 0; j < a.Colonnes; j++) m[i, j] = a[i, j] * k;
return m;
}
public static bool Egale(MatCycloC02 a, MatCycloC02 b)
{
if (a.Lignes != b.Lignes || a.Colonnes != b.Colonnes) return false;
for (int i = 0; i < a.Lignes; i++)
for (int j = 0; j < a.Colonnes; j++)
if (!(a[i, j] == b[i, j])) return false;
return true;
}
}
public static class WeylC02
{
// Le shift X : X|j> = |j+1 mod d>.
public static MatCplxC02 Shift(int d)
{
var m = new MatCplxC02(d, d);
for (int i = 0; i < d; i++) m[(i + 1) % d, i] = CplxC02.Un;
return m;
}
// d = 2 (w = -1, reel) et d = 4 (w = i) : les deux tiennent dans le complexe rationnel.
public static (MatCplxC02 X, MatCplxC02 Z) Lettres(int d, CplxC02 omega)
{
var x = Shift(d);
var z = new MatCplxC02(d, d);
CplxC02 puissance = CplxC02.Un;
for (int j = 0; j < d; j++)
{
z[j, j] = puissance;
puissance *= omega;
}
return (x, z);
}
public static bool RelationWeyl(MatCplxC02 x, MatCplxC02 z, CplxC02 omega) =>
MatCplxC02.Egale(MatCplxC02.Produit(z, x),
MatCplxC02.MultipliePar(MatCplxC02.Produit(x, z), omega));
// d = 3 : w = e^{2 pi i / 3} est une racine de w^2 + w + 1, l'anneau Z[w] porte le calcul.
public static (MatCycloC02 X, MatCycloC02 Z) LettresD3()
{
var x = new MatCycloC02(3, 3);
for (int i = 0; i < 3; i++) x[(i + 1) % 3, i] = CycloC02.Un;
var z = new MatCycloC02(3, 3);
z[0, 0] = CycloC02.Un;
z[1, 1] = CycloC02.Omega;
z[2, 2] = CycloC02.OmegaCarre;
return (x, z);
}
public static bool RelationWeylD3(MatCycloC02 x, MatCycloC02 z) =>
MatCycloC02.Egale(MatCycloC02.Produit(z, x),
MatCycloC02.MultipliePar(MatCycloC02.Produit(x, z), CycloC02.Omega));
// Deux lettres (a,b) et (c,e) commutent exactement quand w^(b*c - a*e) = 1,
// c'est-a-dire quand d divise b*c - a*e : le test est entier, donc exact.
public static bool Commutent(int d, (int a, int b) l1, (int a, int b) l2)
{
int k = (l1.b * l2.a) - (l1.a * l2.b);
return ((k % d) + d) % d == 0;
}
public static List<(int a, int b)> LettresListe(int d)
{
var liste = new List<(int, int)>();
for (int a = 0; a < d; a++)
for (int b = 0; b < d; b++)
if (a != 0 || b != 0) liste.Add((a, b));
return liste;
}
public static int RecenseNonCommutantes(int d)
{
var lettres = LettresListe(d);
int compte = 0;
for (int i = 0; i < lettres.Count; i++)
for (int j = i + 1; j < lettres.Count; j++)
if (!Commutent(d, lettres[i], lettres[j])) compte++;
return compte;
}
// Cliques maximales du graphe de commutation, par recherche exhaustive.
public static List<int[]> BasesDuLien(int d)
{
var lettres = LettresListe(d);
int n = lettres.Count;
var commutent = new bool[n, n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (i != j) commutent[i, j] = Commutent(d, lettres[i], lettres[j]);
var cliques = new List<int[]>();
for (int taille = 1; taille <= d - 1; taille++)
foreach (var comb in Combinaisons(n, taille))
{
bool estClique = true;
for (int i = 0; i < comb.Length && estClique; i++)
for (int j = 0; j < comb.Length; j++)
if (i != j && !commutent[comb[i], comb[j]]) { estClique = false; break; }
if (!estClique) continue;
bool absorbee = false;
foreach (var c in cliques)
if (EstSousEnsemble(comb, c)) { absorbee = true; break; }
if (!absorbee) cliques.Add(comb);
}
var maximales = new List<int[]>();
foreach (var c in cliques)
{
bool absorbee = false;
foreach (var c2 in cliques)
if (EstSousEnsemble(c, c2)) { absorbee = true; break; }
if (!absorbee) maximales.Add(c);
}
return maximales;
}
private static bool EstSousEnsemble(int[] petit, int[] grand)
{
if (petit.Length >= grand.Length) return false;
foreach (int x in petit) if (Array.IndexOf(grand, x) < 0) return false;
return true;
}
private static IEnumerable<int[]> Combinaisons(int n, int k)
{
var indices = new int[k];
for (int i = 0; i < k; i++) indices[i] = i;
while (true)
{
var resultat = new int[k];
Array.Copy(indices, resultat, k);
yield return resultat;
int i = k - 1;
while (i >= 0 && indices[i] == n - k + i) i--;
if (i < 0) yield break;
indices[i]++;
for (int j = i + 1; j < k; j++) indices[j] = indices[j - 1] + 1;
}
}
}