/// <summary> /// Initializes a new instance of the <see cref="TestableComplexMatrixComplexMatrixMultiplication{TExpected}"/> class. /// </summary> /// <param name="expected">The expected result or exception.</param> /// <param name="left">The left operand.</param> /// <param name="right">The right operand.</param> public TestableComplexMatrixComplexMatrixMultiplication( TExpected expected, TestableComplexMatrix left, TestableComplexMatrix right) : base( expected, left, right, leftWritableRightWritableOps: new Func <ComplexMatrix, ComplexMatrix, ComplexMatrix>[2] { (l, r) => l * r, (l, r) => ComplexMatrix.Multiply(l, r) }, leftReadOnlyRightWritableOps: new Func <ReadOnlyComplexMatrix, ComplexMatrix, ComplexMatrix>[2] { (l, r) => l * r, (l, r) => ReadOnlyComplexMatrix.Multiply(l, r) }, leftWritableRightReadOnlyOps: new Func <ComplexMatrix, ReadOnlyComplexMatrix, ComplexMatrix>[2] { (l, r) => l * r, (l, r) => ReadOnlyComplexMatrix.Multiply(l, r) }, leftReadOnlyRightReadOnlyOps: new Func <ReadOnlyComplexMatrix, ReadOnlyComplexMatrix, ComplexMatrix>[2] { (l, r) => l * r, (l, r) => ReadOnlyComplexMatrix.Multiply(l, r) } ) { }
public void MultiplyTest() { Complex[] a = { new Complex(7, 5), new Complex(2, -3), new Complex(-5, 1) }; Complex[] b = { new Complex(1, 5), new Complex(8, -1), new Complex(-4, 8) }; Complex[] expected = { new Complex(-18, 40), new Complex(13, -26), new Complex(12, -44) }; Complex[] actual = ComplexMatrix.Multiply(a, b); Assert.IsTrue(expected.IsEqual(actual)); }
/// <summary> /// Appliy the gate to the one or more qubit /// </summary> /// <param name="qubits">The qubits.</param> public override void Apply(params Qubit[] qubits) { var reg = new QuantumRegister(qubits); var result = QuantumRegister.GetQubits(ComplexMatrix.Multiply(Matrix, reg.BitRegister)); for (int i = 0; i < qubits.Length; i++) { qubits[i].BitRegister = result[i].BitRegister; } }
/// <summary> /// Appliy the gate to the one or more qubit /// </summary> /// <param name="qubits">The qubits.</param> public virtual void Apply(params Qubit[] qubits) { Parallel.For(0, qubits.Length, (i) => { bool run = true; if (ApplyState.HasValue) { run = qubits[i].QState == ApplyState.Value; } qubits[i].BitRegister = ComplexMatrix.Multiply(Matrix, qubits[i].BitRegister); }); }
public void Main() { // Create the left operand. var data = new Complex[6] { new Complex(1, -1), new Complex(5, -5), new Complex(2, -2), new Complex(6, -6), new Complex(3, -3), new Complex(7, -7) }; var left = ComplexMatrix.Dense(3, 2, data, StorageOrder.RowMajor); Console.WriteLine("left ="); Console.WriteLine(left); // Create the right operand. data = new Complex[4] { new Complex(10, -10), new Complex(50, -50), new Complex(20, -20), new Complex(60, -60) }; var right = ComplexMatrix.Dense(2, 2, data, StorageOrder.RowMajor); Console.WriteLine("right ="); Console.WriteLine(right); // Multiply left by right. var result = left * right; Console.WriteLine(); Console.WriteLine("left * right ="); Console.WriteLine(result); // In .NET languages that do not support overloaded operators, // you can use the alternative methods named Multiply. result = ComplexMatrix.Multiply(left, right); Console.WriteLine(); Console.WriteLine("ComplexMatrix.Multiply(left, right) returns"); Console.WriteLine(); Console.WriteLine(result); // Both operators and alternative methods are overloaded to // support read-only matrix arguments. // Compute the product using a read-only wrapper of left. ReadOnlyComplexMatrix readOnlyLeft = left.AsReadOnly(); result = readOnlyLeft * right; Console.WriteLine(); Console.WriteLine("readOnlyLeft * right ="); Console.WriteLine(result); }
public TestableDoubleScalarComplexMatrixMultiplication( TExpected expected, double left, TestableComplexMatrix right) : base( expected, left, right, leftScalarRightWritableOps: new Func <double, ComplexMatrix, ComplexMatrix>[2] { (l, r) => l * r, (l, r) => ComplexMatrix.Multiply(l, r) }, leftScalarRightReadOnlyOps: new Func <double, ReadOnlyComplexMatrix, ComplexMatrix>[2] { (l, r) => l * r, (l, r) => ReadOnlyComplexMatrix.Multiply(l, r) } ) { }
public TestableComplexMatrixDoubleScalarMultiplication( TExpected expected, TestableComplexMatrix left, double right) : base( expected, left, right, leftWritableRightScalarOps: new Func <ComplexMatrix, double, ComplexMatrix>[2] { (l, r) => l * r, (l, r) => ComplexMatrix.Multiply(l, r) }, leftReadOnlyRightScalarOps: new Func <ReadOnlyComplexMatrix, double, ComplexMatrix>[2] { (l, r) => l * r, (l, r) => ReadOnlyComplexMatrix.Multiply(l, r) } ) { }
public void ComplexMatrixMultiplication() { /* * MATLAB: * prod_cc = ca3x2 * cd2x4 * prod_cm = ca3x2 * md2x4 * prod_cs = ca3x2 * s * prod_cc2 = cc2x2 * cc2x2 * prod_cm2 = cc2x2 * mc2x2 * prod_cs2 = cc2x2 * s * prod_ccv = ca3x2 * cv2.' * prod_cv = ca3x2 * v2.' * prod_ccvdl = diag(cv2) * cc2x2 * prod_ccvdr = cc2x2 * diag(cv2) * prod_cvdl = diag(v2) * cc2x2 * prod_cvdr = cc2x2 * diag(v2) */ // ComplexMatrix * ComplexMatrix ComplexMatrix prodCmCm = new ComplexMatrix(new Complex[][] { new Complex[] { -66 + (12 * j), -66 + (72 * j), -66 - (228 * j), -66 + (672 * j) }, new Complex[] { -154 + (268 * j), -154 + (300 * j), -154 + (308 * j), -154 + (368 * j) }, new Complex[] { -352 + (424 * j), -352 + (582 * j), -352 + (44 * j), -352 + (1784 * j) } }); Assert.That(_ca3X2 * _cd2X4, NumericIs.AlmostEqualTo(prodCmCm), "prod cc 1"); Assert.That(_ca3X2.Multiply(_cd2X4), NumericIs.AlmostEqualTo(prodCmCm), "prod cc 2"); // ComplexMatrix * Matrix ComplexMatrix prodCmM = new ComplexMatrix(new Complex[][] { new Complex[] { 6 - (12 * j), 12 - (24 * j), -18 + (36 * j), 72 - (144 * j) }, new Complex[] { 38 - (76 * j), 41.2 - (82.4 * j), 42 - (84 * j), 48 - (96 * j) }, new Complex[] { 68 - (136 * j), 83.8 - (167.6 * j), 30 - (60 * j), 204 - (408 * j) } }); Assert.That(_ca3X2 * _md2X4, NumericIs.AlmostEqualTo(prodCmM), "prod cm 1"); Assert.That(_ca3X2.Multiply(_md2X4), NumericIs.AlmostEqualTo(prodCmM), "prod cm 2"); // ComplexMatrix * Complex ComplexMatrix prodCmC = new ComplexMatrix(new Complex[][] { new Complex[] { 18 - (6 * j), 0 }, new Complex[] { 6 - (2 * j), 36 - (12 * j) }, new Complex[] { 42 - (14 * j), 54 - (18 * j) } }); Assert.That(_ca3X2 * _s, NumericIs.AlmostEqualTo(prodCmC), "prod cs 1"); Assert.That(_ca3X2.Multiply(_s), NumericIs.AlmostEqualTo(prodCmC), "prod cs 2"); // ComplexMatrix * ComplexMatrix (Square) ComplexMatrix prodCmCmSquare = new ComplexMatrix(new Complex[][] { new Complex[] { -1232 - (924 * j), -1368 - (1026 * j) }, new Complex[] { -1520 - (1140 * j), -1688 - (1266 * j) } }); Assert.That(_cc2X2 * _cc2X2, NumericIs.AlmostEqualTo(prodCmCmSquare), "prod cc2 1"); Assert.That(_cc2X2.Multiply(_cc2X2), NumericIs.AlmostEqualTo(prodCmCmSquare), "prod cc2 2"); ComplexMatrix prodCmCmSquareInplace = _cc2X2.Clone(); prodCmCmSquareInplace.MultiplyInplace(_cc2X2); Assert.That(prodCmCmSquareInplace, NumericIs.AlmostEqualTo(prodCmCmSquare), "prod cc2 3"); // ComplexMatrix * Matrix (Square) ComplexMatrix prodCmMSquare = new ComplexMatrix(new Complex[][] { new Complex[] { 35 - (105 * j), 52 - (156 * j) }, new Complex[] { 43 - (129 * j), 64 - (192 * j) } }); Assert.That(_cc2X2 * _mc2X2, NumericIs.AlmostEqualTo(prodCmMSquare), "prod cm2 1"); Assert.That(_cc2X2.Multiply(_mc2X2), NumericIs.AlmostEqualTo(prodCmMSquare), "prod cm2 2"); ComplexMatrix prodCmMSquareInplace = _cc2X2.Clone(); prodCmMSquareInplace.MultiplyInplace(_mc2X2); Assert.That(prodCmMSquareInplace, NumericIs.AlmostEqualTo(prodCmMSquare), "prod cm2 3"); // ComplexMatrix * Complex (Square) ComplexMatrix prodCmCSquare = new ComplexMatrix(new Complex[][] { new Complex[] { 32 - (16 * j), 36 - (18 * j) }, new Complex[] { 40 - (20 * j), 44 - (22 * j) } }); Assert.That(_cc2X2 * _s, NumericIs.AlmostEqualTo(prodCmCSquare), "prod cs2 1"); Assert.That(_cc2X2.Multiply(_s), NumericIs.AlmostEqualTo(prodCmCSquare), "prod cs2 2"); ComplexMatrix prodCmCSquareInplace = _cc2X2.Clone(); prodCmCSquareInplace.MultiplyInplace(_s); Assert.That(prodCmCSquareInplace, NumericIs.AlmostEqualTo(prodCmCSquare), "prod cs2 3"); // ComplexMatrix * ComplexVector (Column) ComplexVector prodCmCvc = new ComplexVector(new Complex[] { 42 - (54 * j), 62 + (66 * j), 170 }); Assert.That(_ca3X2 * _cv2, NumericIs.AlmostEqualTo(prodCmCvc), "prod ccv 1"); Assert.That(_ca3X2.MultiplyRightColumn(_cv2), NumericIs.AlmostEqualTo(prodCmCvc), "prod ccv 2"); // ComplexMatrix * Vector (Column) ComplexVector prodCmVc = new ComplexVector(new Complex[] { 30 - (60 * j), -14 + (28 * j), 34 - (68 * j) }); Assert.That(_ca3X2 * _v2, NumericIs.AlmostEqualTo(prodCmVc), "prod cv 1"); Assert.That(_ca3X2.MultiplyRightColumn(_v2), NumericIs.AlmostEqualTo(prodCmVc), "prod cv 2"); // ComplexMatrix * ComplexVector (Diagonal, Left) ComplexMatrix prodCmCvdl = new ComplexMatrix(new Complex[][] { new Complex[] { 64 - (112 * j), 72 - (126 * j) }, new Complex[] { 70 + (90 * j), 77 + (99 * j) } }); Assert.That(_cc2X2.MultiplyLeftDiagonal(_cv2), NumericIs.AlmostEqualTo(prodCmCvdl), "prod ccv dl 1"); ComplexMatrix prodCmCvdlInplace = _cc2X2.Clone(); prodCmCvdlInplace.MultiplyLeftDiagonalInplace(_cv2); Assert.That(prodCmCvdlInplace, NumericIs.AlmostEqualTo(prodCmCvdl), "prod ccv dl 2"); Assert.That(ComplexMatrix.Diagonal(_cv2) * _cc2X2, NumericIs.AlmostEqualTo(prodCmCvdl), "prod ccv dl 3"); // ComplexMatrix * Vector (Diagonal, Left) ComplexMatrix prodCmVdl = new ComplexMatrix(new Complex[][] { new Complex[] { 40 - (120 * j), 45 - (135 * j) }, new Complex[] { -20 + (60 * j), -22 + (66 * j) } }); Assert.That(_cc2X2.MultiplyLeftDiagonal(_v2), NumericIs.AlmostEqualTo(prodCmVdl), "prod cv dl 1"); ComplexMatrix prodCmVdlInplace = _cc2X2.Clone(); prodCmVdlInplace.MultiplyLeftDiagonalInplace(_v2); Assert.That(prodCmVdlInplace, NumericIs.AlmostEqualTo(prodCmVdl), "prod cv dl 2"); // ComplexMatrix * ComplexVector (Diagonal, Right) ComplexMatrix prodCmCvdr = new ComplexMatrix(new Complex[][] { new Complex[] { 64 - (112 * j), 63 + (81 * j) }, new Complex[] { 80 - (140 * j), 77 + (99 * j) } }); Assert.That(_cc2X2.MultiplyRightDiagonal(_cv2), NumericIs.AlmostEqualTo(prodCmCvdr), "prod ccv dr 1"); ComplexMatrix prodCmCvdrInplace = _cc2X2.Clone(); prodCmCvdrInplace.MultiplyRightDiagonalInplace(_cv2); Assert.That(prodCmCvdrInplace, NumericIs.AlmostEqualTo(prodCmCvdr), "prod ccv dr 2"); Assert.That(_cc2X2 * ComplexMatrix.Diagonal(_cv2), NumericIs.AlmostEqualTo(prodCmCvdr), "prod ccv dr 3"); // ComplexMatrix * Vector (Diagonal, Right) ComplexMatrix prodCmVdr = new ComplexMatrix(new Complex[][] { new Complex[] { 40 - (120 * j), -18 + (54 * j) }, new Complex[] { 50 - (150 * j), -22 + (66 * j) } }); Assert.That(_cc2X2.MultiplyRightDiagonal(_v2), NumericIs.AlmostEqualTo(prodCmVdr), "prod cv dr 1"); ComplexMatrix prodCmVdrInplace = _cc2X2.Clone(); prodCmVdrInplace.MultiplyRightDiagonalInplace(_v2); Assert.That(prodCmVdrInplace, NumericIs.AlmostEqualTo(prodCmVdr), "prod cv dr 2"); }
public void TestComplexMatrix_Multiplicative() { /* * MATLAB: * prod_cc = ca3x2 * cd2x4 * prod_cm = ca3x2 * md2x4 * prod_cs = ca3x2 * s * prod_cc2 = cc2x2 * cc2x2 * prod_cm2 = cc2x2 * mc2x2 * prod_cs2 = cc2x2 * s * prod_ccv = ca3x2 * cv2.' * prod_cv = ca3x2 * v2.' * prod_ccvdl = diag(cv2) * cc2x2 * prod_ccvdr = cc2x2 * diag(cv2) * prod_cvdl = diag(v2) * cc2x2 * prod_cvdr = cc2x2 * diag(v2) */ // ComplexMatrix * ComplexMatrix ComplexMatrix prod_cc = new ComplexMatrix(new Complex[][] { new Complex[] { -66 + 12 * j, -66 + 72 * j, -66 - 228 * j, -66 + 672 * j }, new Complex[] { -154 + 268 * j, -154 + 300 * j, -154 + 308 * j, -154 + 368 * j }, new Complex[] { -352 + 424 * j, -352 + 582 * j, -352 + 44 * j, -352 + 1784 * j } }); NumericAssert.AreAlmostEqual(prod_cc, ca3x2 * cd2x4, "prod cc 1"); NumericAssert.AreAlmostEqual(prod_cc, ca3x2.Multiply(cd2x4), "prod cc 2"); // ComplexMatrix * Matrix ComplexMatrix prod_cm = new ComplexMatrix(new Complex[][] { new Complex[] { 6 - 12 * j, 12 - 24 * j, -18 + 36 * j, 72 - 144 * j }, new Complex[] { 38 - 76 * j, 41.2 - 82.4 * j, 42 - 84 * j, 48 - 96 * j }, new Complex[] { 68 - 136 * j, 83.8 - 167.6 * j, 30 - 60 * j, 204 - 408 * j } }); NumericAssert.AreAlmostEqual(prod_cm, ca3x2 * md2x4, "prod cm 1"); NumericAssert.AreAlmostEqual(prod_cm, ca3x2.Multiply(md2x4), "prod cm 2"); // ComplexMatrix * Complex ComplexMatrix prod_cs = new ComplexMatrix(new Complex[][] { new Complex[] { 18 - 6 * j, 0 }, new Complex[] { 6 - 2 * j, 36 - 12 * j }, new Complex[] { 42 - 14 * j, 54 - 18 * j } }); NumericAssert.AreAlmostEqual(prod_cs, ca3x2 * s, "prod cs 1"); NumericAssert.AreAlmostEqual(prod_cs, ca3x2.Multiply(s), "prod cs 2"); // ComplexMatrix * ComplexMatrix (Square) ComplexMatrix prod_cc2 = new ComplexMatrix(new Complex[][] { new Complex[] { -1232 - 924 * j, -1368 - 1026 * j }, new Complex[] { -1520 - 1140 * j, -1688 - 1266 * j } }); NumericAssert.AreAlmostEqual(prod_cc2, cc2x2 * cc2x2, "prod cc2 1"); NumericAssert.AreAlmostEqual(prod_cc2, cc2x2.Multiply(cc2x2), "prod cc2 2"); ComplexMatrix prod_cc2_inplace = cc2x2.Clone(); prod_cc2_inplace.MultiplyInplace(cc2x2); NumericAssert.AreAlmostEqual(prod_cc2, prod_cc2_inplace, "prod cc2 3"); // ComplexMatrix * Matrix (Square) ComplexMatrix prod_cm2 = new ComplexMatrix(new Complex[][] { new Complex[] { 35 - 105 * j, 52 - 156 * j }, new Complex[] { 43 - 129 * j, 64 - 192 * j } }); NumericAssert.AreAlmostEqual(prod_cm2, cc2x2 * mc2x2, "prod cm2 1"); NumericAssert.AreAlmostEqual(prod_cm2, cc2x2.Multiply(mc2x2), "prod cm2 2"); ComplexMatrix prod_cm2_inplace = cc2x2.Clone(); prod_cm2_inplace.MultiplyInplace(mc2x2); NumericAssert.AreAlmostEqual(prod_cm2, prod_cm2_inplace, "prod cm2 3"); // ComplexMatrix * Complex (Square) ComplexMatrix prod_cs2 = new ComplexMatrix(new Complex[][] { new Complex[] { 32 - 16 * j, 36 - 18 * j }, new Complex[] { 40 - 20 * j, 44 - 22 * j } }); NumericAssert.AreAlmostEqual(prod_cs2, cc2x2 * s, "prod cs2 1"); NumericAssert.AreAlmostEqual(prod_cs2, cc2x2.Multiply(s), "prod cs2 2"); ComplexMatrix prod_cs2_inplace = cc2x2.Clone(); prod_cs2_inplace.MultiplyInplace(s); NumericAssert.AreAlmostEqual(prod_cs2, prod_cs2_inplace, "prod cs2 3"); // ComplexMatrix * ComplexVector (Column) ComplexVector prod_ccv = new ComplexVector(new Complex[] { 42 - 54 * j, 62 + 66 * j, 170 }); NumericAssert.AreAlmostEqual(prod_ccv, ca3x2 * cv2, "prod ccv 1"); NumericAssert.AreAlmostEqual(prod_ccv, ca3x2.MultiplyRightColumn(cv2), "prod ccv 2"); // ComplexMatrix * Vector (Column) ComplexVector prod_cv = new ComplexVector(new Complex[] { 30 - 60 * j, -14 + 28 * j, 34 - 68 * j }); NumericAssert.AreAlmostEqual(prod_cv, ca3x2 * v2, "prod cv 1"); NumericAssert.AreAlmostEqual(prod_cv, ca3x2.MultiplyRightColumn(v2), "prod cv 2"); // ComplexMatrix * ComplexVector (Diagonal, Left) ComplexMatrix prod_ccvdl = new ComplexMatrix(new Complex[][] { new Complex[] { 64 - 112 * j, 72 - 126 * j }, new Complex[] { 70 + 90 * j, 77 + 99 * j } }); NumericAssert.AreAlmostEqual(prod_ccvdl, cc2x2.MultiplyLeftDiagonal(cv2), "prod ccv dl 1"); ComplexMatrix prod_ccvdl_inplace = cc2x2.Clone(); prod_ccvdl_inplace.MultiplyLeftDiagonalInplace(cv2); NumericAssert.AreAlmostEqual(prod_ccvdl, prod_ccvdl_inplace, "prod ccv dl 2"); NumericAssert.AreAlmostEqual(prod_ccvdl, ComplexMatrix.Diagonal(cv2) * cc2x2, "prod ccv dl 3"); // ComplexMatrix * Vector (Diagonal, Left) ComplexMatrix prod_cvdl = new ComplexMatrix(new Complex[][] { new Complex[] { 40 - 120 * j, 45 - 135 * j }, new Complex[] { -20 + 60 * j, -22 + 66 * j } }); NumericAssert.AreAlmostEqual(prod_cvdl, cc2x2.MultiplyLeftDiagonal(v2), "prod cv dl 1"); ComplexMatrix prod_cvdl_inplace = cc2x2.Clone(); prod_cvdl_inplace.MultiplyLeftDiagonalInplace(v2); NumericAssert.AreAlmostEqual(prod_cvdl, prod_cvdl_inplace, "prod cv dl 2"); // ComplexMatrix * ComplexVector (Diagonal, Right) ComplexMatrix prod_ccvdr = new ComplexMatrix(new Complex[][] { new Complex[] { 64 - 112 * j, 63 + 81 * j }, new Complex[] { 80 - 140 * j, 77 + 99 * j } }); NumericAssert.AreAlmostEqual(prod_ccvdr, cc2x2.MultiplyRightDiagonal(cv2), "prod ccv dr 1"); ComplexMatrix prod_ccvdr_inplace = cc2x2.Clone(); prod_ccvdr_inplace.MultiplyRightDiagonalInplace(cv2); NumericAssert.AreAlmostEqual(prod_ccvdr, prod_ccvdr_inplace, "prod ccv dr 2"); NumericAssert.AreAlmostEqual(prod_ccvdr, cc2x2 * ComplexMatrix.Diagonal(cv2), "prod ccv dr 3"); // ComplexMatrix * Vector (Diagonal, Right) ComplexMatrix prod_cvdr = new ComplexMatrix(new Complex[][] { new Complex[] { 40 - 120 * j, -18 + 54 * j }, new Complex[] { 50 - 150 * j, -22 + 66 * j } }); NumericAssert.AreAlmostEqual(prod_cvdr, cc2x2.MultiplyRightDiagonal(v2), "prod cv dr 1"); ComplexMatrix prod_cvdr_inplace = cc2x2.Clone(); prod_cvdr_inplace.MultiplyRightDiagonalInplace(v2); NumericAssert.AreAlmostEqual(prod_cvdr, prod_cvdr_inplace, "prod cv dr 2"); }