From 40fb9d4dfaddaaf902d6c9afc33aee2e45758433 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Tue, 24 May 2016 04:01:35 +0100 Subject: [PATCH] Testing against CHP --- .gitignore | 2 ++ chp/chp.c | 13 +++++------ chp/run.sh | 4 ++++ chp/setup.py | 18 +++++++++++++++ chp/test.py | 5 ++++- tests/test_circuit_model_against_chp.py | 29 +++++++++++++++++++++++++ 6 files changed, 63 insertions(+), 8 deletions(-) create mode 100755 chp/run.sh create mode 100755 chp/setup.py create mode 100644 tests/test_circuit_model_against_chp.py diff --git a/.gitignore b/.gitignore index 9d6b18f..2418191 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +*.o +*.chp .agignore venv/ fresh_venv/ diff --git a/chp/chp.c b/chp/chp.c index cf41cd5..21cd1e3 100644 --- a/chp/chp.c +++ b/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); diff --git a/chp/run.sh b/chp/run.sh new file mode 100755 index 0000000..6cbe369 --- /dev/null +++ b/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 diff --git a/chp/setup.py b/chp/setup.py new file mode 100755 index 0000000..8ee8818 --- /dev/null +++ b/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" +) + diff --git a/chp/test.py b/chp/test.py index c46609b..89bfe49 100644 --- a/chp/test.py +++ b/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) diff --git a/tests/test_circuit_model_against_chp.py b/tests/test_circuit_model_against_chp.py new file mode 100644 index 0000000..854a3ba --- /dev/null +++ b/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() + +