瀏覽代碼

Small fix

master
Pete Shadbolt 8 年之前
父節點
當前提交
2d18fd2f91
共有 7 個文件被更改,包括 66 次插入20 次删除
  1. +24
    -12
      abp/graphstate.py
  2. +5
    -0
      tests/dummy.py
  3. +12
    -0
      tests/test_against_anders_and_briegel.py
  4. +0
    -5
      tests/test_clifford.py
  5. +1
    -1
      tests/test_conjugation.py
  6. +1
    -2
      tests/test_measurement.py
  7. +23
    -0
      tests/test_single_qubit_measurement_against_anb.py

+ 24
- 12
abp/graphstate.py 查看文件

@@ -91,6 +91,13 @@ class GraphState(object):
self.node[v]["vop"] = clifford.times_table[
rotation, self.node[v]["vop"]]

def act_local_rotation2(self, v, op):
""" Act a local rotation """
rotation = clifford.by_name[str(op)]
self.node[v]["vop"] = clifford.times_table[
self.node[v]["vop"], rotation]


def act_hadamard(self, qubit):
""" Shorthand """
self.act_local_rotation(qubit, 10)
@@ -122,11 +129,14 @@ class GraphState(object):
""" Measure in an arbitrary basis """
basis = clifford.by_name[basis]
ha = clifford.conjugation_table[self.node[node]["vop"]]
basis, phase = clifford.conjugate(basis, ha)
#basis, phase = clifford.conjugate(basis, ha)
basis, phase = clifford.conjugate(basis, self.node[node]["vop"])

print "MEASURE"
print "Op: {} Phase: {}".format(basis, phase)

# Flip a coin
result = force if force!=None else random.choice([0, 1])

# Flip the result if we have negative phase
if phase == -1:
result = not result
@@ -159,8 +169,12 @@ class GraphState(object):
def measure_x(self, node, result):
""" Measure the graph in the X-basis """
if len(self.adj[node]) == 0:
print "gXm{},D".format(node),
return 0


print "gXm{},{:d}".format(node, result),

# Pick a vertex
#friend = next(self.adj[node].iterkeys())
# TODO this is enforced determinism for testing purposes
@@ -194,6 +208,7 @@ class GraphState(object):

def measure_y(self, node, result):
""" Measure the graph in the Y-basis """
print "gYm{},{:d}".format(node, result),
# Do some rotations
for neighbour in self.adj[node]:
# NB: should these be hermitian_conjugated?
@@ -204,11 +219,17 @@ class GraphState(object):
for i, j in it.combinations(vngbh, 2):
self.toggle_edge(i, j)

self.act_local_rotation(node, "sqz" if result else "msqz")
# lcoS.herm_adjoint() if result else lcoS
# else smiZ
# 5 else 6
print "RESULT is {:d}, op is {} doing {}".format(result, self.node[node]["vop"], 5 if result else 6)
self.act_local_rotation2(node, 5 if result else 6)
print "BECAME ", self.node[node]["vop"]
return result

def measure_z(self, node, result):
""" Measure the graph in the Z-basis """
print "gZm{},{:d}".format(node, result),
# Disconnect
for neighbour in tuple(self.adj[node]):
self.del_edge(node, neighbour)
@@ -239,15 +260,6 @@ class GraphState(object):

return s

#clean = lambda n: str(n["vop"])
#nodes = ("{}: {}".format(key, clean(value)) for key, value in sorted(self.node.items()))
#nodes = "\n".join(nodes)
#return "Nodes:\n{}\n\nEdges:\n{}".format(nodes, "")
#node = {key: clifford.get_name(value["vop"])
#for key, value in self.node.items()}
#nbstr = str(self.adj)
#return "graph:\n node: {}\n adj: {}\n".format(node, nbstr)

def to_json(self, stringify=False):
"""
Convert the graph to JSON form.


+ 5
- 0
tests/dummy.py 查看文件

@@ -42,3 +42,8 @@ def bell():
a.act_cz(0,1)
b.cphase(0,1)
return a, b

def onequbit():
a = GraphState(range(1))
b = graphsim.GraphRegister(1)
return a, b

+ 12
- 0
tests/test_against_anders_and_briegel.py 查看文件

@@ -40,6 +40,18 @@ def test_local_rotations():
assert_equal(a, b)


def test_times_table():
""" Test times table """
for i, j in it.product(range(24), range(24)):
a = graphsim.GraphRegister(1)
b = GraphState([0])
a.local_op(0, graphsim.LocCliffOp(i))
a.local_op(0, graphsim.LocCliffOp(j))
b.act_local_rotation(0, i)
b.act_local_rotation(0, j)
assert_equal(a, b)


def test_cz_table(N=10):
""" Test the CZ table """



+ 0
- 5
tests/test_clifford.py 查看文件

@@ -62,11 +62,6 @@ def test_conjugation_table():
assert len(set(clifford.conjugation_table)) == 24


def test_times_table():
""" Check the times table """
assert clifford.times_table[0][4] == 4


def test_cz_table_makes_sense():
""" Test the CZ table is symmetric """
hadamard = clifford.by_name["hadamard"]


+ 1
- 1
tests/test_conjugation.py 查看文件

@@ -18,7 +18,7 @@ def test_conjugation():
new_operation = operation.op

NEW_OPERATION, PHASE = clifford.conjugate(operation_index, transform_index)
print PHASE
print new_operation, NEW_OPERATION, " ",
assert new_operation == NEW_OPERATION
assert PHASE == phase


+ 1
- 2
tests/test_measurement.py 查看文件

@@ -46,8 +46,7 @@ def test_projection():
g.measure(0, "pz", 0)
assert np.allclose(g.to_state_vector().state, qi.zero)

def test_another_projection():
""" This one fails at the moment """
# Now project onto |1>
g = GraphState([0])
g.act_local_rotation(0, "hadamard")
g.measure(0, "pz", 1)


+ 23
- 0
tests/test_single_qubit_measurement_against_anb.py 查看文件

@@ -0,0 +1,23 @@
from abp import GraphState, clifford
from anders_briegel import graphsim
import numpy as np
from tqdm import tqdm
import itertools as it
import dummy

N = 10
REPEATS = 10
m = {1: graphsim.lco_X, 2: graphsim.lco_Y, 3: graphsim.lco_Z}

def test_1():
""" Check that single qubits work """
space = it.product(range(24), (3,2,1), (0,1))
for rotation, measurement, outcome in tqdm(space, "Testing single qubit measurements"):
#print "\nr{} m{} o{}".format(rotation, measurement, outcome)
a, b = dummy.onequbit()
#print a.to_json()["node"][0]["vop"], b.to_json()["node"][0]["vop"]
a.measure(0, str(measurement), outcome)
b.measure(0, m[measurement], None, outcome)
#print a.to_json()["node"][0]["vop"], b.to_json()["node"][0]["vop"]
assert a == b, (a.to_json()["node"][0], b.to_json()["node"][0])

Loading…
取消
儲存