public Complex[,] GetMatrix(int bitLen, int bit1Pos, int bit2Pos) { var matrix = GetMatrix(); var table = AlgebraUtility.LookupTable(matrix); var mLen = (1 << bitLen); matrix = new Complex[mLen, mLen]; for (int i = 0; i < mLen; i++) { var x = (BinaryUtility.HasBit(i, bit1Pos) ? 2 : 0) + (BinaryUtility.HasBit(i, bit2Pos) ? 1 : 0); foreach (var y in table[x]) { var j = BinaryUtility.SetBit( i, bit1Pos, BinaryUtility.HasBit(y.Key, 1) ); j = BinaryUtility.SetBit( j, bit2Pos, BinaryUtility.HasBit(y.Key, 0) ); matrix[i, j] = y.Value; matrix[j, i] = y.Value; } } return(matrix); }
public bool Measure(int key) { int pos = posMap[key]; int val = Sample(); bool m = BinaryUtility.HasBit(val, pos); Collapse(pos, m); return(m); }
private void Collapse(int pos, bool b) { for (int i = 0; i < v.Length; i++) { if (BinaryUtility.HasBit(i, pos) != b) { v[i] = Complex.Zero; } } var sum = Math.Sqrt( v.Sum(x => x.Magnitude * x.Magnitude) ); for (int i = 0; i < v.Length; i++) { v[i] /= sum; } }