Skip to content
Permalink
Browse files

Port kite

  • Loading branch information...
vtomole committed Jul 20, 2019
1 parent 41c62c3 commit 7dad1690f1623adfa463ad4d09dd4a4c0a6f3c21
@@ -0,0 +1 @@
from kite.program import *
@@ -0,0 +1,13 @@
QUBITS 5
H 0
H 1
H 4
SWAP 3 4
SWAP 2 4
CNOT 1 4
SWAP 2 4
SWAP 3 4
H 1
H 1
MEASURE 0
MEASURE 1
@@ -0,0 +1,5 @@
QUBITS 2
H 0
CNOT 0 1
MEASURE 0
MEASURE 1
@@ -0,0 +1,60 @@
"""Defines the Gate class"""
import numpy as np


class Gates:
""""Contains basic quantum gates"""

# Rotation
def RX(self, theta):
return np.matrix([[np.cos(theta / 2), -1j * np.sin(theta / 2)],
[-1j * np.sin(theta / 2), np.cos(theta / 2)]])

def RY(self, theta):
return np.matrix([[np.cos(theta / 2), -np.sin(theta / 2)],
[np.sin(theta / 2), np.cos(theta / 2)]])

def RZ(self, theta):
return np.matrix([[np.exp(-1j * theta / 2), 0],
[0, np.exp(1j * theta / 2)]])

# pauli
I = np.matrix([[1, 0], [0, 1]])
X = np.matrix([[0, 1], [1, 0]])
Y = np.matrix([[0, 0 - 1j], [0 + 1j, 0]])
Z = np.matrix([[1, 0], [0, -1]])

# universal gates
H = (X + Z)/np.sqrt(2)
T = np.matrix([[1, 0], [0, np.exp(1j * np.pi / 4)]])
S = np.matrix([[1.0, 0.0], [0.0, 1.0j]])
CNOT = np.matrix([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0]])

# miscellaneous
CZ = np.matrix([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, -1]])
SWAP = np.matrix([[1, 0, 0, 0],
[0, 0, 1, 0],
[0, 1, 0, 0],
[0, 0, 0, 1]])

gates_set = {
"I": I,
"X": X,
"Y": Y,
"Z": Z,
"H": H,
"T": T,
"S": S,
"CNOT": CNOT,
"CZ": CZ,
"SWAP": SWAP,
"RX": RX,
"RY": RY,
"RZ": RZ
}
@@ -0,0 +1,9 @@
import kite as kt

circuit = kt.Program(
kt.QREG(1),
kt.H(0),
kt.MEASURE(0))

before, after = circuit.run()
print("Circuit ", after)
@@ -0,0 +1,51 @@
"""Defines program object"""
import sys

from kite import vm

def H(qubit):
return "H " + str(qubit) + "\n"


def QREG(num_qubits):
return "QUBITS " + str(num_qubits) + "\n"


def X(qubit):
return "X " + str(qubit) + "\n"


def CNOT(qubit, qubit1):
return "CNOT " + str(qubit) + " " + str(qubit1) + "\n"


def MEASURE(qubit):
return "MEASURE " + str(qubit) + "\n"

class API:
"Provides a file API and a CLI API to the VM"

@classmethod
def file_api(self, file_name):
a, b = vm.evaluate(file_name, "file")
return a, b

@classmethod
def cli_api(self, instructions_string):
a, b = vm.evaluate(instructions_string, "string")
return a, b

class Program:
"Consists of a list of instructions"

def __init__(self, *instructions):
program = "". join(instructions)
self.instructions = program

def run(self):
return API.cli_api(self.instructions)


if __name__ == "__main__":
result = API.file_api(sys.argv[1])
print(result)
@@ -1,5 +1,5 @@
from kite import vm
import sys
from kite import vm

if len(sys.argv) == 2:
a, b = vm.evaluate(sys.argv[1], "file")
@@ -0,0 +1,7 @@
QUBITS 3
H 1
CNOT 1 2
CNOT 0 1
H 0
MEASURE 0
MEASURE 1
@@ -0,0 +1,9 @@
QUBITS 3
MEASURE 0
MEASURE 1
H 1
CNOT 1 2
CLASSIC 1 1 1
X 2
CLASSIC 0 1 1
Z 2
@@ -0,0 +1,46 @@
"""
Requirements Installation:
$ sudo pip3 install bottle
Usage:
To run the server:
$ python3 server.py
To open the web-page:
http://127.0.0.1/5000
To teleport:
$ python3 teleport.py <qasm program file_name>
refresh the web page
"""
import os
from bottle import route, template, run, request


class global_values:
value = "Nothing Teleported Yet!"
index_page = """<html>
<body>
<h1>
Teleportation:
</h1>
{{value}}
</body>
</html>"""


@route('/', method='GET')
def get_index():
return template(global_values.index_page, value=global_values.value)


@route('/', method='POST')
def teleport():
q = [request.POST.get('q0'), request.POST.get('q1')]
if len(q) == 2 and None not in q:
global_values.value = "\n".join(q)
else:
global_values.value = "Nothing Teleported Yet!"


if os.environ.get('APP_LOCATION') == 'heroku':
run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))
else:
run(host='localhost', port=5000, debug=True, reloader=True)
@@ -0,0 +1,27 @@
import sys
import re
import requests
import kite as kt

url ='http://127.0.0.1:5000/'

def run(path):
_, msg = kt.API.file_api(f"{path}")
print(msg)
# Very hacky way of doing this... @TODO make this better
m = re.findall(r'====== MEASURE qubit (\d) : (\d)', msg)
if m[0][0] == '0':
q0 = int(m[0][1])
q1 = int(m[1][1])
else :
q1 = int(m[0][1])
q0 = int(m[1][1])
res = requests.post(url, data={
"q0": q0,
"q1" : q1
})
print(res.status_code, res.reason)


if __name__ == "__main__":
run(sys.argv[1])
@@ -0,0 +1,22 @@
import time
def something(duration=0.000001):
"""
Function that needs some serious benchmarking.
"""
time.sleep(duration)
# You may return anything you want, like the result of a computation
return 123

def test_my_stuff(benchmark):
# benchmark something
result = benchmark(something)

# Extra code, to verify that the run completed correctly.
# Sometimes you may want to check the result, fast functions
# are no good if they return incorrect results :-)
assert result == 123

def test_my_stuff_different_arg(benchmark):
# benchmark something, but add some arguments
result = benchmark(something, 0.001)
assert result == 123

0 comments on commit 7dad169

Please sign in to comment.
You can’t perform that action at this time.