Преглед изворни кода

Testing against CHP

master
Pete Shadbolt пре 8 година
родитељ
комит
40fb9d4dfa
6 измењених фајлова са 63 додато и 8 уклоњено
  1. +2
    -0
      .gitignore
  2. +6
    -7
      chp/chp.c
  3. +4
    -0
      chp/run.sh
  4. +18
    -0
      chp/setup.py
  5. +4
    -1
      chp/test.py
  6. +29
    -0
      tests/test_circuit_model_against_chp.py

+ 2
- 0
.gitignore Прегледај датотеку

@@ -1,3 +1,5 @@
*.o
*.chp
.agignore
venv/
fresh_venv/


+ 6
- 7
chp/chp.c Прегледај датотеку

@@ -511,7 +511,7 @@ void printbasisstate(struct QState *q)

void dumpbasisstate(struct QState *q, PyObject *output, int index)

// Prints the result of applying the Pauli operator in the "scratch space" of q to |0...0>
// Dumps the result of applying the Pauli operator in the "scratch space" of q to |0...0>

{

@@ -535,17 +535,16 @@ void dumpbasisstate(struct QState *q, PyObject *output, int index)
if (e==3) strcat(buffer, "-i|");


int key;
key = 0;
for (j = 0; j < q->n; j++)
{
j5 = j>>5;
pw = q->pw[j&31];
if (q->x[2*q->n][j5]&pw) strcat(buffer, "1");
else strcat(buffer, "0");
if (q->x[2*q->n][j5]&pw) key = key ^ (1 << j);
}

strcat(buffer, ">");

PyList_SetItem(output, index, Py_BuildValue("s", buffer));
PyDict_SetItem(output, Py_BuildValue("i", key), Py_BuildValue("i", e));
return;
}

@@ -647,7 +646,7 @@ static PyObject* get_ket(PyObject* self, PyObject* noarg)

g = gaussian(q);

PyObject *output = PyList_New(g+1);
PyObject *output = PyDict_New();
if (g > 30) { return output; }

seed(q, g);


+ 4
- 0
chp/run.sh Прегледај датотеку

@@ -0,0 +1,4 @@
#!/bin/bash
gcc -DNDEBUG -g -O3 -Wall -Wno-unused-result -Wstrict-prototypes -fPIC -DMAJOR_VERSION=1 -DMINOR_VERSION=0 -I/usr/include -I/usr/include/python2.7 -c chp.c -o chp.o
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wno-unused-result -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security chp.o -o chp.so
python test.py

+ 18
- 0
chp/setup.py Прегледај датотеку

@@ -0,0 +1,18 @@
import os
from setuptools import setup, Extension, find_packages

path = "chp.c"
chp = Extension("chp", [path])

extensions = [chp]

setup(
name="chp",
version="0.1",
packages=find_packages(),
ext_modules=extensions,
author="Scott Aaronson & Daniel Gottesman",
description="Ported by Pete Shadbolt",
license="Copyright Scott Aaronson"
)


+ 4
- 1
chp/test.py Прегледај датотеку

@@ -1,8 +1,11 @@
import chp
import numpy as np

chp.init(5)
chp.act_hadamard(0)
chp.act_cnot(0, 1)
chp.act_phase(0)
print chp.get_ket()

for key, value in chp.get_ket().items():
print bin(key), np.exp(1j * value * np.pi/2).round(2)


+ 29
- 0
tests/test_circuit_model_against_chp.py Прегледај датотеку

@@ -0,0 +1,29 @@
import chp
from abp import qi
import numpy as np

n = 10

def get_chp_state():
""" Convert CHP to CircuitModel """
output = qi.CircuitModel(n)
ket = chp.get_ket()
nonzero = np.sqrt(len(ket))
for key, phase in ket.items():
output.state[key] = np.exp(1j*phase*np.pi/2)/nonzero
return output


def test1():
chp.init(5)
chp.act_hadamard(0)
chp.act_cnot(0, 1)

yy = qi.CircuitModel(n)
yy.act_hadamard(0)
yy.act_hadamard(1)
yy.act_cz(0, 1)
yy.act_hadamard(1)
assert yy == get_chp_state()



Loading…
Откажи
Сачувај