Skip to content

Commit

Permalink
Experiments for applying unary gates to state vector
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentInSeattle committed Jan 20, 2025
1 parent 9e69c9b commit 7fe8c14
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions Lyt.Quantics.Engine.Tests/Tests_Gates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,96 @@ static void Verify(string srcCaptionKey, string dstCaptionKey)
Assert.Fail();
}
}

[TestMethod]
public void Test_ApplyUnaryGate()
{
try
{
var identityMatrix = GateFactory.Produce(IdentityGate.Key).Matrix;
// Unary gates
foreach (string gateCaptionKey in
new string[] { "X", "Y", "Z", "H", "T", "S", "SX" })
{
var gate = GateFactory.Produce(gateCaptionKey);
for (int qubitCount = 4; qubitCount <= 8; qubitCount++)
{
Debug.WriteLine(string.Format("Gate: {0} - Qubits: {1} ", gateCaptionKey, qubitCount));
var registerSource = new QuRegister(qubitCount);
var matrix = gate.Matrix;
for (int i = 1; i < qubitCount; i++)
{
matrix = matrix.KroneckerProduct(identityMatrix);
}


var newState = matrix.Multiply(registerSource.State);
// var finalState = swap.Multiply(newState);
// Assert.IsTrue(finalState.IsAlmostEqualTo(registerSource.State));

Debug.WriteLine(registerSource.State.ToString());
Debug.WriteLine(newState.ToString());

//var clone = registerSource.DeepClone();
//var ketMap = new KetMap(qubitCount);
//clone.Swap(ketMap, j, k);
//Debug.WriteLine(clone.ToString());
//Assert.IsTrue(clone.State.IsAlmostEqualTo(newState));
//clone.Swap(ketMap, j, k);
//Assert.IsTrue(clone.State.IsAlmostEqualTo(registerSource.State));
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
Assert.Fail();
}
}
}

/*
Gate: X - Qubits: 3
000 a <0.829769; 0.558107> <-0.592318; 0.805704> e
001 b <0.441098; 0.897459> <-0.91512; 0.403183> f
010 c <0.668347; 0.743849> <-0.771147; 0.636657> g
011 d <0.208571; 0.978007> <-0.985874; 0.16749> h
100 e <-0.592318; 0.805704> <0.829769; 0.558107> a
101 f <-0.91512; 0.403183> <0.441098; 0.897459> b
110 g <-0.771147; 0.636657> <0.668347; 0.743849> c
111 h <-0.985874; 0.16749> <0.208571; 0.978007> d
Apply X on 0-00 and 1-00
Apply X on 0-01 and 1-01
Apply X on 0-10 and 1-10
Apply X on 0-11 and 1-11
*/

/*
Gate: X - Qubits: 4
0000 a <0.2982; 0.954503> <0.80153; -0.597955> i
0001 b <-0.981364; -0.192157> <0.144277; 0.989537> j
0010 c <0.471713; 0.881752> <0.675337; -0.737509> k
0011 d <-0.999988; -0.00493259> <0.327077; 0.944998> l
0100 e <-0.873557; 0.486722> <0.748949; 0.662627> m
0101 f <-0.0121682; -0.999926> <-0.939266; 0.343189> n
0110 g <-0.766925; 0.641737> <0.859812; 0.51061> o
0111 h <-0.199253; -0.979948> <-0.858358; 0.513052> p
1000 i <0.80153; -0.597955> <0.2982; 0.954503> a
1001 j <0.144277; 0.989537> <-0.981364; -0.192157> b
1010 k <0.675337; -0.737509> <0.471713; 0.881752> c
1011 l <0.327077; 0.944998> <-0.999988; -0.00493259> d
1100 m <0.748949; 0.662627> <-0.873557; 0.486722> e
1101 n <-0.939266; 0.343189> <-0.0121682; -0.999926> f
1110 o <0.859812; 0.51061> <-0.766925; 0.641737> g
1111 p <-0.858358; 0.513052> <-0.199253; -0.979948> h
*/

0 comments on commit 7fe8c14

Please sign in to comment.