Introducing QSharp - a C# quantum computing simulator

12 May 2015

C#, F#, Maths, QSharp, Quantum Computing


I remember the first time I heard about Quantum Computing - the basic concept seemed too fantastic to be true, with many of the fundamental principles seeming counter-intuitive and almost ridiculous to me. Over time, my personal interest in this field has steadily increased, culminating with my first foray into gaining an understanding of quantum information and quantum computing.

At some stage, quantum machines will make it out of the lab, become mass produced, and will require programmers to write software for them. Programmers will be required to understand what is going on 'under the hood' in order to efficiently use and exploit a quantum machine in its entirety. Programmers from an Object Oriented and decidedly classical computing background will therefore need assistance making the transition from classical information to quantum information.

There are many great resources available for those who wish to understand quantum computing, however nearly all these resources fail to cater to the complete novice, and many resources simply assume that the reader is already competent with both linear and matrix algebra, as well as intrinsically understanding how a quantum system functions. These resources are all well and good, and probably provide a great deal of insight and amazing information to those whole are capable of digesting them, but for the rest of us, they appear dense and impenetrable. How can you begin to understand something this complex in the first instance? What is needed is an introduction to the introduction, some bridging information that illustrates concepts in 'normal' language, and allows the reader to go from Novice to Somewhat Prepared.

Over the last 12 or so months, I have been developing a quantum computing and mathematics library in an Object Oriented language to introduce your everyday programmer (who might wear tennis shoes, or the occasional python boot) to quantum computing topics using a paradigm they understand. My research mentor has been ever patient and has dutifully answered all my questions, so now, hopefully I can answer yours.

Hello to you, QSharp!

I have set out to build an instructional framework that any developer may tinker with to help reveal the inner workings of a quantum computer. The language of choice here is C# (if enough noise made, a Java port may surface in the future). A C++ version will arrive at some stage. This research is called QSharp. QSharp consists of the following components:

Visit the QSharp research project homepage.

QSharp Library

The library contains all the code one may need to construct a register of computational basis states, manipulate this register with a variety of gates, take measurements, as well as perform other mathematical tasks related to quantum computing.

Writing your own quantum program using the QSharp library is fairly straight forward:

Using C#


int iQubits = 3; 

//    New up a register with some random qubit information
Register oRegister = new Register(Qubit.RandomQubits(iQubits)); 

//    Print the state vector to the console
foreach (ComputationalBasisState oComputationalBasisState in oRegister.StateVector) 
{ 
    Console.WriteLine(oComputationalBasisState); 
} 

//    Easy as!

Using F#


let iQubits = 3 

//    New up a register with some random qubit information
let oRegister = new Register(Qubit.RandomQubits(iQubits)) 

//    Print the state vector to the console
for oComputationalBasisState in oRegister.StateVector do 
    let sComputationalBasisState = oComputationalBasisState.ToString() 
    printfn "%s" sComputationalBasisState 

 //    Easy as!

You are able to manipulate a quantum simulator using familiar OO methodologies.

QSharp Command Parser

The command parser interprets a series of commands and executes them on a register. These commands are documented, and you can even execute them against use a register on this site.

Using C#


int iQubits = 3; 

//    New up a register with some random qubit information
Register oRegister = new Register(Qubit.RandomQubits(iQubits)); 

//    Execute a Controlled-NOT on qubits 0 and 1, then perform a NOT on qubit 2
string sCommand = "CNOT(0, 1); X(2);"; 
Parser oParser = new Parser(sCommand); 
ParseResult oParseResult = oParser.Parse(oRegister); 

//    Print the state vector to the console
foreach (ComputationalBasisState oComputationalBasisState in oRegister.StateVector) 
{ 
    Console.WriteLine(oComputationalBasisState); 
} 

Using F#


let iQubits = 3 

//    New up a register with some random qubit information
let oRegister = new Register(Qubit.RandomQubits(iQubits)) 

//    Execute a Controlled-NOT on qubits 0 and 1, then perform a NOT on qubit 2
let sCommand = "CNOT(0, 1); X(2);";  
let oParser = new Parser(sCommand);  
let oParseResult = oParser.Parse(oRegister);  

//    Print the state vector to the console
for oComputationalBasisState in oRegister.StateVector do 
    let sComputationalBasisState = oComputationalBasisState.ToString() 
    printfn "%s" sComputationalBasisState 

Using the parser is always going to be slower than coding up the actual manipulation using gates, and isn't really recommended at all.

You can execute these commands from code, allowing for the creation of quantum script files to execute.

Using C#


int iQubits = 3; 

//    New up a register with some random qubit information
Register oRegister = new Register(Qubit.RandomQubits(iQubits)); 

//    Read a bunch of commands from a text file
string sCommands = ""; 
using (StreamReader oStreamReader = new StreamReader(File.OpenRead(@"QuantumCommands.txt"))) 
{ 
    sCommands = oStreamReader.ReadToEnd(); 
} 

Parser oParser = new Parser(sCommands); 
ParseResult oParseResult = oParser.Parse(ref oRegister); 

//    Print the state vector to the console
foreach (ComputationalBasisState oComputationalBasisState in oRegister.StateVector) 
{ 
    Console.WriteLine(oComputationalBasisState); 
} 

Using F#


let iQubits = 3 

//    New up a register with some random qubit information
let oRegister = new Register(Qubit.RandomQubits(iQubits)) 

//    Read a bunch of commands from a text file
let sCommands = 
    ( 
        use oStreamReader = new StreamReader(File.OpenRead(@"QuantumCommands.txt")) 
        oStreamReader.ReadToEnd() 
    ) 

let oParser = new Parser(sCommands);  
let oParseResult = oParser.Parse(oRegister);  

//    Print the state vector to the console
for oComputationalBasisState in oRegister.StateVector do 
    let sComputationalBasisState = oComputationalBasisState.ToString() 
    printfn "%s" sComputationalBasisState 

Commands are separated using the semicolon in the fashion of C#, C, C++, Java etc.

Quantum Console

The command line tool is a simple console application that executes commands via the parser against a register. The command line tool is not limited in its memory usage, and you will be able to perform larger and more complex operations using this.

Over the coming weeks I will publish further articles on how to use the framework - see the download page for instructions on how you can get started.

QSharp is new software, so please treat it as such. If something doesn't make sense, please let me know. In the meantime, look forward to more articles and updates in the future.

Enjoy QSharp!


 

Copyright © 2024 carlbelle.com