Browse Source

Move project to CPP and create QReg class

- Include Eigen matrix library
master
watkinsr 3 years ago
parent
commit
2f402b8146
  1. 13
      Makefile
  2. 57
      README.html
  3. 4
      README.org
  4. BIN
      bin/parse
  5. 60
      include/QReg.h
  6. 4
      include/parse.h
  7. BIN
      obj/parse.o
  8. 23
      src/parse.cpp

13
Makefile

@ -5,24 +5,23 @@
# @version 0.1
IDIR=include
CC=gcc
CFLAGS=-I$(IDIR)
CPPFLAGS=-g -I$(IDIR) -I/usr/include/eigen3 -lstdc++
LIBS=-lm
LIBS=
ODIR=obj
LDIR=lib
BDIR=bin
_DEPS = parse.h
_DEPS = parse.h QReg.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ = parse.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/%.o: src/%.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
$(ODIR)/%.o: src/%.cpp $(DEPS)
$(CXX) -c -o $@ $< $(CPPFLAGS)
MKDIR_P = mkdir -p
@ -31,7 +30,7 @@ MKDIR_P = mkdir -p
all: directories parse
parse: $(OBJ)
$(CC) -o $(BDIR)/$@ $^ $(CFLAGS) $(LIBS)
$(CXX) -o $(BDIR)/$@ $^ $(CPPFLAGS)
directories: ${BDIR} ${ODIR}

57
README.html

@ -3,7 +3,7 @@
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2020-10-02 Fri 07:19 -->
<!-- 2020-10-02 Fri 07:32 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>QPL Interpreter</title>
@ -225,20 +225,19 @@
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org4b06fdb">1. Prerequisites</a></li>
<li><a href="#org6a9647f">2. Build</a></li>
<li><a href="#org0359988">3. Usage</a></li>
<li><a href="#orgf85dfbf">4. Description</a></li>
<li><a href="#org905d7f3">5. Example input(HADAMARD -&gt; CNOT)</a></li>
<li><a href="#org33ea045">6. TODOS</a></li>
<li><a href="#org58fad8f">7. Documentation</a></li>
<li><a href="#org028c422">8. Licence</a></li>
<li><a href="#org752e057">1. Prerequisites</a></li>
<li><a href="#orge750c52">2. Build</a></li>
<li><a href="#org90f7b11">3. Usage</a></li>
<li><a href="#org5a820fc">4. Description</a></li>
<li><a href="#org4189a63">5. Example input(HADAMARD -&gt; CNOT)</a></li>
<li><a href="#orgef0e774">6. TODOS</a></li>
<li><a href="#org6fc4821">7. Licence</a></li>
</ul>
</div>
</div>
<div id="outline-container-org4b06fdb" class="outline-2">
<h2 id="org4b06fdb"><span class="section-number-2">1</span> Prerequisites</h2>
<div id="outline-container-org752e057" class="outline-2">
<h2 id="org752e057"><span class="section-number-2">1</span> Prerequisites</h2>
<div class="outline-text-2" id="text-1">
<ul class="org-ul">
<li>gcc</li>
@ -246,8 +245,8 @@
</div>
</div>
<div id="outline-container-org6a9647f" class="outline-2">
<h2 id="org6a9647f"><span class="section-number-2">2</span> Build</h2>
<div id="outline-container-orge750c52" class="outline-2">
<h2 id="orge750c52"><span class="section-number-2">2</span> Build</h2>
<div class="outline-text-2" id="text-2">
<div class="org-src-container">
<pre class="src src-shell"><span style="color: #ECBE7B;">git</span> clone <span style="color: #ECBE7B;">git</span>@git.ryanwatkins.me:base/sqint.git;
@ -258,8 +257,8 @@
</div>
</div>
<div id="outline-container-org0359988" class="outline-2">
<h2 id="org0359988"><span class="section-number-2">3</span> Usage</h2>
<div id="outline-container-org90f7b11" class="outline-2">
<h2 id="org90f7b11"><span class="section-number-2">3</span> Usage</h2>
<div class="outline-text-2" id="text-3">
<div class="org-src-container">
<pre class="src src-shell">./bin/parse
@ -268,8 +267,8 @@
</div>
</div>
<div id="outline-container-orgf85dfbf" class="outline-2">
<h2 id="orgf85dfbf"><span class="section-number-2">4</span> Description</h2>
<div id="outline-container-org5a820fc" class="outline-2">
<h2 id="org5a820fc"><span class="section-number-2">4</span> Description</h2>
<div class="outline-text-2" id="text-4">
<p>
Currently this implements some quantum gates and quantum adder operations as found in research papers.
@ -277,8 +276,8 @@ Currently this implements some quantum gates and quantum adder operations as fou
</div>
</div>
<div id="outline-container-org905d7f3" class="outline-2">
<h2 id="org905d7f3"><span class="section-number-2">5</span> Example input(HADAMARD -&gt; CNOT)</h2>
<div id="outline-container-org4189a63" class="outline-2">
<h2 id="org4189a63"><span class="section-number-2">5</span> Example input(HADAMARD -&gt; CNOT)</h2>
<div class="outline-text-2" id="text-5">
<pre class="example">
INITIALIZE R 2 0
@ -290,8 +289,8 @@ PEEK R RES
</div>
</div>
<div id="outline-container-org33ea045" class="outline-2">
<h2 id="org33ea045"><span class="section-number-2">6</span> TODOS</h2>
<div id="outline-container-orgef0e774" class="outline-2">
<h2 id="orgef0e774"><span class="section-number-2">6</span> TODOS</h2>
<div class="outline-text-2" id="text-6">
<ul class="org-ul">
<li>Rebuild in C (in progress)</li>
@ -301,19 +300,9 @@ PEEK R RES
</div>
</div>
<div id="outline-container-org58fad8f" class="outline-2">
<h2 id="org58fad8f"><span class="section-number-2">7</span> Documentation</h2>
<div id="outline-container-org6fc4821" class="outline-2">
<h2 id="org6fc4821"><span class="section-number-2">7</span> Licence</h2>
<div class="outline-text-2" id="text-7">
<ul class="org-ul">
<li><a href="https://github.com/watkinsr/sqasm-thesis/blob/master/thesis/thesis.pdf">Thesis</a></li>
<li><a href="https://github.com/watkinsr/sqasm-thesis/blob/master/talks/slides_uor_may_2016.pdf">Slides</a></li>
</ul>
</div>
</div>
<div id="outline-container-org028c422" class="outline-2">
<h2 id="org028c422"><span class="section-number-2">8</span> Licence</h2>
<div class="outline-text-2" id="text-8">
<p>
GNU GPL3
</p>
@ -322,7 +311,7 @@ GNU GPL3
</div>
<div id="postamble" class="status">
<p class="author">Author: Ryan Watkins</p>
<p class="date">Created: 2020-10-02 Fri 07:19</p>
<p class="date">Created: 2020-10-02 Fri 07:32</p>
</div>
</body>
</html>

4
README.org

@ -34,9 +34,5 @@ PEEK R RES
- Nice to have: Include quantum algorithms
- Rewrite simulator to be fully quantum circuit driven
* Documentation
+ [[https://github.com/watkinsr/sqasm-thesis/blob/master/thesis/thesis.pdf][Thesis]]
+ [[https://github.com/watkinsr/sqasm-thesis/blob/master/talks/slides_uor_may_2016.pdf][Slides]]
* Licence
GNU GPL3

BIN
bin/parse

Binary file not shown.

60
include/QReg.h

@ -0,0 +1,60 @@
#include <assert.h>
#include <stdlib.h>
#include <Eigen/Dense>
#include <iostream>
using Eigen::MatrixXd;
/*
def __init__(self, n_qubits, setVal=-1):
self.n_qubits = n_qubits
self.qubits = [0] * n_qubits
# in this classical simulation, we use 2^n_qubits complex numbers
self.amps = [0] * (1 << n_qubits)
self.amps[len(self.amps) - 1] = 1
if (setVal != -1):
self.amps[setVal] = 1
if (setVal != len(self.amps) - 1):
self.amps[len(self.amps) - 1] = 0
self.amps = np.matrix(self.amps).T
*/
class QReg
{
public:
QReg(size_t _numQubits, int _val)
{
numQubits = _numQubits;
qubits = (int *)calloc(sizeof(int), _numQubits);
assert(qubits);
amps = (int *)calloc(sizeof(int), _numQubits);
assert(amps);
for (int i = 0; i < (1 << numQubits); i++)
{
amps[i] = 0;
}
amps[numQubits - 1] = 1;
if (_val != -1)
{
amps[_val] = 1;
if (_val != numQubits - 1)
{
amps[numQubits - 1] = 0;
}
}
MatrixXd m(2, 2);
m(0, 0) = 3;
m(1, 0) = 2.5;
m(0, 1) = -1;
m(1, 1) = m(1, 0) + m(0, 1);
std::cout << m << std::endl;
}
private:
int numQubits;
int *qubits;
int *amps;
};

4
include/parse.h

@ -1,3 +1,5 @@
#include <string>
#define LSH_RL_BUFSIZE 1024
#define LSH_TOK_BUFSIZE 64
#define LSH_TOK_DELIM " \t\r\n\a"
@ -9,7 +11,7 @@ int lsh_exit(char **args);
int lsh_execute(char **args);
int lsh_init(char **args);
char *tokens[] = {
const char *tokens[] = {
"INIT"};
int (*builtin_func[])(char **) = {

BIN
obj/parse.o

Binary file not shown.

23
src/parse.c → src/parse.cpp

@ -5,6 +5,7 @@
#include <string.h>
#include "../include/parse.h"
#include "../include/QReg.h"
// Taken from: https://brennan.io/2015/01/16/write-a-shell-in-c/
@ -40,7 +41,7 @@ void lsh_loop(void)
char *lsh_read_line(void)
{
char *line = NULL;
ssize_t bufsize = 0; // have getline allocate a buffer for us
size_t bufsize = 0; // have getline allocate a buffer for us
if (getline(&line, &bufsize, stdin) == -1)
{
@ -60,9 +61,9 @@ char *lsh_read_line(void)
char **lsh_split_line(char *line)
{
int bufsize = LSH_TOK_BUFSIZE;
size_t bufsize = LSH_TOK_BUFSIZE;
int position = 0;
char **tokens = malloc(bufsize * sizeof(char *));
char **tokens = (char **)malloc(bufsize * sizeof(char *));
char *token;
if (!tokens)
@ -81,7 +82,7 @@ char **lsh_split_line(char *line)
if (position >= bufsize)
{
bufsize += LSH_TOK_BUFSIZE;
tokens = realloc(tokens, bufsize * sizeof(char *));
tokens = (char **)realloc(tokens, bufsize * sizeof(char *));
if (!tokens)
{
fprintf(stderr, "lsh: allocation error\n");
@ -106,13 +107,18 @@ int lsh_num_builtins()
int lsh_init(char **args)
{
if (args[1] == NULL)
if (args[1] == NULL || args[2] == NULL || args[3] == NULL)
{
fprintf(stderr, "we require a register to INIT, example: INIT R2 2 0\n");
fprintf(stderr, "invalid args to INIT, example: INIT R2 2 0\n");
}
else
{
printf("Received a register\n");
char *r = args[1];
int arg1 = atoi(args[2]);
int arg2 = atoi(args[3]);
printf("Valid input, register: %s, arg1: %i, arg2: %i\n", r, arg1, arg2);
QReg reg = QReg(arg1, arg2);
}
return 1;
}
@ -152,6 +158,5 @@ int lsh_execute(char **args)
return (*builtin_func[i])(args);
}
}
// return lsh_launch(args);
return 0;
}
Loading…
Cancel
Save