/* * Generalized Hadamard gate */ public static QuantumGate HadamardGateOfLength(int registerLength) { if (registerLength == 1) { return(QuantumGate.HadamardGate); } else { return(new QuantumGate(QuantumGate.HadamardGate, QuantumGate.HadamardGateOfLength(registerLength - 1))); } }
/* * Determines whether the specified quantum gate is equal to the current quantum gate */ public override bool Equals(object obj) { QuantumGate quantumGate = obj as QuantumGate; if (quantumGate == null || this.Matrix.ColumnCount != quantumGate.Matrix.ColumnCount || this.Matrix.RowCount != quantumGate.Matrix.RowCount) { return(false); } return(this.Matrix.Equals(quantumGate.Matrix)); }
/* * Determines whether the specified quantum gate is equal to the current quantum gate, ignoring floating-point precision issues */ public bool AlmostEquals(object obj) { QuantumGate quantumGate = obj as QuantumGate; if (quantumGate == null || this.Matrix.ColumnCount != quantumGate.Matrix.ColumnCount || this.Matrix.RowCount != quantumGate.Matrix.RowCount) { return(false); } return(Precision.AlmostEqual <Complex>(this.Matrix, quantumGate.Matrix, 15)); }
/* * Controlled gate */ public static QuantumGate ControlledGate(QuantumGate gate) { if (gate.Matrix.ColumnCount != 2 || gate.Matrix.RowCount != 2) { throw new ArgumentException("A controlled gate can only be created from a unary gate."); } return(new QuantumGate(new Complex[, ] { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, gate.Matrix.At(0, 0), gate.Matrix.At(0, 1) }, { 0, 0, gate.Matrix.At(1, 0), gate.Matrix.At(1, 1) }, })); }