Quantum computing SDK with pyhon flask+blueqat for making own API

Yuichiro Minato
Nov 4 · 3 min read

First

Some people may want to do quantum computing business on cloud services.

This time we are going to see a simple quantum computing API.

blueqat SDK

Let’s try use blueqat quatnum computing SDK

https://github.com/Blueqat/Blueqat

EC2

Now we try to make an instance on amazon ec2 with amazon linux on t2.nano instance.

Once the instance made, use ssh to go into the server.

Preparing

Preparing all we need. First we need “git”

sudo yum install git -y

Installing pyenv

git clone https://github.com/yyuu/pyenv.git ~/.pyenv

Configuration

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
source ~/.bash_profile

Let’s check

pyenv -v

Now we have,

pyenv 1.2.14–11-gb5f69fe5

Installing Python

sudo yum install gcc zlib-devel bzip2 bzip2-devel readline readline-devel sqlite sqlite-devel openssl openssl-devel -ypyenv install 3.6.2

Now we have,

Installed Python-3.6.2 to /home/ec2-user/.pyenv/versions/3.6.2

Changing the version

pyenv global 3.6.2
pyenv rehash

Check

python --version

Now we have Python 3.6.2 installed!

Installing blueqat

pip3 install --user blueqat

It’s easy we have,

Successfully installed blueqat-0.3.10 numpy-1.17.3 scipy-1.3.1

Additionally we update the pip itself for convenience.

pip3 install --upgrade pip

We succeeded to update
Successfully installed pip-19.3.1

Check

We make a file

vi test.py

And inside it we prepare some quantum circuit

from blueqat import Circuita = Circuit().h[0].m[:].run(shots=100)
print(a)

And test it.

python3 test.py

We get

Counter({'0': 52, '1': 48})

Now we have superposition of quantum circuit.

flask

To make an API we use flask this time.

pip3 install Flask

We prepare a file.

vi hello.py

And put some sample

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello World!'
if __name__ == '__main__':
app.run(host='0.0.0.0')

Testing it.

python3 hello.py

And we have correctly working on flask

* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

Checking through web

Configure the security group on ec2 and we put the ip address.

http://hogehoge:5000

We successfully get “Hello World!” on the web browser.

Using json through API

Prepare a file first.

vi api.py

Inside it prepare,

from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/', methods=['POST'])
def post_json():
json = request.get_json()
return jsonify(json)
if __name__ == '__main__':
app.run(host='0.0.0.0')

and we try,

python3 api.py

Now we have a server side and let’s try testing on post a json file to the server through internet.

curl -X POST -H "Content-Type: application/json" -d '{"circuit":"h"}' hogehoge:5000

We have a correct response.

{"circuit":"h"}

It is OK.

Sending a quantum circuit

Now we do a simple thing, just send one qubit quantum circuit and receive at server side. Changing the existing api.py to get json and if we have “h” we do superposition on blueqat. If we have “x” we do X-gate operation to flip the qubit. Other input are ignored.

from flask import Flask, request, jsonify
from blueqat import Circuit
app = Flask(__name__)@app.route('/', methods=['POST'])
def post_json():
json = request.get_json()
a = Circuit()
if(json["circuit"] == "h"):
a.h[0]
elif(json["circuit"] == "x"):
a.x[0]
return jsonify(a.m[:].run(shots=100))
if __name__ == '__main__':
app.run(host='0.0.0.0')

And run it.

python3 api.py

If we send H to the API

curl -X POST -H "Content-Type: application/json" -d '{"circuit":"h"}' hogehoge:5000
{"0":58,"1":42}

If we send X

curl -X POST -H "Content-Type: application/json" -d '{"circuit":"x"}' hogehoge:5000
{"1":100}

And others

curl -X POST -H "Content-Type: application/json" -d '{"circuit":"a"}' hogehoge:5000
{}

We have correct response.

Perspective

Next we want to try on serverless architecture to get API through aws lambda.

MDR Inc.

quantum computing company in Tokyo

Yuichiro Minato

Written by

MDR, Quantum Computing@Tokyo, https://mdrft.com/

MDR Inc.

MDR Inc.

quantum computing company in Tokyo

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade