From b4872397e9ce2abc912fc55d27f45909ace4b742 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Mon, 4 Jul 2016 21:48:14 +0100 Subject: [PATCH] Progress towards working measurement --- .gitignore | 1 + abp/clifford.py | 2 +- abp/graphstate.py | 24 ++++++++++++++++++++++-- abp/util.py | 4 ++++ tests/test_measurement.py | 15 +++++---------- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 0ec9d42..144aa78 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.noseids debug_anders.txt *.o *.chp diff --git a/abp/clifford.py b/abp/clifford.py index 28fe836..af5885a 100644 --- a/abp/clifford.py +++ b/abp/clifford.py @@ -6,7 +6,7 @@ It provides tables for Clifford group multiplication and conjugation, as well as CZ and decompositions of the 2x2 Cliffords. """ -import os, json, tempfile, os, sys, json, string +import os, json, tempfile, json from functools import reduce import itertools as it import numpy as np diff --git a/abp/graphstate.py b/abp/graphstate.py index 66bcdcf..4573fb1 100644 --- a/abp/graphstate.py +++ b/abp/graphstate.py @@ -117,6 +117,26 @@ class GraphState(object): if new_edge != edge: self.toggle_edge(a, b) + def measure(self, node, basis, force=None): + """ Measure in an arbitrary basis """ + basis = clifford.by_name[basis] + old_basis = basis + ha = clifford.conjugation_table[self.node[node]["vop"]] + basis, phase = clifford.conjugate(basis, ha) + print basis, phase + assert phase in (-1, 1) # TODO: remove + + # TODO: wtf + force = force ^ 0x01 if force != -1 and phase == 0 else force + + which = {1: self.measure_x, 2: self.measure_y, 3: self.measure_z}[basis] + res = which(node, force) + res = res if phase == 1 else not res + + # TODO: put the asserts from graphsim.cpp into tests + return res + + def measure_z(self, node, force=None): """ Measure the graph in the Z-basis """ res = force if force != None else random.choice([0, 1]) @@ -136,12 +156,12 @@ class GraphState(object): return res - def measure_x(self, i): + def measure_x(self, node, force=None): """ Measure the graph in the X-basis """ # TODO pass - def measure_y(self, i): + def measure_y(self, node, force=None): """ Measure the graph in the Y-basis """ # TODO pass diff --git a/abp/util.py b/abp/util.py index b4e15f6..a468b14 100644 --- a/abp/util.py +++ b/abp/util.py @@ -1,2 +1,6 @@ +""" +Utility functions for ABP +""" + def xyz(x, y, z=0): return {"x": x, "y": y, "z": z} diff --git a/tests/test_measurement.py b/tests/test_measurement.py index 01eb0f0..d164c1f 100644 --- a/tests/test_measurement.py +++ b/tests/test_measurement.py @@ -8,13 +8,8 @@ def test_z_measurement(): def test_z_measurement_against_ab(): - a = graphsim.GraphRegister(1) - b = GraphState() - b.add_node(0) - - print a.measure(0, graphsim.lco_Z) - print b.measure_z(0) - - - - + for i in range(100): + a = graphsim.GraphRegister(1) + b = GraphState() + b.add_node(0) + assert a.measure(0, graphsim.lco_Z) == b.measure(0, "pz")