Browse Source

Fix #7 -- alias literal clifford.by_names

master
Pete Shadbolt 7 years ago
parent
commit
f4631c552f
6 changed files with 29 additions and 20 deletions
  1. +9
    -0
      abp/clifford.py
  2. +1
    -1
      abp/fancy.py
  3. +6
    -6
      abp/graphstate.py
  4. +3
    -3
      abp/stabilizer.py
  5. +1
    -1
      tests/test_clifford.py
  6. +9
    -9
      tests/test_graphstate.py

+ 9
- 0
abp/clifford.py View File

@@ -42,6 +42,15 @@ The complete set of aliases for single-qubit Cliffords is as follows:

from tables import *

# Aliases
identity = by_name["identity"]
hadamard = by_name["hadamard"]
px = by_name["px"]
py = by_name["py"]
pz = by_name["pz"]
msqx_h = by_name["msqx_h"]
sqz_h = by_name["sqz_h"]

def conjugate(operator, unitary):
""" Returns transform * vop * transform^dagger and a phase in {+1, -1} """
return measurement_table[operator, unitary]


+ 1
- 1
abp/fancy.py View File

@@ -60,6 +60,6 @@ class GraphState(graphstate.GraphState, nx.Graph):
""" Automatically add vops if they're not present """
for key in self.node:
if not "vop" in self.node[key]:
self.node[key]["vop"] = clifford.by_name["identity"]
self.node[key]["vop"] = clifford.identity


+ 6
- 6
abp/graphstate.py View File

@@ -31,7 +31,7 @@ class GraphState(object):
self.adj = data.adj.copy()
self.node = data.node.copy()
for key, value in self.node.items():
self.node[key]["vop"] = data.node[key].get("vop", clifford.by_name["identity"])
self.node[key]["vop"] = data.node[key].get("vop", clifford.identity)
except AttributeError:
try:
# Provided with a list of node names?
@@ -144,10 +144,10 @@ class GraphState(object):
self._toggle_edge(i, j)

self.node[v]["vop"] = clifford.times_table[
self.node[v]["vop"], clifford.by_name["msqx_h"]]
self.node[v]["vop"], clifford.msqx_h]
for i in self.adj[v]:
self.node[i]["vop"] = clifford.times_table[
self.node[i]["vop"], clifford.by_name["sqz_h"]]
self.node[i]["vop"], clifford.sqz_h]

def act_local_rotation(self, node, operation):
""" Act a local rotation on a qubit
@@ -229,11 +229,11 @@ class GraphState(object):
if phase == -1:
result = not result

if basis == clifford.by_name["px"]:
if basis == clifford.px:
result, determinate = self._measure_graph_x(node, result)
elif basis == clifford.by_name["py"]:
elif basis == clifford.py:
result, determinate = self._measure_graph_y(node, result)
elif basis == clifford.by_name["pz"]:
elif basis == clifford.pz:
result, determinate = self._measure_graph_z(node, result)
else:
raise ValueError("You can only measure in {X,Y,Z}")


+ 3
- 3
abp/stabilizer.py View File

@@ -5,9 +5,9 @@ Implements a simple Stabilizer object.
import itertools as it
from abp import clifford

I = clifford.by_name["identity"]
X = clifford.by_name["px"]
Z = clifford.by_name["pz"]
I = clifford.identity
X = clifford.px
Z = clifford.pz

class Stabilizer(object):
def __init__(self, g):


+ 1
- 1
tests/test_clifford.py View File

@@ -65,7 +65,7 @@ def test_conjugation_table():

def test_cz_table_makes_sense():
""" Test the CZ table is symmetric """
hadamard = clifford.by_name["hadamard"]
hadamard = clifford.hadamard
assert all(clifford.cz_table[0, 0, 0] == [1, 0, 0])
assert all(clifford.cz_table[1, 0, 0] == [0, 0, 0])
assert all(


+ 9
- 9
tests/test_graphstate.py View File

@@ -10,9 +10,9 @@ DEPTH = 100

def test_initialization():
g = GraphState("abc")
assert g.node["a"]["vop"] == clifford.by_name["identity"]
assert g.node["a"]["vop"] == clifford.identity
g = GraphState("abc", vop="hadamard")
assert g.node["c"]["vop"] == clifford.by_name["hadamard"]
assert g.node["c"]["vop"] == clifford.hadamard
g = GraphState(5)
assert len(g.node) == 5

@@ -44,13 +44,13 @@ def test_remove_vop():
""" Test that removing VOPs really works """
g = mock.simple_graph()
g.remove_vop(0, 1)
assert g.node[0]["vop"] == clifford.by_name["identity"]
assert g.node[0]["vop"] == clifford.identity
g.remove_vop(1, 1)
assert g.node[1]["vop"] == clifford.by_name["identity"]
assert g.node[1]["vop"] == clifford.identity
g.remove_vop(2, 1)
assert g.node[2]["vop"] == clifford.by_name["identity"]
assert g.node[2]["vop"] == clifford.identity
g.remove_vop(0, 1)
assert g.node[0]["vop"] == clifford.by_name["identity"]
assert g.node[0]["vop"] == clifford.identity


def test_edgelist():
@@ -75,9 +75,9 @@ def test_stress(n=int(1e5)):
def test_cz():
""" Test CZ gate """
g = GraphState([0, 1], vop="hadamard")
g.act_local_rotation(0, clifford.by_name["hadamard"])
g.act_local_rotation(1, clifford.by_name["hadamard"])
g.act_local_rotation(1, clifford.by_name["py"])
g.act_local_rotation(0, clifford.hadamard)
g.act_local_rotation(1, clifford.hadamard)
g.act_local_rotation(1, clifford.py)
assert not g.has_edge(0, 1)
g.act_cz(0, 1)
assert g.has_edge(0, 1)


Loading…
Cancel
Save