瀏覽代碼

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 8 年之前
父節點
當前提交
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:
json.dump(graph.to_json(True), f)

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

"""
if stringify:
node = {str(key): value for key, value in self.node.items()}
@@ -417,6 +414,13 @@ class GraphState(object):
else:
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):
""" 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!


+ 1
- 0
bin/abpserver 查看文件

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

clients = []
local_state = abp.GraphState()

def new_message(client, server, message):
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




Loading…
取消
儲存