/// <summary> /// Controlled not (CNOT) gate. /// [ 1 0 0 0 ] /// [ 0 1 0 0 ] /// [ 0 0 0 1 ] /// [ 0 0 1 0 ] /// /// The CNOT gate works by combining the two input qubits (control and target) into a vector (of length 4, using tensor product). /// Then it uses the assumption that the output control qubit is constant to calculate the target output qubit. /// </summary> /// <returns>Target qubit affected by the CNOT gate. The target qubit os constant (and is therefore not returned).</returns> public static qubit CNOT(qubit control, qubit target) // TODO: introduce qubit pair? or Qubit vector? { // First get tensor product of the two qubits complex[] tensorProduct = TensorProduct(control, target); // Put result here complex[] result = new complex[4]; // Multiply with CNOT gate matrix result[0] = tensorProduct[0]; result[1] = tensorProduct[1]; result[2] = tensorProduct[3]; result[3] = tensorProduct[2]; // Assumptions // 1) control output qubit α equals control qubit α // 2) control output qubit β equals control qubit β // => // result[0] = α_control * α2 => α2 = result[0] / α_control // result[1] = α_control * β2 => β2 = result[1] / α_control // => (or, if α_control is zero) // result[2] = β_control * α2 => α2 = result[2] / β_control // result[3] = β_control * β2 => β2 = result[3] / β_control if (control.Alpha != complex.Zero) { return(new qubit(result[0] / control.Alpha, result[1] / control.Alpha)); } else { return(new qubit(result[2] / control.Beta, result[3] / control.Beta)); } }
/// <summary> /// Performs tensor product of two qubit components. /// See http://www.quantiki.org/wiki/Tensor_product. /// See http://www.cs.miami.edu/~burt/learning/Csc687.041/notes/qgates.html. /// [ α1 α2 ] /// [ α1 ] [ α2 ] = [ α1 β2 ] /// [ β1 ] [ β2 ] = [ β1 α2 ] /// [ β1 β2 ] /// </summary> /// <param name="q1"></param> /// <param name="q2"></param> /// <returns>Returns an array of four items.</returns> private static complex[] TensorProduct(qubit q1, qubit q2) { return(new complex[] { q1.Alpha *q2.Alpha, q1.Alpha *q2.Beta, q1.Beta *q2.Alpha, q1.Beta *q2.Beta }); }
/// <summary> /// CNOT gate tests. /// </summary> public static void Test1() { qubit q0 = qubit.Zero; qubit q1 = qubit.One; Debug.Assert(qubit.CNOT(q0, q0) == q0); Debug.Assert(qubit.CNOT(q0, q1) == q1); Debug.Assert(qubit.CNOT(q1, q0) == q1); Debug.Assert(qubit.CNOT(q1, q1) == q0); }
public override bool Equals(object obj) { qubit q = (qubit)obj; return(q.Alpha.Equals(Alpha) && q.Beta.Equals(Beta)); }