private static ComplexMatrix GetMatrix(int registerSize, int control1Index, int control2Index, int notIndex) { var matrixSize = (int)System.Math.Pow(2, registerSize); var matrix = new ComplexMatrix(new OrderedPair(matrixSize, matrixSize)); var control1Bit = (int)System.Math.Pow(2, registerSize - control1Index - 1); var control2Bit = (int)System.Math.Pow(2, registerSize - control2Index - 1); var notBit = (int)System.Math.Pow(2, registerSize - notIndex - 1); // Go through each column of the matrix and determine which row it should map to for (var i = 0; i < matrixSize; i++) { // Are the control bits set to 1 if ((i & control1Bit) > 0 && (i & control2Bit) > 0) { // Is the not bit set to 1 if ((i & notBit) > 0) { matrix[i - notBit][i] = new Complex(1, 0); } else { matrix[i + notBit][i] = new Complex(1, 0); } } else { matrix[i][i] = new Complex(1, 0); } } return matrix; }
private static ComplexMatrix GetMatrix(int registerSize) { var matrixSize = (int)System.Math.Pow(2, registerSize); var matrix = new ComplexMatrix(new OrderedPair(matrixSize, matrixSize)); for (var i = 0; i < matrixSize; i++) { matrix[i][i] = new Complex(1, 0); } return matrix; }
private static ComplexMatrix GetMatrix(int registerSize, int controlIndex, int flip1Index, int flip2Index) { var matrixSize = (int)System.Math.Pow(2, registerSize); var matrix = new ComplexMatrix(new OrderedPair(matrixSize, matrixSize)); var controlBit = (int)System.Math.Pow(2, registerSize - controlIndex - 1); var flip1Bit = (int)System.Math.Pow(2, registerSize - flip1Index - 1); var flip2Bit = (int)System.Math.Pow(2, registerSize - flip2Index - 1); // Go through each column of the matrix and determine which row it should map to for (var i = 0; i < matrixSize; i++) { // Is the control bit set to 1 if ((i & controlBit) > 0) { // Is the first flip bit set to 1 if ((i & flip1Bit) > 0) { // Is the second flip bit set to 1 if ((i & flip2Bit) > 0) { // The flip bits are the same, so no need to flip matrix[i][i] = new Complex(1, 0); } else { matrix[i - flip1Bit + flip2Bit][i] = new Complex(1, 0); } } else { // Is the second flip bit set to 1 if ((i & flip2Bit) > 0) { matrix[i + flip1Bit - flip2Bit][i] = new Complex(1, 0); } else { // The flip bits are the same, so no need to flip matrix[i][i] = new Complex(1, 0); } } } else { matrix[i][i] = new Complex(1, 0); } } return matrix; }
public Complex[] Root(int n) { var modulus = System.Math.Pow(this.Modulus, 1.0 / n); var phase = this.Phase / n; var result = new Complex[n]; for (var i = 0; i < n; i++) { result[i] = new Complex(modulus, 2 * System.Math.PI * i / n + phase, Representation.Polar); } return result; }
public void ComplexRoot() { var expected = new Complex(2, 11.0 / 12.0 * System.Math.PI, Representation.Polar); var resultIsInArray = false; var roots = c8.Root(3); foreach (var root in roots) { if (root == expected) { resultIsInArray = true; break; } } Assert.IsTrue(resultIsInArray); }