/// <summary> /// Initialise a gate by the given line code. /// </summary> /// <param name="code"> The line code which represents a gate. </param> /// <returns> /// The gate which is represented by the given line. If the given /// line doesn't represent a valid gate or a gate which is not /// implemented, then is null returned. /// </returns> private static Gate InitialiseGate(string code) { if (!Regex.Match(code, @"^\S+ (q[\D+],)*(q[\D])").Success || Regex.Match(code, @"^qreg\s").Success) { return(null); } string prefix = code.Split()[0].ToUpper(); List <double> numbers = Regex.Split(code, @"[^0-9\.]+").Where(x => !string.IsNullOrEmpty(x)).Select(x => Convert.ToDouble(x)).ToList(); switch (prefix) { case "CX": // CNOT gate return(new CNOT((int)numbers[0], (int)numbers[1])); case "H": // Hadamard gate return(U3.GetHadamardGate((int)numbers[0])); case "U3": case "U": // U3 rotation gate return(new U3((int)numbers[3], numbers[0], numbers[1], numbers[2], GatePart.U3)); case "RX": // Rotation over x axis return(new U3((int)numbers[1], numbers[0], -Math.PI / 2, Math.PI / 2, GatePart.Rx)); case "RY": // Rotation over y axis return(new U3((int)numbers[1], numbers[0], 0, 0, GatePart.Ry)); case "RZ": // Rotation over z axis return(new U3((int)numbers[1], 0, numbers[0], 0, GatePart.Rz)); case "X": // Pauli X gate return(new U3((int)numbers[0], Math.PI, -Math.PI / 2, Math.PI / 2, GatePart.X)); case "Y": // Pauli Y gate return(new U3((int)numbers[0], Math.PI, 0, 0, GatePart.Y)); case "Z": // Pauli Z gate return(new U3((int)numbers[0], 0, Math.PI, 0, GatePart.Z)); case "T": // T gate return(new U3((int)numbers[0], 0, Math.PI / 4, 0, GatePart.T)); case "TDG": // Invers of t gate return(new U3((int)numbers[0], 0, -Math.PI / 4, 0, GatePart.TDG)); } return(null); }
/// <summary> /// See <see cref="Architecture.AddSwapGates(PhysicalCircuit, Swap)"/>. /// </summary> public override void AddSwapGates(PhysicalCircuit circuit, Swap swap) { if (HasConnection(swap.Qubit1, swap.Qubit2)) { circuit.AddGate(new CNOT(swap.Qubit1, swap.Qubit2)); circuit.AddGate(U3.GetHadamardGate(swap.Qubit1)); circuit.AddGate(U3.GetHadamardGate(swap.Qubit2)); circuit.AddGate(new CNOT(swap.Qubit1, swap.Qubit2)); circuit.AddGate(U3.GetHadamardGate(swap.Qubit1)); circuit.AddGate(U3.GetHadamardGate(swap.Qubit2)); circuit.AddGate(new CNOT(swap.Qubit1, swap.Qubit2)); } else { circuit.AddGate(new CNOT(swap.Qubit2, swap.Qubit1)); circuit.AddGate(U3.GetHadamardGate(swap.Qubit1)); circuit.AddGate(U3.GetHadamardGate(swap.Qubit2)); circuit.AddGate(new CNOT(swap.Qubit2, swap.Qubit1)); circuit.AddGate(U3.GetHadamardGate(swap.Qubit1)); circuit.AddGate(U3.GetHadamardGate(swap.Qubit2)); circuit.AddGate(new CNOT(swap.Qubit2, swap.Qubit1)); } }