19 Jul 2015
C#, F#, QSharp, Quantum Computing
The QSharp library will allow you to generate a quantum circuit diagram from a complete set of parsed commands.
First, construct a new QuantumCircuit type from a Register type and a List<Command> list, generated by parsing a command through the CommandParser type.
In the following example, diagrams are generated for three quantum circuits, the in-place majority (MAJ) gate and both 2 and 3 CNOT versions of the unmajority-and-add (UMA) gate as described by Cuccaro, Draper, Kutin and Moulton in their 2008 paper 'A new quantum ripple-carry addition circuit'.
Consider the following C# code, which demonstrates the generation of quantum circuits:
public static void Main(string[] Arguments) { // Construct a register with 3 random qubits Register oRegister = new Register(3); // Always check that we start off with a valid register if (oRegister.IsNormalised() == false) { oRegister.Normalise(); } // Majority (MAJ) Circuit GenerateCircuit(oRegister, "CNOT(2,1);CNOT(2,0);TOFFOLI(0,1,2);", "001-MAJ"); // 2 CNOT Unmajority-add (UMA) Circuit GenerateCircuit(oRegister, "TOFFOLI(0,1,2);CNOT(2,0);CNOT(0,1);", "002-2CNOT-UMA"); // 2 CNOT Unmajority-add (UMA) Circuit GenerateCircuit(oRegister, "X(1);CNOT(0,1);TOFFOLI(0,1,2);X(1);CNOT(2,0);CNOT(2,1);", "003-3CNOT-UMA"); } private static void GenerateCircuit(Register Register, string Command, string CircuitImageName) { Parser oParser = new Parser(Command); ParseResult oParseResult = oParser.Parse(Register); QuantumCircuit oQuantumCircuit = new QuantumCircuit(Register, oParseResult.Commands); using (Bitmap oBitmap = oQuantumCircuit.ToBitmap((oParseResult.Commands.Count * 50), 100)) { oBitmap.Save(String.Format("{0}.bmp", CircuitImageName)); } }
And again, this time in F#
type Program() = [<EntryPoint>] static let main argv = // Construct a register with 3 random qubits let oRegister = new Register(3) // Always check that we start off with a valid register if oRegister.IsNormalised() = false then oRegister.Normalise() // Majority (MAJ) Circuit Program.GenerateCircuit(oRegister, "CNOT(2,1);CNOT(2,0);TOFFOLI(0,1,2);", "001-MAJ") // 2 CNOT Unmajority-add (UMA) Circuit Program.GenerateCircuit(oRegister, "TOFFOLI(0,1,2);CNOT(2,0);CNOT(0,1);", "002-2CNOT-UMA") // 2 CNOT Unmajority-add (UMA) Circuit Program.GenerateCircuit(oRegister, "X(1);CNOT(0,1);TOFFOLI(0,1,2);X(1);CNOT(2,0);CNOT(2,1);", "003-3CNOT-UMA") 0 // return an integer exit code static member GenerateCircuit(register: Register, command: string, circuitimagename: string) = let oParser = new Parser(command) let oParseResult = oParser.Parse(register) let oQuantumCircuit = new QuantumCircuit(register, oParseResult.Commands) (use oBitmap = oQuantumCircuit.ToBitmap((oParseResult.Commands.Count * 50), 100) ( let sFileName = sprintf "%s.bmp" circuitimagename oBitmap.Save(sFileName) ))
These images will be saved into the same directory as the executable, usually, bin\Debug.
Copyright © 2025 carlbelle.com