Quark

The Team

  • Daria Jung
  • Jamis Johnson
  • Linxi Fan (Jim)
  • Parthiban Loganathan

Why Quark?

 

Quantum computing has the potential to become a reality in the next few decades. We're thinking ahead of the curve and have developed a language that makes it easy to build quantum circuits, which consist of quantum gates and quantum registers holding qubits.

Quantum Computing will allow us to:

  • Factorize large integers in polynomial time (Shor's algorithm)
  • Search unsorted database in sublinear time (Grover's Search)
  • Build the Infinite Improbability Drive and solve intergalactic travel

What is Quark?

"QUantum Analysis and Realization Kit"

 

A high-level language for quantum computing that encapsulates mathematical operations and quantum computing specific components like quantum registers.

 

A futuristic compiler on your laptop.

Features

  • Easy-to-use, high-level language influenced by MATLAB and Python
  • Useful built-in data types for fractions and complex numbers
  • Support for matrices and matrix operations
  • Quantum registers and ability to query them
  • Built-in quantum gate functions
  • Imports
  • Informative semantic error messages
  • Cross-platform

 

 

How did we do it?

Compiler flow:

  • Preprocessor
  • Scanner
  • Parser
  • AST
  • Semantic Checker
  • SAST
  • Code Generator
  • OS-aware g++ invocation
  • Quantum Simulator (Quark++)

Preprocessor

  • Resolves import statements before the scanner and parser stages
  • Recursively finds all imports and prepends them to the file
  • Handles cyclic and repetitive imports

 

 

Scanner

Based on MicroC

All the usual tokens + specific ones for

  • fractions : 1$2
  • complex numbers : i(3, 4)
    • i can still be used as a variable, not a function
  • matrix operations : [|1,2; 3,4|], A', A ** B
  • quantum registers and querying :  qreg, <|10,1|>,
    q ? [1:5],  q ?' 3

 

Parser

Grammar was developed incrementally

  • Quantum registers query
  • Matrix and high dimensional array literals
  • Membership
  • Fractions, complex numbers
  • Pythonic for-loops

 

Pacman Parsing

Some example rules

expr:  
  ...
  /* Query */
  | expr ? expr    
  | expr ? [ : expr ]
  | expr ? [expr : expr ]
  ...
  /* Membership testing with keyword 'in' */
  | expr in expr
  ...
  /* literals */
  | expr $ expr                          
  | [| matrix_row_list |]
  | i( expr , expr ) 
  | <| expr , expr |>  

iterator:
  | ident in [range]
  | datatype ident in [range] 
  | datatype ident in expr

Lexical and syntactical analysis complete

Now we need semantic checks

Valid syntax doesn't always make sense

The importance of semantic checks in real life

Semantic Checker

  • StrMap hashtables
    • Variable table



       
    • Function table

Semantic Checker

  • Environment struct



Semantic Checker

  • From AST to SAST

 

 

Semantic Checker

  • Traverse AST recursively to produce SAST

 

 

Semantic Checker

  • Tag the SAST with op_tag constants to facilitate code generation

 

 

Semantic Checker

  • Tag the SAST with op_tag constants to facilitate code generation

 

 

Semantic Checker

  • Separate source file for built-in functions (e.g. quantum gates)
  • Can be overridden by users
  • print() and print_noline() support any number of args of any type

 

 

Semantic Checker

Error messages

  • "A function is confused with a variable: u"
  • "Function foo() is forward declared, but called without definition"
  • "If statement predicate must be bool, but fraction provided"
  • "Array style for-loop must operate on array type, not complex[|]"
  • "Matrix element unsupported: string"
  • "Incompatible operands for **: string -.- fraction"
  • "All rows in a matrix must have the same length"

Code Generation

Code Generation

  • Recursively walks the SAST to generate a string of valid C++ program
  • The generated string, concatenated with a header string, should compile with the simulator and Eigen library




     
  • No exception should be thrown at this stage

Code Generation

 

  • int → C++ int64_t
  • float → C++ primitive float
  • string → C++ std::string
  • complex → C++ std::complex<float>
  • arrays → C++ std::vector<>
  • matrices → Eigen::Matrix<float, Dynamic, Dynamic>
  • fraction → Quark++ Frac class
  • qreg → Quark++ Qureg class

Type Mapping

Code Generation

Op Tag

 

Code Generation

Pythonic for-loop

 

  • [len(a) : 0 : step(x)]   the step size can be negative
  • Whether step(x) is negative or not can only be determined at runtime
  • We use system generated temp variables to handle this.
    Always prefixed with "_QUARK_" and followed by a string of 10 random chars.

Code Generation

Pythonic for-loop

 

Code Generation

More examples

 

Code Generation

More examples

 

Simulator: Quark++

Simulator: Quark++

  • Written over the summer. Built from scratch except for the Eigen matrix library.
  • Features optimized C++11 code for quantum register manipulation and quantum gates/operations. 
  • Can be used as a standalone library for any quantum computing education or research project
  • Minor modification to accomodate the Quark language.

User Interface

  • Command line args
    • -s: source
    • -c: generated.cpp
    • -o: excutable
    • -sc, -sco
    • -static 
  • Precompiled dynamic/static libraries
  • Minimal user effort to install dependencies
  • OS aware. Supports all major OSes

Let's look at some code

A simple Hello World

def int main:
{
    print("Hello, Ground!");

    return 0;
}

It was unfortunately a very short hello for our whale friend

Defining types

int i = 4;
float f = 2.0;
bool b = true;
string s = "So Long, and Thanks for All the Fish";
string[] arr = ["Ford", "Prefect", "Zaphod", "Beeblebrox"];
int[][] arr2 = [[1,2,3],[4,5,6]];

fraction f = 84$2;
complex c = i(5.0, 7.0);
float[|] = [|1.0, 2.1; 3.2, 46.1|];

qreg q = <| 42, 0 |>;

Special operations

% FRACTIONS
frac foo = 2$3;
~foo; % 3$2
int i = 5;
i > foo; % true 

% COMPLEX NUMBERS
complex cnum = i(3.0, 1);
real(cnum); % 3.0
imag(cnum); % 1
complex cnum2 = i(9) % this gives us i(9, 0)

% MATRICES
float[|] mat = [| 1.2, 3.4; 5.6, 7.8 |];
mat[2, 1];
mat'; % transpose matrix

% QUANTUM REGISTERS
qreg q = <|10, 3|>;
hadamard(q);
q ? [2:10];  % measures qubit 2 to 10

Control flow

if x > 0:
    print("positive");
elif x < 0:
    print("negative");
else:
    print("zero");


while x > 42: {
    print(x);
    x = x - 1;
}


int[] arr = [1,2,3];
for int i in arr:
    print i;


int i;
for i in [1:10]
for int i in [1:10:2]

Imports

import ../lib/mylib1;
import ../lib/herlib2;

import imported_file;

def int main:
{
    return imported_file.function(5);
}

So Fancy!

Simple GCD

def int gcd: int x, int y
{
    while y != 0:
    {
        int r = x mod y;
        x = y;
        y = r;
    }
    return x;
}

def int main:
{
    % prints the greatest common divisor of 10 and 20
    print(gcd(10, 20));
    return 0;
}

Quantum Computing Demo Time

 

 

 

 

Hang on to your Towel!

 

Let's see Shor's algorithm and Grover's Search in action! Real quantum computing programs running on a not-so-real quantum computer (our simulator)

What did we learn?

Start early!!!

OCaml:

[oh-kam-uh l]

Mostly harmless

Interacting with other homo sapiens

  • Group projects are painful (more so than Vogon poetry)
  • Allocating work strategically avoids bottlenecks in pipeline
  • Better communication saves time and headaches
  • Dictatorship > Democracy when it comes to software
Made with Slides.com