Quantum computing SDK with pyhon flask+blueqat for making own API
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 -yInstalling pyenv
git clone https://github.com/yyuu/pyenv.git ~/.pyenvConfiguration
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_profileLet’s check
pyenv -vNow we have,
pyenv 1.2.14–11-gb5f69fe5Installing 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.2Changing the version
pyenv global 3.6.2
pyenv rehashCheck
python --versionNow we have Python 3.6.2 installed!
Installing blueqat
pip3 install --user blueqatIt’s easy we have,
Successfully installed blueqat-0.3.10 numpy-1.17.3 scipy-1.3.1Additionally we update the pip itself for convenience.
pip3 install --upgrade pipWe succeeded to update
Successfully installed pip-19.3.1
Check
We make a file
vi test.pyAnd 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.pyWe 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 FlaskWe prepare a file.
vi hello.pyAnd 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.pyAnd 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:5000We successfully get “Hello World!” on the web browser.
Using json through API
Prepare a file first.
vi api.pyInside 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.pyNow 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:5000We 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 Circuitapp = 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.pyIf 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.

