@@ -1,4 +1,3 @@ | |||||
# Alias some stuff to make imports cleaner | # Alias some stuff to make imports cleaner | ||||
from abp.graphstate import GraphState | from abp.graphstate import GraphState | ||||
from abp.qi import CircuitModel | from abp.qi import CircuitModel | ||||
from abp.client import Client |
@@ -1,48 +0,0 @@ | |||||
import requests | |||||
import abp, json | |||||
class ClientError(Exception): | |||||
def __init__(self, message): | |||||
self.message = message | |||||
class Client(object): | |||||
def __init__(self, host="localhost", port=5000, clear=False): | |||||
self.session = requests.Session() | |||||
self.root = "http://{}:{}".format(host, port) | |||||
if clear: | |||||
self.clear() | |||||
def get(self, endpoint): | |||||
url =self.root+endpoint | |||||
response = self.session.get(url) | |||||
if response.status_code == 404: | |||||
message = "404. Check that the server is running!".format(self.root, endpoint) | |||||
raise ClientError(message) | |||||
return response.content | |||||
def get_state(self): | |||||
response = self.get("/state") | |||||
output = abp.GraphState() | |||||
output.from_json(json.loads(response)) | |||||
return output | |||||
def set_state(self, state): | |||||
response = self.session.post(self.root+"/state", data=state.to_json()) | |||||
if not response.status_code == 200: | |||||
print response.status_code | |||||
return response.content | |||||
def add_node(self, node): | |||||
return self.get("/add_node/{}".format(node)) | |||||
def act_local_rotation(self, node, operation): | |||||
return self.get("/act_local_rotation/{}/{}".format(node, operation)) | |||||
def act_cz(self, a, b): | |||||
return self.get("/act_cz/{}/{}".format(a, b)) | |||||
def clear(self): | |||||
return self.get("/clear") | |||||
def kill(self): | |||||
self.session.close() |
@@ -67,8 +67,8 @@ def get_by_name(unitaries): | |||||
""" Get a lookup table of cliffords by name """ | """ Get a lookup table of cliffords by name """ | ||||
a = {name: find_clifford(u, unitaries) | a = {name: find_clifford(u, unitaries) | ||||
for name, u in qi.by_name.items()} | for name, u in qi.by_name.items()} | ||||
b = {get_name(i): i for i in range(24)} | |||||
a.update(b) | |||||
a.update({get_name(i): i for i in range(24)}) | |||||
a.update({i: i for i in range(24)}) | |||||
return a | return a | ||||
@@ -88,14 +88,10 @@ class GraphState(object): | |||||
for i in self.ngbh[v]: | for i in self.ngbh[v]: | ||||
self.vops[i] = clifford.times_table[self.vops[i], sqz_h] | self.vops[i] = clifford.times_table[self.vops[i], sqz_h] | ||||
def act_local_rotation(self, a, op): | |||||
def act_local_rotation(self, v, op): | |||||
""" Act a local rotation """ | """ Act a local rotation """ | ||||
self.vops[a] = clifford.times_table[op, self.vops[a]] | |||||
def act_local_rotation_by_name(self, qubit, name): | |||||
""" Shorthand """ | |||||
rotation = clifford.by_name[name] | |||||
self.act_local_rotation(qubit, rotation) | |||||
rotation = clifford.by_name[str(op)] | |||||
self.vops[v] = clifford.times_table[rotation, self.vops[v]] | |||||
def act_hadamard(self, qubit): | def act_hadamard(self, qubit): | ||||
""" Shorthand """ | """ Shorthand """ | ||||
@@ -123,15 +119,14 @@ class GraphState(object): | |||||
for neighbour in self.ngbh[node]: | for neighbour in self.ngbh[node]: | ||||
self.del_edge(node, neighbour) | self.del_edge(node, neighbour) | ||||
if res: | if res: | ||||
self.act_local_rotation_by_name(neighbour, "pz") | |||||
self.act_local_rotation(neighbour, "pz") | |||||
# Set final state as appropriate | |||||
# TODO: cache these lookups. sux | |||||
# Rotate | |||||
if res: | if res: | ||||
self.act_local_rotation_by_name(node, "px") | |||||
self.act_local_rotation_by_name(node, "hadamard") | |||||
self.act_local_rotation(node, "px") | |||||
self.act_local_rotation(node, "hadamard") | |||||
else: | else: | ||||
self.act_local_rotation_by_name(node, "hadamard") | |||||
self.act_local_rotation(node, "hadamard") | |||||
return res | return res | ||||
@@ -7,8 +7,8 @@ def test_single_qubit(): | |||||
g = GraphState() | g = GraphState() | ||||
g.add_node(0) | g.add_node(0) | ||||
g.add_node(1) | g.add_node(1) | ||||
g.act_local_rotation_by_name(0, "hadamard") | |||||
g.act_local_rotation_by_name(1, "hadamard") | |||||
g.act_local_rotation(0, "hadamard") | |||||
g.act_local_rotation(1, "hadamard") | |||||
g.act_cz(0, 1) | g.act_cz(0, 1) | ||||
assert np.allclose(g.to_state_vector().state, qi.bond) | assert np.allclose(g.to_state_vector().state, qi.bond) | ||||