Просмотр исходного кода

Add & test GraphState.from_json

We needed to implement `GraphState.from_json` so that the server can
track a copy of the client's state. That's a useful byproduct of this
work!
master
Pete Shadbolt 9 лет назад
Родитель
Сommit
d6b5f540df
3 измененных файлов: 39 добавлений и 3 удалений
  1. +7
    -3
      abp/graphstate.py
  2. +1
    -0
      bin/abpserver
  3. +31
    -0
      tests/test_json.py

+ 7
- 3
abp/graphstate.py Просмотреть файл

@@ -405,9 +405,6 @@ class GraphState(object):
>>> with open("graph.json") as f: >>> with open("graph.json") as f:
json.dump(graph.to_json(True), f) json.dump(graph.to_json(True), f)


.. todo::
Implement ``from_json()``!

""" """
if stringify: if stringify:
node = {str(key): value for key, value in self.node.items()} node = {str(key): value for key, value in self.node.items()}
@@ -417,6 +414,13 @@ class GraphState(object):
else: else:
return {"node": self.node, "adj": self.adj} return {"node": self.node, "adj": self.adj}


def from_json(self, data):
""" Construct the graph from JSON data
:param data: JSON data to be read.
"""
self.node = data["node"]
self.adj = data["adj"]

def to_state_vector(self): def to_state_vector(self):
""" Get the full state vector corresponding to this stabilizer state. Useful for debugging, interface with other simulators. """ Get the full state vector corresponding to this stabilizer state. Useful for debugging, interface with other simulators.
This method becomes very slow for more than about ten qubits! This method becomes very slow for more than about ten qubits!


+ 1
- 0
bin/abpserver Просмотреть файл

@@ -16,6 +16,7 @@ import json
from pkg_resources import resource_filename from pkg_resources import resource_filename


clients = [] clients = []
local_state = abp.GraphState()


def new_message(client, server, message): def new_message(client, server, message):
if message.startswith("edit:"): if message.startswith("edit:"):


+ 31
- 0
tests/test_json.py Просмотреть файл

@@ -0,0 +1,31 @@
import mock
import abp

def test_json():
""" Test to_json and from_json """
a = mock.named_node_graph()
j = a.to_json()

b = abp.GraphState()
b.from_json(j)
assert a == b


def test_json_again():
""" Test to_json and from_json """
# Make a random graph
a = abp.GraphState(10)
a.act_circuit(mock.random_graph_circuit())

# Dump it to JSON
j = a.to_json()

# Reconstruct from JSON
b = abp.GraphState()
b.from_json(j)

# Check equality
assert a == b




Загрузка…
Отмена
Сохранить